Introduction to Feign
Feign is a declared web service client, which makes it easier to write a web service client. Use Feign to create an interface and annotate it. It has pluggable annotation support, including Feign annotation and JAX-RS annotation. Feign also supports pluggable encoder and decoder. Spring Cloud adds annotation to Spring MVC. Spring Web uses HttpMessageConverters by default, and Spring Cloud integrates the load-balanced HTTP client Feign provided by Ribbon and Eureka.
Declarative REST client: Feign
First, start the eureka_register_service project (registration center) and biz-service-0 project (service producer)
Create a 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 function through @EnableFeignClients annotation in the main application class
Start the file FeignApplication.java
@SpringBootApplication@EnableDiscoveryClient@EnableFeignClientspublic class FeignApplication { public static void main(String[] args) { SpringApplication.run(FeignApplication.class, args); }}Define service interface class UserClient.java
Use the @FeignClient("biz-service-0") annotation to bind the corresponding biz-service-0 service of this interface
@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 the UserController defined above in the web layer, as follows
@RestControllerpublic 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-consumerserver.port=8004eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
Visit http://127.0.0.1:8004/getuserinfo
Summarize:
In fact, the HTTP call service method is encapsulated through Feign, so that the client calls the method directly like calling a local method. Similar to the way of exposing remote services in Dubbo, the difference is that Dubbo is based on a private binary protocol, while Feign is essentially an HTTP client.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.