Spring Cloud Cognitive Learning: The Use of Declarative Calls to Feign


(viii)After the previous article introduced the construction of microservices, Ribbon was used to solve load balancing problems for multiple services. Spring Cloud Cognitive Learning (2): Ribbon Use

(viii)This article introduces a component, Fegin, that is used to declaratively invoke services, mainly to solve the previous problem of tight coupling between service calls and restTemplate.


(viii)Feign is used to declaratively invoke services
(viii)In the above service calls, we have always been able to get rid of restTemplate, which is always used to initiate our calls to other services.Think about how we developed it before (three-tier architecture, controllercall service,service call dao), controllercall service, feign is generated for this Interface-oriented programming requirement.Why does he say he can program for interfaces?Let's show it next.

Use examples

Code reference: Feign Simple Use Experiment
Considering that a service may have multiple consumers, we write common code into spring-cloud-common-data so that all consumers can inherit this dependency package to obtain Feign-modified interfaces.

1. Import dependencies:

Import dependencies in spring-cloud-common-data:

        <!--increase feign rely on start-->
            <!--Old version-->
        <!--increase feign rely on end-->

2. New Feign Interface

Create an interface in spring-cloud-common-data:

package com.progor.study.service;

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

import java.util.Map;

// Since there may be more consumers of this service, it is placed on a common dependency.
@FeignClient(value = "MESSAGESERIVE")
public interface MessageService {
    // Use RequestMapping here to map the Service provider's methods to local Service methods
    @RequestMapping(value = "/msg/list", method = RequestMethod.GET)
    Map<String, String> list();

(viii)@FeignClient is used to configure information to which service the current interface corresponds.The effect of matching @RequestMapping is to call the corresponding API interface of the corresponding service.The effect of the above code is to get a service example named MESSAGESERIVE from eureka and call the/msg/list routing method of the service instance.

3. Create service consumers

3.1 Create a MessageController 2 in the service consumer spring-cloud-user-consumer-80 and inject the MessageService:

package com.progor.study.Controller;

import com.progor.study.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

// This controller is used to handle situations where fegin is used

public class MessageController2 {

    // Inject MessageService
    private MessageService messageService; 

    public Map<String, String> list() {
        return messageService.list();


3.2. Enable Feign on spring-cloud-user-consumer-80:
(viii)Since fegin dependencies are imported into spring-cloud-common-data, no more imports are required here.

@RibbonClient(name = "USERSERIVE", configuration = MyConfig.class)
@EnableFeignClients // Use feign
public class UserConsumer80Application {
    public static void main(String[] args) {
        SpringApplication.run(UserConsumer80Application.class, args);

4. Testing

4.1: Start spring-cloud-eureka-server-7001, spring-cloud-message-service-8004, spring-cloud-message-service-8005

4.2: Access http://localhost/msg2/list, which calls the API of MessageController 2, which calls the Fegin encapsulated method of MessageController 2.
If access succeeds, it means we succeeded with Feign.
As you can see from the above, using Feign, we can call Service to invoke business logic as before.


  • Feign is load balanced by default. Look at the spring-cloud-starter-openfeign dependency package and you will see that it imports the spring-cloud-starter-netflix-ribbon dependency
  • More includes how it works, which will be explained in a single chapter, croaking.

Tags: Java Spring Programming

Posted on Fri, 15 May 2020 00:37:08 -0400 by injh85