栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

springcloud最新版本(SpringCloud07)

springcloud最新版本(SpringCloud07)

1、消息总线Bus

(1)基本概念与原理
总线:

在为服务架构中使用轻量级的消息代理来构建一个公用的消息主题,并让系统中所有的微服务实例都连接,该主题中产生的消息会被所有的实例监听和消费,所以称为消息总线。

消息总线Bus:

概念:配合spring cloud config实现配置的自动动态刷新,支持RabbitMQ和Kafka消息中间件;原理:ConfigClient实例监听MQ中同一个topic(默认SpringCloudBus),当一个服务刷新数据时候,就会把这个信息放入topic,这样它监听的同一个tpoic的服务就能得到通知,然后更新自身配置。

(2)RabbitMQ配置与安装

安装Erlang安装RabbitMQ进入RabbitMQ安装目录下sbin目录输入命名启动管理功能:rabbitmq-pligins enable rabbitmq_management查看安装插件

(3)实现
有两种实现方式:

通知其中一个客户端,再由该客户端通知其他客户端;

通知一个服务端,而刷新所有客户端配置;

以上两种方式选择第二种,原因如下:

破坏各客户端之前平衡性,增加该客户端压力

以下代码实现在上一节消息配置基础上完成。

1.3.1 服务端配置

第一步:服务端增加消息总线依赖
pom.xml

  
            org.springframework.cloud
            spring-cloud-starter-bus-amqp
        


    
        cloud2022
        org.example
        1.0-SNAPSHOT
    
    4.0.0

    cloud-config
    cloud-config
    
        
            com.commons
            commons
            1.0-SNAPSHOT
            compile
        

        
            org.springframework.cloud
            spring-cloud-starter-bus-amqp
        
        
            org.springframework.cloud
            spring-cloud-config-server
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        

    


第二步:配置RabbitMQ与Bus刷新端点
application.yml

#   配置rabbitMq
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

#暴露bus刷新端点
management:
  endpoints:
    web:
      exposure:
        include: "bus-refresh"
server:
  port: 3344

eureka:
  client:
    register-with-eureka: true #是否要注册
    fetchRegistry: true #是否抓取注册信息
    service-url:
      defaultZone: http://eureka7001:7001/eureka #,http://eureka7002:7002/eureka
  instance: #修改主机名
    instance-id: config3344
    prefer-ip-address: true #访问路径显示IP

spring:
  application:
    name : config
  cloud:
    config:
      server:
        git:
#          uri: git@github.com:wozaixianaichilamian/springboot_config.git #仓库路径
          search-paths: -springboot_config #仓库名称
          uri: https://gitee.com/wozaixianaichilamian/springboot_config
      label: master #分支名称

  datasource:
    url: jdbc:mysql://localhost:3306/springboot-mybatisplus?serverTimezone=Asia/Shanghai&useSSL=false&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
    username: root
    password: 123456
    driver-class-name=com: mysql.cj.jdbc.Driver
    
#   配置rabbitMq
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

#暴露bus刷新端点
management:
  endpoints:
    web:
      exposure:
        include: "bus-refresh"
1.3.2 客户端端配置

第一步:pom.xml添加依赖



    
        cloud2022
        org.example
        1.0-SNAPSHOT
    
    4.0.0

    config-cilent
    config-cilent

    
        
            org.springframework.cloud
            spring-cloud-starter-bus-amqp
            2.2.0.RELEASE
        
        
            org.springframework.cloud
            spring-cloud-config
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.springframework.cloud
            spring-cloud-netflix-eureka-client
        

    


第二步:配置RabbitMQ,无需配置刷新

#   配置rabbitMq
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

management:
  endpoints:
    web:
      exposure:
        include: "*"
server:
  port: 3355

eureka:
  client:
    register-with-eureka: true #是否要注册
    fetchRegistry: true #是否抓取注册信息
    service-url:
      defaultZone: http://eureka7001:7001/eureka #,http://eureka7002:7002/eureka
  instance: #修改主机名
    instance-id: config3355
    prefer-ip-address: true #访问路径显示IP

spring:
  application:
    name : config-client
  cloud:
    config:
      label: master #分支名称
      name: config #配置文件地址
      profile: dev #读取后缀名称
      uri: http://localhost:3344 #配置中心地址
     # 以上组合:http://config3344:3344/mastre/config-dev

  datasource:
    url: jdbc:mysql://localhost:3306/springboot-mybatisplus?serverTimezone=Asia/Shanghai&useSSL=false&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8
    username: root
    password: 123456
    driver-class-name=com: mysql.cj.jdbc.Driver

    #   配置rabbitMq
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

management:
  endpoints:
    web:
      exposure:
        include: "*"

第三步:启动消息服务端与客户端

第四步:修改gitu配置

第五步:cmd使用post刷新服务端

第七步:访问客户端与服务端:刷新服务端同时客户端也进行了刷新
第八步:登录RabbitMQ:可以看到ConfigClient实例监听MQ中同一个topic。(SpringCloudBus)
(4)动态刷新的定点通知

只通知一部分客户端进行刷新,另一部分不刷新。服务端永远自动刷新。

命令公式:

http://localhost:{配置端口号}/actuator/bus-refresh/{需要变更配置的服务或者实例}

curl -POST "http://localhost:3344/actuator/bus-refresh/config-cilent:3355"
//config0cilent 为配置文件yml中的application.name,3355为服务端口号

(5)总结

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/771995.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号