Building a microservices Architecture Spring Cloud: Service consumption (Ribbon)

Source: Internet
Author: User

Spring Cloud Ribbon

The Spring Cloud Ribbon is a set of client-side load balancing tools implemented on the Netflix ribbon. It is a client load balancer based on HTTP and TCP. It can set the server-side list to poll access to achieve a balanced load by configuring Ribbonserverlist in the client.

When the ribbon is used in conjunction with Eureka, Ribbonserverlist is discoveryenabledniwsserverlist rewritten to obtain a list of service instances from the Eureka registry. It also replaces iping with niwsdiscoveryping, which delegates responsibilities to Eureka to determine whether the server has been started.

When the ribbon is used in conjunction with Consul, Ribbonserverlist is consulserverlist to expand the list of service instances from consul. It is also implemented by Consulping as the Iping interface.

When we use the Spring cloud ribbon, whether it's combined with Eureka or consul, we're introducing Spring cloud Eureka or Spring cloud Consul relies on the automation configuration to load the above mentioned configuration, so we can quickly implement the load balancing of inter-service calls in spring cloud.

Let's look at a concrete example of how to use the Spring Cloud Ribbon to implement service invocation and client-side load balancing.
Give it a try.

In the following example, we will use the previously built Eureka-server as the service registry, Eureka-client as the service provider. And based on the spring Cloud Ribbon implementation of the consumer, we can according to Eureka-consumer implementation of the content can be easily changed to complete, the specific steps are as follows:

Copy a service consumer project according to Eureka-consumer, named: Eureka-consumer-ribbon. Add the following dependencies in the Pom.xml:

<dependencies>    ...    <dependency>        <groupId>org.springframework.cloud</groupId>        <artifactId>spring-cloud-starter-ribbon</artifactId>    </dependency></dependencies>

Modify the main class of the application. Add @loadbalanced annotations for Resttemplate:

@EnableDiscoveryClient@SpringBootApplicationpublic class Application {    @Bean    @LoadBalanced    public RestTemplate restTemplate() {        return new RestTemplate();    }    public static void main(String[] args) {        new SpringApplicationBuilder(Application.class).web(true).run(args);    }}

Modify the controller. Remove the previous step of selecting instances and stitching URLs through loadbalancerclient and initiating requests directly through Resttemplate.

@RestControllerpublic class DcController {    @Autowired    RestTemplate restTemplate;    @GetMapping("/consumer")    public String dc() {        return restTemplate.getForObject("http://eureka-client/dc", String.class);    }}

As you can see here, in addition to removing the original logic related to Loadbalancerclient, our first URL parameter is somewhat special for resttemplate use. The host location requested here is not in the form of a specific IP address and port, but is made up of a service name. So why can such a request be called successful? Because the spring Cloud ribbon has an interceptor that is able to make the actual call here, it automatically picks up the service instance and replaces the actual requested IP address and port with the service name, which completes the invocation of the service interface.

After you have finished writing your code above, the reader can start Eureka-server, Eureka-client, Eureka-consumer-ribbon, and then access the Http://localhost:2101/consumer To track how the Eureka-consumer-ribbon service consumes the/DC interface of the Eureka-client service, and can also observe its load balancing effect by initiating multiple eureka-client services.

Building a microservices Architecture Spring Cloud: Service consumption (Ribbon)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.