First acquainted with Ribbon service call

The previous articles have briefly introduced the related technologies of the service registry. Now let’s take a brief look at Ribbon:

One: What is Ribbon, what is the difference between it and nginx?

 Ribben is the client, so in this case it is on the consumer side.

the difference:

When I learned eureka before, I used the Ribbon for remote call RPC, so I didn't add the dependency of Ribbon before. Why is the ribbon used by default? Look at the picture below:

<!--eureka-server  2020.02最新版的eureka服务【客户端】-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

 

Therefore, if the ribbon dependency is already included, there is no need to add it.

 Therefore: Ribben mainly has two functions, one: to realize the load balancing of the client. Two: the service remote call (RPC) uses the RestTemplate; plus @LoadBalanced gives the RestTemplate the ability to load balance.

Therefore, to use ribbon, first of all RestTemplate is the foundation, and the following is a brief description:

There are two methods in the above picture: getForObject(), getForEntity(), the difference has been introduced in the blue text above, and there are two other main methods postForObject() and postForEntity().

 Two: There are a total of 7 methods for Ribbon load calling rules, among which polling is the default. For the specific 7 methods, see the following figure:

If you no longer use the polling method and want to change to another method, such as random, how to deal with it? See the figure below for the steps:

Pay attention to the configuration details mentioned in the figure above: 

 Since the rules need to be changed, the official website clearly pointed out that the custom configuration class cannot be placed under the current package and sub-reports scanned by @componentScan, so create a new package and put the custom configuration class in it.

MyselfRule: custom rule configuration class:

 Modify the main function and load the newly created configuration class:

At this point, it is modified.

1. Supplement: eureka uses ribbon by default to achieve load balancing and remote invocation. The bottom layer is explained by a picture:

2. Reference: https://blog.csdn.net/qq_40794266/article/details/85162882   [Client Load Balancer Ribbon integrates Eureka to achieve remote calls] Better understand! ! !

Guess you like

Origin blog.csdn.net/zhangleiyes123/article/details/106786083