时间:2022-02-15 11:08:31 | 栏目:JAVA代码 | 点击:次
1. 服务监控是针对客户端(消费者)的,所以客户端需要做出一些配置
2. 普通消费者只需要添加hystrix和dashboard的依赖+@EnableHystrixDashboard就可以把消费者变成一个监控中心,同时也失去了消费者的功能,不能再访问注册中心
1. 新建消费者服务9001(复制),新增监控依赖
<!--Hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
2. 修改配置文件
server: port: 9001 hystrix: dashboard: proxy-stream-allow-list: "*"
3. 为启动类添加支持监控的注解

//Eureka和Ribbon整合以后,客户端可以根据服务名称直接调用,不用关心IP地址和端口号
@SpringBootApplication
@EnableHystrixDashboard
//@RibbonClient(name = "SPRINGCLOUD-PROVIDER-DEPT",configuration = MyLoaderBalanceConfig.class) //在微服务启动的时候加载自定义的Ribbon
public class DeptConsumer_hystrix_dashboard_9001 {
public static void main(String[] args) {
SpringApplication.run(DeptConsumer_hystrix_dashboard_9001.class,args);
}
}
1. 所以的服务提供者都要添加被监控的依赖和Hystrix的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
2. 为被监控的服务提供者的启动类添加一个Bean

@Bean
public ServletRegistrationBean hystrixMetricsStreamServlet() {
ServletRegistrationBean registration = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
registration.addUrlMappings("/actuator/hystrix.stream");
return registration;
}


疑问:9001作为一个消费者模块,为什么不能访问生产者,难道这个模块只是用来监控的平台?
tips:


