Feign client load balancing

Source address: https://gitee.com/peachtec/springcloud

What is Feign

  Feign is a declarative web service client, which makes calls between microservices easier, similar to the controller calling service; SpringCloud integrates Ribbon and Eureka, which can provide a load-balanced http client when using Feign

What can Feign do

  Feign aims to make it easier to write java http clients.
  When Ribbon+RestTemplate is used, RestTemplate is used to encapsulate http requests to form a set of templated invocation methods; however, in actual development, due to the invocation of services It may not be in one place. Often an interface will be called in multiple places. Therefore, some client classes are usually encapsulated for each microservice to encapsulate the calls of these microservices. Therefore, Feign has further encapsulated on this basis. To help us define and implement the definition of the dependent service interface, under the implementation of Feign, we only need to create an interface and use annotations to configure it (similar to the previous Dao interface with Mapper annotations, now it is on the microservice interface Just mark the Feign annotation); the interface binding to the service provider can be completed, which simplifies the concurrency of automatically encapsulating the service and calling the client when the Spring Cloud Ribbon is used.

Feign integrates Ribbon

  Use Ribbon to maintain the service list information of microservices, and implement load balancing on the client side through polling. Unlike Ribbon, only the service binding interface needs to be defined through Feign and the implementation is elegant and simple in a declarative way. Service call
  

Use Feign

  The project code continues to reference the previous code, but the service consumer module is rebuilt to facilitate the distinction when you look at it later

  1. Import Feign's jar package in the public service module, and write public interface classes to facilitate calling in other modules
<!-- Feign -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
//Feign客户端注解,并指服务名,在调用时会自动去找服务下面的接口
@FeignClient(value = "SPRINGCLOUD-SERVER")
@Component
public interface ClientService {
    
    
    @PostMapping("/dept/add")
    boolean addDept(Dept dept);

    @GetMapping("/dept/get/{id}")
    Dept queryById(@PathVariable("id") Integer id);

    @GetMapping("/dept/get")
    List<Dept> queryAll();
}
  1. In the service consumer module, import Feign's dependencies and rewrite the control layer interface
<!-- Feign -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
@RestController
@RequestMapping("/dept")
public class ConsumerApi {
    
    
	//通过去调用公共模块的接口的方式去访问微服务
    @Autowired
    private ClientService clientService;

    @GetMapping("/get/{id}")
    public Dept getDeptById(@PathVariable Integer id) {
    
    
        return clientService.queryById(id);
    }

    @PostMapping("/add")
    private boolean addDept(Dept dept) {
    
    
        return clientService.addDept(dept);
    }

    @GetMapping("/get")
    private List<Dept> queryAll() {
    
    
        return clientService.queryAll();
    }
}
  1. Open Feign in the startup class and specify the package to be scanned
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = {
    
    "com.peach"})
@ComponentScan("com.peach")
public class FeignResumer_80 {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(FeignResumer_80.class, args);
    }
}
  1. Open the test, the default is that the round training
      
      Ribbon is based on the RestFul style, and Feign's annotation-based, readability may be better, but it still uses the Ribbon implementation method at the bottom, which is equivalent to putting a layer of shell on the outside. Therefore, its performance is not as high as Ribbon; both methods can be used in actual development.

Guess you like

Origin blog.csdn.net/weixin_45481406/article/details/110292130