springCloud快速实践2:服务监控、熔断器监控及zipkin调用链
完整代码下载链接:
https://github.com/2010yhh/springCloud-demos.git
环境
idea2018,jdk1.8,
springboot版本:springboot2.0.3.RELEASE,
springcloud版本:Finchley.SR1(2.0.1.RELEASE,)
1.服务监控
1.1服务端配置
pom:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>2.0.2</version>
</dependency>
admin-web配置
server:
port: 8760
spring:
application:
name: admin-server
security:
user:
name: 'admin'
password: 'admin'
boot:
admin:
client:
prefer-ip: true #解决window下无法识别主机名的问题
turbine:
location: admin-server #管理所有的监控
clusters: default
turbine:
clusterNameExpression: new String("default")
appConfig: cloud-feign #监控的应用,逗号隔开
instanceUrlSuffix: hystrix.stream
eureka:
client:
registryFetchIntervalSeconds: 5
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
metadata-map:
user.name: ${spring.security.user.name}
user.password: ${spring.security.user.password}
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
1.2客户端配置
pom:
<!--admin客户端只需引入这个jar即可-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.0.1</version>
</dependency>
<!--监控更多信息-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--jmx bean管理需要-->
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
admin client配置,在eureka注册中心注册
# 如果被监控的服务没有注册到服务中心,需要增加admin的地址
# spring.boot.admin.url=http://localhost:8760
依次启动各服务:
访问服务监控界面:http://localhost:8760/
2.熔断器监控
2.1配置
pom依赖:
hystrix:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
turbine:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</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-turbine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
cloud-feign cloud-feign2的配置文件示例:
spring.application.name=cloud-feign
server.port=8765
#eureka注册地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
#开启hystrix熔断器
feign.hystrix.enabled=true
management.endpoints.web.exposure.include = '*'
#Zipkin服务器地址
spring.zipkin.baseUrl=http://192.168.159.142:9411
#便于测试
spring.sleuth.sampler.percentage=1
spring.application.name=cloud-feign2
server.port=8865
#eureka注册地址
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
#开启hystrix熔断器
feign.hystrix.enabled=true
#turbine配置
turbine.app-config=cloud-feign,cloud-feign2
#集群(cluster)(turbine聚集数据的粒度)
turbine.aggregator.cluster-config=default
turbine.cluster-name-expression=new String("default")
management.endpoints.web.exposure.include = '*'
#更改turbine连接的默认uri ,默认为/actuator/hystrix.stream
turbine.instanceUrlSuffix = hystrix.stream
#Zipkin服务器地址
spring.zipkin.baseUrl=http://192.168.159.142:9411
#便于测试
spring.sleuth.sampler.percentage=1
2.2测试
1.服务监控中启动各程序后,访问hystrix界面:http://localhost:8865/hystrix
以cloud-feign2作为turbine聚合客户端,在界面中输入
http://localhost:8765/hystrix.stream
http://localhost:8865/hystrix.stream
http://localhost:8865/turbine.stream
或者直接请求上述http url
cloud-feign2作为turbine聚合客户端,采集信息
3.zipkin调用链跟踪
3.1配置
pom依赖:
<!-- 配置zipkin服务链路追踪 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
配置文件:
#Zipkin服务器地址
spring.zipkin.baseUrl=http://192.168.159.142:9411
#修改采集频率,便于测试
spring.sleuth.sampler.percentage=1
#其它配置根据需求添加
3.2测试
zipkin使用参考:https://zipkin.io/pages/quickstart.html
单独启动zipkin server步骤:
#下载 wget -O zipkin.jar 'https://search.maven.org/remote_content?g=io.zipkin.java&a=zipkin-server&v=LATEST&c=exec' #启动zipkin server nohup java -jar zipkin.jar >>zipkin.out 2>&1 &
访问:http://ip:9411
1.Trace:表示对一次请求的跟踪,包括多个span,Trace就是树结构的Span集合
2.Span:一个服务的处理
3.traceId:标记一次请求的跟踪,相关的Spans都有相同的traceId
spanId:span的名称,一般是接口方法的名称
parentId:可选的id,当前Span的父Span id,通过parentId来保证Span之间的依赖关系,如果没有parentId,表示当前Span为根Span;
测试:
1依次启动zipkin服务,eureka各服务
2.访问:http://localhost:8765/test?name=zyq http://localhost:8865/test?name=zyq
3.查看zipkin界面:
实际项目中根据需要进行改造,如zipkin数据存储由内存改为db或者els,采集上报由http改为mq等