开始语
一位普通的程序员,慢慢在努力变强!
1.安装
1.1 下载文件压缩包
wget https://dlcdn.apache.org/skywalking/9.3.0/apache-skywalking-apm-9.3.0.tar.gz
wget https://dlcdn.apache.org/skywalking/java-agent/8.14.0/apache-skywalking-java-agent-8.14.0.tgz
1.2 解压压缩包
tar -xzvf apache-skywalking-apm-9.3.0.tar.gz
tar -zxvf apache-skywalking-java-agent-8.14.0.tgz
1.3 修改web-ui启动端口
# 找到webapp目录下的yml文件
vim apache-skywalking-apm-bin/webapp/application.yml
serverPort: ${SW_SERVER_PORT:-8686} # 修改这行,默认是8080
1.4 修改存储:我这里使用的是mysql
# 找到config目录下对应的yml文件
vim apache-skywalking-apm-bin/config/application.yml
storage:
selector: ${SW_STORAGE:mysql} # 默认是h2,将数据进行持久化。重启数据不丢失。 关于存储类选型,设置方式都差不多
mysql:
properties:
jdbcUrl: ${SW_JDBC_URL:"jdbc:mysql://localhost:3306/skywalking_data?rewriteBatchedStatements=true"}
dataSource.user: ${SW_DATA_SOURCE_USER:root}
dataSource.password: ${SW_DATA_SOURCE_PASSWORD:root@1234} # 配置数据库
温馨提示:由于缺少mysql驱动,自行下载将驱动jar放置此目录下:/apache-skywalking-apm-bin/oap-libs
再去mysql中新增库:skywalking_data # 不然启动会报错!
1.4 执行文件:startup.sh
./startup.sh
# 出现下面提示说明执行成功:
SkyWalking OAP started successfully!
SkyWalking Web Application started successfully!
1.5 访问展示
浏览器输入:ip:8686
上面是接入了两个服务!
# 接入方法: java-ops
-javaagent:/skywalking/skywalking-agent/skywalking-agent.jar # jar包位置
-DSW_AGENT_NAME=dev::demo2 # 前面的代表分组,后面的是项目名称
-DSW_AGENT_COLLECTOR_BACKEND_SERVICES=127.0.0.1:11800 # 数据接入点
# 每个项目需要拷贝一份skywalking-agent.jar
温馨提示:如果您是jenkins-k8s集成,请查看此篇文章:jenkins自动化部署👈
2.SpringBoot集成
温馨提示:此处的接入重点是针对jenkins-k8s的方式进行部署
2.1 本地项目部署
2.2 jenkins项目部署
2.2.1 将skywalking-agent.jar打包成镜像
vim Dockerfile
# (方式一)注意Dockerfile文件放置skywalking-agent根目录
FROM busybox:latest
LABEL maintainer="tianyu"
COPY ./ /usr/skywalking/agent/
# (方式二[推荐])如果你不想将Dockerfile放置容器中,那么你的Dockerfile需要放置skywalking-agent父级目录下
FROM busybox:latest
LABEL maintainer="tianyu"
COPY skywalking-agent/ /usr/skywalking/agent/
# 开始制作镜像,版本最好是和你的jar一致
docker build -t skywalking-agent:8.14.0 .
# (此处是进入了容器内部)因为在容器中接入agent和Dockerfile没有关系,所以不需要将它也复制到容器内部,如下:
root@demo3-75c8d69dfb-ff85z:/usr/skywalking/agent# ls
Dockerfile(这个复制到容器内部中了) LICENSE NOTICE activations bootstrap-plugins config licenses logs optional-plugins optional-reporter-plugins plugins skywalking-agent.jar
2.2.2 开始编写Deployment.yaml文件
温馨提示:如果您使用的是jenkins-k8s的方式,则可以忽略次此步骤!
请查看此篇文章:jenkins自动化部署👈
# 编写yml文件
cat > demo-deployment.yaml <<EOF
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: test-apps
name: demo3
labels:
app.kubernetes.io/name: demo3
spec:
replicas: 1
selector:
matchLabels:
name: demo3
template:
metadata:
labels:
name: demo3
spec:
# 构建初始化镜像 (通过初始化镜像的方式集成SkyWalking Agent)
initContainers:
- name: skywalking-agent
imagePullPolicy: IfNotPresent
image: skywalking-agent:8.14.0 # 2.2.1 构建的镜像
command: [ "sh" ]
args:
[
"-c",
"cp -R /usr/skywalking/agent/* /skywalking/agent", # 将文件copy到容器内部,下列-javaagent需要使用
]
volumeMounts:
- mountPath: /skywalking/agent
name: sw-agent
containers:
- name: demo3
image: demo3:v43 # 通过docker build 进行构建镜像
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80
name: "demo3"
resources:
requests:
cpu: "100m" #CPU限制
memory: "1024Mi" #内存限制
env:
- name: SPRING_PROFILES_ACTIVE
value: "test"
- name: SERVER_PORT
value: "80"
- name: SW_AGENT_NAME
value: "dev::demo3"
- name: SW_AGENT_COLLECTOR_BACKEND_SERVICES
value: 124.222.177.150:11800 # ip:11800采集接口
- name: JAVA_TOOL_OPTIONS
value: "-javaagent:/usr/skywalking/agent/skywalking-agent.jar" # 容器内部的jar位置
volumeMounts:
- mountPath: /usr/skywalking/agent
name: sw-agent
volumes:
- name: sw-agent
emptyDir: { }
---
apiVersion: v1
kind: Service
metadata:
namespace: test-apps
name: demo3
spec:
selector:
name: demo3
ports:
- name: demo3
protocol: TCP
port: 80
targetPort: 80
EOF
# 开始部署yml文件
kubectl apply -f demo-deployment.yaml
2.2.3 验证结果
2.3 集成日志采集
2.3.1 pom文件中添加依赖
<!--start skywalking8.5集成-->
<!--skywalking8.5日志集成-->
<dependency>
<groupId>org.apache.skywalking</groupId>
<artifactId>apm-toolkit-logback-1.x</artifactId>
<version>${skywalking.version}</version>
</dependency>
<!-- 该引用用于代码获取tranceId -->
<dependency>
<groupId>org.apache.skywalking</groupId>
<artifactId>apm-toolkit-trace</artifactId>
<version>${skywalking.version}</version>
</dependency>
<!--end skywalking8.5集成-->
<!--xml.value解析表达式-->
<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>janino</artifactId>
<version>3.0.6</version>
</dependency>
2.3.2 生成两个xml日志文件
温馨提示:我分了两个文件进行配置,此处可以使用一个配置文件!
#文件1:logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="logback-skywalking.xml"/> # 引入sky日志(也可以写在一个文件中)
<property name="LOG_PATH" value="logs"/>
<property name="MAX_FILE_SIZE" value="10MB"/>
<property name="MAX_HISTORY" value="30"/>
<springProperty scope="context" name="applicationName" source="spring.application.name" defaultValue="demo" />
<springProperty scope="context" name="loglevel" source="logging.level.root" defaultValue="info" />
<springProperty scope="context" name="skyLogEnable" source="skywalking.log.enable" defaultValue="false"/> # 新增sky日志的开关,yml中配置
<!-- 控制台日志样式 -->
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%d{[${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}]}-[${LOG_LEVEL_PATTERN:-%5p}]-[%-30.30logger{29}] : %m%n}"/>
<!-- 文件日志样式 -->
<property name="FILE_LOG_PATTERN" value="${FILE_LOG_PATTERN:-%d{[${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}]}-[${LOG_LEVEL_PATTERN:-%5p}]-[%-30.30logger{29}] : %m%n}"/>
<!-- 控制台 -->
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}</pattern>
</encoder>
</appender>
<!-- 日志文件 -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
<!-- <file>${LOG_PATH}/ztproject.log</file> -->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/${applicationName}-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>${MAX_FILE_SIZE}</maxFileSize>
<maxHistory>${MAX_HISTORY}</maxHistory>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
</rollingPolicy>
</appender>
<!-- 错误日志文件 我们在k8s环境下 没必要做错误日志文件 -->
<!-- <appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
<file>${LOG_PATH}/ztproject-error-error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/${applicationName}-error-%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<maxFileSize>${MAX_FILE_SIZE}</maxFileSize>
<maxHistory>${MAX_HISTORY}</maxHistory>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender> -->
<!-- 异步写日志 -->
<appender name="ASYNC_FILE" class="ch.qos.logback.classic.AsyncAppender">
<discardingThreshold>0</discardingThreshold>
<queueSize>1024</queueSize>
<appender-ref ref ="FILE"/>
</appender>
<!-- <appender name="ASYNC_ERROR_FILE" class="ch.qos.logback.classic.AsyncAppender">
<discardingThreshold>0</discardingThreshold>
<queueSize>1024</queueSize>
<appender-ref ref ="ERROR_FILE"/>
</appender> -->
<root level="INFO">
<appender-ref ref="ASYNC_FILE"/>
<if condition='${skyLogEnable}'> # js表达式
<then>
<appender-ref ref="SKY_WALKING_CONSOLE"/>
<appender-ref ref="GRPC_LOG"/>
</then>
<else>
<appender-ref ref="CONSOLE"/>
</else>
</if>
</root>
</configuration>
#文件2:logback-skywalking.xml
<?xml version="1.0" encoding="UTF-8"?>
<included>
<!--日志渲染-->
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
<!-- skywalking日志样式 -->
<property name="SKY_WALKING_PATTERN" value="[%tid] ${SKY_WALKING_PATTERN:-%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39} %line){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<!-- skywalking控制台 -->
<appender name="SKY_WALKING_CONSOLE" 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>${SKY_WALKING_PATTERN}</pattern>
</layout>
</encoder>
</appender>
<!--推送日志至管理后台-->
<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>%date [%file:%line]-%msg%n</pattern>
</layout>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level>
</filter>
</appender>
<!-- skywalking控制台 -->
</included>
2.3.3 配置yml开关,启动日志,测试结果
#application.yml开启日志追踪
skywalking:
log:
enable: true # false是关闭,不配默认关闭
2.3.4 验证是否配置成功
温馨提示:项目自行定义接口,然后通过日志@Slf4j来记录日志!
温馨提示:如果需要k8s或者docker部署skywalking方式的猿友们,在下方评论或者联系我,可以对其进行编写部署方案提供猿友们参考!