What is Feign
Feign is a declarative pseudo HTTP client provided by Spring Cloud that makes calling remote services as simple as calling local services, creating an interface and adding one-day annotations.
Nacos is compatible with Feign, which inherits the Ribbon by default, so using Feign in Nacos provides load balancing by default.
Load balancing policies supported by the Ribbon
Load balancing is the process of splitting requests among multiple instances.
Load balancing can be classified into server load balancing and client load balancing based on the location of load balancing.
Server-side load balancing refers to what happens on the service provider side, such as the common NGINx load balancing.
Client-side load balancing occurs on the side of the service request, which instance is selected before the service request is processed.
We use client load balancing in microservices. The Ribbon loads on the client.
Ribbon built in a variety of load balancing strategy, internal load balance of the top interface to: com.net flix. Loadbalancer. IRule, specific load strategy as shown in the figure below:
Strategy class | named | describe |
---|---|---|
RandomRule | Random strategy | Randomly select server |
RoundRobinRule | Polling strategy | Select servers in order (Ribbon default policy) |
RetryRule | Retry strategy | During a configuration period, if the server selection fails, the system keeps trying to select an available server |
BestAvailableRule | Minimum concurrency strategy | Check the servers one by one. If the server circuit breaker is on, ignore it and select the server with the lowest concurrent connection |
AvailabilityFilteringRule | Available filtering policies | Filter out servers that tripped consistently and were labeled circuit tripped, those with high concurrent links (active connections tripped above configured threshold) |
ResponseTimeWeightedRule | Response time weighted weighting strategy | The weights are assigned according to the response time of the server. The longer the response time, the lower the weight and the lower the probability of being selected. The shorter the response time, the higher the weight, the higher the probability of being selected, this strategy is very appropriate, integrated various factors, such as: network, disk, IO, etc., all directly affect the response time |
ZoneAvoidanceRule | Regional weight strategy | To comprehensively determine the performance and availability of the server in the region where the server is located, select the server in polling and determine whether the operating performance of an AWS Zone is available, and eliminate all servers in the unavailable Zone |
How to use Feign
We use nacos-Discovery-Server from Chapter 1 as the service provider, providing a “/hello” interface. Create a new Alibaba-client-Feign service as a consumer.
- Pom files are provided as openFeign dependencies
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
Copy the code
- Annotate the startup class with support for FeIGN
@EnableFeignClients / / open feign
Copy the code
- Create a Feign client and invoke the microservice using Feign
@RestController
class TestController{
@Autowired
Client client;
@GetMapping("/test")
String test(a){
return client.hello("hahaha"); }}@FeignClient(name="nacos-discovery-server")
interface Client{
@GetMapping("/hello")
String hello(@RequestParam String name);
}
Copy the code
Note: In real development, the Feign client is written in the public module, which service needs to be called into the public module.
Two implementations of Feign
- The first is described in this article, where Feign and producer RequestMapping are aligned.
- The second option is to provide the interface for all requests in a feignClient, which the Controller then implements so that it doesn’t have to write duplicate code.
Pay attention to the point
- Request parameters for the object is, using @requestBody
- RequestParam (@requestParam(“name”));
- Request parameters received with @pathvariable must have a value attribute, such as @pathvariable (“name”).