欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring Cloud:Sleuth(七)

程序员文章站 2022-06-12 15:25:01
...

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:Sleuth(七)

 

端口信息

  • spring-cloud-zuul:7070
  • person-client:8080
  • person-service:9090
  • Eureka Server:12345
  • ZipKin Server:23456
  • Config Server:10001

服务启动顺序

  1. zipkin Server
  2. Eureka Server
  3. spring-cloud-config-server
  4. person-server
  5. person-client
  6. spring-cloud-zuul
  7. 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/

相关标签: 微服务