Spring Cloud(05) -- Feign uses interface to call services

Spring Cloud(05) -- Feign uses interface to call services

In the last blog Spring Cloud(04) -- Introduction and use of Ribbon In, we know that in Ribbon, service invocation is realized by Ribbon combined with RestTemplate and specifying service name. This method obviously does not conform to the habit of interface oriented programming in Java, so Feign appears.

1. Feign introduction

  • Feign is a declarative and templated HTTP client developed by Netflix. Feign can help us call HTTP API more quickly and gracefully.

  • Feign is a more convenient client. It makes calling between micro services easier. It is similar to calling a service by a controller. It is like calling a local method. It doesn't feel like calling a remote method at all.

  • Feign can be used in combination with Eureka and Ribbon to support load balancing.

2. Feign usage steps

1. Services are provided in the restful interface. The restful interface module is spring cloud API, which is in the Spring Cloud(02) - build Rest Service Has been created in.

Service provider interface:

//Interface to provide services
@FeignClient(value = "SPRINGCLOUD-PROVIDER-DEPT")//Feign will automatically find a service named SPRINGCLOUD-PROVIDER-DEPT
@Repository
public interface DeptFeignClient {

    @PostMapping("/dept/add")
    public boolean add(Dept dept);


    @GetMapping("/dept/queryById/{deptNo}")
    public Dept queryById(@PathVariable("deptNo") Long deptNo);


    @GetMapping("/dept/queryAll")
    List<Dept> queryAll();

}

Add feign dependency

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-feign</artifactId>
    <version>1.4.6.RELEASE</version>
</dependency>

2. In order to distinguish the difference between feign and ribbon calling services, a feign module spring cloud consumer feign is created by imitating the service consumer (ribbon calling) module. Add feign dependency in feign module.

controller uses feign client:

@RestController
public class DeptConsumerController {

    @Autowired
    private DeptFeignClient deptFeignClient;//Implementation of @ FeignClient based on annotation


    @RequestMapping("/consumer/dept/add")
    public boolean add(Dept dept){
        return deptFeignClient.add(dept);
    }
    @RequestMapping("/consumer/dept/queryById/{deptNo}")
    public Dept queryById(@PathVariable("deptNo")Long deptNo){
        return deptFeignClient.queryById(deptNo);
    }

    @RequestMapping("consumer/dept/queryAll")
    private List<Dept> queryAll(){
        return deptFeignClient.queryAll();
    }
}

3. Add comments on the main startup class:

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = {"com.cheng.springcloud"})Specified FeignClient The package name of the interface class and open it feign client
public class Feign_Dept_Consumer_80 {
    public static void main(String[] args) {
        SpringApplication.run(Feign_Dept_Consumer_80.class,args);
    }
}

4. Start the program test and find that feign uses the default load balancing strategy and rotation strategy. This is because Spring Cloud Feign is based on the implementation of Netflix feign and integrates the Spring Cloud Ribbon.

3. The difference between feign and ribbon

Ribbon and Feign are both used to call other services, but in different ways.

**1.** The annotations used by the startup class are different. Ribbon uses @ RibbonClient and Feign uses @ EnableFeignClients.

**2.** The specified locations of services are different. Ribbon is declared on the @ RibbonClient annotation, while Feign uses the @ FeignClient declaration in the interface defining abstract methods.

**3.** Different calling methods,

  • Ribbon needs to build its own http request, simulate the http request, and then send it to other services using RestTemplate. The steps are quite cumbersome.

  • Feign makes an improvement on the basis of Ribbon. It uses the interface method to define the methods of other services that need to be called as abstract methods without building http requests. However, it should be noted that the annotation and method signature of the abstract method should be completely consistent with the method of providing the service.

Keywords: Spring Cloud ribbon feign

Added by Koobazaur on Sun, 23 Jan 2022 06:05:12 +0200