前面简单说了一下SpringCloud技术栈之zuul api网关、服务注册与发现的eureka和服务间调用的feign的使用,当然SpringCloud作为一个微服务的一站式配齐的全家桶,其组件远不止于此,在这里就简单的说一下其他的一些组件,
同时附上我练习时搭建的一个简单的SpringCloud项目,其中包含了feign、swagger-ui、rabbitmq、redis、aop、定时任务、文件上传于下载、excel导出、多数据源配置等demo,该项目也包含了SpringCloud的常用组件:
下载链接://download.youkuaiyun.com/download/weixin_45417573/12104123
hystrixDashboard
从词意上理解,这个组件的主要功能就是一个仪表盘,既然是仪表盘,就是用来检测状态用的,这就是一个检测hystrix服务降级与熔断的调用状态的一个组件,下面就介绍一下简单的使用(因为我们现在基本都是使用feign,而非单独使用ribbon和hystrix,所以这里我们主要介绍的也是结合feign的使用)
1、首先我们要明确我们需要监控的服务。在该服务添加依赖,并暴露一个监控端点,以供我们实现对该服务的监控
(1)添加依赖
<!-- 暴露监控端点 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 虽然feign集成了hystrix,但集成的并不完整,如需进行健康监测,还需要单独添加hystrix的依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>
spring-cloud-starter-netflix-hystrix
</artifactId>
</dependency>
(2)暴露监控端点
在服务的yml配置中添加以下配置:
#暴露监控断点,供hystrix仪表盘监控健康状态
management:
endpoints:
web:
exposure:
include: hystrix.stream
(3)在主启动类添加hystrix的注解
@EnableCircuitBreaker
2、创建hystrixDashboard服务
(1)添加依赖(主要是hystrixDashboard依赖,服务需注册到eureka中,多以也需要添加eurekaClient的依赖)
<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-dashboard
</artifactId>
</dependency>
(2)主启动类注解
@EnableHystrixDashboard
(3)yml配置
yml中没有什么特别的配置,就是简单的端口,服务名和eureka的配置
server:
port: 8006
spring:
application:
name: hystrix
eureka:
instance:
prefer-ip-address: true #以IP地址注册到服务中心,相互注册使用IP地址
client:
serviceUrl:
defaultZone: http://localhost:8000/eureka/
到这里位置,所有的准备工作就完成了,接下来可以开始测试了
3、测试
(1)测试监控端点(访问暴露监控端点的服务的+actuator)例如:
localhost:8002/user/actuator
访问后会得到一个json串,这个json串中就包含我们要监控的服务端点
(2)测试监控
访问hystrixDashboard服务+hystrix,例如:
http://localhost:8006/hystrix
在红色框中填写要监控的服务(上面测试的json串中的那个),点击确认后就可以开始监控,接下来我们反复多访问一下检测的服务(访问使用feign调用的接口),就会看到我们的监控结果!
4、聚合监控(我使用聚合监控一直报错,并未成功,哪位大神看到了帮我知道一二!感激不尽,报错内容如下)
com.netflix.turbine.monitor.instance.InstanceMonitor$MisconfiguredHostException: []
at com.netflix.turbine.monitor.instance.InstanceMonitor.init(InstanceMonitor.java:318) ~[turbine-core-1.0.0.jar:na]
at com.netflix.turbine.monitor.instance.InstanceMonitor.access$100(InstanceMonitor.java:103) ~[turbine-core-1.0.0.jar:na]
at com.netflix.turbine.monitor.instance.InstanceMonitor$2.call(InstanceMonitor.java:235) [turbine-core-1.0.0.jar:na]
at com.netflix.turbine.monitor.instance.InstanceMonitor$2.call(InstanceMonitor.java:229) [turbine-core-1.0.0.jar:na]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_221]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_221]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_221]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_221]
但是,这个服务监控是有缺陷的,因为它一次只能监控一个服务断点,对我们实际使用造成了很大的不便,如果我们希望同时监控多个服务,就使用turbine进行聚合监控!用法与hystrixDashboard类似,新建一个turbine服务,
(1)添加依赖:
<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-turbine</artifactId>
</dependency>
(2)添加主启动类注解:
@EnableTurbine
(3)yml配置:
turbine:
# 需要监控的应用名称,默认逗号隔开,内部使用Stringutils.commaDelimitedListToStringArray分割
app-config: jt-order,jt-user
# 集群名称
cluster-name-expression: new String('default')
这样就可以监控多个服务了!!