Spring Cloud服务跟踪

Zipkin-Server服务搭建

zipkin服务可以用单独的运行也可以集成到spring boot上运行

创建 zipkin-server 模块,用来启动一个zipkin-server服务,引入依赖

<dependency>
    <groupId>io.zipkin.java</groupId>
    <artifactId>zipkin-server</artifactId>
</dependency>
<dependency>
    <groupId>io.zipkin.java</groupId>
    <artifactId>zipkin-autoconfigure-ui</artifactId>
    <scope>runtime</scope>
</dependency>

注意版本不能太高,2.12.6 版本后要使用 Armeria 作为HTTP服务器,推荐用 2.12.3

application.properties

server.port=8081

# 加上这个配置,不然访问zipkin-server会报错
management.metrics.web.server.auto-time-requests=false

在启动类上加上 @EnableZipkinServer 注解,因为zipkin是以单独的服务发布的,所以可以不用注册到eureka上

监控服务调用

创建一个服务提供者和一个服务消费者,监控服务消费者掉服务提供者的调用

服务提供者

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>
eureka.client.service-url.defaultZone=https://localhost:8080/eureka
spring.application.name=zipkin-provider
server.port=8082

# zipkin-server地址
spring.zipkin.base-url=https://localhost:8081
# 收集频率,默认0.1
spring.sleuth.sampler.probability=1

提供一个测试接口

@RestController
public class ProviderController {
    @RequestMapping("/test")
    public String test() {
        return "test";
    }
}

服务消费者

使用feign请求provider

添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>
eureka.client.service-url.defaultZone=https://localhost:8080/eureka
spring.application.name=zipkin-consumer
server.port=8083

# zipkin-server地址
spring.zipkin.base-url=https://localhost:8081
# 收集频率,默认0.1
spring.sleuth.sampler.probability=1

feign接口

@FeignClient("ZIPKIN-PROVIDER")
public interface ProviderClient {
    @RequestMapping("/test")
    String test();
}

测试接口

@RestController
public class ConsumerController {

    @Autowired
    private ProviderClient providerClient;

    @RequestMapping("/test")
    public String test() {
        return providerClient.test();
    }
}

访问consumer的 /test 接口,就能看到consumer调用provider的调用过程

image20200614143844074.png

调用 localhost:8083/test 接口,在 https://localhost:8081/zipkin 就能看到调用过程,每个服务花费的时间和一共调用了几个服务。后期微服务多的情况,可以通过这种方式判断哪个服务耗时最长,哪个服务调用失败。就可以知道关键是哪个服务除了问题,缩小排错范围。

image20200614143945089.png

image20200614143957293.png

zipkin-consumer依赖zipkin-provider服务

image20200614144010865.png

zipkin-providerzipkin-consumer服务依赖

image20200614144017856.png

使用Mysql存储日志

创建 zipkin-server-mysql 模块,在 zipkin-server 基础上添加数据库依赖

<dependency>
    <groupId>io.zipkin.java</groupId>
    <artifactId>zipkin-autoconfigure-storage-mysql</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

添加数据库配置

server.port=8081

# 加上这个配置,不然访问zipkin-server会报错
management.metrics.web.server.auto-time-requests=false

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://192.168.159.128:3306/test?characterEncoding=UTF-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.continue-on-error=true

zipkin-storage-mysql 依赖下找到 mysql.sql 脚本,在 test 数据库上运行

image20200615194223053.png

CREATE TABLE IF NOT EXISTS zipkin_spans (
  `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
  `trace_id` BIGINT NOT NULL,
  `id` BIGINT NOT NULL,
  `name` VARCHAR(255) NOT NULL,
  `parent_id` BIGINT,
  `debug` BIT(1),
  `start_ts` BIGINT COMMENT 'Span.timestamp(): epoch micros used for endTs query and to implement TTL',
  `duration` BIGINT COMMENT 'Span.duration(): micros used for minDuration and maxDuration query'
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;

ALTER TABLE zipkin_spans ADD UNIQUE KEY(`trace_id_high`, `trace_id`, `id`) COMMENT 'ignore insert on duplicate';
ALTER TABLE zipkin_spans ADD INDEX(`trace_id_high`, `trace_id`, `id`) COMMENT 'for joining with zipkin_annotations';
ALTER TABLE zipkin_spans ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTracesByIds';
ALTER TABLE zipkin_spans ADD INDEX(`name`) COMMENT 'for getTraces and getSpanNames';
ALTER TABLE zipkin_spans ADD INDEX(`start_ts`) COMMENT 'for getTraces ordering and range';

CREATE TABLE IF NOT EXISTS zipkin_annotations (
  `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
  `trace_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.trace_id',
  `span_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.id',
  `a_key` VARCHAR(255) NOT NULL COMMENT 'BinaryAnnotation.key or Annotation.value if type == -1',
  `a_value` BLOB COMMENT 'BinaryAnnotation.value(), which must be smaller than 64KB',
  `a_type` INT NOT NULL COMMENT 'BinaryAnnotation.type() or -1 if Annotation',
  `a_timestamp` BIGINT COMMENT 'Used to implement TTL; Annotation.timestamp or zipkin_spans.timestamp',
  `endpoint_ipv4` INT COMMENT 'Null when Binary/Annotation.endpoint is null',
  `endpoint_ipv6` BINARY(16) COMMENT 'Null when Binary/Annotation.endpoint is null, or no IPv6 address',
  `endpoint_port` SMALLINT COMMENT 'Null when Binary/Annotation.endpoint is null',
  `endpoint_service_name` VARCHAR(255) COMMENT 'Null when Binary/Annotation.endpoint is null'
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;

ALTER TABLE zipkin_annotations ADD UNIQUE KEY(`trace_id_high`, `trace_id`, `span_id`, `a_key`, `a_timestamp`) COMMENT 'Ignore insert on duplicate';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`, `span_id`) COMMENT 'for joining with zipkin_spans';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id_high`, `trace_id`) COMMENT 'for getTraces/ByIds';
ALTER TABLE zipkin_annotations ADD INDEX(`endpoint_service_name`) COMMENT 'for getTraces and getServiceNames';
ALTER TABLE zipkin_annotations ADD INDEX(`a_type`) COMMENT 'for getTraces and autocomplete values';
ALTER TABLE zipkin_annotations ADD INDEX(`a_key`) COMMENT 'for getTraces and autocomplete values';
ALTER TABLE zipkin_annotations ADD INDEX(`trace_id`, `span_id`, `a_key`) COMMENT 'for dependencies job';

CREATE TABLE IF NOT EXISTS zipkin_dependencies (
  `day` DATE NOT NULL,
  `parent` VARCHAR(255) NOT NULL,
  `child` VARCHAR(255) NOT NULL,
  `call_count` BIGINT,
  `error_count` BIGINT
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;

ALTER TABLE zipkin_dependencies ADD UNIQUE KEY(`day`, `parent`, `child`);

和之前一样,只是把 zipkin-server 替换成了 zipkin-server-mysql, 启动服务之后,访问 localhost:8083/test 接口,发现数据被保存到了Mysql中


源码:https://codox.coding.net/public/springcloud-learning/springcloud-learning/git/files

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×