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

Posted Jun 15, 20203 min read

What

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)

why

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

How

  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

     <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-openfeign</artifactId>
     <version>2.2.2.RELEASE</version>
     </dependency>
  3. After adding the dependencies of eureka and Feign, add corresponding annotations on the main method of the client project:

     @SpringBootApplication
     @EnableEurekaClient
     @EnableFeignClients
  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
     @GetMapping("hello")
     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:

     @RestController
     public class IndexController {
    
         UserFeignClient userFeignClient;
    
         @Autowired
         public IndexController(UserFeignClient userFeignClient) {
         this.userFeignClient = userFeignClient;
         }
    
         @GetMapping("/test")
         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.

Questions

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:

      <properties>
          <spring-cloud.version>Hoxton.SR4</spring-cloud.version>
      </properties>
      <dependencies>
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-starter-openfeign</artifactId>
          </dependency>
          <dependency>
              <groupId>org.springframework.cloud</groupId>
              <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
          </dependency>
      </dependencies>
      <dependencyManagement>
          <dependencies>
              <dependency>
                  <groupId>org.springframework.cloud</groupId>
                  <artifactId>spring-cloud-dependencies</artifactId>
                  <version>${spring-cloud.version}</version>
                  <type>pom</type>
                  <scope>import</scope>
              </dependency>
          </dependencies>
      </dependencyManagement>
    • 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.
?
image.png

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:
image.png


Related Posts