1.部署skywalking
https://blog.csdn.net/qq_40942490/article/details/144701194
2.添加依赖
<!-- 日志采集 -->
<dependency>
<groupId>org.apache.skywalking</groupId>
<artifactId>apm-toolkit-logback-1.x</artifactId>
<version>9.3.0</version>
</dependency>
<!-- 自定义链路追踪 《如果不需要就会用默认的》 -->
<dependency>
<groupId>org.apache.skywalking</groupId>
<artifactId>apm-toolkit-trace</artifactId>
<version>9.3.0</version>
</dependency>
3.增加日志配置文件
增加logback-spring.xml文件:
文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="false" scanPeriod="10 seconds">
<contextName>logback</contextName>
<property name="log.path" value="./log/bztc-study01"/>
<!--1. 输出到控制台-->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%tid] [%thread] %-5level %logger{36} -%msg%n</Pattern>
</layout>
</encoder>
</appender>
<root level="info">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
4.打印日志
其中,TID需要打印出来
5. windos环境–在IDEA中配置SkyWalking
下载地址 https://skywalking.apache.org/downloads/
在运行的程序配置jvm参数,如下图所示:
-javaagent:C:\Users\admin\Downloads\skywalking-agent\skywalking-agent.jar
-Dskywalking.agent.service_name=portrait-model
-Dskywalking.collector.backend_service=172.16.2.43:11800
6.skywalking网页查询链路
在搜索框中查询TID:
7.自定义链路追踪
7.1代码中使用traceId
@RequestMapping("/list")
public List<User> list(){
//TraceContext可以绑定key-value
TraceContext.putCorrelation("name", "fox");
Optional<String> op = TraceContext.getCorrelation("name");
log.info("name = {} ", op.get());
//获取跟踪的traceId
String traceId = TraceContext.traceId();
log.info("traceId = {} ", traceId);
return userService.list();
}
7.2@Trace 将方法加入追踪链路
调用链路中显示返回值,以及参数值信息,使用@Tag或者@Tags
@Tag注解中key=方法名 value=returnedObj 返回值 agr[0] 参数
param标识参数,returnedObj标识返回值
@RequestMapping("/add")
@Trace
@Tag(key = "add",value ="returnedObj")
public String add() {
System.out.println("下单成功");
// String forObject = restTemplate.getForObject("http://localhost:8083/stock/reduct", String.class);
String forObject = restTemplate.getForObject("http://stock-service/stock/reduct", String.class);
//int a= 1/0;
return "add order " + forObject;
}
@RequestMapping("/getById/{id}")
@Trace
@Tags({@Tag(key = "getById",value ="returnedObj"),
@Tag(key="param",value = "arg[0]")
})
public String getById(@PathVariable("id") Integer id) {
return "根据id获取信息";
}
8.日志收集
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="false" scanPeriod="10 seconds">
<contextName>logback</contextName>
<property name="log.path" value="./log/bztc-study01"/>
<!--1. 输出到控制台-->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%tid] [%thread] %-5level %logger{36} -%msg%n</Pattern>
</layout>
</encoder>
</appender>
<!--2. 日志收集-->
<appender name="grpc-log" class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender">
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.mdc.TraceIdMDCPatternLogbackLayout">
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%X{tid}] [%thread] %-5level %logger{36} -%msg%n</Pattern>
</layout>
</encoder>
</appender>
<root level="info">
<appender-ref ref="STDOUT"/>
<appender-ref ref="grpc-log"/>
</root>
</configuration>