The previous article described how to consume services through RestTemplate+Ribbon. This article mainly describes how to consume services through Feign.
1. Introduction to Feign
Feign is a declarative pseudo Http client that makes writing Http clients easier. 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:
- Feign uses interface-based annotations
- 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 ports are 8762 and 8773.
3. Create a feign service
Create a new spring-boot project, named serice-feign, and introduce Feign's starting dependencies spring-cloud-starter-feign, Eureka's starting dependencies spring-cloud-starter-eureka, and Web's starting dependencies 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 and specify which service to call through @ FeignClient ("service name"). For example, the "/hi" interface of the service-hi service is called in the code, and the code is as follows:
/**
* Created by fangzhipeng on 2017/4/6.
*/
@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, a "/hi" API interface 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
Download the source code of this article:
https://github.com/forezp/SpringCloudLearning/tree/master/chapter3