Feign是一个声明式的伪Http客户端,它使得写Http客户端变得更简单。使用Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用Feign 注解和JAX-RS注解。Feign支持可插拔的编码器和解码器。Feign默认集成了Ribbon,并和Eureka结合,默认实现了负载均衡的效果。
简而言之:
- Feign 采用的是基于接口的注解
- Feign 整合了ribbon,具有负载均衡的能力
- 整合了Hystrix,具有熔断的能力
首先创建一个工程:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-cloud-feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud-feign</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.6.RELEASE</version>
</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>${spring-cloud.version}</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>
</project>
2 启动类配置
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.ConfigurableEnvironment;
import feign.Request;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class SpringCloudFeignApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudFeignApplication.class, args);
}
// @Bean
// public static Request.Options requestOptions(ConfigurableEnvironment env){
// return new Request.Options(1,1);
// }
}
3 服务接口
import java.util.List;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.hdu.server.bean.User;
@FeignClient(name="eureka-provider")
public interface UserService {
@RequestMapping("/getUser")
List<User> getUserList();
@RequestMapping("/getUserByName")
User getUserByName(@RequestParam("name") String name);
@RequestMapping("/getResult")
String getResult(@RequestBody User user);
}
4 Controller层调用
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hdu.server.bean.User;
import com.hdu.server.service.UserService;
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/getUserList")
public List<User> getUserList(){
return userService.getUserList();
}
@RequestMapping("/getUserByName")
public User getUserByName(String name){
return userService.getUserByName(name);
}
@RequestMapping("/getResult")
public String getResult(User user){
return userService.getResult(user);
}
}
5 application.yml文件配置
spring:
application:
name: feign-consumer
server:
port: 8500
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/
6 启动feign-consumer并访问http://localhost:8500/getUserList
7 feign的服务降级处理
可以通过实现UserService并配置@FeignClient,其实现如下:
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import com.hdu.server.bean.User;
@Component
public class UserServiceFallback implements UserService{
@Override
public List<User> getUserList() {
// TODO Auto-generated method stub
List<User> list = new ArrayList<>();
list.add(new User("未知","-1"));
return list;
}
@Override
public User getUserByName(@RequestParam("name") String name) {
// TODO Auto-generated method stub
return new User("未知","-1");
}
@Override
public String getResult(@RequestBody User user) {
// TODO Auto-generated method stub
return "error";
}
}
@FeignClient(name="eureka-provider",fallback=UserServiceFallback.class)
public interface UserService {
@RequestMapping("/getUser")
List<User> getUserList();
@RequestMapping("/getUserByName")
User getUserByName(@RequestParam("name") String name);
@RequestMapping("/getResult")
String getResult(@RequestBody User user);
}
同时还需要配置feign的请求超时时间等配置。