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:
78
labx-13/labx-13-sc-sleuth-db-mongodb/pom.xml
Normal file
78
labx-13/labx-13-sc-sleuth-db-mongodb/pom.xml
Normal file
@@ -0,0 +1,78 @@
|
||||
<?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-mongodb</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 Mongodb -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-mongodb</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 对 MongoDB 的支持 -->
|
||||
<dependency>
|
||||
<groupId>io.opentracing.contrib</groupId>
|
||||
<artifactId>opentracing-mongo-driver</artifactId>
|
||||
<version>0.1.5</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,22 @@
|
||||
package cn.iocoder.springcloud.labx13.springmvcdemo.config;
|
||||
|
||||
import com.mongodb.MongoClientOptions;
|
||||
import io.opentracing.Tracer;
|
||||
import io.opentracing.contrib.mongo.common.TracingCommandListener;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
public class SleuthConfiguration {
|
||||
|
||||
// ==================== MongoDB 相关 ====================
|
||||
|
||||
@Bean
|
||||
public MongoClientOptions mongoClientOptions(Tracer tracer) {
|
||||
// 创建 TracingCommandListener 对象
|
||||
TracingCommandListener listener = new TracingCommandListener.Builder(tracer).build();
|
||||
// 创建 MongoClientOptions 对象,并设置监听器
|
||||
return MongoClientOptions.builder().addCommandListener(listener).build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package cn.iocoder.springcloud.labx13.springmvcdemo.controller;
|
||||
|
||||
import cn.iocoder.springcloud.labx13.springmvcdemo.dataobject.UserDO;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
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 MongoTemplate mongoTemplate;
|
||||
|
||||
@GetMapping("/get")
|
||||
public String get(@RequestParam("id") Integer id) {
|
||||
this.findById(1);
|
||||
return "success";
|
||||
}
|
||||
|
||||
public UserDO findById(Integer id) {
|
||||
return mongoTemplate.findOne(new Query(Criteria.where("_id").is(id)), UserDO.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package cn.iocoder.springcloud.labx13.springmvcdemo.dataobject;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.mongodb.core.mapping.Document;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 用户 DO
|
||||
*/
|
||||
@Document(collection = "User")
|
||||
public class UserDO {
|
||||
|
||||
@Id
|
||||
private Integer id;
|
||||
/**
|
||||
* 账号
|
||||
*/
|
||||
private String username;
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
private String password;
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private Date createTime;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "UserDO{" +
|
||||
"id=" + id +
|
||||
", username='" + username + '\'' +
|
||||
", password='" + password + '\'' +
|
||||
", createTime=" + createTime +
|
||||
'}';
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
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
|
||||
|
||||
data:
|
||||
# MongoDB 配置项,对应 MongoProperties 类
|
||||
mongodb:
|
||||
host: 127.0.0.1
|
||||
port: 27017
|
||||
database: yourdatabase
|
||||
username: test01
|
||||
password: password01
|
||||
# 上述属性,也可以只配置 uri
|
||||
@@ -10,7 +10,7 @@ import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
|
||||
|
||||
@Configuration
|
||||
public class ZipkinConfiguration {
|
||||
public class SleuthConfiguration {
|
||||
|
||||
// ==================== Redis 相关 ====================
|
||||
@Bean
|
||||
@@ -21,6 +21,7 @@
|
||||
|
||||
<module>labx-13-sc-sleuth-db-mysql</module>
|
||||
<module>labx-13-sc-sleuth-db-redis</module>
|
||||
<module>labx-13-sc-sleuth-db-mongodb</module>
|
||||
</modules>
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user