spring-cloud微服务项目实战(10)-集成sleuth
2019-02-17 本文已影响45人
爱编程的凯哥
目的
部署zipkin服务,配合sleuth进行调用分析
简介
zipkin是Twitter的开源的分布式跟踪系统,基于Dapper的论文设计而来。主要功能就是收集系统的调用数据,进行分析.论文参考http://bigbully.github.io/Dapper-translation/,其实现框架如下:
工具名称 | 提供商 | 类型 | 源码地址 |
---|---|---|---|
Zipkin | 开源 | https://github.com/openzipkin/zipkin | |
pinpoint | naver | 开源 | https://github.com/naver/pinpoint |
appdash | sourcegraph | 开源 | https://github.com/sourcegraph/appdash |
cat | 大众点评 | 开源 | https://github.com/dianping/cat |
hydra | 京东 | 开源 | https://github.com/odenny/hydra |
鹰眼 | 阿里 | 闭源 | - |
oneAPM | oneapm | 闭源 | - |
开工
- 各系统添加zipkin依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
- application.yml中添加zipkin配置
spring:
Jackson:
serialization:
indent-output: true
application:
name: udm-server
zipkin:
#我本地部署的zipkin服务用的9411端口
base-url: http://localhost:9411
sleuth:
sampler:
percentage: 1.0
- 新建zipkin-dog项目module,我们将采用mysql作为数据存储(不配置,默认会存储到内存中),本次未集成mq或kafka等异步组件,有需要后续会介绍,依赖添加
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>open.template.work</groupId>
<artifactId>zipkin-dog</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>zipkin-dog</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-storage-mysql</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
- 更新zipkin-dog到application.yml配置文件,我用的本地mysql数据库
server:
port: 9411
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://local.p2.cn:3306/zipkin
username: root
password: yeepay.com
zipkin:
storage:
type: mysql
- 附加步,去掉udm-client中的当时的自定义负载均衡配置,测试发现配置了自定义的负载后,再集成zipkin,feign有偶尔找不到服务的bug,所以去掉
#udm-server:
# NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
---
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients(basePackages = "open.template.work.udm.client.feign")
@EnableHystrix
public class UdmClientApplication {
public static void main(String[] args) {
SpringApplication.run(UdmClientApplication.class, args);
}
//
// @Bean
// public IRule ribbonRoundRobinRule() {
// return new RandomRule();
// }
}
6.启动zipkin-dog,重启其他各相应服务,切记所有需要zipkin记录调用链的服务都要添加1、2步的配置
-
验收
zipkin展示
再次重启zipkin-dog,数据依然存在,查看数据库已有请求数据,但spanid可能和日志打印不太一样,因为日志打印未16进制,数据库存储为bigint。
- 附,为了提高性能可以集成mq或kafka等异步组件,本章暂未展示
完工!