Details of SpringCloud's Feign example
- 2020-12-20 03:38:03
- OfStack
Feign profile
Feign is a declaration of web service client, which makes it easier to write web service client. It uses Feign to create an interface and annotate it. It has pluggable annotation support including Feign annotation and ES9en-ES10en annotation, Feign also supports pluggable encoder and decoder. Spring Web uses HttpMessageConverters by default, Spring Cloud integrates Ribbon and Eureka provides load balancing HTTP client Feign.
Declarative REST client: Feign
Start the eureka_register_service project (registry) and biz-service-0 project (Service Producer)
Create an maven project eureka_feign_client
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Brixton.SR5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Enable Feign functionality in the application main class with the @EnableFeignClients annotation
The startup file FeignApplication.java
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
Define the service interface class UserClient.java
Bind the interface to the ES66en-ES67en-0 service using the @FeignClient (" ES64en-ES65en-0 ") annotation
@FeignClient("biz-service-0")
public interface UserClient {
@RequestMapping(method = RequestMethod.GET, value = "/getuser")
public User getuserinfo();
@RequestMapping(method = RequestMethod.GET, value = "/getuser")
public String getuserinfostr();
@RequestMapping(method = RequestMethod.GET, value = "/info")
public String info();
}
Call UserController as defined above in the web layer, as follows
@RestController
public class UserController {
@Autowired
UserClient userClient;
@RequestMapping(value = "/getuserinfo", method = RequestMethod.GET)
public User getuserinfo() {
return userClient.getuserinfo();
}
@RequestMapping(value = "/getuserinfostr", method = RequestMethod.GET)
public String getuserinfostr() {
return userClient.getuserinfostr();
}
@RequestMapping(value = "/info", method = RequestMethod.GET)
public String info() {
return userClient.info();
}
}
application. properties configuration variables
spring.application.name=feign-consumer
server.port=8004
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
Visit http: / / 127.0.0.1:8004 / getuserinfo
Conclusion:
In fact, Feign encapsulates HTTP's invocation of the service method, making the client invoke the method directly like calling the local method, similar to the way in WHICH Dubbo exposes the remote service. The difference is that Dubbo is based on the private base 2 protocol, while Feign is essentially an HTTP client