java
使用上一章建立的 Feign 客户端
添加配置文件
1 2 3
| feign: hystrix: enabled: true
|
fallback 类
1 2 3 4 5 6 7
| @Component public class ServiceHystrixImpl implements UserService { @Override public User getUser(Long id) { return new User(12345l); } }
|
修改 Service 接口
1 2 3 4 5 6 7 8
|
@FeignClient(value ="PROVIDER-USER",fallback = ServiceHystrixImpl.class) public interface UserService { @GetMapping("/user/{id}") User getUser(@PathVariable Long id); }
|
测试
1
| 启动Feign客户端,访问提供者服务,url地址同上,停止服务提供者,再次访问,会返回服务降级内容。
|
熔断器监控 Hystrix Dashboard(单一应用)
添加依赖
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
| <?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"> <parent> <artifactId>springcloud</artifactId> <groupId>com.qn</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>consumer-feign</artifactId>
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> </dependencies> </project>
|
添加配置文件内容
1 2 3 4 5 6
| management: endpoints: web: exposure: include: "*"
|
启动类添加注解
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrixDashboard
@EnableCircuitBreaker public class ConsumerFeignApp { public static void main(String[] args) { SpringApplication.run(ConsumerFeignApp.class,args); } }
|
测试
- 启动 Feign 客户端,访问网址http://localhost:8900/hystrix/,显示界面如下:
- 输入http://localhost:8900/actuator/hystrix.stream,点击monitor,显示界面如下:
- 只使用 Hystrix Dashboard 的话, 你只能看到单个应用内的服务信息, 这明显不够. 我们需要一个工具能让我们汇总系统内多个服务的数据并显示到 Hystrix Dashboard 上,这个工具就是 Turbine.详见springcloud实战之Hystrix(2)