[Spring Cloud]-Basic use of micro service communication client Feign

Posted Jun 15, 20203 min read


Through the definition of Feign, we know that Feign is a client
So when configuring Feign into the project, you only need to configure Feign to the service caller.
Through interface call, access to Http request is realized.(Similar to the way Mybatis accesses the database)


Before Feign, we have been using RestTemplate to implement calls between microservices. If a small amount is okay, but with the increase of microservices, the complexity of their mutual calls also increases exponentially. So there is the interface-style http tool Feign(Feign follows Retrofit). These things can refer to follow me to learn Spring Cloud(Finchley version)-09-Feign


  1. Connect to Eureka server(Feign will be called by the microservice name on Eureka server)
    _Note:This step is optional, Feign can also directly access the service through url, but this article does not involve _

  2. Add Feign dependency and specify Feign version

  3. After adding the dependencies of eureka and Feign, add corresponding annotations on the main method of the client project:

  4. Write the FeignClient interface, which corresponds to the back-end microservices-similar to Mybatis mapper
    Interface => backend microservice project
    Interface method => Microservice RequestMapping

     //Corresponding to the microservice named Feign-provider1 in Eureka
     @FeignClient(name = "feign-provider1")
     public interface UserFeignClient {
     //Corresponding to the Get request named hello in this microservice
     String findString();
  5. Use this interface(send request)

    At this time, whether it is in a common method, or a filter, controller, you can perform the operation of requesting the call.
    Here I use the most introductory way:Put the request in the Controller.
    code show as below:

     public class IndexController {
         UserFeignClient userFeignClient;
         public IndexController(UserFeignClient userFeignClient) {
         this.userFeignClient = userFeignClient;
         public String index(){
         return userFeignClient.findString();

    At this time, if you visit localhost:XXX/test, the request will be automatically handed over to the back-end microservice for processing and return.


About specifying Feign version:

  • You can add the version tag directly to the dependency in the second step above

  • You can also let Spring cloud manage its version, this way see the following code:

    • The second method is recommended. It is more convenient to manage with Spring Cloud and easy to upgrade.

Author's Note: _ Spring Cloud is known as a large version(Hoxton.SR4 in this example), but you want to add dependencies in the first way, so how do you determine a specific dependency version? _

In pom.xml, hold down the ctrl key and click on the groupid or artifactId in dependencyManagement to open the pom of the current version of spring cloud. All the minor versions of the projects provided under this version of spring cloud are declared in it. Just use it.

You can also find it in the local maven repository. For example, the pom.xml of SR4 is in the path C:\Users\XXX\.m2\repository\org\springframework\cloud\spring-cloud-dependencies\Hoxton.SR4\spring-cloud- dependencies-Hoxton.SR4.pom
For example, I have the following large versions locally:

Related Posts