Spring Cloud (Greenwich Edition)-05-Spring Cloud Integration Ribbon (Guest

image

Preface

The registration of the product and user microservices was completed in the previous section. It is based on the call between point-to-point microservices. In the actual scenario, the microservices are all deployed in clusters. The call relationship is shown in the following figure:


image


When you see this, everyone will think that it is load balancing ! Nginx is often used in projects to solve the service cluster problem in the figure. What if there are 1,000+ or ​​even 10,000+ services? Every time a service is added, do I need to configure it in Nginx and then restart it? Uh... think about it, my scalp is tingling! And Nginx's load balancing mode is different from what this article will talk about.

Then this chapter will use Ribbon components to solve the above problems.

Introduction to Ribbon and usage scenarios

In general, Ribbon is a load balancer used to control HTTP and TCP client behavior. Ribbon obtains service configuration information from the configuration service provider list library (for example: Eureka, Zookeeper), and calculates the request based on the load balancing algorithm Target service address.

In Spring Cloud, Ribbon is usually used with Eureka. Ribbon can automatically obtain a list of service provider addresses from Eureka Server and select one of the service provider instances based on the load balancing algorithm. The following figure shows the general structure of Ribbon when used with Eureka.


image


Spring Cloud integrated Ribbon

Step 1: Copy products and user microservice projects

Copy the project microservice-consumer-goods and modify the ArtifactId content in the pom.xml file to microservice-consumer-goods-ribbon. As shown below:

image

Copy commodity microservices

Copy the project microservice-provider-user and modify the ArtifactId content in the pom.xml file to microservice-provider-user-ribbon. As shown below:

image


Note: Remember to modify the port, otherwise there will be port conflicts when starting up.

Step 2: Add Ribbon dependency

Since spring-cloud-starter-netflix-eureka-client already includes spring-cloud-starter-netfilx-ribbon, this step can be omitted.

Step 3: Add @LoadBalanced annotation support on RestTemplate
MicroserviceConsumerGoodsRibbonApplication.java
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
   return new RestTemplate();
};

Yes, you read that right, it's over here! Just add the LoadBalanced annotation to let RestTemplate integrate Ribbon.

Step 4: Modify and call user microservices
GoodsController.java
@GetMapping("/goods/{id}")
   public User findById(@PathVariable Long id){
       return this.restTemplate.getForObject("http://microservice-provider-user/simple/"+id,User.class);
}

Earlier, we used the method of IP+port+target service endpoint when calling user microservices. Here we change the request target to the form of http://{target service name}/{target service endpoint}. In this case, Ribbon will automatically replace the target service name with the IP and port of the service when it is actually called .

Step 5: Start the test
  • Start microservice-discovery-eureka

  • Start microservice-provider-user

  • Start microservice-provider-user-ribbon

  • Start microservice-consumer-goods-ribbon

image

Microservice registration

As shown in the figure above, two user microservices and one commodity microservice are registered to Eureka Server. Since the names of the two user microservices are both microservice-provider-user, they will automatically be identified as the same cluster service, which is the above Commodity microservice calls the target service name of the user microservice.

Visit http://127.0.0.1:8091/goods/2
10 times, and you will find that both user microservice examples will print logs and each is requested 5 times.

Matching code

Main code base: https://github.com/yundianzixun/spring-cloud-study


Guess you like

Origin blog.51cto.com/15127576/2667934