文章目录
上一篇讲了hystrix-dashboard,断路仪表盘。在上一篇的仪表盘上,可以看到有很多排序方式,很明显,这个是针对多服务多节点的情况。
要显示多服务多节点,需要开启Turbine功能。
项目结构:准备两个消费节点,一个Turbine节点
0.准备工作
服务中心和服务提供者(双开):
1.消费节点
1.1 pom.xml
新建两个项目,勾选eureka discovery,feign,hystrix,hystrix dashboard和actuator
<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-hystrix-dashboard</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>
1.2 application配置
spring:
application:
name: node1
server:
port: 8820
feign:
hystrix:
enabled: true
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8800/eureka/
spring:
application:
name: node2
server:
port: 8821
feign:
hystrix:
enabled: true
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8800/eureka/
1.3 application注解
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrixDashboard
@EnableCircuitBreaker
public class SixConsumerNode1Application {
public static void main(String[] args) {
SpringApplication.run(SixConsumerNode1Application.class, args);
}
/**
* 2.0以后需要加上
* @return
*/
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
同上一篇
1.4 服务声明
node1(hi):
@FeignClient(name = "eureka-client", fallback = HiServiceHystrix.class)
public interface HiService {
@RequestMapping("/hi")
String hi(@RequestParam("name")String name);
}
@Component
public class HiServiceHystrix implements HiService {
@Value("${server.port}")
private String port;
@Override
public String hi(String name) {
return "Sorry "+name+", in port "+port;
}
}
node2(hello):
@FeignClient(name = "eureka-client", fallback = HelloServiceHystrix.class)
public interface HelloService {
@RequestMapping("/hello")
String hello(@RequestParam("name")String name);
}
@Component
public class HelloServiceHystrix implements HelloService {
@Value("${server.port}")
private String port;
@Override
public String hello(String name) {
return "Sorry "+name+", in port "+port;
}
}
1.5 测试接口
node1:
@RestController
public class TestController {
@Autowired
private HiService hiService;
@GetMapping("/hi/{name}")
public String index(@PathVariable("name")String name) {
return hiService.hi(name);
}
}
node2:
@RestController
public class TestController {
@Autowired
private HelloService helloService;
@GetMapping("/hello/{name}")
public String index(@PathVariable("name")String name) {
return helloService.hello(name);
}
}
注:这些断路器节点需要配置成仪表盘模式,参考上一篇。
2.Turbine节点
2.1 pom.xml
新建项目,勾选eureka discovery,actuator,hystrix-dashboard和turbine
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<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>
2.2 application配置
spring:
application:
name: six-hystrix-dashboard-turbine
server:
port: 8830
turbine:
app-config: node1,node2
aggregator:
cluster-config: default
cluster-name-expression: new String("default")
## TODO 此处必须要配置
instanceUrlSuffix: /hystrix.stream
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8800/eureka/
此处turbine. instanceUrlSuffix需要配置,否则会一直处于Loading状态。
turbine.appConfig :配置Eureka中的serviceId列表,表明监控哪些服务
turbine.aggregator.clusterConfig :指定聚合哪些集群,多个使用”,”分割,默认为default。可使用http://…/turbine.stream?cluster={clusterConfig之一}访问
turbine.clusterNameExpression : 1. clusterNameExpression指定集群名称,默认表达式appName;此时:turbine.aggregator.clusterConfig需要配置想要监控的应用名称;2. 当clusterNameExpression: default时,turbine.aggregator.clusterConfig可以不写,因为默认就是default;3. 当clusterNameExpression: metadata[‘cluster’]时,假设想要监控的应用配置了eureka.instance.metadata-map.cluster: ABC,则需要配置,同时turbine.aggregator.clusterConfig: ABC
2.3 application注解
@SpringBootApplication
@EnableHystrixDashboard
@EnableTurbine
public class SixTurbineApplication {
public static void main(String[] args) {
SpringApplication.run(SixTurbineApplication.class, args);
}
}
打开HystrixDashboard和Turbine注解。
3.运行测试
运行两个消费节点和一个turbine节点,此时如下:
访问消费node1 http://localhost:8820/hi/Steven
交替出现下面结果。
Hi Steven, I’m from port : 8811
Hi Steven, I’m from port : 8810
配置node1 仪表盘,显示
访问消费node2 http://localhost:8821/hello/Steven
交替出现
Hello Steven, I’m from port : 8811
Hello Steven, I’m from port : 8810
配置node2仪表盘,显示
此时,这两个消费节点都可以单独查看仪表盘。
再打开Turbine节点仪表盘
访问 http://localhost:8830/hystrix/
配置http://localhost:8830/turbine.stream,title取名,然后monitor
再访问下上面两个消费节点
可以看到,两个都在实时刷新。因为提供服务者就一个,所以下面显示一个eureka-client。这个服务提供者提供了两个接口,分别给HelloService和HiService。
到此,配置完成。