Spring cloud Alibaba series (3) using feign to call services


What is feign

Feign is a declarative pseudo HTTP client provided by spring cloud. It makes calling remote services as simple as calling local services. It only needs to create an interface and add a one-day annotation.

Nacos is well compatible with feign. Feign inherits ribbon by default, so load balancing is achieved by using feign default under Nacos.

Load balancing strategy supported by ribbon

Load balancing is to allocate requests to multiple instances for processing.

According to the location of load balancing, it is generally divided into server-side load balancing and client-side load balancing.

Server side load balancing refers to the service provider side, such as the common nginx load balancing.

Client load balancing refers to the side of the service request, which instance has been selected to process before the service request.

In microservices, we usually choose the client load balancing, and ribbon is the load balancing on the client.

Ribbon has a variety of built-in load balancing strategies. The top-level interface of internal load balancing is as follows: com.netflix.loadbalancer . iRule. The specific load strategy is shown in the following figure:
Spring cloud Alibaba series (3) using feign to call services

How to use feign

Using the Nacos discovery server in Chapter 1 as the service provider, we provide an interface of “/ Hello”. Create a new Alibaba client feign service as a consumer.

  1. If openfeign is dependent on POM file


  2. Add feign support annotation on the startup class

@Enable feign clients / / enable feign

  1. Create a feign client and use feign to call microservices

    class TestController{
    Client client;
    String test(){

return client.hello(“hahaha”);

interface Client{
String hello(@RequestParam String name);

Note: in actual development, feign clients are written in the public module, which service needs to be called and introduced into the public module.

Two ways to realize feign

  1. The first one is introduced in this paper,Feign is consistent with the requestmapping of the producer
  2. The second is to provide all the requested interfaces in the feign client, and then the controller implements the feign client, so it is unnecessary to write duplicate code.

Attention point

  1. The request parameter is an object, using @ requestbody
  2. When the request parameter is received with @ requestparam, the value attribute must be added, such as @ requestparam (“name”)
  3. When the request parameter is received with @ pathvariable, the value attribute, such as @ pathvariable (“name”), must be added

Recommended Today

Blog based on beego, go blog

Go Blog A beego based development, can quickly create personal blog, CMS system Include functions see Official website of go bloggo-blog.cn Demo siteleechan.online Update log time function January 23, 2020 New top post function February 2, 2020 New custom navigation function February 4, 2020 New site announcement function February 6, 2020 New link module February […]