spring cloud sleuth 整合zipkin 把数据存储到mysql

在默认情况下,zipkin server会将跟踪信息存储在内存中,每次重启zipkin server 都会是之前收集的跟踪信息丢失。为了便于分析,我们一般将跟踪信息 放到外部存储

zipkin 的 storage组件默认提供了对mysql 的支持,所以我们可以很轻松地为zipkin-server增加mysql存储功能,

下面把代码演示一下

 

版本 spring boot 1.5.14

spring cloud version Edgware.SR4

 

注册中心

配置文件

server.port=1111
eureka.instance.hostname=localhost
#由于该应用为注册中心,所有设置为false,代表不向注册中心注册自己
eureka.client.register-with-eureka=false 
#由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

启动类

package springcloud.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer // 注解启动一个服务注册中心
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}

pom 依赖



org.springframework.cloud
spring-cloud-starter-eureka-server



org.springframework.boot
spring-boot-starter-test
test

 

 

 

zipkin 服务器

先引入 zipkin-storage-mysql module,然后在该 包下 找到(zipkin\java\zipkin-storage-mysql\2.7.1\zipkin-storage-mysql-2.7.1.jar!\mysql.sql) mysql.sql 文件 ,然后运行它

mysql 版本 5.6

 

配置文件,对mysql 进行配置

spring.application.name=zipkin-server
server.port=9411
zipkin.storage.type=mysql
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.86.132:3306/zipkin
spring.datasource.username=root
spring.datasource.password=root

启动类

package springcloud.zipkin_server_mysql;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import zipkin.server.internal.EnableZipkinServer;
import zipkin.storage.mysql.MySQLStorage;

import javax.sql.DataSource;

@EnableZipkinServer
@SpringBootApplication
public class ZipkinServerMysqlApplication {

public static void main(String[] args) {
SpringApplication.run(ZipkinServerMysqlApplication.class, args);
}

@Bean
@Primary
public MySQLStorage mySQLStorage(DataSource datasource) {
return MySQLStorage.builder().datasource(datasource).executor(Runnable::run).build();
}

}

pom 依赖



org.jooq
jooq
3.8.0



io.zipkin.java
zipkin-server



io.zipkin.java
zipkin-storage-mysql
2.7.1



io.zipkin.java
zipkin-autoconfigure-ui



org.springframework.boot
spring-boot-starter-jdbc



mysql
mysql-connector-java
runtime


org.springframework.boot
spring-boot-starter-test
test

 

zipkin 应用一

 

配置文件

spring.application.name=sleuthone
server.port=9101
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/

启动类

package springcloud.sleuth_one;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableDiscoveryClient
@SpringBootApplication
public class SleuthOneApplication {


@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}

public static void main(String[] args) {
SpringApplication.run(SleuthOneApplication.class, args);
}
}

controller

package springcloud.sleuth_one;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class SleuthOneController {
private Logger logger = LoggerFactory.getLogger(getClass());

@Autowired
protected RestTemplate restTemplate;
@RequestMapping(value = "/trace-1",method = RequestMethod.GET)
public String trace(){
logger.info("=====call trace-1=======");
return restTemplate.getForEntity("http://sleuthtwo/trace-2",String.class).getBody();
}
}

pom 依赖



org.springframework.boot
spring-boot-starter-web


org.springframework.cloud
spring-cloud-starter-eureka


org.springframework.cloud
spring-cloud-starter-sleuth



org.springframework.cloud
spring-cloud-sleuth-zipkin



org.springframework.boot
spring-boot-starter-test
test

 

 

zipkin应用二

 

配置

spring.application.name=sleuthtwo
server.port=9102
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/

启动类

package springcloud.sleuth_two;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class SleuthTwoApplication {

public static void main(String[] args) {
SpringApplication.run(SleuthTwoApplication.class, args);
}
}

controller

package springcloud.sleuth_two;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SleuthTwoController {
private Logger logger = LoggerFactory.getLogger(getClass());
@RequestMapping(value = "/trace-2",method = RequestMethod.GET)
public String trace(){
logger.info("===============");


return "Trace";
}

}

pom 依赖



org.springframework.boot
spring-boot-starter-web


org.springframework.cloud
spring-cloud-starter-eureka


org.springframework.cloud
spring-cloud-starter-sleuth



org.springframework.cloud
spring-cloud-sleuth-zipkin




org.springframework.boot
spring-boot-starter-test
test

 

运行后 连续 访问 http://localhost:9101/trace-1 就可以 看到 ,mysql 出现数据

 

你可能感兴趣的:(springcloud)