Feign

Feign is a declarative web service client, which makes it easier to write web service clients. Use Feign to create an interface and annotate it. It has pluggable annotation support including Feign annotations and JAX-RS annotations. Feign also Supports pluggable encoders and decoders, Spring Cloud adds annotations to Spring MVC, Spring Web uses HttpMessageConverters by default, Spring Cloud integrates the load balancing HTTP client Feign provided by Ribbon and Eureka.

First start the eureka_register_service project (registration center) and the biz-service-0 project (service provider)

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 class of the application

Start the 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

Use the @FeignClient("biz-service-0") annotation to bind the interface corresponding to the biz-service-0 service

@FeignClient ( "biz-service-0" )
public  interface  UserClient {
     @RequestMapping (method = RequestMethod.GET, value =  "/访问路径名称/{id}" )
     public  User getuserinfo(@PathVariale("id") Long id);//参数不为基本类型,是对象的时候只能使用post方法;
     
     @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
@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 variable

spring.application.name=feign-consumer
server.port= 8004
eureka.client.serviceUrl.defaultZone=http: //localhost:8000/eureka/

Summarize:

In fact, Feign encapsulates the HTTP call service method, so that the client can directly call the method like a local method, similar to the way Dubbo exposes remote services, the difference is that Dubbo is based on a private binary protocol, while Feign is essentially an HTTP client

Reprinted from: http://www.cnblogs.com/xiaojunbo/p/7094377.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325054636&siteId=291194637
Recommended