5

实现起来超简单的zipkin+sleuth微服务链路跟踪方案

 3 years ago
source link: https://www.skypyb.com/2019/06/jishu/914/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

实现起来超简单的zipkin+sleuth微服务链路跟踪方案

说是简单,tm那是找到解决方案之后才简单。

可能是我用的SpringCloud版本太新了,自己配zipkin server把我给配吐了。

又是版本冲突、又是注册不进去Eureka、又是访问ui报错、又是找不到ObjectProvider.orderedStream()方法的,我从百度搜到必应搜到google搜到Stack Overflow,整了一个上午。

我就想着,这玩意怎么能这么恶心呢?感觉像写maven的时候狂报xml错误那种感觉。

后来怼到zipkin官网 zipkin.io 上去,卧槽,快速入门里有这么句话:

If you have Java 8 or higher installed, the quickest way to get started is to fetch the latest release as a self-contained executable jar:

curl -sSL https://zipkin.io/quickstart.sh | bash -s
java -jar zipkin.jar

我佛了。然后又查了些关于高版本资料,关于 Zipkin 的服务端,在使用 Spring Boot 2.x 版本后,官方就不推荐自行定制编译了,反而是直接提供了编译好的 jar 包来给我们使用,这是官方提供的演示: https://github.com/openzipkin/docker-zipkin

白搞了半天,原来只要自己去下他的jar包然后启动这个服务就行了,至于如何将微服务跟踪的数据存到mysql/kafka/es 里边去,也可以直接用启动参数指定。

那我还自己配个蛇皮的zipkin server。

可以去这个网址: https://dl.bintray.com/openzipkin/maven/io/zipkin/java/zipkin-server/ 选择下载zipkin的各个不同版本的启动jar,我随便找了一个,启动很顺畅,进入 localhost:9411 可以访问ui。

那服务端就算这样配完了,全部流程: 下载 –> 使用 java -jar 命令运行下载的jar

服务端搞完后,就到客户端了。

微服务链路跟踪嘛,需要得到请求经过了哪些微服务,了解请求流转的具体参数,那么客户端自然就是一个个不同的微服务了。

以我之前的user微服务为例,配置都一样。

build.gradle中加上这一行:

compile("org.springframework.cloud:spring-cloud-starter-zipkin")

本来应该是spring-cloud-starter-sleuth,spring-cloud-sleuth-zipkin这两个依赖,但是spring-cloud-starter-zipkin这个集成了前面那俩,和前面那俩依赖效果是一样的。

加完依赖后只需要指定zipkin服务地址就好了

我的applicayion.yml:

server:
  port: 8088
spring:
  application:
    name: sc-demo-microservice-user #这个名字会注册到 Eureka 里去
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.8.113:3306/scdemo?serverTimezone=Asia/Shanghai
    username: root
    password: 614
  zipkin:
    base-url: http://localhost:9411
  sleuth:
    sampler:
      probability: 1.0 #采样百分比,这里设置的是100%
logging:
  level:
    root: info
    org.springframework.web.servlet.DispatcherServlet: debug
#mybatis实体类名
mybatis:
  type-aliases-package: com.skypyb.sc.entity
  configuration:
  #到下划线的表字段自动映射成驼峰命名法
    map-underscore-to-camel-case: true
  mapper-locations: classpath:mybatis/mapper/*.xml
#Actuato 配置
management:
  endpoint:
# 暴露shutdown功能
#    shutdown:
#      enabled: true
  endpoints:
      exposure:
        include: '*'  #暴露哪些端点
        exclude:      #隐藏哪些端点
#Eureka client端配置
eureka:
  client:
    service-url:
        defaultZone: http://user:614@localhost:8080/eureka/,http://user:614@localhost:8081/eureka/
  instance:
    prefer-ip-address: true #将自己ip注册到Eureka Server

spring.zipkin.base-url 指定地址。zipkin端口号默认9411。

spring.sleuth.sampler.probability 设置采样百分比,采样百分比就是会按比例来跟踪/保存多少条请求数据,因为在线上运行的话那数据是相当庞大的。默认设置的是0.1,也就是百分之十,我这设置百分之百是为了本地调试。

然后就完事了

对,没问题。所有微服务开起来,Eureka、Zuul等等,当然还有Zipkin官方服务。网页请求几个API,再去 localhost:9411 刷新一下,就可以看到每一条请求的详细信息了。

实现微服务跟踪需要的步骤:

1、 下载官方推荐的jar包

2、运行起来

3、改下微服务,加个依赖加个配置

完事了,如果想要保存数据到es/mysql/kafka里分析,可以设置启动参数,具体可以去看官方、github

我的使用Gradle搭的SpringCloud Finchley.SR3 项目整体地址:

https://github.com/skypyb/codeDemo/tree/master/sc-demo-microservice


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK