Spring Cloud Starter series four - Declarative calling component Feign and client load balancer Ribbon

1 What is Feign?

Feign is a pseudo HTTP client a declarative, we just need to create an interface and can be annotated using Feign. It has pluggable annotation feature can be used Feign JAX-RS annotations and notes, pluggable encoder and decoder. Feign default integrated Ribbon, and Eureka and combined, the default implementation of load balancing.

2 Getting real

In fact, I've covered in previous blog had Feign, the specific implementation code can refer to on a blog, here it is just excerpt some of them to explain.

package com.example.EurekaConsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class EurekaConsumerApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaConsumerApplication.class, args);
	}

}

If we need to use Feign function, you need to add @EnableFeignClients comment open Spring Cloud Feign support functions on startup class.

package com.example.EurekaConsumer.Service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Component
@FeignClient(name= "producer")//name为远程服务名,即远程服务spring.application.name设置的名字
public interface HelloWorldService {

	//调用方法需要与远程服务中的方法名和参数一致
	@RequestMapping("/helloWorld")
    public String helloWorld(@RequestParam(value = "id")int id,@RequestParam(value = "saying")String saying);
}

Interface @FeignClient annotation specifies the service name to bind the service, the method we use Spring MVC annotation REST interface to bind specific services provided.

Note that there is a point, @FeignClient(name= "producer")the service name is not case sensitive.

3 Load balancing entry

Load balancing There are two main ways, one server load algorithm, one client load algorithms. Server algorithm on the server side implementation, mainly through the reverse proxy way to distribute client requests to available nodes, the process for the client -> server load balancing -> provide real business server -> Load Balancing Server -> Client end, e.g. Nginx. The load balancing algorithm client algorithm in client implementations, when initiated the request to the server, perform algorithm to calculate which initiates a request to the server, and then initiate a request to the server, such as Ribbon.

4 Ribbon Introduction

Ribbon is an intermediate layer Netflix released cloud services open source project, and its main function is to achieve load balancing algorithm on the client. Ribbon client component features a variety of configuration items, such as connection timeout, retry the like. Simply put, Ribbon is a client load balancer.

In the Spring Cloud, Ribbon when used in conjunction with the Eureka, Eureka Ribbon Server can be acquired from the service provider list of addresses and load balancing algorithm, wherein a service provider requesting instance. Ribbon's work is divided into two steps: Step 1. Select Eureka Server, it preferred the same Zone and less loaded Server in; the second step and then based on user-specified policies, and then choose from a list to take to the registration service in Server an address.

Spring Cloud Feign already integrated by default Ribbon.

5 Ribbon load balancing strategy

Ribbon a total of seven strategies, let's introduce them separately.

  1. RoundRobinRule: That poll strategy is the default strategy Ribbon
  2. RandomRule: ie, random tactics
  3. BestAvailableRule: The strategy will choose the smallest server concurrent requests
  4. AvailabilityFilteringRule: selected according to the state of the server
  5. WeightedResponseTimeRule: selected according to the response time
  6. RetryRule: according to the selected load balancing policy and retry mechanism
  7. ZoneAvoidanceRule: selected according to Zone status and service status

6 Ribbon combat

Since Spring Cloud Feign tacitly Ribbon integrated, so the Ribbon on the basis Feign, Ribbon not need to import dependence.

Our project is implemented in the prior two blog basis, it is recommended not to see my previous two blog friends look at.

We then create a project, the project name was changed to EurekaProducerCopy, then use port 8083 instead of other configurations can be the same as our previous EurekaProducer. Then we were running Eureka, EurekaProducer, EurekaProducerCopy, EurekaConsumer four projects.

Eureka open interface, we found that there are two service providers.
Here Insert Picture Description
Enter your browser to http: // localhost: 8082 / helloWorld id = 4 & saying =% E7% 89% 9B% E9% 80% BC five times that show?

From 8080: No. 4 released for users of a new message: regressed
from 8083 Ports: Serial posted a new message for the user 4: regressed
from 8080: No. 4 user posted a new message: Cattle force
from 8083 ports: serial number 4 users posted a new message: regressed
from 8080: No. 4 user posted a new message: Niubi

We found two results appear alternately, indicating that the service has achieved the load balancing functionality, and client load balancer Ribbon default policy for polling policies.

If we want to change that strategy, such as switching to a random strategy, how should we do? EurekaConsumer modify the configuration file, add a line configuration

# 使用随机策略,producer是作用到的微服务名
producer.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule

Now we re-run the project, type in the browser http: // localhost: 8082 / helloWorld id = 4 & saying =% E7% 89% 9B% E9% 80% BC five times, and the results are shown below?:

Port from 8083: No. 4 user posted a new message: regressed
from 8080: No. 4 released for users of a new message: regressed
from 8080 Ports: Serial posted a new message for the user 4: Cattle force
from 8083 ports: serial number 4 users posted a new message: regressed
from 8083 ports: serial number 4 users posted a new message: Niubi

We can see two random service is called, the configuration that we have entered into force.

Published 113 original articles · won praise 206 · views 10000 +

Guess you like

Origin blog.csdn.net/Geffin/article/details/102757256