首页 > 其他分享 >day23 SpringCloud应用改造实践 (8.3.1-8.4.1)

day23 SpringCloud应用改造实践 (8.3.1-8.4.1)

时间:2023-12-28 15:16:35浏览次数:35  
标签:8.3 financial name acme SpringCloud app agent skywalking 8.4

8.3-1-SpringCloud应用改造实践上

一、使用SkyWalking Agent

Java中使用agent,提供以下三种方式实现

  • 使用官方提供的基础镜像skywalking-base
  • 将agent 包构建到已经存在的基础镜像中
  • sidecar模式挂载agent (推荐)

1.1 使用官方提供的基础镜像

https://skywalking.apache.org/downloads/

1.2 将agent 包构建到已经存在的基础镜像中

提供这种方式的原因是:官方的镜像属于精简镜像,并且是openjdk,可能需要安装很多常用命令。

1.3 sidecar 模式挂载agent

  • 不需要修改原来的基础镜像,也无需重新构建服务镜像。以sidecar模式,通过共享volume的方式将agent所需的相关文件挂载到已经存在的服务镜像中。
  • 在sidecar模式下,SkyWalking代理程序与应用程序是分离的,不会影响应用程序的执行,也不会干扰到应用的性能问题。

1.4 构建skywalking agent image

通过以下dockerfile 进行构建

FROM alpine:3.8

LABEL maintainer="ZHDYA"

ENV SKYWALKING_VERSION=8.5.0

ADD https://archive.apache.org/dist/skywalking/${SKYWALKING_VERSION}/apache-skywalking-apm-${SKYWALKING_VERSION}.tar.gz /

RUN tar -zxvf /apache-skywalking-apm-${SKYWALKING_VERSION}.tar.gz && \
    mv apache-skywalking-apm-bin skywalking && \
    mv /skywalking/agent/optional-plugins/apm-trace-ignore-plugin* /skywalking/agent/plugins/ && \
    sed -i 's@# logging.max_history_files=${SW_LOGGING_MAX_HISTORY_FILES:-1}@logging.max_history_files=${SW_LOGGING_MAX_HISTORY_FILES:2}@' /skywalking/agent/config/agent.config && \
    echo -e "\n# Ignore Path" >> /skywalking/agent/config/agent.config && \
    echo "# see https://github.com/apache/skywalking/blob/v8.5.0/docs/en/setup/service-agent/java-agent/agent-optional-plugins/trace-ignore-plugin.md" >> /skywalking/agent/config/agent.config && \
    echo 'trace.ignore_path=${SW_IGNORE_PATH:/health}' >> /skywalking/agent/config/agent.config

待docker build 完毕后,push到仓库即可

docker build -t harbor-local.kubernets.cn/library/skywalking-agent:8.5.0 .

docker push harbor-local.kubernets.cn/library/skywalking-agent:8.5.0

1.5 使用sidecar挂载

测试demo应用

apiVersion: apps/v1
kind: Deployment
metadata:
  name: demo-skywalking
spec:
  replicas: 1
  selector:
    matchLabels:
      app: demo-skywalking
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: demo-skywalking
    spec:
      initContainers:
        - name: init-skywalking-agent
          image: harbor-local.kubernets.cn/library/skywalking-agent:8.5.0
          command:
            - 'sh'
            - '-c'
            - 'set -ex;mkdir -p /vmskywalking/agent;cp -r /skywalking/agent/* /vmskywalking/agent;'
          volumeMounts:
            - mountPath: /vmskywalking/agent
              name: skywalking-agent
      containers:
        - image: nginx:1.7.9
          imagePullPolicy: Always
          name: nginx
          ports:
            - containerPort: 80
              protocol: TCP
          volumeMounts:
            - mountPath: /opt/skywalking/agent
              name: skywalking-agent
      volumes:
        - name: skywalking-agent
          emptyDir: {}

以上是挂载sidecar的deployment.yaml文件,以nginx作为服务为例,主要是通过共享volume的方式挂载agent

首先initContainers通过skywalking-agent 卷挂载了sw-agent-sidecar中的/vmskywalking/agent ,并且将上面构建好的镜像中的agent目录cp到/vmskywalking/agent 目录。完成之后nginx启动时也挂载了skywalking-agent卷,并将其挂载到容器的/opt/skywalking/agent目录

二、改造SpringCloud应用

2.1 docker 打包并推送到仓库

修改dockerfile 配置,集成skywalking agent

# FROM openjdk:8-jdk
FROM harbor-local.kubernets.cn/library/openjdk:8-jdk
COPY ./springdemo2023.jar /opt/
RUN mkdir -p data/logs/ \
&& mkdir -p data/skywalking/agent
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' > /etc/timezone

ENTRYPOINT [ "java","-Dapp.id=springdemo2023","-javaagent:/data/skywalking/agent/skywalking-agent.jar","-Dskywalking.agent.service_name=springdemo2023","-Dskywalking.collector.backend_service=skywalking-oap.devops.svc.cluster.local:11800", "-jar", "/opt/springdemo2023.jar" ,"-Dfile.encoding=UTF-8"]

注意:k8s创建Service时,它会创建相应的DNS条目。此条例的格式为<service-name>.<namespace-name>.svc-cluster.local,如果容器只是有<Service-name>它将解析为本地服务到命名空间。如果要跨命名空间访问,泽需要使用完全限定的域名。

编译推送到私有仓库测试:

docker build -t harbor-local.kubernets.cn/demo/springdemo-skywalking:v1 .

2.2 编写k8s的yaml部署

    apiVersion: v1
    kind: Service
    metadata:
      annotations:
        prometheus.io/http-probe: "true"
        prometheus.io/http-probe-port: "8080"
        prometheus.io/http-probe-path: "/apptwo"
      name: springdemo2023
      namespace: default
    spec:
      ports:
      - name: web
        port: 8080
        protocol: TCP
        targetPort: 8080
      selector:
        app: springdemo2023
      sessionAffinity: None
      type: ClusterIP
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: springdemo2023
      namespace: default
    spec:
      replicas: 1
      revisionHistoryLimit: 10
      selector:
        matchLabels:
          app: springdemo2023
      strategy:
        rollingUpdate:
          maxSurge: 25%
          maxUnavailable: 25%
        type: RollingUpdate
      template:
        metadata:
          labels:
            app: springdemo2023
        spec:
          imagePullSecrets:
          - name: harborsecret
          initContainers:
            - name: init-skywalking-agent
              image: harbor-local.kubernets.cn/library/skywalking-agent:8.5.0
              command:
                - 'sh'
                - '-c'
                - 'set -ex;mkdir -p /vmskywalking/agent;cp -r /skywalking/agent/* /vmskywalking/agent;'
              volumeMounts:
                - mountPath: /vmskywalking/agent
                  name: skywalking-agent
          containers:
          - image: harbor-local.kubernets.cn/demo/springdemo-skywalking:v1
            imagePullPolicy: IfNotPresent
        livenessProbe:
          failureThreshold: 5
          httpGet:
            path: /apptwo
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 30
          periodSeconds: 10
          successThreshold: 1
          timeoutSeconds: 2
        name: springdemo2023
        ports:
        - containerPort: 8080
          name: web
          protocol: TCP
        readinessProbe:
          failureThreshold: 5
          httpGet:
            path: /apptwo
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 20
          periodSeconds: 5
          successThreshold: 1
          timeoutSeconds: 2
        volumeMounts:
        - mountPath: /data/skywalking/agent
          name: skywalking-agent
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      terminationGracePeriodSeconds: 60
      volumes:
      - name: skywalking-agent
        emptyDir: {}

 

8.3-2-SpringCloud应用改造实践下

三、线上真实场景模拟(spingboot微服务)

3.1 springboot微服务-ui服务

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: acme-financial-ui
  name: acme-financial-ui
spec:
  replicas: 1
  selector:
    matchLabels:
      app: acme-financial-ui
  template:
    metadata:
      labels:
        app: acme-financial-ui
    spec:
      initContainers:
        - name: init-skywalking-agent
          image: harbor-local.kubernets.cn/library/skywalking-agent:8.5.0
          command:
            - 'sh'
            - '-c'
            - 'set -ex;mkdir -p /vmskywalking/agent;cp -r /skywalking/agent/* /vmskywalking/agent;'
          volumeMounts:
            - mountPath: /vmskywalking/agent
              name: skywalking-agent
      containers:
      - env:
        - name: JAVA_OPTS
          value: "-javaagent:/opt/sw/agent/skywalking-agent.jar"
        - name: SW_AGENT_NAME
          value: "acme-financial-ui"
        - name: SW_AGENT_COLLECTOR_BACKEND_SERVICES
          value: "skywalking-oap.devops.svc.cluster.local:11800"
        image: registry.cn-shanghai.aliyuncs.com/zhdya/acme-financial-ui:v0.1
        imagePullPolicy: Always
        name: ui
        ports:
        - containerPort: 8081
          protocol: TCP
        volumeMounts:
        - mountPath: /opt/sw/agent
          name: skywalking-agent
      volumes: 
      - name: skywalking-agent
        emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: acme-financial-ui
  name: acme-financial-ui
spec:
  ports:
  - name: http
    port: 8081
    protocol: TCP
    targetPort: 8081
  selector:
    app: acme-financial-ui
  sessionAffinity: None
  type: ClusterIP

 

 

3.2 Spingboot微服务-office服务

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: acme-financial-office
  name: acme-financial-office
spec:
  replicas: 1
  selector:
    matchLabels:
      app: acme-financial-office
  template:
    metadata:
      labels:
        app: acme-financial-office
    spec:
      initContainers:
        - name: init-skywalking-agent
          image: harbor-local.kubernets.cn/library/skywalking-agent:8.5.0
          command:
            - 'sh'
            - '-c'
            - 'set -ex;mkdir -p /vmskywalking/agent;cp -r /skywalking/agent/* /vmskywalking/agent;'
          volumeMounts:
            - mountPath: /vmskywalking/agent
              name: skywalking-agent
      containers:
      - env:
        - name: JAVA_OPTS
          value: "-javaagent:/opt/sw/agent/skywalking-agent.jar"
        - name: SW_AGENT_NAME
          value: "acme-financial-office"
        - name: SW_AGENT_COLLECTOR_BACKEND_SERVICES
          value: "skywalking-oap.devops.svc.cluster.local:11800"
        image: registry.cn-shanghai.aliyuncs.com/zhdya/acme-financial-office:v0.1
        imagePullPolicy: Always
        name: office
        ports:
        - containerPort: 8082
          protocol: TCP
        volumeMounts:
        - mountPath: /opt/sw/agent
          name: skywalking-agent
      volumes: 
      - name: skywalking-agent
        emptyDir: {}

---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: acme-financial-office
  name: acme-financial-back-office
spec:
  ports:
  - name: http
    port: 8082
    protocol: TCP
    targetPort: 8082
  selector:
    app: acme-financial-office
  sessionAffinity: None
  type: ClusterIP

 

3.3 Spingboot微服务 -account服务

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: acme-financial-account
  name: acme-financial-account
spec:
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: acme-financial-account
  template:
    metadata:
      labels:
        app: acme-financial-account
    spec:
      initContainers:
        - name: init-skywalking-agent
          image: harbor-local.kubernets.cn/library/skywalking-agent:8.5.0
          command:
            - 'sh'
            - '-c'
            - 'set -ex;mkdir -p /vmskywalking/agent;cp -r /skywalking/agent/* /vmskywalking/agent;'
          volumeMounts:
            - mountPath: /vmskywalking/agent
              name: skywalking-agent
      containers:
      - env:
        - name: JAVA_OPTS
          value: "-javaagent:/opt/sw/agent/skywalking-agent.jar"
        - name: SW_AGENT_NAME
          value: "acme-financial-account"
        - name: SW_AGENT_COLLECTOR_BACKEND_SERVICES
          value: "skywalking-oap.devops.svc.cluster.local:11800"
        image: registry.cn-shanghai.aliyuncs.com/zhdya/acme-financial-account:v0.1
        imagePullPolicy: Always
        name: account
        ports:
        - containerPort: 8083
          protocol: TCP
        volumeMounts:
        - mountPath: /opt/sw/agent
          name: skywalking-agent
      volumes: 
      - name: skywalking-agent
        emptyDir: {}
---

apiVersion: v1
kind: Service
metadata:
  labels:
    app: acme-financial-account
  name: acme-financial-account
spec:
  ports:
  - name: http
    port: 8083
    protocol: TCP
    targetPort: 8083
  selector:
    app: acme-financial-account
  sessionAffinity: None
  type: ClusterIP

 

3.4 Spingboot微服务-customer服务

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: acme-financial-customer
  name: acme-financial-customer
spec:
  replicas: 1
  selector:
    matchLabels:
      app: acme-financial-customer
  template:
    metadata:
      labels:
        app: acme-financial-customer
    spec:
      initContainers:
        - name: init-skywalking-agent
          image: harbor-local.kubernets.cn/library/skywalking-agent:8.5.0
          command:
            - 'sh'
            - '-c'
            - 'set -ex;mkdir -p /vmskywalking/agent;cp -r /skywalking/agent/* /vmskywalking/agent;'
          volumeMounts:
            - mountPath: /vmskywalking/agent
              name: skywalking-agent
      containers:
      - env:
        - name: JAVA_OPTS
          value: "-javaagent:/opt/sw/agent/skywalking-agent.jar"
        - name: SW_AGENT_NAME
          value: "acme-financial-customer"
        - name: SW_AGENT_COLLECTOR_BACKEND_SERVICES
          value: "skywalking-oap.devops.svc.cluster.local:11800"
        image: registry.cn-shanghai.aliyuncs.com/zhdya/acme-financial-customer:v0.1
        imagePullPolicy: Always
        name: customer
        ports:
        - containerPort: 8084
          protocol: TCP
        volumeMounts:
        - mountPath: /opt/sw/agent
          name: skywalking-agent
      volumes: 
      - name: skywalking-agent
        emptyDir: {}

---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: acme-financial-customer
  name: acme-financial-customer
  namespace: default
spec:
  ports:
  - name: http
    port: 8084
    protocol: TCP
    targetPort: 8084
  selector:
    app: acme-financial-customer
  sessionAffinity: None
  type: ClusterIP

3.5 创建UI-ingress

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  namespace: default
  name: acme-financial-ing
spec:
  ingressClassName: nginx
  rules:
  - host: acme.kubernets.cn
    http:
      paths:
        - pathType: Prefix
          backend:
            service:
              name: acme-financial-ui
              port:
                number: 8081
          path: /

3.6 测试Spring 微服务API

通过Ingress访问UI服务的三个接口:/hello、/start、/readtimeout。

## 查看服务svc
kubectl get svc

## 模拟访问
for i in {1..5000}; do curl http://acme.kubernets.cn/hello && sleep 1; done

for i in {1..5000}; do curl http://acme.kubernets.cn/start && sleep 1; done

for i in {1..5000}; do curl http://acme.kubernets.cn/readtimeout && sleep 1; done

 

3.7 查看SkyWalking UI界面

  1. 吞吐量CPM,表示每分钟的调用
  2. Apdex分数:衡量服务器性能的标准
  3. 响应时间百分比,包括p99,p95,p75,p50
  4. SLA表示成功率。对应HTTP,表示响应为200的请求

拓扑图:用指标显示服务和实例之间的关系,点击服务显示监控数据。

跟踪查询:skywalk提供的分布式代理一样的典型特性

具体接口服务报错/响应慢的主要原因

四、Go应用如歌接入SkyWalking?

参考:https://skywalking.apache.org/zh/2023-06-01-quick-start-with-skywalking-go-agent/

五、总结

SkyWalking的主要功能和特点

  • 分布式追踪:一个分布式追踪功能,可以帮助研发识别和调式分布式微服务应用中的性能问题。
  • 应用性能监测:监测应用程序的性能情况,包括吞吐量、响应时间、错误率等指标
  • 自定义告警:自定义告警条件和操作,以便及时发现和解决性能问题
  • 多语言支持:多种变成语音。包括:Java、.net 、NodeJS、GO等
  • 可扩展性:支持插件式的价格,可以方便地添加新的监测模块和数据存储引擎

 

标签:8.3,financial,name,acme,SpringCloud,app,agent,skywalking,8.4
From: https://www.cnblogs.com/pythonlx/p/17924600.html

相关文章

  • SpringCloud与Dubbo的区别(九)
    前言参看链接:https://blog.csdn.net/huangtenglong/article/details/131144602一、SpringCloud与Dubbo的区别初始定位不同:SpringCloud定位为微服务架构下的一站式解决方案;Dubbo是SOA时代的产物,它的关注点主要在于服务的调用和治理生态环境不同:SpringCloud依托于Spring平台,具......
  • 浅聊SpringCloud的网关
    为什么要设计网关?上网搜罗了一下,觉得别人说的挺好,就引用了一下,在使用微服务的时候,不同的功能业务会集成一个服务群,而网关是基于服务群上的一个服务层,也是单独暴露给客户端的APIs。客户端对微服务的依赖直接使重构服务变得困难。一种直观的方法是将这些服务隐藏在一个新的服务层后面......
  • minikube 部署 k8s v1.28.3
    安装kubeadm、kubelet 和 kubectl此步骤可选下载kubeadm#wget-P/usr/local/binhttps://dl.k8s.io/release/v1.28.3/bin/linux/amd64/kubeadm下载kubelet#wget-P/usr/local/binhttps://dl.k8s.io/release/v1.28.3/bin/linux/amd64/kubelet下载kubectl#wget-......
  • SpringCloud版本升级个组件替代方案
     转载自:https://juejin.cn/post/7196140566110355515SpringCloud版本升级个组件替代方案 SpringBootSpringBoot版本截止2023年1月20日,SpringBoot的最新版本为 3.0.2,最新的版本信息可以查阅以下网站来获取:Maven中央仓库SpringBoot官方文档SpringB......
  • 初识SpringCloud
    一、SpringCloudSpringCloud是目前国内使用最广泛的微服务框架。官网地址:https://spring.io/projects/spring-cloud/SpringCloud集成了各种微服务功能组件,并基于SpringBoot实现了这些组件的自动装配,从而提供了良好的开箱即用体验:服务注册发现:Eureka、Nacos、Consul服务远程调用:Ope......
  • SpringCloudAlibaba商城
    功能预览用户端登录界面注册界面网站介绍保健品保健品详情养生知识养生知识详情文章中心文章详情--用户关注个人中心我的购物车我的订单我的养生知识文章管理文章发布/编辑  支付宝沙箱保健品结算保健品支付界面保健品支付成功界面支......
  • 【Java】Spring、SpringBoot、SpringCloud理解和入门
    关于Spring、SpringBoot、SpringCloud我总结了几点。Spring是轻量级的容器框架,目前大多数项目不直接使用Spring的。SpringBoot是Spring的升级版,主要应用单个微服务,关联性小的项目、规模小、硬件要求不高的项目。SpringCloud基于SpringBoot来实现微服务开发组件。主要应用于规......
  • 搭建springcloud注册中心eureka以及admin监控
    现在springcloud微服务是当下盛行的主流框架,我们可以通过eureka实现服务监控和预警。接下来是项目搭建的主要步骤一、搭建eureka-server注册中心注意点有三个,分别是1.pom导入依赖(下面是完整的pom文件):springboot、eureka-server、springcloud<?xmlversion="1.0"encoding="UTF-......
  • springcloud集成 openfeign报错
    集成openfeign做远程调用和负载均衡如果没有学feign的同学,可以使用restTemplate来做出处有坑步骤回显引入openfeign的依赖<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></depende......
  • 算法学习笔记(8.3): 网络最大流 - 模型篇
    本文慢慢整理部分模型。DAG最小路径覆盖经典的题目,经典的思想。网络流常见的将图上的点拆为入点和出点,那么路径由若干出-入-出-入的循环构成。于是在拆好的图上流一流即可。[CTSC2008]祭祀典中祭黑白染色利用黑白染色将整个图变成一个二分图是网络流常见的套路,......