springcloud集成zipkin
2023-02-06 18:29:43 0 举报
springcloud集成zipkin,持久化
作者其他创作
大纲/内容
简介
zipkin是twitter开源的分布式跟踪系统,主要用来收集系统的实时数据,从而追踪系统的调用问题。
官网
https://zipkin.io/
spring 文档
https://docs.spring.io/spring-cloud-sleuth/docs/current/reference/html/integrations.html#sleuth-openfeign-integration
安装zipkin
docker-compose.yml代码
version: '3'
services:
zipkin:
image: openzipkin/zipkin
container_name: zipkin
ports:
- 9411:9411
services:
zipkin:
image: openzipkin/zipkin
container_name: zipkin
ports:
- 9411:9411
启动命令 docker-compose up -d
服务项目代码
项目导入pom依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>
application.yml配置
spring
zipkin:
base-url: http://192.168.56.1:9411
discovery-client-enabled: false #关闭服务发现,否则springcloud 会把zipkin的服务url当做服务名称
sender:
type: web #使用http的方式传输数据
sleuth:
trace-id128: true
sampler:
probability: 1 #抽样采集率为100% 默认为0.1即10%
baggage:
tag-fields: tag_name
zipkin:
base-url: http://192.168.56.1:9411
discovery-client-enabled: false #关闭服务发现,否则springcloud 会把zipkin的服务url当做服务名称
sender:
type: web #使用http的方式传输数据
sleuth:
trace-id128: true
sampler:
probability: 1 #抽样采集率为100% 默认为0.1即10%
baggage:
tag-fields: tag_name
HelloController.java代码
@Slf4j
@RestController
public class HelloController {
@Autowired
ZipkinService zipkinService;
@SentinelResource(value="hello")
@RequestMapping(value = "/hello",produces = "text/json")
public String hello(){
log.info("say hello word!");
//远程调用
String s = zipkinService.helloWord();
return s;
}
}
@Slf4j
@RestController
public class HelloController {
@Autowired
ZipkinService zipkinService;
@SentinelResource(value="hello")
@RequestMapping(value = "/hello",produces = "text/json")
public String hello(){
log.info("say hello word!");
//远程调用
String s = zipkinService.helloWord();
return s;
}
}
验证
浏览器输入请求地址:http://localhost:9000/hello
浏览器访问zipkin地址查看效果:http://192.168.56.10:9411/zipkin/
nginx配置
upstream zipkin{
server 192.168.56.10:9411;
}
server {
listen 9411;
server_name localhost;
location / {
proxy_pass http://zipkin;
}
}
server 192.168.56.10:9411;
}
server {
listen 9411;
server_name localhost;
location / {
proxy_pass http://zipkin;
}
}
错误
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties
解决方案:将pom.xml的依赖替换
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
替换为
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
替换为
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
直接持久化到mysql
优点:简单,无需添加额外的中间件,缺点:服务性能消耗较大
创建数据库
create DATABASE zipkin;
建表
CREATE TABLE IF NOT EXISTS zipkin_spans (
`trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
`trace_id` BIGINT NOT NULL,
`id` BIGINT NOT NULL,
`name` VARCHAR(255) NOT NULL,
`remote_service_name` VARCHAR(255),
`parent_id` BIGINT,
`debug` BIT(1),
`start_ts` BIGINT COMMENT 'Span.timestamp(): epoch micros used for endTs query and to implement TTL',
`duration` BIGINT COMMENT 'Span.duration(): micros used for minDuration and maxDuration query',
PRIMARY KEY (`trace_id_high`, `trace_id`, `id`)
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;
ALTER TABLE zipkin_spans ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTracesByIds';
ALTER TABLE zipkin_spans ADD INDEX(`name`) COMMENT 'for getTraces and getSpanNames';
ALTER TABLE zipkin_spans ADD INDEX(`remote_service_name`) COMMENT 'for getTraces and getRemoteServiceNames';
ALTER TABLE zipkin_spans ADD INDEX(`start_ts`) COMMENT 'for getTraces ordering and range';
CREATE TABLE IF NOT EXISTS zipkin_annotations (
`trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
`trace_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.trace_id',
`span_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.id',
`a_key` VARCHAR(255) NOT NULL COMMENT 'BinaryAnnotation.key or Annotation.value if type == -1',
`a_value` BLOB COMMENT 'BinaryAnnotation.value(), which must be smaller than 64KB',
`a_type` INT NOT NULL COMMENT 'BinaryAnnotation.type() or -1 if Annotation',
`a_timestamp` BIGINT COMMENT 'Used to implement TTL; Annotation.timestamp or zipkin_spans.timestamp',
`endpoint_ipv4` INT COMMENT 'Null when Binary/Annotation.endpoint is null',
`endpoint_ipv6` BINARY(16) COMMENT 'Null when Binary/Annotation.endpoint is null, or no IPv6 address',
`endpoint_port` SMALLINT COMMENT 'Null when Binary/Annotation.endpoint is null',
`endpoint_service_name` VARCHAR(255) COMMENT 'Null when Binary/Annotation.endpoint is null'
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;
ALTER TABLE zipkin_annotations ADD UNIQUE KEY(`trace_id_high`, `trace_id`, `span_id`, `a_key`, `a_timestamp`) COMMENT 'Ignore insert on duplicate';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`, `span_id`) COMMENT 'for joining with zipkin_spans';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTraces/ByIds';
ALTER TABLE zipkin_annotations ADD INDEX(`endpoint_service_name`) COMMENT 'for getTraces and getServiceNames';
ALTER TABLE zipkin_annotations ADD INDEX(`a_type`) COMMENT 'for getTraces and autocomplete values';
ALTER TABLE zipkin_annotations ADD INDEX(`a_key`) COMMENT 'for getTraces and autocomplete values';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id`, `span_id`, `a_key`) COMMENT 'for dependencies job';
CREATE TABLE IF NOT EXISTS zipkin_dependencies (
`day` DATE NOT NULL,
`parent` VARCHAR(255) NOT NULL,
`child` VARCHAR(255) NOT NULL,
`call_count` BIGINT,
`error_count` BIGINT,
PRIMARY KEY (`day`, `parent`, `child`)
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;
`trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
`trace_id` BIGINT NOT NULL,
`id` BIGINT NOT NULL,
`name` VARCHAR(255) NOT NULL,
`remote_service_name` VARCHAR(255),
`parent_id` BIGINT,
`debug` BIT(1),
`start_ts` BIGINT COMMENT 'Span.timestamp(): epoch micros used for endTs query and to implement TTL',
`duration` BIGINT COMMENT 'Span.duration(): micros used for minDuration and maxDuration query',
PRIMARY KEY (`trace_id_high`, `trace_id`, `id`)
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;
ALTER TABLE zipkin_spans ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTracesByIds';
ALTER TABLE zipkin_spans ADD INDEX(`name`) COMMENT 'for getTraces and getSpanNames';
ALTER TABLE zipkin_spans ADD INDEX(`remote_service_name`) COMMENT 'for getTraces and getRemoteServiceNames';
ALTER TABLE zipkin_spans ADD INDEX(`start_ts`) COMMENT 'for getTraces ordering and range';
CREATE TABLE IF NOT EXISTS zipkin_annotations (
`trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
`trace_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.trace_id',
`span_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.id',
`a_key` VARCHAR(255) NOT NULL COMMENT 'BinaryAnnotation.key or Annotation.value if type == -1',
`a_value` BLOB COMMENT 'BinaryAnnotation.value(), which must be smaller than 64KB',
`a_type` INT NOT NULL COMMENT 'BinaryAnnotation.type() or -1 if Annotation',
`a_timestamp` BIGINT COMMENT 'Used to implement TTL; Annotation.timestamp or zipkin_spans.timestamp',
`endpoint_ipv4` INT COMMENT 'Null when Binary/Annotation.endpoint is null',
`endpoint_ipv6` BINARY(16) COMMENT 'Null when Binary/Annotation.endpoint is null, or no IPv6 address',
`endpoint_port` SMALLINT COMMENT 'Null when Binary/Annotation.endpoint is null',
`endpoint_service_name` VARCHAR(255) COMMENT 'Null when Binary/Annotation.endpoint is null'
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;
ALTER TABLE zipkin_annotations ADD UNIQUE KEY(`trace_id_high`, `trace_id`, `span_id`, `a_key`, `a_timestamp`) COMMENT 'Ignore insert on duplicate';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`, `span_id`) COMMENT 'for joining with zipkin_spans';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTraces/ByIds';
ALTER TABLE zipkin_annotations ADD INDEX(`endpoint_service_name`) COMMENT 'for getTraces and getServiceNames';
ALTER TABLE zipkin_annotations ADD INDEX(`a_type`) COMMENT 'for getTraces and autocomplete values';
ALTER TABLE zipkin_annotations ADD INDEX(`a_key`) COMMENT 'for getTraces and autocomplete values';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id`, `span_id`, `a_key`) COMMENT 'for dependencies job';
CREATE TABLE IF NOT EXISTS zipkin_dependencies (
`day` DATE NOT NULL,
`parent` VARCHAR(255) NOT NULL,
`child` VARCHAR(255) NOT NULL,
`call_count` BIGINT,
`error_count` BIGINT,
PRIMARY KEY (`day`, `parent`, `child`)
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;
调整zipkin的docker-compose.yml新增mysql相关配置
docker-compose.yml文件
version: '3'
services:
zipkin:
image: openzipkin/zipkin
environment:
- TZ=Asia/Shanghai
- JAVA_OPTS=-XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -Xms100m -Xmx1024m -Xmn256m
- STORAGE_TYPE=mysql
- MYSQL_DB=zipkin
- MYSQL_USER=root
- MYSQL_PASS=root
- MYSQL_HOST=192.168.56.10
- MSYQL_TCP_PORT=3306
container_name: zipkin
ports:
- 9411:9411
services:
zipkin:
image: openzipkin/zipkin
environment:
- TZ=Asia/Shanghai
- JAVA_OPTS=-XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -Xms100m -Xmx1024m -Xmn256m
- STORAGE_TYPE=mysql
- MYSQL_DB=zipkin
- MYSQL_USER=root
- MYSQL_PASS=root
- MYSQL_HOST=192.168.56.10
- MSYQL_TCP_PORT=3306
container_name: zipkin
ports:
- 9411:9411
重新启动zipkin
docker-compose down &&docker-compose up -d&&dopcker-compose logs -f
通过rabbitmq持久化到mysql
基于以上直接持久化到mysql的数据库脚本
安装rabbitmq,docker方式启动,根据实际情况修改启动脚本
docker run -d --restart=always --name rabbitmq -p 5672:5672 \
-p 15672:15672 -e RABBITMQ_DEFAULT_USER='admin' \
-e RABBITMQ_DEFAULT_PASS='admin' \
-e RABBITMQ_NODENAME='MYNODE@localhost' \
-v /home/rabbitmq/data:/var/lib/rabbitmq \
-v /home/rabbitmq/definitions.json:/opt/definitions.json \
-v /home/rabbitmq/rabbitmq.config:/etc/rabbitmq/rabbitmq.config \
-v /etc/localtime:/etc/localtime rabbitmq:management-alpine
-p 15672:15672 -e RABBITMQ_DEFAULT_USER='admin' \
-e RABBITMQ_DEFAULT_PASS='admin' \
-e RABBITMQ_NODENAME='MYNODE@localhost' \
-v /home/rabbitmq/data:/var/lib/rabbitmq \
-v /home/rabbitmq/definitions.json:/opt/definitions.json \
-v /home/rabbitmq/rabbitmq.config:/etc/rabbitmq/rabbitmq.config \
-v /etc/localtime:/etc/localtime rabbitmq:management-alpine
服务项目application.yml配置
子主题
添加rabbitmq配置项
version: '3'
services:
zipkin:
image: openzipkin/zipkin
environment:
- TZ=Asia/Shanghai
- JAVA_OPTS=-XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -Xms100m -Xmx1024m -Xmn256m
- STORAGE_TYPE=mysql
#数据库链接信息
- MYSQL_DB=zipkin
- MYSQL_USER=root
- MYSQL_PASS=root
- MYSQL_HOST=192.168.56.10
- MSYQL_TCP_PORT=3306
#rabitmq链接信息
- RABBIT_ADDRESSES=192.168.56.10:5672
- RABBIT_USER=admin
- RABBIT_PASSWORD=admin
- RABBIT_QUEUE=zipkin
- RABBIT_VIRTUAL_HOST=/
container_name: zipkin
ports:
- 9411:9411
services:
zipkin:
image: openzipkin/zipkin
environment:
- TZ=Asia/Shanghai
- JAVA_OPTS=-XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m -Xms100m -Xmx1024m -Xmn256m
- STORAGE_TYPE=mysql
#数据库链接信息
- MYSQL_DB=zipkin
- MYSQL_USER=root
- MYSQL_PASS=root
- MYSQL_HOST=192.168.56.10
- MSYQL_TCP_PORT=3306
#rabitmq链接信息
- RABBIT_ADDRESSES=192.168.56.10:5672
- RABBIT_USER=admin
- RABBIT_PASSWORD=admin
- RABBIT_QUEUE=zipkin
- RABBIT_VIRTUAL_HOST=/
container_name: zipkin
ports:
- 9411:9411
项目启动报错
Parameter 2 of method reporter in org.springframework.cloud.sleuth.zipkin2.ZipkinAutoConfiguration required a bean of type 'zipkin2.reporter.Sender' that could not be found.
需要注入Sender与ConnectFactory
@Configuration
public class ZipkinConfig {
@Autowired
private RabbitProperties rabbitProperties;
@Bean
Sender rabbitSender2(){
ConnectionFactory connectionFactory=new ConnectionFactory();
connectionFactory.setHost(rabbitProperties.getHost());
connectionFactory.setPort(rabbitProperties.getPort());
connectionFactory.setUsername(rabbitProperties.getUsername());
connectionFactory.setPassword(rabbitProperties.getPassword());
return RabbitMQSender.newBuilder()
.connectionFactory(connectionFactory)
.queue("zipkin")
.addresses(rabbitProperties.getHost()+":"+rabbitProperties.getPort())
.build();
}
}
public class ZipkinConfig {
@Autowired
private RabbitProperties rabbitProperties;
@Bean
Sender rabbitSender2(){
ConnectionFactory connectionFactory=new ConnectionFactory();
connectionFactory.setHost(rabbitProperties.getHost());
connectionFactory.setPort(rabbitProperties.getPort());
connectionFactory.setUsername(rabbitProperties.getUsername());
connectionFactory.setPassword(rabbitProperties.getPassword());
return RabbitMQSender.newBuilder()
.connectionFactory(connectionFactory)
.queue("zipkin")
.addresses(rabbitProperties.getHost()+":"+rabbitProperties.getPort())
.build();
}
}
浏览器访问验证
持久化到elasticsearch
待完善。。。。
0 条评论
下一页