Spring Cloud:Sleuth(七)
Spring Cloud Sleuth 整合
引入Maven依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency>
日志发生的变化
当应用ClassPath下存在org.springfreamwork.cloud:spring-cloud-starter-sleuth的时候,日志会发生调整。
**
@SpringBootApplication public class SpringCloudSleuthDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudSleuthDemoApplication.class, args); } }
Zipkin整合
创建 Spring Cloud Zipkin服务器
增加Maven依赖
<!-- 服务器依赖 --> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-server</artifactId> <version>2.11.11</version> </dependency> <!-- 服务器ui控制器 --> <dependency> <groupId>io.zipkin.java</groupId> <artifactId>zipkin-autoconfigure-ui</artifactId> <version>2.11.11</version> </dependency>
**Zipkin服务器
@SpringBootApplication @EnableZipkinServer public class SpringCloudZipkinDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudZipkinDemoApplication.class, args); } }
HTTP 收集 (HTTP调用)
简单整合spring-cloud-sleuth
增加Maven依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency>
Spring Cloud 服务整合
端口信息
- spring-cloud-zuul:7070
- person-client:8080
- person-service:9090
- Eureka Server:12345
- ZipKin Server:23456
- Config Server:10001
服务启动顺序
- zipkin Server
- Eureka Server
- spring-cloud-config-server
- person-server
- person-client
- spring-cloud-zuul
- spring-cloud-sleuth
spring-cloud-sleuth-demo改造
增加Eureka客户端依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
配置调整
spring.application.name = spring-cloud-sleuth server.port = 6060 spring.zipkin.base-url=http://localhost:23456/ eureka.client.serviceUrl.defaultZone=http://localhost:12345/eureka
调整代码链接:spring-cloud-zuul
完整调用链路
spring-cloud-sleuth → spring-cloud-zuul → person-client → person-service
@RestController
public class TestLoggerController {
final static Logger LOGGER = LoggerFactory.getLogger(TestLoggerController.class);
@Autowired
@Qualifier("restTemplate")
private RestTemplate restTemplate;
@GetMapping("/send")
public void send() {
LOGGER.info(" 欢迎欢迎!");
}
@GetMapping("/to/zuul/pseron-clint/findall")
public Object findall() {
LOGGER.info("TestLoggerController#findall()");
return restTemplate.getForObject("http://spring-cloud-zuul/person-client/person/findall", Object.class);
}
}
spring-cloud-zuul上报Zipkin服务器
依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency>
配置
spring.zipkin.base-url=http://localhost:23456/
person-client上报Zipkin服务器
依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency>
配置
spring.zipkin.base-url=http://localhost:23456/
person-service上报Zipkin服务器
依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency>
配置
spring.zipkin.base-url=http://localhost:23456/
Spring Cloud Stream 收集消息(消息)
调整spring-cloud-zipkin-server 通过Steam来收集
增加Maven依赖
<dependency><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-sleuth-zipkin-stream</artifactId> </dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-stream-binder-kafka</artifactId> </dependency>
**Zipkin Stream
@SpringBootApplication //@EnableZipkinServer @EnableZipkinStreamServer public class SpringCloudZipkinDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudZipkinDemoApplication.class, args); } }
调整spring-cloud-zuul
增加依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-sleuth-stream</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-stream-binder-kafka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency>
注释HTTP上报URL
##日志上报
##spring.zipkin.base-url=http://localhost:23456/
推荐阅读
-
详解基于Spring Cloud几行配置完成单点登录开发
-
Spring Cloud zuul自定义统一异常处理实现方法
-
Spring Cloud体系实现标签路由的方法示例
-
Spring Cloud学习教程之DiscoveryClient的深入探究
-
Spring Cloud Gateway入门解读
-
Spring Cloud学习教程之Zuul统一异常处理与回退
-
详解spring cloud整合Swagger2构建RESTful服务的APIs
-
[Spring cloud 一步步实现广告系统] 16. 增量索引实现以及投送数据到MQ(kafka)
-
spring cloud config 配置中心快速实现过程解析
-
Spring Cloud Gateway 服务网关快速实现解析