Przeglądaj źródła

:sparkles: Introducing new features.add zipkin

萌萌哒Sakura酱 6 lat temu
rodzic
commit
5b83aa1162

+ 78 - 0
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;

+ 3 - 1
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
+COPY ./db/3pigxx_ac.sql /docker-entrypoint-initdb.d
+
+COPY ./db/4pigxx_zipkin.sql /docker-entrypoint-initdb.d

+ 10 - 0
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: ./

+ 13 - 1
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

+ 18 - 0
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

+ 15 - 0
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

+ 81 - 0
pigx-visual/pigx-zipkin/pom.xml

@@ -0,0 +1,81 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+		 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+		 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+	<parent>
+		<artifactId>pigx-visual</artifactId>
+		<groupId>com.pig4cloud</groupId>
+		<version>2.5.1</version>
+	</parent>
+	<modelVersion>4.0.0</modelVersion>
+
+	<artifactId>pigx-zipkin</artifactId>
+	<description>pigx zipkin追踪服务</description>
+
+	<dependencies>
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter-web</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter-undertow</artifactId>
+		</dependency>
+		<!--配置中心客户端-->
+		<dependency>
+			<groupId>org.springframework.cloud</groupId>
+			<artifactId>spring-cloud-starter-config</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter-jdbc</artifactId>
+		</dependency>
+		<!--zipkin-->
+		<dependency>
+			<groupId>io.zipkin.java</groupId>
+			<artifactId>zipkin-server</artifactId>
+			<version>${zipkin.version}</version>
+		</dependency>
+		<dependency>
+			<groupId>io.zipkin.java</groupId>
+			<artifactId>zipkin-autoconfigure-ui</artifactId>
+			<version>${zipkin.version}</version>
+		</dependency>
+		<!-- 使用mysql存储-->
+		<dependency>
+			<groupId>io.zipkin.java</groupId>
+			<artifactId>zipkin-autoconfigure-storage-mysql</artifactId>
+			<version>${zipkin.version}</version>
+		</dependency>
+		<dependency>
+			<groupId>io.zipkin.java</groupId>
+			<artifactId>zipkin-storage-mysql</artifactId>
+			<version>${zipkin-storage-mysql.version}</version>
+		</dependency>
+		<!-- zipkin2.x 需要此包连接操作mysql -->
+		<dependency>
+			<groupId>org.jooq</groupId>
+			<artifactId>jooq</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>mysql</groupId>
+			<artifactId>mysql-connector-java</artifactId>
+		</dependency>
+	</dependencies>
+
+	<build>
+		<plugins>
+			<plugin>
+				<groupId>org.springframework.boot</groupId>
+				<artifactId>spring-boot-maven-plugin</artifactId>
+			</plugin>
+			<plugin>
+				<groupId>com.spotify</groupId>
+				<artifactId>docker-maven-plugin</artifactId>
+				<version>${docker.plugin.version}</version>
+			</plugin>
+		</plugins>
+	</build>
+
+
+</project>

+ 29 - 0
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();
+	}
+
+}

+ 27 - 0
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/

+ 2 - 1
pigx-visual/pom.xml

@@ -36,7 +36,8 @@
 		<module>pigx-codegen</module>
 		<module>pigx-daemon</module>
 		<module>pigx-monitor</module>
-		<module>pigx-tx-manager</module>
 		<module>pigx-sso-client-demo</module>
+		<module>pigx-tx-manager</module>
+		<module>pigx-zipkin</module>
 	</modules>
 </project>

+ 11 - 0
pom.xml

@@ -62,6 +62,8 @@
 		<security.oauth.version>2.3.4.RELEASE</security.oauth.version>
 		<security.oauth.auto.version>2.1.2.RELEASE</security.oauth.auto.version>
 		<activiti.version>5.22.0</activiti.version>
+		<zipkin.version>2.12.2</zipkin.version>
+		<zipkin-storage-mysql.version>2.8.4</zipkin-storage-mysql.version>
 		<docker.url>http://192.168.0.13:4243</docker.url>
 		<docker.plugin.version>0.4.12</docker.plugin.version>
 		<registry.url>192.168.0.13:5000</registry.url>
@@ -96,6 +98,15 @@
 			<artifactId>spring-boot-admin-starter-client</artifactId>
 			<version>${spring-boot-admin.version}</version>
 		</dependency>
+		<!--服务追踪-->
+		<dependency>
+			<groupId>org.springframework.cloud</groupId>
+			<artifactId>spring-cloud-starter-sleuth</artifactId>
+		</dependency>
+		<dependency>
+			<groupId>org.springframework.cloud</groupId>
+			<artifactId>spring-cloud-sleuth-zipkin</artifactId>
+		</dependency>
 		<!--断路器依赖-->
 		<dependency>
 			<groupId>org.springframework.cloud</groupId>