1.监控单体应用
首先搭建一个spring cloud 环境,然后创建一个服务注册中心。然后新建一个module,我的项目名为hystrix-dashboard。
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
<version>1.4.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.0.6.RELEASE</version>
</dependency>
配置文件
spring.application.name=hystrix-dashboard
server.port=8001
feign.hystrix.enabled=true
eureka.client.service-url.defaultZone=http://localhost:8000/eureka/
主启动类
@SpringBootApplication
@EnableFeignClients
@EnableHystrix
@EnableCircuitBreaker
@EnableDiscoveryClient
@EnableEurekaClient
@EnableHystrixDashboard
public class HystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardApplication.class, args);
}
@Bean
public ServletRegistrationBean hystrixMetricsStreamServlet() {
ServletRegistrationBean registration = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
registration.addUrlMappings("/actuator/hystrix.stream");
return registration;
}
}
主启动类中需要配置"/actuator/hystrix.stream",不然无法访问http://localhost:8001/actuator/hystrix.stream
然后创建Controller层,service层。
@RestController
public class ConsumerController {
@Autowired
HelloRemote helloRemote;
@RequestMapping("/hello")
public String index(@RequestParam("name") String name) {
return helloRemote.hello(name);
}
}
@FeignClient(name = "service-node3", fallback = HelloRemoteHystrix.class)
@Service
public interface HelloRemote {
@RequestMapping(value = "/hello")
public String hello(@RequestParam(value = "name") String name);
}
@Service
public class HelloRemoteHystrix implements HelloRemote {
@Override
public String hello(String name) {
return "hello " + name + ", this message send faild .";
}
}
好了,该项目创建完成了。然后创建一个专门用来调用的工程service-node3,这个项目只有一个接口/hello?name=xx
启动服务中心eureka-server,hystrix-dashboard,service-node3
然后访问http://localhost:8001//actuator/hystrix.stream,会一直ping,调用/hello?name=xx,接口之后,会刷新出数据如下图所示:
访问http://localhost:8001/hystrix
之后进入页面
页面中的具体参数如下:
Turbine集群监控
重新创建一个项目hystrix-dashboard-turbine,pom.xml文件
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
<version>1.4.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-turbine</artifactId>
<version>1.1.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-turbine</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
启动类
@SpringBootApplication
@EnableHystrixDashboard
@EnableTurbine
public class HystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardApplication.class, args);
}
}
properties配置
spring.application.name=hystrix-dashboard-turbine
server.port=8001
feign.hystrix.enabled=true
eureka.client.service-url.defaultZone=http://localhost:8000/eureka/
turbine.app-config=service-node,service-node2
turbine.cluster-name-expression= new String("default")
turbine.aggregator.clusterConfig=default
该项目创建完成,然后创建service-node,service-node2
pom.xml文件
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
<version>1.4.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.0.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
properties配置
spring.application.name=service-node
server.port=9001
feign.hystrix.enabled=true
eureka.client.service-url.defaultZone=http://localhost:8000/eureka/
service-node2 的端口号为9002
主启动类
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
@EnableHystrixDashboard
@EnableCircuitBreaker
public class ServiceNodeApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceNodeApplication.class, args);
}
@Bean
public ServletRegistrationBean hystrixMetricsStreamServlet() {
ServletRegistrationBean registration = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
registration.addUrlMappings("/actuator/hystrix.stream");
return registration;
}
}
controlle层和service层如前面一样,全部调用service-node3
service-node中我多加了一个接口hi?name=xx
访问完两个项目的接口之后,访问http://localhost:8001/turbine.stream有数据。
访问http://localhost:8001/hystrix,然后填入http://localhost:8001/turbine.stream
然后出现仪表盘
over