Spring Cloud Building MicroServices architecture-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.

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:

    • According to eureka-consumer the copy of a service consumer project, named: eureka-consumer-ribbon . pom.xmlAdd the following dependencies in:
<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 @ Springbootapplication public class application {   @Bean public resttemplate Resttemplate return new resttemplate (); }  public static void main (string[] args) { span class= "keyword" >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.
@RestController public class Dccontroller { @Autowiredresttemplate 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.

After you have finished writing your code, the reader can start Eureka-server, eureka-client, and Eureka-consumer-ribbon. 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.

From now on, I will record the process and the essence of the recently developed Springcloud micro-service cloud architecture, and help more friends who are interested in developing the Spring cloud framework, hoping to help more good scholars. Let's explore how the Spring cloud architecture is built and how it can be used in enterprise projects. SOURCE Source

Spring Cloud Building MicroServices architecture-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.