springcloud集成zipkin
2023-02-06 18:29:43 0 举报
springcloud集成zipkin,持久化
作者其他创作
大纲/内容
zipkin是twitter开源的分布式跟踪系统,主要用来收集系统的实时数据,从而追踪系统的调用问题。
https://zipkin.io/
官网
https://docs.spring.io/spring-cloud-sleuth/docs/current/reference/html/integrations.html#sleuth-openfeign-integration
spring 文档
简介
version: '3'services: zipkin: image: openzipkin/zipkin container_name: zipkin ports: - 9411:9411
docker-compose.yml代码
启动命令 docker-compose up -d
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-sleuth-zipkin</artifactId> </dependency>
项目导入pom依赖
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
application.yml配置
@Slf4j@RestControllerpublic class HelloController { @Autowired ZipkinService zipkinService; @SentinelResource(value=\"hello\") @RequestMapping(value = \"/hello\
HelloController.java代码
浏览器输入请求地址:http://localhost:9000/hello
浏览器访问zipkin地址查看效果:http://192.168.56.10:9411/zipkin/
验证
服务项目代码
安装zipkin
upstream zipkin{ server 192.168.56.10:9411;}server { listen 9411; server_name localhost; location / { proxy_pass http://zipkin; }}
nginx配置
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>
错误
优点:简单,无需添加额外的中间件,缺点:服务性能消耗较大
create DATABASE zipkin;
创建数据库
建表
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
docker-compose.yml文件
调整zipkin的docker-compose.yml新增mysql相关配置
docker-compose down &&docker-compose up -d&&dopcker-compose logs -f
重新启动zipkin
直接持久化到mysql
基于以上直接持久化到mysql的数据库脚本
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
安装rabbitmq,docker方式启动,根据实际情况修改启动脚本
子主题
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
添加rabbitmq配置项
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.
@Configurationpublic 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(); }}
需要注入Sender与ConnectFactory
项目启动报错
浏览器访问验证
服务项目application.yml配置
通过rabbitmq持久化到mysql
待完善。。。。
持久化到elasticsearch
springcloud集成zipkin
0 条评论
回复 删除
下一页