调用链sleauth+zipkin/cat/pinpoint
参考链接:
https://blog.csdn.net/jrn1012/article/details/77837710
https://blog.csdn.net/forezp/article/details/70162074
调用链的对比
https://blog.csdn.net/u012394095/article/details/79700200
Sleuth会在每个请求的header上添加跟踪需求的重要信息,例如:
X-B3-TraceId:对应TraceID;
X-B3-SpanId:对应SpanID;
X-B3-ParentSpanId:前面一环的SpanID;
X-B3-Sampled:是否被选中抽样输出;
X-Span-Name:工作单元名称。
日志的pattern可以使用这些信息
Zipkin
(1)@EnableZipkinServer 已经废弃,不能通过自定义项目的方式来实现。可以在官网上下载独立的jar包,来单独启动。
(2)Zipkin指定数据库
新建数据zipkin
zipkin数据中导入一下脚本 SQL
新建shell脚本zipkin.sh
#!/bin/bash
java -jar zipkin-server-2.11.3-exec.jar --STORAGE_TYPE=mysql --MYSQL_DB=zipkin --MYSQL_USER=root --MYSQL_PASS=654321 --MYSQL_HOST=localhost --MYSQL_TCP_PORT=3306
增加执行权限
chmod +x zipkin.sh
运行
./zipkin.sh
(3)zipkin集群
Zipkin + Kafka + Cassandra 集群搭建
架构图:
cat
cat 项目地址
参考地址:
https://github.com/dianping/cat
https://my.oschina.net/wwh/blog/1483697
官网有详细的使用说明。基本步骤:
(1)服务器端
通过mvn方式安装:
mvn clean install -DskipTests
mvn cat:install
会报错误:
[ERROR] No plugin found for prefix 'cat' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (/var/root/.m2/repository), central (http://repo1.maven.org/maven2), cat.repo (https://raw.github.com/dianping/cat/mvn-repo)] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoPluginFoundForPrefixException
搜索了多种方式,依然出错,所以采用手动安装的方式:
下载官网包放到tomcat的webapps下
建/data/目录
mkdir /data
chmod 777 /data/ -R
新建数据库配置文件 /data/appdatas/cat/datasource.xml
<?xml version="1.0" encoding="utf-8"?>
<data-sources>
<data-source id="cat">
<maximum-pool-size>3</maximum-pool-size>
<connection-timeout>1s</connection-timeout>
<idle-timeout>10m</idle-timeout>
<statement-cache-size>1000</statement-cache-size>
<properties>
<driver>com.mysql.jdbc.Driver</driver>
<url><![CDATA[jdbc:mysql://127.0.0.1:3306/cat]]></url>
<user>root</user>
<password></password>
<connectionProperties><![CDATA[useUnicode=true&autoReconnect=true]]></connectionProperties>
</properties>
</data-source>
<data-source id="app">
<maximum-pool-size>3</maximum-pool-size>
<connection-timeout>1s</connection-timeout>
<idle-timeout>10m</idle-timeout>
<statement-cache-size>1000</statement-cache-size>
<properties>
<driver>com.mysql.jdbc.Driver</driver>
<url><![CDATA[jdbc:mysql://127.0.0.1:3306/cat]]></url>
<user>root</user>
<password></password>
<connectionProperties><![CDATA[useUnicode=true&autoReconnect=true]]></connectionProperties>
</properties>
</data-source>
</data-sources>
/data/appdatas/cat/server.xml
<config local-mode="false" hdfs-machine="false" job-machine="true" alert-machine="false">
<storage local-base-dir="/data/appdatas/cat/bucket/" max-hdfs-storage-time="15" local-report-storage-time="7" local-logivew-storage-time="7">
</storage>
<console default-domain="Cat" show-cat-domain="true">
<remote-servers>127.0.0.1:8080</remote-servers> <!--服务cat服务端地址-->
</console>
</config>
/data/appdatas/cat/client.xml
<?xml version="1.0" encoding="utf-8"?>
<config mode="client" xmlns:xsi="http://www.w3.org/2001/XMLSchema" xsi:noNamespaceSchemaLocation="config.xsd">
<servers>
<server ip="127.0.0.1" port="2280" http-port="8080" />
</servers>
</config>
启动tomcat,进入 localhost:8080
cat home
启动成功~
(2)客户端配置
windows在工程目录的盘下建文件夹:/data/appdatas/cat
添加client.xml
<?xml version="1.0" encoding="utf-8"?>
<config mode="client" xmlns:xsi="http://www.w3.org/2001/XMLSchema" xsi:noNamespaceSchemaLocation="config.xsd">
<servers>
<server ip="127.0.0.1" port="2280" http-port="8080" />
</servers>
</config>
在pom.xml中增加如下依赖:
<!--dianping-->
<dependency>
<groupId>com.dianping.cat</groupId>
<artifactId>cat-core</artifactId>
<version>1.3.7</version>
</dependency>
<dependency>
<groupId>com.dianping.cat</groupId>
<artifactId>cat-client</artifactId>
<version>1.3.8</version>
</dependency>
<dependency>
<groupId>org.unidal.framework</groupId>
<artifactId>foundation-service</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-container-default</artifactId>
<version>1.6</version>
<exclusions>
<exclusion>
<groupId>com.google.collections</groupId>
<artifactId>google-collections</artifactId>
</exclusion>
</exclusions>
</dependency>
添加测试代码:
public static void main(String[] args){
while (true) {
System.out.println("=== one transaction ===");
Transaction transaction = Cat.newTransaction("MyType","UserServiceA-test");
try{
Cat.logEvent("URL.Server","192.168.4.5", Event.SUCCESS,"ip=192.168.4.5");
Cat.logMetricForCount("main.count");
Cat.logMetricForSum("main.sum",10);
transaction.setStatus(Transaction.SUCCESS);
}catch (Exception e){
transaction.setStatus(e);
}
transaction.complete();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
启动项目,查看CAT记录
CAT日志
(3)Springboot 集成