Spring Cloud Spring Boot mybatis Enterprise Distribution Micro Service Cloud (iv) service consumption (Ribbon) "Dalston Edition"

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 as a service registry as a service eureka-server eureka-client provider as the basis. And based on the spring Cloud Ribbon implementation of the consumer, we can be implemented according to eureka-consumer the content of the simple change can be completed, the specific steps are as follows:

  • Undereureka-consumerCopy a service consumer project, named:eureka-consumer-ribbon。 Inpom.xmlThe following dependencies are added:
    <dependencies> ...    <dependency>        <groupId>org.springframework.cloud</groupId>        <artifactId> Spring-cloud-starter-ribbon</artifactid>    </dependency></dependencies>

    Modify the main class of the application. To RestTemplate add @LoadBalanced annotations:

    @EnableDiscoveryClient @springbootapplicationpublic class Application {@Bean @loadbalancedpublic resttemplate Resttemplate () {return new resttemplate ();} public static void Main (string[] args) {new Springapplicationbuilder (Application.class). Web (True). Run (args);}

    Modify the controller. Remove the original by LoadBalancerClient selecting the instance and stitching the URL step, directly through the resttemplate to initiate the request.

    @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 and LoadBalancerClient related logic, for RestTemplate use, our first URL parameter has some special. 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.

    Source Source

    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 interface of the Eureka-client service /dc , and can also observe its load balancing effect by initiating multiple eureka-client services.

Spring Cloud Spring Boot mybatis Enterprise Distribution Micro Service Cloud (iv) service consumption (Ribbon) "Dalston Edition"

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.