From b65302ba98cf3998c3bb2277d1d6abc013b02860 Mon Sep 17 00:00:00 2001 From: lishangbu Date: Sat, 23 Feb 2019 21:59:12 +0800 Subject: [PATCH] :sparkles: Introducing new features.add zipkin --- db/pig.sql | 58 +++++++++++ docker-compose.yml | 10 ++ .../main/resources/config/application-dev.yml | 15 ++- .../main/resources/config/pig-zipkin-dev.yml | 18 ++++ pig-visual/pig-zipkin/Dockerfile | 15 +++ pig-visual/pig-zipkin/pom.xml | 98 +++++++++++++++++++ .../pig/zipkin/PigZipkinApplication.java | 27 +++++ .../pig-zipkin/src/main/resources/banner.txt | 16 +++ .../src/main/resources/bootstrap.yml | 24 +++++ pig-visual/pom.xml | 1 + pom.xml | 15 ++- 11 files changed, 293 insertions(+), 4 deletions(-) create mode 100644 pig-config/src/main/resources/config/pig-zipkin-dev.yml create mode 100644 pig-visual/pig-zipkin/Dockerfile create mode 100644 pig-visual/pig-zipkin/pom.xml create mode 100644 pig-visual/pig-zipkin/src/main/java/com/pig4cloud/pig/zipkin/PigZipkinApplication.java create mode 100644 pig-visual/pig-zipkin/src/main/resources/banner.txt create mode 100644 pig-visual/pig-zipkin/src/main/resources/bootstrap.yml diff --git a/db/pig.sql b/db/pig.sql index b4ddcbc08..d38724492 100644 --- a/db/pig.sql +++ b/db/pig.sql @@ -283,4 +283,62 @@ BEGIN; INSERT INTO `sys_user_role` VALUES ('1', '1'), ('2', '2'); COMMIT; +-- ---------------------------- +-- Table structure for `zipkin_spans` +-- ---------------------------- +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'; + +-- ---------------------------- +-- Table structure for `zipkin_annotations` +-- ---------------------------- +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'; + +-- ---------------------------- +-- Table structure for `zipkin_dependencies` +-- ---------------------------- +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`); + SET FOREIGN_KEY_CHECKS = 1; diff --git a/docker-compose.yml b/docker-compose.yml index ba7b006d2..9cc304599 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -88,3 +88,13 @@ services: container_name: pig-codegen hostname: pig-codegen image: pig-codegen + + pig-zipkin: + build: + context: ./ + dockerfile: ./pig-visual/pig-zipkin/Dockerfile + restart: always + image: pig-zipkin + container_name: pig-zipkin + ports: + - 5002:5002 diff --git a/pig-config/src/main/resources/config/application-dev.yml b/pig-config/src/main/resources/config/application-dev.yml index d19c41877..2cd015a2c 100755 --- a/pig-config/src/main/resources/config/application-dev.yml +++ b/pig-config/src/main/resources/config/application-dev.yml @@ -8,9 +8,18 @@ spring: redis: password: host: pig-redis - jackson: - time-zone: GMT+8 - date-format: yyyy-MM-dd HH:mm:ss + # zipkin + zipkin: + enabled: true + base-url: http://pig-zipkin + sleuth: + web: + client: + enabled: true + sampler: + # 默认的采样比率为0.1,不能看到所有请求数据 + # 更改采样比率为1,就能看到所有的请求数据了,但是这样会增加接口调用延迟 + probability: 1.0 # 暴露监控端点 management: diff --git a/pig-config/src/main/resources/config/pig-zipkin-dev.yml b/pig-config/src/main/resources/config/pig-zipkin-dev.yml new file mode 100644 index 000000000..7e3f50bd6 --- /dev/null +++ b/pig-config/src/main/resources/config/pig-zipkin-dev.yml @@ -0,0 +1,18 @@ +spring: + # 数据源 + datasource: + type: com.zaxxer.hikari.HikariDataSource + driver-class-name: com.mysql.jdbc.Driver + username: root + password: root + url: jdbc:mysql://pig-mysql:3306/pig?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai +management: + metrics: + web: + server: + auto-time-requests: false +zipkin: + storage: + type: mysql +sleuth: + enabled: true diff --git a/pig-visual/pig-zipkin/Dockerfile b/pig-visual/pig-zipkin/Dockerfile new file mode 100644 index 000000000..479c1429d --- /dev/null +++ b/pig-visual/pig-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 /pig-zipkin + +WORKDIR /pig-zipkin + +EXPOSE 5002 + +ADD ./pig-visual/pig-zipkin/target/pig-zipkin.jar ./ + +CMD java -Djava.security.egd=file:/dev/./urandom -jar pig-zipkin.jar diff --git a/pig-visual/pig-zipkin/pom.xml b/pig-visual/pig-zipkin/pom.xml new file mode 100644 index 000000000..1218c8ac5 --- /dev/null +++ b/pig-visual/pig-zipkin/pom.xml @@ -0,0 +1,98 @@ + + + + pig-visual + com.pig4cloud + 2.0.2 + + 4.0.0 + + pig-zipkin + jar + + pig 服务链路跟踪模块,基于zipkin + + + + org.springframework.boot + spring-boot-starter-web + + + + spring-boot-starter-tomcat + org.springframework.boot + + + + + 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} + + + + org.springframework.boot + spring-boot-starter-log4j2 + + + + + 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 + ${jooq.version} + + + mysql + mysql-connector-java + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + com.spotify + docker-maven-plugin + + + + + + diff --git a/pig-visual/pig-zipkin/src/main/java/com/pig4cloud/pig/zipkin/PigZipkinApplication.java b/pig-visual/pig-zipkin/src/main/java/com/pig4cloud/pig/zipkin/PigZipkinApplication.java new file mode 100644 index 000000000..51caec1ba --- /dev/null +++ b/pig-visual/pig-zipkin/src/main/java/com/pig4cloud/pig/zipkin/PigZipkinApplication.java @@ -0,0 +1,27 @@ +package com.pig4cloud.pig.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 lishangbu + * @date 2019/2/23 + */ +@EnableZipkinServer +@SpringCloudApplication +public class PigZipkinApplication { + public static void main(String[] args) { + SpringApplication.run(PigZipkinApplication.class, args); + } + @Bean + public MySQLStorage mySQLStorage(DataSource datasource) { + return MySQLStorage.builder().datasource(datasource).executor(Runnable::run).build(); + } +} diff --git a/pig-visual/pig-zipkin/src/main/resources/banner.txt b/pig-visual/pig-zipkin/src/main/resources/banner.txt new file mode 100644 index 000000000..806a6a1be --- /dev/null +++ b/pig-visual/pig-zipkin/src/main/resources/banner.txt @@ -0,0 +1,16 @@ +${AnsiColor.BRIGHT_YELLOW} + + ::::::::: ::::::::::: :::::::: + :+: :+: :+: :+: :+: + +:+ +:+ +:+ +:+ + +#++:++#+ +#+ :#: + +#+ +#+ +#+ +#+# + #+# #+# #+# #+# + ### ########### ######## + + www.pig4cloud.com + + Pig Microservice Architecture + + + diff --git a/pig-visual/pig-zipkin/src/main/resources/bootstrap.yml b/pig-visual/pig-zipkin/src/main/resources/bootstrap.yml new file mode 100644 index 000000000..bb997ffc1 --- /dev/null +++ b/pig-visual/pig-zipkin/src/main/resources/bootstrap.yml @@ -0,0 +1,24 @@ +server: + port: 5002 + +spring: + application: + name: pig-zipkin + profiles: + active: dev + #配置中心 + cloud: + config: + fail-fast: true + name: ${spring.application.name} + profile: ${spring.profiles.active} + discovery: + enabled: true + service-id: pig-config +# 注册中心配置 +eureka: + instance: + prefer-ip-address: true + client: + service-url: + defaultZone: http://pig:pig@pig-eureka:8761/eureka/ diff --git a/pig-visual/pom.xml b/pig-visual/pom.xml index 7e32c5211..55d363cee 100755 --- a/pig-visual/pom.xml +++ b/pig-visual/pom.xml @@ -31,5 +31,6 @@ pig-codegen pig-monitor + pig-zipkin diff --git a/pom.xml b/pom.xml index 39af9a1f2..5f3da85a3 100755 --- a/pom.xml +++ b/pom.xml @@ -29,7 +29,7 @@ 2.0.8.RELEASE Finchley.SR2 - Cairo-SR5 + Cairo-SR7 UTF-8 1.8 1.8 @@ -41,6 +41,10 @@ 2.1.0 2.3.3.RELEASE 2.9.6 + + 3.11.9 + 2.12.2 + 2.8.4 http://192.168.0.13:4243 192.168.0.13:5000 @@ -73,6 +77,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