Feign Feign is a declarative pseudo Http client, which makes it easier to write Http clients. With Feign, you just need to create an interface and annotate it. It has a pluggable annotation feature and can use Feign annotations and JAX-RS annotations. Feign supports pluggable encoders and decoders. Feign integrates Ribbon by default and combines with Eureka to achieve load balancing by default.
In short:
1. Feign uses interface-based annotations
2. Feign integrates ribbon
2. Preparations
Continue to use the project in the previous section, start eureka-server, the port is 8761; start service-hi twice, the port They are 8762 and 8773 respectively.
3. Create a feign service
Create a new spring-boot project named serice-feign, and introduce Feign's starting dependencies spring-cloud-starter-feign and Eureka into its pom file, spring-cloud-starter-feign, Eureka's starting dependencies spring -cloud-starter-eureka, Web start depends on spring-boot-starter-web, the code is as follows:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.forezp</groupId> <artifactId>service-feign</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>service-feign</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</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>Dalston.RC1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
In the project configuration file application.yml file, specify the program name as service-feign, the port number as 8765, and the service registration address as http://localhost:8761/eureka/ , the code is as follows:
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ server: port: 8765 spring: application: name: service-feign
In the startup class ServiceFeignApplication of the program, add the @EnableFeignClients annotation to enable Feign's function:
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class ServiceFeignApplication { public static void main(String[] args) { SpringApplication.run(ServiceFeignApplication.class, args); } }
Define a feign interface, through @ FeignClient ("service name") , to specify which service to call. For example, the "/hi" interface of the service-hi service is called in the code, and the code is as follows:
@FeignClient(value = "service-hi") public interface SchedualServiceHi { @RequestMapping(value = "/hi",method = RequestMethod.GET) String sayHiFromClientOne(@RequestParam(value = "name") String name); }
In the controller layer of the Web layer, an API interface of "/hi" is exposed to the outside, and the service is consumed through the Feign client ScheduleServiceHi defined above. code show as below:
@RestController public class HiController { @Autowired SchedualServiceHi schedualServiceHi; @RequestMapping(value = "/hi",method = RequestMethod.GET) public String sayHi(@RequestParam String name){ return schedualServiceHi.sayHiFromClientOne(name); } }
Start the program, visit http://localhost:8765/hi?name=forezp multiple times, the browser will display alternately:
hi forezp,i am from port:8762
hi forezp,i am from port:8763
Feign source code analysis: http:/ /blog.csdn.net/forezp/article/details/73480304
from: http://blog.csdn.net/forezp/article/details/69808079