一、zipkin服務端配置
需要JAVA Spring Cloud大型企業分佈式微服務雲構建的B2B2C電子商務平臺源碼
1.引入依賴
//===========消息隊列方式的依賴=============
//此依賴會自動引入spring-cloud-sleuth-stream並且引入zipkin的依賴包
compile("org.springframework.cloud:spring-cloud-sleuth-zipkin-stream")
compile("org.springframework.cloud:spring-cloud-starter-stream-rabbit")
compile('io.zipkin.java:zipkin-autoconfigure-ui')
//保存到數據庫需要如下依賴
compile('mysql:mysql-connector-java')
compile('org.springframework.boot:spring-boot-starter-jdbc')
2.啓動類配置
在啓動類加上@EnableZipkinStreamServer
@EnableZipkinStreamServer
@SpringBootApplication
public class ZipkinApplication {
public static void main(String[] args) {
SpringApplication.run(ZipkinApplication.class, args);
}
}
3.配置文件
完整配置如下,根據自己的設置進行修改即可
#================================基礎配置==============================
#應用名
spring.application.name=zipking-server-v1
#端口
server.port=9411
#================================消息隊列==============================
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
#================================數據源配置==============================
#zipkin數據保存到數據庫中需要進行如下配置
#表示當前程序不使用sleuth
spring.sleuth.enabled=false
#表示zipkin數據存儲方式是mysql
zipkin.storage.type=mysql
#數據庫腳本創建地址,當有多個是可使用[x]表示集合第幾個元素
spring.datasource.schema[0]=classpath:/zipkin.sql
#spring boot數據源配置
spring.datasource.url=jdbc:mysql://localhost:3306/zipkin
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.initialize=true
spring.datasource.continue-on-error=true
4.數據庫文件
在本地數據庫建立一個數據庫,起名爲zipkin。
在resources目錄下新建zipkin.sql文件,內容如下
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for zipkin_annotations
-- ----------------------------
CREATE TABLE `zipkin_annotations` (
`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) 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(11) NOT NULL COMMENT 'BinaryAnnotation.type() or -1 if Annotation',
`a_timestamp` bigint(20) DEFAULT NULL COMMENT 'Used to implement TTL; Annotation.timestamp or zipkin_spans.timestamp',
`endpoint_ipv4` int(11) DEFAULT NULL COMMENT 'Null when Binary/Annotation.endpoint is null',
`endpoint_ipv6` binary(16) DEFAULT NULL COMMENT 'Null when Binary/Annotation.endpoint is null, or no IPv6 address',
`endpoint_port` smallint(6) DEFAULT NULL COMMENT 'Null when Binary/Annotation.endpoint is null',
`endpoint_service_name` varchar(255) DEFAULT NULL COMMENT 'Null when Binary/Annotation.endpoint is null',
UNIQUE KEY `trace_id` (`trace_id`,`span_id`,`a_key`,`a_timestamp`) COMMENT 'Ignore insert on duplicate',
KEY `trace_id_2` (`trace_id`,`span_id`) COMMENT 'for joining with zipkin_spans',
KEY `trace_id_3` (`trace_id`) COMMENT 'for getTraces/ByIds',
KEY `endpoint_service_name` (`endpoint_service_name`) COMMENT 'for getTraces and getServiceNames',
KEY `a_type` (`a_type`) COMMENT 'for getTraces',
KEY `a_key` (`a_key`) COMMENT 'for getTraces'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPRESSED;
-- ----------------------------
-- Table structure for zipkin_dependencies
-- ----------------------------
CREATE TABLE `zipkin_dependencies` (
`day` date NOT NULL,
`parent` varchar(255) NOT NULL,
`child` varchar(255) NOT NULL,
`call_count` bigint(20) DEFAULT NULL,
UNIQUE KEY `day` (`day`,`parent`,`child`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPRESSED;
-- ----------------------------
-- Table structure for zipkin_spans
-- ----------------------------
CREATE TABLE `zipkin_spans` (
`trace_id` bigint(20) NOT NULL,
`id` bigint(20) NOT NULL,
`name` varchar(255) NOT NULL,
`parent_id` bigint(20) DEFAULT NULL,
`debug` bit(1) DEFAULT NULL,
`start_ts` bigint(20) DEFAULT NULL COMMENT 'Span.timestamp(): epoch micros used for endTs query and to implement TTL',
`duration` bigint(20) DEFAULT NULL COMMENT 'Span.duration(): micros used for minDuration and maxDuration query',
UNIQUE KEY `trace_id` (`trace_id`,`id`) COMMENT 'ignore insert on duplicate',
KEY `trace_id_2` (`trace_id`,`id`) COMMENT 'for joining with zipkin_annotations',
KEY `trace_id_3` (`trace_id`) COMMENT 'for getTracesByIds',
KEY `name` (`name`) COMMENT 'for getTraces and getSpanNames',
KEY `start_ts` (`start_ts`) COMMENT 'for getTraces ordering and range'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPRESSED;
zipkin的服務端就配置完成了,別忘記重新刷新下項目。
二、zipkin客戶端配置
引入依賴
compile("org.springframework.cloud:spring-cloud-sleuth-zipkin-stream")
compile("org.springframework.cloud:spring-cloud-starter-stream-rabbit")
然後客戶端就配置完成了
三、啓動
這時候分別啓動客戶端和服務端,在日誌中可以看到初始化mq連接。
然後訪問一個客戶端的rest接口,看看打開服務端locahost:9411,看看有沒有生成記錄,這時看看數據庫,會建立三個表。就說明我們配置成功了。java B2B2C Springcloud電子商城系統