前言
Feign的功能:声明式客户端、集成Ribbon(负载均衡和重试)、集成Hystrix(降级和熔断)
添加依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <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-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </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>
|
配置
1 2 3 4 5 6 7 8 9 10 11
| spring: application: name: feign server: port: 3001 eureka: client: service-url: defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
|
在启动类添加注解
- 在启动类添加
@EnableDiscoveryClient
和@EnableFeignClients
注解
1 2 3 4 5 6 7 8
| @EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
|
定义声明式客户端
@FeignClient()
:标注这个接口为声明式客户端
name = ""
:name属性定义spring.application.name名,通过name可以在Eureka的注册表找到对应的ip
@GetMapping()
、@PostMapping()
:定义访问的路径
@PathVariable
:定义Rest风格的参数传递(不能省略)
@RequestParam
:定义普通的参数传递(不能省略)
1 2 3 4 5 6 7 8 9 10
| @FeignClient(name = "item-service") public interface ItemClient {
@GetMapping("/{id}") String method1(@PathVariable String id);
@PostMapping("/") String method2(@RequestParam String id);
}
|
- 此时如果调用
itemClient.method1(1)
方法,则会向http://localhost:3001/item-service/1
发送请求
- 此时如果调用
itemClient.method2(1)
方法,则会向http://localhost:3001/item-service?id=1
发送请求
负载均衡和重试
ConnectTimeout
:连接超时时间,默认为1000毫秒
ReadTimeout
:读取超时,默认为1000毫秒
MaxAutoRetries
:单台服务器重试次数,默认为0次
MaxAutoRetriesNextServer
:多台服务器重试次数,默认为1次
1 2 3
| ribbon: ConnectTimeout: 1000 ReadTimeout: 1000
|
- 修改
application.yml
配置文件(修改指定服务器的方式)
1 2 3 4
| item-service: ribbon: ConnectTimeout: 1000 ReadTimeout: 1000
|
降级和熔断
1 2 3 4
| @FeignClient(name = "item-service", fallback = ItemClientFB.class) public interface ItemClient { ... }
|
- 定义降级类,实现声明式客户端接口,在重写的方法中添加降级代码
- 降级类需要添加
@Component
注解
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @Component public class ItemClientFB implements ItemClient {
@Override String method1(@PathVariable String id) { ... } @Override String method2(@RequestParam String id) { ... }
}
|
监控
- Feign集成Hystrix监控
- 添加
actuator
依赖
- 暴露
hystrix.stream
监控端点
- 查看监控日志
完成