微服务之分布式跟踪系统(springboot+zipkin+mysql)

释放双眼,带上耳机,听听看~!

  通过上一节《微服务之分布式跟踪系统(springboot+zipkin)》我们简单熟悉了zipkin的使用,但是收集的数据都保存在内存中重启后数据丢失,不过zipkin的Storage除了内存,还有Cassandra、MYSQL、ElasticSearch。

二、zipkin的各种Storage配置简介

          zipkin存在一些公用的配置,同时存在一些私有的配置(详细信息地址为:https://github.com/openzipkin/zipkin/tree/master/zipkin-server\#configuration-for-the-ui),此处不做配置说明的翻译(因为都比较简单易懂),其公用的配置如下所示:

[sql] view plain
 copy

*

1
1`

QUERY_PORT

1
1`

: Listen port 
for
 the http api 
and
 web ui; Defaults 
to
 9411  
1.
*

1
1`

QUERY_LOG_LEVEL

1
1`

: Log 
level
 written 
to
 the console; Defaults 
to
 INFO  
1.
*

1
1`

QUERY_LOOKBACK

1
1`

: How many milliseconds queries can look back 
from
 endTs;Defaults 
to
 7 days  
1.
*

1
1`

STORAGE_TYPE

1
1`

: SpanStore implementation: one 
of
 

1
1`

mem

1
1`

1
1`

mysql

1
1`

1
1`

cassandra

1
1`

,

1
1`

elasticsearch

1
1`

  
1.
*

1
1`

COLLECTOR_PORT

1
1`

: Listen port 
for
 the scribe thrift api; Defaults 
to
 9410  
1.
*

1
1`

COLLECTOR_SAMPLE_RATE

1
1`

: Percentage 
of
 traces 
to
 retain, defaults 
to
 alwayssample (1.0).  

(1)Cassandra Storage配置

[sql] view plain
 copy

1
1`

CASSANDRA_KEYSPACE

1
1`

: The keyspace 
to
 use. Defaults 
to
 
"zipkin"
.  
1.

1
1`

CASSANDRA_CONTACT_POINTS

1
1`

: Comma separated list 
of
 hosts / ip addresses part 
of
 Cassandra cluster. Defaults 
to
 localhost  
1.

1
1`

CASSANDRA_LOCAL_DC

1
1`


Name
 
of
 the datacenter that will be considered 
"local"
 
for
 latency 
load
 balancing. 
When
 unset, 
load
-balancing 
is
 round-robin.  
1.

1
1`

CASSANDRA_ENSURE_SCHEMA

1
1`

: Ensuring cassandra has the latest 
schema
. If enabled tries 
to
 
execute
 scripts 
in
 the classpath prefixed 
with
 

1
1`

cassandra-
schema
-cql3

1
1`

. Defaults 
to
 
true
  
1.

1
1`

CASSANDRA_USERNAME

1
1`

 
and
 

1
1`

CASSANDRA_PASSWORD

1
1`

: Cassandra authentication. Will throw an exception 
on
 startup if authentication fails. 
No
 
default
  
1.

1
1`

CASSANDRA_USE_SSL

1
1`

: Requires 

1
1`

javax.net.ssl.trustStore

1
1`

 
and
 

1
1`

javax.net.ssl.trustStorePassword

1
1`

, defaults 
to
 
false
.  

(2)MySQL Storage配置

[sql] view plain
 copy

1
1`

MYSQL_DB

1
1`

: The 
database
 
to
 use. Defaults 
to
 
"zipkin"
.  
1.

1
1`

MYSQL_USER

1
1`

 
and
 

1
1`

MYSQL_PASS

1
1`

: MySQL authentication, which defaults 
to
 empty string.  
1.

1
1`

MYSQL_HOST

1
1`

: Defaults 
to
 localhost  
1.

1
1`

MYSQL_TCP_PORT

1
1`

: Defaults 
to
 3306  
1.

1
1`

MYSQL_MAX_CONNECTIONS

1
1`

: Maximum concurrent connections, defaults 
to
 10  
1.

1
1`

MYSQL_USE_SSL

1
1`

: Requires 

1
1`

javax.net.ssl.trustStore

1
1`

 
and
 

1
1`

javax.net.ssl.trustStorePassword

1
1`

, defaults 
to
 
false
.  

(3)Elasticsearch Storage配置

[sql] view plain
 copy

1
1`

ES_CLUSTER

1
1`

: The 
name
 
of
 the elasticsearch cluster 
to
 
connect
 
to
. Defaults 
to
 
"elasticsearch"
.  
1.

1
1`

ES_HOSTS

1
1`

: A comma separated list 
of
 elasticsearch hostnodes 
to
 
connect
 
to

When
 
in
 host:port  
1.
              format, they should use the transport port, 
not
 the http port. 
To
 use http, specify  
1.
              base urls, ex. http://host:9200. Defaults 
to
 
"localhost:9300"

When
 
not
 using http,  
1.
              
Only
 one 
of
 the hosts needs 
to
 be available 
to
 
fetch
 the remaining nodes 
in
 the  
1.
              cluster. It 
is
 recommended 
to
 
set
 this 
to
 
all
 the master nodes 
of
 the cluster.  
1.
   
1.
              If the http URL 
is
 an AWS-hosted elasticsearch installation (e.g.  
1.
              https://search-domain-xyzzy.us-west-2.es.amazonaws.com) 
then
 Zipkin will attempt 
to
  
1.
              use the 
default
 AWS credential provider (env variables, system properties, config  
1.
              files, 
or
 ec2 profiles) 
to
 sign outbound requests 
to
 the cluster.  
1.

1
1`

ES_PIPELINE

1
1`


Only
 valid 
when
 the destination 
is
 Elasticsearch 5.x. Indicates the ingest  
1.
                 pipeline used before spans are indexed. 
No
 
default
.  
1.

1
1`

ES_MAX_REQUESTS

1
1`


Only
 valid 
when
 the transport 
is
 http. Sets maximum 
in
-flight requests 
from
  
1.
                     this process 
to
 
any
 Elasticsearch host. Defaults 
to
 64.  
1.

1
1`

ES_AWS_DOMAIN

1
1`

: The 
name
 
of
 the AWS-hosted elasticsearch domain 
to
 use. Supercedes 
any
 
set
  
1.
                   

1
1`

ES_HOSTS

1
1`

. Triggers the same request signing behavior 
as
 
with
 

1
1`

ES_HOSTS

1
1`

, but  
1.
                   requires the additional IAM permission 
to
 describe the given domain.  
1.

1
1`

ES_AWS_REGION

1
1`

: An optional override 
to
 the 
default
 region lookup 
to
 search 
for
 the domain  
1.
                   given 
in
 

1
1`

ES_AWS_DOMAIN

1
1`

. Ignored if 
only
 

1
1`

ES_HOSTS

1
1`

 
is
 present.  
1.

1
1`

ES_INDEX

1
1`

: The 
index
 prefix 
to
 use 
when
 generating daily 
index
 names. Defaults 
to
 zipkin.  
1.

1
1`

ES_DATE_SEPARATOR

1
1`

: The 
date
 separator 
to
 use 
when
 generating daily 
index
 names. Defaults 
to
 
'-'
.  
1.

1
1`

ES_INDEX_SHARDS

1
1`

: The number 
of
 shards 
to
 split the 
index
 
into
. Each shard 
and
 its replicas  
1.
                     are assigned 
to
 a machine 
in
 the cluster. Increasing the number 
of
 shards  
1.
                     
and
 machines 
in
 the cluster will improve 
read
 
and
 write performance. Number  
1.
                     
of
 shards cannot be changed 
for
 existing indices, but new daily indices  
1.
                     will pick up changes 
to
 the setting. Defaults 
to
 5.  

三、zipkin环境准备与启动

       在本节中,以MySQL为例进行说明,由于目前只是Mysql5.6和5.7进行测试过,所以本次我选择Mysql5.7版本。

(1)    初始化数据库

       安装好Mysql5.7后新建zipkin的数据库,然后执行下面的SQL语句新建表:

[sql] view plain
 copy

CREATETABLE IF 
NOT
 EXISTS zipkin_spans (  
1.
  

1
1`

trace_id_high

1
1`

 
BIGINT
 
NOT
 
NULL
 
DEFAULT
 0COMMENT 
'If non zero, this means the trace uses 128 bit traceIds instead of 64bit'
,  
1.
  

1
1`

trace_id

1
1`

 
BIGINT
 
NOT
 
NULL
,  
1.
  

1
1`

id

1
1`

 
BIGINT
 
NOT
 
NULL
,  
1.
  

1
1`

name

1
1`

 
VARCHAR
(255) 
NOT
 
NULL
,  
1.
  

1
1`

parent_id

1
1`

 
BIGINT
,  
1.
  

1
1`

debug

1
1`

 
BIT
(1),  
1.
  

1
1`

start_ts

1
1`

 
BIGINT
 COMMENT 
'Span.timestamp():epoch micros used for endTs query and to implement TTL'
,  
1.
  

1
1`

duration

1
1`

 
BIGINT
 COMMENT 
'Span.duration():micros used for minDuration and maxDuration query'
  
1.
)ENGINE=InnoDB ROW_FORMAT=COMPRESSED 
CHARACTER
 
SET
=utf8 
COLLATE
 utf8_general_ci;  
1.
   
1.
ALTERTABLE zipkin_spans 
ADD
 
UNIQUE
 
KEY
(

1
1`

trace_id_high

1
1`

1
1`

trace_id

1
1`

1
1`

id

1
1`

) COMMENT
'ignore insert on duplicate'
;  
1.
ALTERTABLE zipkin_spans 
ADD
 
INDEX
(

1
1`

trace_id_high

1
1`

1
1`

trace_id

1
1`

1
1`

id

1
1`

) COMMENT 
'forjoining with zipkin_annotations'
;  
1.
ALTERTABLE zipkin_spans 
ADD
 
INDEX
(

1
1`

trace_id_high

1
1`

1
1`

trace_id

1
1`

) COMMENT 
'forgetTracesByIds'
;  
1.
ALTERTABLE zipkin_spans 
ADD
 
INDEX
(

1
1`

name

1
1`

) COMMENT 
'for getTraces and getSpanNames'
;  
1.
ALTERTABLE zipkin_spans 
ADD
 
INDEX
(

1
1`

start_ts

1
1`

) COMMENT 
'for getTraces ordering andrange'
;  
1.
   
1.
CREATETABLE IF 
NOT
 EXISTS zipkin_annotations (  
1.
  

1
1`

trace_id_high

1
1`

 
BIGINT
 
NOT
 
NULL
 
DEFAULT
 0COMMENT 
'If non zero, this means the trace uses 128 bit traceIds instead of 64bit'
,  
1.
  

1
1`

trace_id

1
1`

 
BIGINT
 
NOT
 
NULL
 COMMENT 
'coincideswith zipkin_spans.trace_id'
,  
1.
  

1
1`

span_id

1
1`

 
BIGINT
 
NOT
 
NULL
 COMMENT 
'coincideswith zipkin_spans.id'
,  
1.
  

1
1`

a_key

1
1`

 
VARCHAR
(255) 
NOT
 
NULL
 COMMENT
'BinaryAnnotation.key or Annotation.value if type == -1'
,  
1.
  

1
1`

a_value

1
1`

 BLOB COMMENT
'BinaryAnnotation.value(), which must be smaller than 64KB'
,  
1.
  

1
1`

a_type

1
1`

 
INT
 
NOT
 
NULL
 COMMENT
'BinaryAnnotation.type() or -1 if Annotation'
,  
1.
  

1
1`

a_timestamp

1
1`

 
BIGINT
 COMMENT 
'Used toimplement TTL; Annotation.timestamp or zipkin_spans.timestamp'
,  
1.
  

1
1`

endpoint_ipv4

1
1`

 
INT
 COMMENT 
'Null whenBinary/Annotation.endpoint is null'
,  
1.
  

1
1`

endpoint_ipv6

1
1`

 
BINARY
(16) COMMENT 
'Null whenBinary/Annotation.endpoint is null, or no IPv6 address'
,  
1.
  

1
1`

endpoint_port

1
1`

 
SMALLINT
 COMMENT 
'Null whenBinary/Annotation.endpoint is null'
,  
1.
  

1
1`

endpoint_service_name

1
1`

 
VARCHAR
(255) COMMENT
'Null when Binary/Annotation.endpoint is null'
  
1.
)ENGINE=InnoDB ROW_FORMAT=COMPRESSED 
CHARACTER
 
SET
=utf8 
COLLATE
 utf8_general_ci;  
1.
   
1.
ALTERTABLE zipkin_annotations 
ADD
 
UNIQUE
 
KEY
(

1
1`

trace_id_high

1
1`

1
1`

trace_id

1
1`

1
1`

span_id

1
1`

,

1
1`

a_key

1
1`

1
1`

a_timestamp

1
1`

) COMMENT 
'Ignore insert on duplicate'
;  
1.
ALTERTABLE zipkin_annotations 
ADD
 
INDEX
(

1
1`

trace_id_high

1
1`

1
1`

trace_id

1
1`

1
1`

span_id

1
1`

)COMMENT 
'for joining with zipkin_spans'
;  
1.
ALTERTABLE zipkin_annotations 
ADD
 
INDEX
(

1
1`

trace_id_high

1
1`

1
1`

trace_id

1
1`

) COMMENT 
'forgetTraces/ByIds'
;  
1.
ALTERTABLE zipkin_annotations 
ADD
 
INDEX
(

1
1`

endpoint_service_name

1
1`

) COMMENT 
'forgetTraces and getServiceNames'
;  
1.
ALTERTABLE zipkin_annotations 
ADD
 
INDEX
(

1
1`

a_type

1
1`

) COMMENT 
'for getTraces'
;  
1.
ALTERTABLE zipkin_annotations 
ADD
 
INDEX
(

1
1`

a_key

1
1`

) COMMENT 
'for getTraces'
;  
1.
ALTERTABLE zipkin_annotations 
ADD
 
INDEX
(

1
1`

trace_id

1
1`

1
1`

span_id

1
1`

1
1`

a_key

1
1`

) COMMENT 
'fordependencies job'
;  
1.
   
1.
CREATETABLE IF 
NOT
 EXISTS zipkin_dependencies (  
1.
  

1
1`

day

1
1`

 
DATE
 
NOT
 
NULL
,  
1.
  

1
1`

parent

1
1`

 
VARCHAR
(255) 
NOT
 
NULL
,  
1.
  

1
1`

child

1
1`

 
VARCHAR
(255) 
NOT
 
NULL
,  
1.
  

1
1`

call_count

1
1`

 
BIGINT
  
1.
)ENGINE=InnoDB ROW_FORMAT=COMPRESSED 
CHARACTER
 
SET
=utf8 
COLLATE
 utf8_general_ci;  
1.
   
1.
ALTERTABLE zipkin_dependencies 
ADD
 
UNIQUE
 
KEY
(

1
1`

day

1
1`

1
1`

parent

1
1`

1
1`

child

1
1`

);  

(2)    启动实例

       执行命令:
java -jar zipkin-server-1.17.1-exec.jar –STORAGE_TYPE=mysql–MYSQL_DB=zipkin –MYSQL_USER=root –MYSQL_PASS=root –MYSQL_HOST=localhost–MYSQL_TCP_PORT=3306
,启动成功如下图所示:

(3)    查看运行效果

       通过上图,我们发现zipkin使用springboot,并且启动的端口为9411,然后我们通过浏览器访问,效果如下:

四、分布式跟踪系统实践(springboot+zipkin+mysql)

 4.1场景设置与分析

      现在有一个服务A调用服务B,服务B又分别调用服务C和D,整个链路过程的关系图如下所示:

4.2 代码编写

      具体代码和上一节代码相同,源代码下载地址:https://github.com/dreamerkr/mircoservice.git文件夹springboot+zipkin下面。

4.3运行效果

 (1)分别启动每个服务,然后访问服务1,浏览器访问(http://localhost:8081/service1/test)

(2)输入zipkin地址,每次trace的列表

点击其中的trace,可以看trace的树形结构,包括每个服务所消耗的时间:

点击每个span可以获取延迟信息:

同时可以查看服务之间的依赖关系:

同时查看zipkin数据库表已经存在数据:

给TA打赏
共{{data.count}}人
人已打赏
安全技术

C++异常

2022-1-11 12:36:11

安全运维

Elasticsearch模块功能之-映射(Mapper)

2021-12-11 11:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索