Sleuth简介
Sleuth是SpringCloud微服务系统中的一个组件,实现了链路追踪解决方案。可以定位一个请求到底请求了哪些具体的服务。在复杂的微服务系统中,如果请求发生了异常,可以快速捕获问题所在的服务。
项目结构
//注册中心
why-eureka-2001
//链路数据收集服务
why-zipkin-5001
//服务提供
why-provider-1001
why-provider-1002
//网关路由
//why-zuul-3001
eureka注册中心
所属模块:why-eureka-2001
核心依赖
<!--eureka-server服务端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
YML配置
server:
port: 2001
spring:
application:
name: why-eureka-2001
eureka:
instance:
hostname: why.eureka.com
prefer-ip-address: true
client:
# false表示不向注册中心注册自己
register-with-eureka: false
# false表示该端就是注册中心,维护服务实例,不去检索服务
fetch-registry: false
service-url:
# 单点注册中心
defaultZone: http://localhost:2001/eureka/
启动类
@SpringBootApplication
@EnableEurekaServer // 注册中心注解
public class Application_7001 {
public static void main(String[] args) {
SpringApplication.run(Application_7001.class,args) ;
}
}
提供方1001
所属模块:why-provider-1001
核心依赖
<!-- 将微服务provider侧注册进eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<!-- Ribbon 组件 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
YML配置
server:
port: 1001
spring:
application:
name: why-provider-1001
zipkin:
base-url: http://localhost:5001
sleuth:
sampler:
# 数据 100% 上传
percentage: 1.0
eureka:
instance:
hostname: why-provider-1001
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:2001/eureka/
启动类
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class Application_1001 {
public static void main(String[] args) {
SpringApplication.run(Application_1001.class,args) ;
}
}
文件配置
@Configuration
public class LoadConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate (){
return new RestTemplate() ;
}
@Bean
public IRule getIRule (){
// 默认轮询算法
return new RoundRobinRule() ;
}
}
提供方式
@RestController
public class ProviderController {
@Autowired
private RestTemplate restTemplate ;
@RequestMapping(value = "/getInfo/{authorId}",method = RequestMethod.GET)
public String getAuthorInfo (@PathVariable("authorId") String authorId) {
return "我最帅气了"+authorId ;
}
@RequestMapping("/getInfo1002")
public String get6001Info (){
return restTemplate.getForObject("http://why-provider-1002/getInfo",String.class) ;
}
}
提供方1002
所属模块:why-provider-1002
核心依赖
<!-- 将微服务provider侧注册进eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
YML配置
server:
port: 1002
spring:
application:
name: why-provider-1002
zipkin:
base-url: http://localhost:5001
sleuth:
sampler:
# 数据 100% 上传
percentage: 1.0
eureka:
instance:
hostname: why-provider-1002
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:2001/eureka/
启动类
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class Application_1002 {
public static void main(String[] args) {
SpringApplication.run(Application_1002.class,args) ;
}
}
提供方式
@RestController
public class ProviderController {
@RequestMapping(value = "/getInfo/{authorId}",method = RequestMethod.GET)
public String getAuthorInfo (@PathVariable("authorId") String authorId) {
return "我最帅了"+authorId ;
}
@RequestMapping(value = "/getInfo1002",method = RequestMethod.GET)
public String get6002Info () {
return "Info1002" ;
}
}
Zuul网关路由
所属模块:why-zuul-3001
核心依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 路由网关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
YML配置
server:
port: 3001
spring:
application:
name: why-zuul-3001
zipkin:
base-url: http://localhost:5001
sleuth:
sampler:
# 数据 100% 上传
percentage: 1.0
eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:2001/eureka/
zuul:
# 前缀,可以用来做版本控制
prefix: /pre
# 禁用默认路由,执行配置的路由
ignored-services: "*"
routes:
# 配置1001接口微服务
pro6001:
serviceId: why-provider-1001
path: /api-1001/**
# 配置1002接口微服务
pro6002:
serviceId: why-provider-1002
path: /api-1002/**
启动类
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class Application_3001 {
public static void main(String[] args) {
SpringApplication.run(Application_3001.class,args) ;
}
}
Zipkin链路收集
所属模块:why-zipkin-5001
核心依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-server</artifactId>
</dependency>
<dependency>
<groupId>io.zipkin.java</groupId>
<artifactId>zipkin-autoconfigure-ui</artifactId>
</dependency>
YML配置
server:
port: 5001
spring:
application:
name: why-zipkin-5001
eureka:
instance:
hostname: why-zipkin-5001
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:2001/eureka/
启动类
@SpringBootApplication
@EnableEurekaClient
@EnableZipkinServer
public class Application_5001 {
public static void main(String[] args) {
SpringApplication.run(Application_5001.class,args) ;
}
}
启动流程
注意:服务启动顺序如下:
注册中心
why-eureka-2001
链路数据收集服务
why-zipkin-5001
服务提供
why-provider-1001
why-provider-1002
网关路由
why-zuul-3001
我们先启动所有的服务,eureka中可以看到存在了服务。
接口访问流程
访问接口
http://localhost:7002/v1/api-6001/getInfo/11111
这个请求从网关服务进入,到达1001端口服务之后,请求1002端,最终返回结果。
1001接口
@RequestMapping("/getInfo1002")
public String get6001Info (){
return restTemplate.getForObject("http://why-provider-1002/getInfo/55555",String.class) ;
}
1002接口
@RequestMapping(value = "/getInfo/{authorId}",method = RequestMethod.GET)
public String getAuthorInfo (@PathVariable("authorId") String authorId) {
return "我最帅了"+authorId ;
}
链路管理界面
访问接口:http://localhost:5001/zipkin/
依赖分析