mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
增加 spring cloud sleuth 链路追踪
This commit is contained in:
95
labx-13/labx-13-sc-sleuth-db-redis/pom.xml
Normal file
95
labx-13/labx-13-sc-sleuth-db-redis/pom.xml
Normal file
@@ -0,0 +1,95 @@
|
||||
<?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">
|
||||
<parent>
|
||||
<artifactId>labx-13</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-13-sc-sleuth-db-redis</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<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>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 实现对 Spring Data Redis 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
<exclusions>
|
||||
<!-- 去掉对 Lettuce 的依赖,因为 Spring Boot 优先使用 Lettuce 作为 Redis 客户端 -->
|
||||
<exclusion>
|
||||
<groupId>io.lettuce</groupId>
|
||||
<artifactId>lettuce-core</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<!-- 引入 Jedis 的依赖,这样 Spring Boot 实现对 Jedis 的自动化配置 -->
|
||||
<dependency>
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Spring Cloud Sleuth + Zipkin 相关依赖,实现对它们的自动配置,从而实现链路追踪 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-zipkin</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Brave 对 Opentracing 的实现 -->
|
||||
<dependency>
|
||||
<groupId>io.opentracing.brave</groupId>
|
||||
<artifactId>brave-opentracing</artifactId>
|
||||
<version>0.35.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Opentracing 对 Redis 的支持 -->
|
||||
<dependency>
|
||||
<groupId>io.opentracing.contrib</groupId>
|
||||
<artifactId>opentracing-redis-jedis3</artifactId>
|
||||
<version>0.1.14</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.opentracing.contrib</groupId>
|
||||
<artifactId>opentracing-redis-spring-data</artifactId>
|
||||
<version>0.1.14</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,13 @@
|
||||
package cn.iocoder.springcloud.labx13.springmvcdemo;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class UserServiceApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(UserServiceApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package cn.iocoder.springcloud.labx13.springmvcdemo.config;
|
||||
|
||||
import io.opentracing.Tracer;
|
||||
import io.opentracing.contrib.redis.common.TracingConfiguration;
|
||||
import io.opentracing.contrib.redis.spring.data.connection.TracingRedisConnectionFactory;
|
||||
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
||||
|
||||
@Configuration
|
||||
public class ZipkinConfiguration {
|
||||
|
||||
// ==================== Redis 相关 ====================
|
||||
@Bean
|
||||
public RedisConnectionFactory redisConnectionFactory(Tracer tracer, RedisProperties redisProperties) {
|
||||
// 创建 JedisConnectionFactory 对象
|
||||
RedisConnectionFactory connectionFactory = new JedisConnectionFactory();
|
||||
// 创建 TracingConfiguration 对象
|
||||
TracingConfiguration tracingConfiguration = new TracingConfiguration.Builder(tracer)
|
||||
// 设置拓展 Tag ,设置 Redis 服务器地址。因为默认情况下,不会在操作 Redis 链路的 Span 上记录 Redis 服务器的地址,所以这里需要设置。
|
||||
.extensionTag("Server Address", redisProperties.getHost() + ":" + redisProperties.getPort())
|
||||
.build();
|
||||
// 创建 TracingRedisConnectionFactory 对象
|
||||
return new TracingRedisConnectionFactory(connectionFactory, tracingConfiguration);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package cn.iocoder.springcloud.labx13.springmvcdemo.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/user")
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private StringRedisTemplate redisTemplate;
|
||||
|
||||
@GetMapping("/get")
|
||||
public String get(@RequestParam("id") Integer id) {
|
||||
this.get("demo");
|
||||
return "success";
|
||||
}
|
||||
|
||||
public void get(String key) {
|
||||
redisTemplate.opsForValue().get(key);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
spring:
|
||||
application:
|
||||
name: user-service # 服务名
|
||||
|
||||
# Zipkin 配置项,对应 ZipkinProperties 类
|
||||
zipkin:
|
||||
base-url: http://127.0.0.1:9411 # Zipkin 服务的地址
|
||||
|
||||
# Spring Cloud Sleuth 配置项
|
||||
sleuth:
|
||||
# Spring Cloud Sleuth 针对 Web 组件的配置项,例如说 SpringMVC
|
||||
web:
|
||||
enabled: true # 是否开启,默认为 true
|
||||
|
||||
# 对应 RedisProperties 类
|
||||
redis:
|
||||
host: 127.0.0.1
|
||||
port: 6379
|
||||
password: # Redis 服务器密码,默认为空。生产中,一定要设置 Redis 密码!
|
||||
database: 0 # Redis 数据库号,默认为 0 。
|
||||
timeout: 0 # Redis 连接超时时间,单位:毫秒。
|
||||
# 对应 RedisProperties.Jedis 内部类
|
||||
jedis:
|
||||
pool:
|
||||
max-active: 8 # 连接池最大连接数,默认为 8 。使用负数表示没有限制。
|
||||
max-idle: 8 # 默认连接数最小空闲的连接数,默认为 8 。使用负数表示没有限制。
|
||||
min-idle: 0 # 默认连接池最小空闲的连接数,默认为 0 。允许设置 0 和 正数。
|
||||
max-wait: -1 # 连接池最大阻塞等待时间,单位:毫秒。默认为 -1 ,表示不限制。
|
||||
@@ -20,6 +20,7 @@
|
||||
<module>labx-13-sc-sleuth-dubbo</module>
|
||||
|
||||
<module>labx-13-sc-sleuth-db-mysql</module>
|
||||
<module>labx-13-sc-sleuth-db-redis</module>
|
||||
</modules>
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user