Skywalking分布式链路追踪对Spring cloud Gateway无法记录链路问题记录
前因
最近在与技术进行链路验证,在测试Gateway
服务一直无法加载显示,其他业务正常展示链路信息。
解决
如果你要追踪Gateway的话,你会发现:无法通过gateway发现路由的服务链路?
通过查看官方文档,原因: Spring Cloud Gateway
是基于 WebFlux
实现,必须搭配上apm-spring-cloud-gateway-2.1.x-plugin
和 apm-spring-webflux-x.x-plugin
两个插件。
由于 SkyWalking 对于 spring-cloud-gateway
的支持不是默认的,所以需要将 agent/optional-plugins/
上的几个插件复制到 agent/plugins
下,然后重启即可。
由此可见,
agent
,是通过加载,一系列的plugins
插件,来帮我们干活。
最后
SkyWalking 上面这两个功能就已经非常强大,能够有效帮助我们优化我们的程序,监控系统的问题,并及时报警。
附上基于K8s部署的gateway
apiVersion: apps/v1
kind: Deployment
metadata:
name: gateway
namespace: ftintserv
labels:
app: gateway
spec:
replicas: 1
selector:
matchLabels:
app: gateway
strategy:
rollingUpdate:
maxSurge: 70%
maxUnavailable: 30%
type: RollingUpdate
template:
metadata:
labels:
app: gateway
spec:
terminationGracePeriodSeconds: 60
initContainers:
- name: agent-container
imagePullPolicy: IfNotPresent
image: reg-hub.gzeport.com/library/apache/skywalking-java-agent:9.2.0-alpine
volumeMounts:
- mountPath: /agent
name: skywalking-agent
command:
- "/bin/sh"
- "-c"
- "set -ex;cp -r /skywalking/agent /agent/;cp /skywalking/agent/optional-plugins/apm-spring-cloud-gateway* /agent/agent/plugins/;cp /skywalking/agent/optional-plugins/apm-spring-webflux* /agent/agent/plugins/;"
# args: [ "-c","cp -R /skywalking/agent /agent/ " ]cp /skywalking/agent/optional-plugins/ /agent/plugins
resources:
requests:
cpu: 0.2
memory: 200Mi
limits:
cpu: 0.2
memory: 200Mi
containers:
- name: gateway
image: reg-hub.gzeport.com/ftintserv/gateway:1.0.1.RELEASE
imagePullPolicy: Always
ports:
- containerPort: 8081
env:
- name: JAVA_SERVICE_NAME
valueFrom:
fieldRef:
fieldPath: metadata.labels['app']
- name: JAVA_SERVICE_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: JAVA_OPTS
value: "-Xms128m -Xmx512m -XX:MetaspaceSize=256M -XX:MaxMetaspaceSize=256M -Dspring.profiles.active=prod"
- name: TZ
value: "Asia/Shanghai"
- name: AGENT_ARGS
value: "-javaagent:/AppHome/skywalking-agent/agent/skywalking-agent.jar"
- name: SW_AGENT_COLLECTOR_BACKEND_SERVICES
value: skywalking-oap.logging.svc.cluster.local:11800
- name: SW_AGENT_NAME
valueFrom:
fieldRef:
fieldPath: metadata.labels['app']
- name: SW_AGENT_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: SW_LOGGING_LEVEL
value: ERROR
- name: SW_LOGGING_DIR
value: /AppHome/logs/skywalking-agent
# ### 启动探针
# startupProbe:
# httpGet:
# port: 8081
# path: /actuator/health/ping
# initialDelaySeconds: 300
# failureThreshold: 30
# periodSeconds: 30
# ### 就绪探针
# readinessProbe:
# httpGet:
# port: 8081
# path: /actuator/health/ping
# initialDelaySeconds: 30 # 第一次执行探针前等待的时间
# periodSeconds: 30 # 每隔多少秒执行一次
# failureThreshold: 5
# ### 存活探针
# livenessProbe:
# httpGet:
# port: 8081
# path: /actuator/health/ping
# initialDelaySeconds: 30
# periodSeconds: 30
# failureThreshold: 3
# timeoutSeconds: 5
resources:
requests:
cpu: 0.5
memory: 512Mi
limits:
cpu: 1
memory: 1024Mi
volumeMounts:
- mountPath: /etc/localtime
name: localtime
- mountPath: /AppHome/logs
subPath: gateway
name: ftintserv-log-data
- mountPath: /AppHome/skywalking-agent
name: skywalking-agent
volumes:
- name: localtime
hostPath:
path: /etc/localtime
- name: ftintserv-log-data
persistentVolumeClaim:
claimName: ftintserv-log-data-pvc
- name: skywalking-agent
emptyDir: { }
---
apiVersion: v1
kind: Service
metadata:
name: ftintserv-gateway-svc
namespace: ftintserv
spec:
type: ClusterIP
ports:
- port: 8081
protocol: TCP
targetPort: 8081
selector:
app: gateway