菜鸟学渣接触spring cloud 系列...
公司也上微服务了,再不学习下就凉了,所以来踩坑吧...
版本:
spring-boot: 2.0
spring-cloud: Finchley.SR1
已有项目:
[eureka-server] # 注册中心
[eureka-client-one] # 微服务1
[eureka-client-two] # 微服务2
能上图绝不BB:
hystrix.stream接口返回hystrix的实时信息,供HystrixDashboard面板展示状态
turbine.stream可以将多条hystrix.stream聚合在一起,便可在HystrixDashboard同时监控多个微服务的状态
一、[eureka-client-one] 添加Hystrix
依赖包、application.yml、启动类注释与上一节[eureka-client-two]相同操作..
Rest服务 HelloWorld.java
@RestController public class HelloWorld { @RequestMapping("/") @HystrixCommand(fallbackMethod = "someBoom") public String home(){ int a = 0, c = 1; int d = c/a; // 抛出异常触发 return "hello world"; } public String someBoom(){ return "wokao, 魂淡"; } }
启动并访问 http://192.168.1.103:8501/ 断路生效
访问 http://192.168.1.103:8501/actuator/hystrix.stream
二、聚合[eureka-client-one]和[eureka-client-two]的hystrix.stream
新建项目 [eureka-client-turbine] : 包含hystrix面板和turbine功能
引入依赖 spring-cloud-starter-netflix-hystrix-dashboard、spring-cloud-starter-netflix-turbine
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
配置文件 application.ym
server:
port: 8503
spring:
application:
name: eureka-client-turbine
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
# 聚合
turbine:
app-config: eureka-client-one, eureka-client-two # 直接写serviceId: 即2个微服务的‘spring.application.name’
cluster-name-expression: new String("default")
启动类 EurekaClientTurbineApplication.java
@Configuration @ComponentScan @EnableAutoConfiguration @EnableEurekaClient @EnableTurbine @EnableHystrixDashboard // 开启断路器面板 public class EurekaClientTurbineApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientTurbineApplication.class, args); } }
目录结构
三、结果
确保 [eureka-server]、[eureka-client-one]、[eureka-client-two]、[eureka-client-turbine]都已启动
先访问 http://localhost:8501/ 在[eureka-client-one]上产生数据
再访问 http://localhost:8502/ 在[eureka-client-two]上产生数据
接着访问 http://localhost:8503/hystrix 进入hystrix dashboard
输入 http://localhost:8501/actuator/hystrix.stream或者 http://localhost:8501/actuator/hystrix.stream 点击 查看单个微服务的状态
输入聚合后的 http://localhost:8503/turbine.stream 点击 查看2个微服务的状态
面板含义