关于zipkin:SpringCloud七SleuthZipkinBus消息总线

7次阅读

共计 4134 个字符,预计需要花费 11 分钟才能阅读完成。

sleuth 链路跟踪

随着零碎规模越来越大,微服务之间调用关系变得盘根错节,一条调用链路中可能调用多个微服务,任何一个微服务不可用都可能造整个调用过程失败

spring cloud sleuth 能够跟踪调用链路,剖析链路中每个节点的执行状况

微服务中增加 spring cloud sleuth 依赖

批改以下微服务的 pom.xml,增加 sleuth 依赖

  • sp02-item-service
  • sp03-user-service
  • sp04-order-service
  • sp11-zuul

编辑起步依赖,别离 sleuth 依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>

在控制台查看链路跟踪日志

  • 通过 zuul 网关,拜访 order-service
    http://localhost:3001/order-service/112233

四个微服务的控制台日志中,能够看到以下信息:
[服务 id, 申请 id,span id, 是否发送到 zipkin]

  • 申请 id:申请达到第一个微服务时生成一个申请 id,该 id 在调用链路中会始终向前面的微服务传递
  • span id:链路中每一步微服务调用,都生成一个新的 id

[zuul,6c24c0a7a8e7281a,6c24c0a7a8e7281a,false]
[order-service,6c24c0a7a8e7281a,993f53408ab7b6e3,false]
[item-service,6c24c0a7a8e7281a,ce0c820204dbaae1,false]
[user-service,6c24c0a7a8e7281a,fdd1e177f72d667b,false]

sleuth + zipkin 链路剖析

sleuth

生成链路跟踪日志的工具

zipkin

能够收集链路跟踪数据,提供可视化的链路剖析

  • 增加 sleuth 只须要增加它的依赖,它是主动配置的
  • 增加 zipkin 客户端依赖、amqp 依赖
  • yml 增加 rabbitmq 的连贯信息
  • yml 配置日志发送形式:rabbitmq

链路数据抽样比例

默认 10% 的链路数据会被发送到 zipkin 服务。能够配置批改抽样比例

spring:
  sleuth:
    sampler:
      probability: 0.1

zipkin 服务

下载 zipkin 服务器

  • https://github.com/openzipkin/zipkin

启动 zipkin 时,连贯到 rabbitmq

java -jar zipkin-server-2.12.9-exec.jar --zipkin.collector.rabbitmq.uri=amqp://admin:admin@192.168.64.140:5672

  • http://localhost:9411/zipkin

微服务增加 zipkin 起步依赖

批改以下微服务

  • sp02-item-service
  • sp03-user-service
  • sp04-order-service
  • sp11-zuul
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>

如果没有配置过 spring cloud bus,还须要增加 rabbitmq 依赖和连贯信息

启动并拜访服务,拜访 zipkin 查看链路剖析

  • http://localhost:3001/order-service/112233
    刷新拜访屡次,链路跟踪数据中,默认只有 10% 会被收集到 zipkin
  • 拜访 zipkin
    http://localhost:9411/zipkin

向 eureka 注册正确的 ip 地址

eureka 客户端向 eureka 注册时, 会主动抉择网卡, 并可能注册主机名而不是 ip 地址.

上面配置能够抉择正确网卡的 ip 向 eureka 进行注册.

抉择正确网卡

服务器有多块网卡, 要抉择正确网卡的 ip 地址向 eureka 进行注册

批改 bootstrap.yml

spring:
  cloud:
    inetutils:
      ignored-interfaces: # 疏忽的网卡
        - VM.*
      preferred-networks: # 要是用的网卡的网段
        - 192.168.0

注册 ip 地址, 而不是主机名

注册时, 有可能主动抉择主机名进行注册, 而不应用 ip 地址. 主机名在局域网内有可能不会被正确的解析

最好应用 ip 地址进行注册, 而不注册主机名

在利用配置 application.yml 中配置:

eureka:
  instance:
    prefer-ip-address: true # 应用 ip 进行注册
    instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port} # 界面列表中显示的格局也显示 ip

config bus + rabbitmq 音讯总线配置刷新

post 申请音讯总线刷新端点,服务器会向 rabbitmq 公布刷新音讯,接管到音讯的微服务会向配置服务器申请刷新配置信息

rabbitmq 装置笔记

  • https://blog.csdn.net/weixin_38305440/article/details/102810522

须要动静更新配置的微服务,增加 spring cloud bus 依赖,并增加 rabbitmq 连贯信息

批改以下微服务

  • sp02-item-service
  • sp03-user-service
  • sp04-order-service
  • sp11-zuul
  • sp12-config

pom.xml 增加 spring cloud bus 依赖

应用 STS 编辑起步依赖,别离增加 busrabbitmq 依赖

批改 5 个我的项目

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-bus</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit-test</artifactId>
            <scope>test</scope>
        </dependency>

配置文件中增加 rabbitmq 连贯信息

在以下配置文件中批改:

  • config 中的 4 个配置文件
  • sp12-config 我的项目的 application.yml

留神:

  • 连贯信息请批改成你的连贯信息
  • config 我的项目须要提交
spring:
  ......
  rabbitmq:
    host: 192.168.64.140
    port: 5672
    username: admin
    password: admin

config-server 裸露 bus-refresh 刷新端点

批改 sp12-config 我的项目的 application.yml, 裸露 bus-refresh 端点

management:
  endpoints:
    web:
      exposure:
        include: bus-refresh
  • 查看刷新端点
    http://localhost:6001/actuator

启动服务,申请刷新端点公布刷新音讯

  • postman 向 bus-refresh 刷新端点发送 post 申请
    http://localhost:6001/actuator/bus-refresh

留神:

  • 在新标签中测试
  • 如果刷新指定的微服务,可按上面格局拜访:
    http://localhost:6001/actuator/bus-refresh/user-service:8101

config 本地文零碎

能够把配置文件保留在配置核心服务的 resources 目录下,间接拜访本地文件

把配置文件保留到 sp12-config 我的项目的 resources/config 目录下

批改 application.yml 激活 native profile,并指定配置文件目录
  • 必须配置 spring.profiles.active=native 来激活本地文件系统
  • 本地门路默认:[classpath:/, classpath:/config, file:./, file:./config]
spring:
  application:
    name: config-server
  profiles:
    active: native
  
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/config

#        git:
#          uri: https://github.com/ 你的用户门路 /sp-config
#          searchPaths: config
#          username: your-username
#          password: your-password
        
    
  rabbitmq:
    host: 192.168.64.140
    port: 5672
    username: admin
    password: admin

    
server:
  port: 6001
    
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
      
management:
  endpoints:
    web:
      exposure:
        include: bus-refresh
正文完
 0