Feign's understanding and examples

1, What is Feign

Feign is a lightweight open-source Rest client of Netflix company. Spring cloud encapsulates feign. Feign integrates Ribbon by default to realize client load balancing calls. To use feign, you only need to define an interface and configure it with annotations (just add annotations on the interface).

There are two ways to call between microservices:

1. Obtain the call address of the service through the name of the microservice -- Ribbon

2. Get the call of service through interface + annotation --- Feign

It is similar to using the @Mapper annotation on the Mapper interface for identification, while using Feign only needs to mark the @Feign annotation on the interface.

2, Feign instance

2.1 create Feign module (only slightly modify the previous consumer module)

2.2 configuration of pom.xml (add configuration of feign)

<!--feign rely on--> 

2.3 create a new ProductClientService interface

The /product/list, /product/get/{id} and /product/add interfaces of the MICROSERVICE-PRODUCT service are invoked in the code. The code is as follows:

package com.dsx.service;

import java.util.List;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.dsx.domain.Product;
//Specify the called service MICROSERVICE-PRODUCT
public interface ProductClientService {
	@RequestMapping(value = "/product/get/{id}",method = RequestMethod.GET) 
	Product get(Long id); 
	@RequestMapping(value = "/product/list",method = RequestMethod.GET) 
	List<Product> list(); 
	@RequestMapping(value = "/product/add",method = RequestMethod.POST) 
	boolean add(Product product);

2.4 create consumercontroller? Feign

package com.dsx.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dsx.domain.Product;
import com.dsx.service.ProductClientService;

public class ConsumerControler_Feign {
    private ProductClientService service;

    @RequestMapping(value = "/consumer/product/add")
    public boolean add(Product product) {
        return service.add(product);

    @RequestMapping(value = "/consumer/product/get/{id}")
    public Product get(@PathVariable("id") Long id) {
        return service.get(id);

    @RequestMapping(value = "/consumer/product/list")
    public List<Product> list() {
        return service.list();

2.5 modify startup class

package com.dsx;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

//The interface marked with @ FeignClient annotation under the specified package will be scanned, and the proxy object of this interface will be generated
@EnableFeignClients(basePackages= {"com.dsx"})
@EnableEurekaClient  //Identify Eureka client
public class ConsumerApp_Feign {
	public static void main(String[] args) {
		SpringApplication.run(ConsumerApp_Feign.class, args);

2.6 function test

Start 2 Eureka clusters

Start 2 product providers 8001 / 8002
Start consumerapp? Feign
Visit http://localhost/consumer/product/list It is found that it has the same effect of load balancing.
3, How Feign works
Feign calls the Rest Service (previously Ribbon+RestTemplate) through the interface method, and sends the request to Eureka server. Feign finds the service interface directly through feign. Because of the integration of Ribbon technology, feign has its own load balancing configuration function.
1. Add @ EnableFeignClients annotation to the startup class. Spring will scan the interface marked with @ FeignClient annotation and generate a proxy for this interface
2. @ FeignClient("service name") specifies the product service name. Feign will obtain the product service list from Eureka registration center,
And through the load balancing algorithm for service calls.
3. Use the annotation @ RequestMapping(value = "/product/list",method = RequestMethod.GET) in the interface method to specify the call
Feign will make a remote call based on the url.
4, feign notes
Spring cloud has enhanced Feign and is compatible with spring MVC annotations. When using spring MVC annotations, we need to pay attention to:
1. The @ FeignClient interface method has a basic type parameter, @ PathVariable("XXX") or @ RequestParam("XXX") must be added to the parameter
2. When the return value of the @ FeignClient interface method is a complex object, this type must have a parameter constructor
Feign is a declarative web service client, which makes calls between microservices easier.
Published 109 original articles, won praise 1, visited 5629
Private letter follow

Tags: Spring REST Java xml

Posted on Sun, 16 Feb 2020 05:23:45 -0800 by ManInBlack