原创

Spring Cloud 2 集成Zipkin server方案(第一阶段)

温馨提示:
本文最后更新于 2019年09月17日,已超过 1,892 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

因为某些原因,我们需要将Zipkin集成到我们的微服务里进行部署。在zipkin2.7之前,是支持这样的集成方式。但是,在2.7之后,官方已经不建议自建zipkin server。取而代之的是以下三种方案:

  1. 使用官方的Docker镜像
  2. 使用官方提供的可运行jar
  3. 使用官方的源码编译后部署

基于以上的任何方式,都是很好的方案,但是,对于某些情况,我们只能使用自建server,那又该如何呢?官方不支持自然会有很多的坑,如下Simon给出的一种解决方案:

版本说明:

 序号ArtifactIdVersion 
 1 zipkin-server 2.12.8
 2 zipkin-autoconfigure-ui 2.12.8
 3 zipkin-autoconfigure-collector-rabbitmq 2.12.8
 4 zipkin-autoconfigure-storage-elasticsearch-http 2.8.4


先看我们pom.xml文件的配置

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- zipkin server -->
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</exclusion>
</exclusions>
<version>${io.zipkin.java.version}</version>
</dependency>
<!-- zipkin ui -->
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
<version>${io.zipkin.java.version}</version>
</dependency>

<!-- 使用消息的方式收集数据(使用rabbitmq) -->
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-collector-rabbitmq</artifactId>
<version>${io.zipkin.java.version}</version>
</dependency>

<!-- 采用 elasticsearch 存储数据 -->
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-storage-elasticsearch-http</artifactId>
<version>${io.zipkin.java-elasticsearch.version}</version>
</dependency>

<!--Spring Boot Admin中获取JVM信息-->
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
</dependency>
</dependencies>

需要注意的是,exclusions是必须的,因为springboot和zipkin都有slf4j的实现,如果不做处理,会出现包冲突的问题:

<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</exclusion>
</exclusions>


我们的application.yml配置如下:

server:
#服务端口
port: 8007
armeria:
ports:
- port: 8008
protocols:
- http
gracefulShutdownQuietPeriodMillis: -1
gracefulShutdownTimeoutMillis: -1

spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/zipkin?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&useSSL=false
username: sit_mysql_user
password: 123654
driver-class-name: com.mysql.jdbc.Driver

main:
allow-bean-definition-overriding: true

zipkin:
collector:
#消息队列配置
rabbitmq:
addresses: 127.0.0.1:5672
username: guest
password: guest
virtual-host: /
queue: zipkin
storage:
type: elasticsearch
elasticsearch:
hosts: 127.0.0.1:9200
cluster: elasticsearch
index: zipkin
index-shards: 1
index-replicas: 1

eureka:
instance:
instance-id: ${spring.cloud.client.ip-address}:${server.port}
prefer-ip-address: true
metadata-map:
cluster: mycluster
client:
service-url:
defaultZone: http://localhost:8001/eureka


# 加载所有的端点/默认只加载了 info / health
management:
endpoints:
web:
exposure:
include: "*"
metrics:
web:
server:
auto-time-requests: false
endpoint:
health:
show-details: always
enabled: true

这里需要注意的是,除了spring cloud启动需要的端口,zipkin默认使用了armeria代替了Tomcat。

armeria:
ports:
- port: 8008

如下是取自官网的配置注释说明:

此处要如何处理,等Simon把源码下载后再看[坏笑]

先看看目前的效果:






目前还差最后一步,就是将zipkin默认的Armeria剔除,直接使用springcloud2内置的Tomcat启动。


未完待续。zipkin源码下载中...

正文到此结束
本文目录