Spring cloud entry notes Feign declarative HTTP call

Keywords: Spring REST xml

In spring cloud, every microservice is a service provider as well as a service consumer, and each microservice often needs to call each other. Generally, services provide some REST interfaces for external services to call. Feign is a component that provides explicit HTTP calls between services. With feign, we can use spring MVC annotations to create HTTP clients that access other service interfaces.

This article takes the user MS and role MS microservices built earlier as examples to configure Feign components.

Configure Feign components

Add the following dependencies to pom.xml:


Add @ EnableFeignClients annotation to the startup class, as follows:

public class UserMsApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserMsApplication.class, args);

Create a FeignClient interface in user MS as Feign client, and access role MS micro service through spring MVC annotation, as shown below:

@FeignClient(name = "role-ms")
public interface RoleFeignClient {

    @RequestMapping(value = {"/role/list"}, method = RequestMethod.GET)
    Object list();

    @RequestMapping(value = "/role/insert", method = RequestMethod.POST)
    String insert(@RequestBody Role role);


Define the interface to access role MS in UserController as follows:

@RequestMapping(value = "/user")
public class UserController {
    private UserService userService;
    private RoleFeignClient roleFeignClient;

    @RequestMapping(value = {"/list", ""}, method = RequestMethod.GET)
    public Object list() {
        List<User> users = userService.findAllList();
        return users;

     * Access role MS microservice to get role list
     * @return Role list
    @RequestMapping(value = {"/role/list"}, method = RequestMethod.GET)
    public Object roleList() {
        return roleFeignClient.list();

     * Access role MS microservice and save role data
     * @param role Role data to be saved
     * @return Data saving results
    @RequestMapping(value = {"/role/insert"}, method = RequestMethod.POST)
    public Object roleInsert(@RequestBody Role role) {
        return roleFeignClient.insert(role);



Through the postman test service, the results are as follows:

Posted by Shibby on Wed, 04 Dec 2019 04:20:22 -0800