diff --git a/db/4pigxx_zipkin.sql b/db/4pigxx_zipkin.sql new file mode 100644 index 000000000..e43df51a3 --- /dev/null +++ b/db/4pigxx_zipkin.sql @@ -0,0 +1,78 @@ +/* + Navicat Premium Data Transfer + + Source Server : 本机mysql + Source Server Type : MySQL + Source Server Version : 50724 + Source Host : localhost:3306 + Source Schema : pigxx_zipkin + + Target Server Type : MySQL + Target Server Version : 50724 + File Encoding : 65001 + + Date: 22/02/2019 11:44:20 +*/ + +SET NAMES utf8mb4; +SET FOREIGN_KEY_CHECKS = 0; + +-- ---------------------------- +-- Table structure for zipkin_annotations +-- ---------------------------- +DROP TABLE IF EXISTS `zipkin_annotations`; +CREATE TABLE `zipkin_annotations` ( + `trace_id_high` bigint(20) NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit', + `trace_id` bigint(20) NOT NULL COMMENT 'coincides with zipkin_spans.trace_id', + `span_id` bigint(20) NOT NULL COMMENT 'coincides with zipkin_spans.id', + `a_key` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'BinaryAnnotation.key or Annotation.value if type == -1', + `a_value` blob NULL COMMENT 'BinaryAnnotation.value(), which must be smaller than 64KB', + `a_type` int(11) NOT NULL COMMENT 'BinaryAnnotation.type() or -1 if Annotation', + `a_timestamp` bigint(20) NULL DEFAULT NULL COMMENT 'Used to implement TTL; Annotation.timestamp or zipkin_spans.timestamp', + `endpoint_ipv4` int(11) NULL DEFAULT NULL COMMENT 'Null when Binary/Annotation.endpoint is null', + `endpoint_ipv6` binary(16) NULL DEFAULT NULL COMMENT 'Null when Binary/Annotation.endpoint is null, or no IPv6 address', + `endpoint_port` smallint(6) NULL DEFAULT NULL COMMENT 'Null when Binary/Annotation.endpoint is null', + `endpoint_service_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT 'Null when Binary/Annotation.endpoint is null', + UNIQUE INDEX `trace_id_high`(`trace_id_high`, `trace_id`, `span_id`, `a_key`, `a_timestamp`) USING BTREE COMMENT 'Ignore insert on duplicate', + INDEX `trace_id_high_2`(`trace_id_high`, `trace_id`, `span_id`) USING BTREE COMMENT 'for joining with zipkin_spans', + INDEX `trace_id_high_3`(`trace_id_high`, `trace_id`) USING BTREE COMMENT 'for getTraces/ByIds', + INDEX `endpoint_service_name`(`endpoint_service_name`) USING BTREE COMMENT 'for getTraces and getServiceNames', + INDEX `a_type`(`a_type`) USING BTREE COMMENT 'for getTraces', + INDEX `a_key`(`a_key`) USING BTREE COMMENT 'for getTraces', + INDEX `trace_id`(`trace_id`, `span_id`, `a_key`) USING BTREE COMMENT 'for dependencies job' +) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compressed; + +-- ---------------------------- +-- Table structure for zipkin_dependencies +-- ---------------------------- +DROP TABLE IF EXISTS `zipkin_dependencies`; +CREATE TABLE `zipkin_dependencies` ( + `day` date NOT NULL, + `parent` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, + `child` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, + `call_count` bigint(20) NULL DEFAULT NULL, + `error_count` bigint(20) NULL DEFAULT NULL, + UNIQUE INDEX `day`(`day`, `parent`, `child`) USING BTREE +) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compressed; + +-- ---------------------------- +-- Table structure for zipkin_spans +-- ---------------------------- +DROP TABLE IF EXISTS `zipkin_spans`; +CREATE TABLE `zipkin_spans` ( + `trace_id_high` bigint(20) NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit', + `trace_id` bigint(20) NOT NULL, + `id` bigint(20) NOT NULL, + `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, + `parent_id` bigint(20) NULL DEFAULT NULL, + `debug` bit(1) NULL DEFAULT NULL, + `start_ts` bigint(20) NULL DEFAULT NULL COMMENT 'Span.timestamp(): epoch micros used for endTs query and to implement TTL', + `duration` bigint(20) NULL DEFAULT NULL COMMENT 'Span.duration(): micros used for minDuration and maxDuration query', + UNIQUE INDEX `trace_id_high`(`trace_id_high`, `trace_id`, `id`) USING BTREE COMMENT 'ignore insert on duplicate', + INDEX `trace_id_high_2`(`trace_id_high`, `trace_id`, `id`) USING BTREE COMMENT 'for joining with zipkin_annotations', + INDEX `trace_id_high_3`(`trace_id_high`, `trace_id`) USING BTREE COMMENT 'for getTracesByIds', + INDEX `name`(`name`) USING BTREE COMMENT 'for getTraces and getSpanNames', + INDEX `start_ts`(`start_ts`) USING BTREE COMMENT 'for getTraces ordering and range' +) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compressed; + +SET FOREIGN_KEY_CHECKS = 1; diff --git a/db/Dockerfile b/db/Dockerfile index 6bbef9e13..d6da73e97 100644 --- a/db/Dockerfile +++ b/db/Dockerfile @@ -8,4 +8,6 @@ COPY ./db/1schema.sql /docker-entrypoint-initdb.d COPY ./db/2pigxx.sql /docker-entrypoint-initdb.d -COPY ./db/3pigxx_ac.sql /docker-entrypoint-initdb.d \ No newline at end of file +COPY ./db/3pigxx_ac.sql /docker-entrypoint-initdb.d + +COPY ./db/4pigxx_zipkin.sql /docker-entrypoint-initdb.d \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 3cbe8accf..3eb10cdbf 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -80,6 +80,16 @@ services: ports: - 5001:5001 + pigx-zipkin: + build: + context: ./ + dockerfile: ./pigx-visual/pigx-zipkin/Dockerfile + restart: always + image: pigx-zipkin + container_name: pigx-zipkin + ports: + - 5006:5006 + pigx-daemon: build: context: ./ diff --git a/pigx-config/src/main/resources/config/application-dev.yml b/pigx-config/src/main/resources/config/application-dev.yml index 1a0db6139..263f6a97f 100644 --- a/pigx-config/src/main/resources/config/application-dev.yml +++ b/pigx-config/src/main/resources/config/application-dev.yml @@ -7,6 +7,18 @@ spring: redis: password: host: pigx-redis + # zipkin + zipkin: + enabled: true + base-url: http://pigx-zipkin + sleuth: + web: + client: + enabled: true + sampler: + # 默认的采样比率为0.1,不能看到所有请求数据 + # 更改采样比率为1,就能看到所有的请求数据了,但是这样会增加接口调用延迟 + probability: 1.0 # 暴露监控端点 management: @@ -98,4 +110,4 @@ security: - /v2/api-docs resource: loadBalanced: true - token-info-uri: http://pigx-auth/oauth/check_token + token-info-uri: http://pigx-auth/oauth/check_token \ No newline at end of file diff --git a/pigx-config/src/main/resources/config/pigx-zipkin-dev.yml b/pigx-config/src/main/resources/config/pigx-zipkin-dev.yml new file mode 100644 index 000000000..ddd466c11 --- /dev/null +++ b/pigx-config/src/main/resources/config/pigx-zipkin-dev.yml @@ -0,0 +1,18 @@ +spring: + # 数据源 + datasource: + type: com.zaxxer.hikari.HikariDataSource + driver-class-name: com.mysql.cj.jdbc.Driver + username: root + password: root + url: jdbc:mysql://pigx-mysql:3306/pigxx_zipkin?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8 +management: + metrics: + web: + server: + auto-time-requests: false +zipkin: + storage: + type: mysql +sleuth: + enabled: true \ No newline at end of file diff --git a/pigx-visual/pigx-zipkin/Dockerfile b/pigx-visual/pigx-zipkin/Dockerfile new file mode 100644 index 000000000..3de381941 --- /dev/null +++ b/pigx-visual/pigx-zipkin/Dockerfile @@ -0,0 +1,15 @@ +FROM anapsix/alpine-java:8_server-jre_unlimited + +MAINTAINER wangiegie@gmail.com + +RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime + +RUN mkdir -p /pigx-zipkin + +WORKDIR /pigx-zipkin + +EXPOSE 5006 + +ADD ./pigx-visual/pigx-zipkin/target/pigx-zipkin.jar ./ + +CMD java -Djava.security.egd=file:/dev/./urandom -jar pigx-zipkin.jar diff --git a/pigx-visual/pigx-zipkin/pom.xml b/pigx-visual/pigx-zipkin/pom.xml new file mode 100644 index 000000000..0bc9ed101 --- /dev/null +++ b/pigx-visual/pigx-zipkin/pom.xml @@ -0,0 +1,81 @@ + + + + pigx-visual + com.pig4cloud + 2.5.1 + + 4.0.0 + + pigx-zipkin + pigx zipkin追踪服务 + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-undertow + + + + org.springframework.cloud + spring-cloud-starter-config + + + org.springframework.boot + spring-boot-starter-jdbc + + + + io.zipkin.java + zipkin-server + ${zipkin.version} + + + io.zipkin.java + zipkin-autoconfigure-ui + ${zipkin.version} + + + + io.zipkin.java + zipkin-autoconfigure-storage-mysql + ${zipkin.version} + + + io.zipkin.java + zipkin-storage-mysql + ${zipkin-storage-mysql.version} + + + + org.jooq + jooq + + + mysql + mysql-connector-java + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + com.spotify + docker-maven-plugin + ${docker.plugin.version} + + + + + + \ No newline at end of file diff --git a/pigx-visual/pigx-zipkin/src/main/java/com/pig4cloud/pigx/zipkin/PigxZipkinApplication.java b/pigx-visual/pigx-zipkin/src/main/java/com/pig4cloud/pigx/zipkin/PigxZipkinApplication.java new file mode 100644 index 000000000..89279ef5b --- /dev/null +++ b/pigx-visual/pigx-zipkin/src/main/java/com/pig4cloud/pigx/zipkin/PigxZipkinApplication.java @@ -0,0 +1,29 @@ +package com.pig4cloud.pigx.zipkin; + +import org.springframework.boot.SpringApplication; +import org.springframework.cloud.client.SpringCloudApplication; +import org.springframework.context.annotation.Bean; +import zipkin.storage.mysql.MySQLStorage; +import zipkin2.server.internal.EnableZipkinServer; + +import javax.sql.DataSource; + +/** + * 服务链路追踪 + * + * @author: 仙(๑ • ㅂ • ́)و✧酱 + * @date: 2019/2/22 + */ +@SpringCloudApplication +@EnableZipkinServer +public class PigxZipkinApplication { + public static void main(String[] args) { + SpringApplication.run(PigxZipkinApplication.class, args); + } + + @Bean + public MySQLStorage mySQLStorage(DataSource datasource) { + return MySQLStorage.builder().datasource(datasource).executor(Runnable::run).build(); + } + +} diff --git a/pigx-visual/pigx-zipkin/src/main/resources/bootstrap.yml b/pigx-visual/pigx-zipkin/src/main/resources/bootstrap.yml new file mode 100644 index 000000000..c32ca91e0 --- /dev/null +++ b/pigx-visual/pigx-zipkin/src/main/resources/bootstrap.yml @@ -0,0 +1,27 @@ +server: + port: 5006 + +spring: + application: + name: @artifactId@ + #配置中心 + cloud: + config: + fail-fast: true + name: ${spring.application.name} + profile: ${spring.profiles.active} + discovery: + enabled: true + service-id: pigx-config + main: + allow-bean-definition-overriding: true + profiles: + active: dev + +# 注册中心配置 +eureka: + instance: + prefer-ip-address: true + client: + service-url: + defaultZone: http://pig:pig@pigx-eureka:8761/eureka/ \ No newline at end of file diff --git a/pigx-visual/pom.xml b/pigx-visual/pom.xml index bed6736fc..0f491321b 100644 --- a/pigx-visual/pom.xml +++ b/pigx-visual/pom.xml @@ -36,7 +36,8 @@ pigx-codegen pigx-daemon pigx-monitor - pigx-tx-manager pigx-sso-client-demo + pigx-tx-manager + pigx-zipkin diff --git a/pom.xml b/pom.xml index ff38f40b2..b46cc56bd 100644 --- a/pom.xml +++ b/pom.xml @@ -62,6 +62,8 @@ 2.3.4.RELEASE 2.1.2.RELEASE 5.22.0 + 2.12.2 + 2.8.4 http://192.168.0.13:4243 0.4.12 192.168.0.13:5000 @@ -96,6 +98,15 @@ spring-boot-admin-starter-client ${spring-boot-admin.version} + + + org.springframework.cloud + spring-cloud-starter-sleuth + + + org.springframework.cloud + spring-cloud-sleuth-zipkin + org.springframework.cloud