SpringCloud学习-Config配置中心,Bus消息总线,RabbitMQ

https://cloud.spring.io/spring-cloud-config/reference/html/
友情提示:此文章看看思路就好

SpringCloud Config

  • Spring Cloud Config 可以为微服务架构中的应用提供集中化的外部配置支持
  • Spring Cloud Config 分为服务端客户端两部分。
  1. 服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口。
  2. 客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认釆用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。

Config服务端:

在这里插入图片描述

		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
server:
  port: 3344
spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          # 码云地址亲测可用!
          uri: https://github.com/huazhaolu/springcloud-config.git
          # 搜索目录
          search-paths:
            - springcloud-config
          username: *******
          password: *******
      # 读取分支
      label: master
eureka:
  client:
    register-with-eureka: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/
/**
 * 配置中心
 * @author hzlmit
 */
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableConfigServer
public class ConfigCenterMain {
    public static void main(String[] args) {
        SpringApplication.run(ConfigCenterMain.class,args);
    }
}
读取远程配置文件:http://localhost:3344/master/config-dev.yml

在这里插入图片描述
在这里插入图片描述

http://localhost:3344/master/config-dev.yml
http://localhost:3344/config-dev.yml

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

Config客户端:

		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
		<!-- SpringCloud 2020.* 版本把bootstrap禁用了 -->
		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
            <version>3.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
bootstrap.yml
  • applicaiton,yml是用户级的资源配置项
  • bootstrap.yml是系统级的,优先级更加高
  • Spring Cloud会创建一个“ Bootstrap Cortext",作为 Spring应用的 Application Context的父上下文。初始化的时候, BootstrapContext负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的 “Environment”
  • “Bootstrap”属性有高优先级,默认情况下,它们不会被本地配置覆盖。 Bootstrap context和 Application Context有着不同的约定,所以新增了一个 bootstrap.ym文件,保证 Bootstrap Context和 Application Co ntext配置的分离
  • 要将 Client模块下的 application.ym文件改为 bootstrap ym这是很关键的,因为 bootstrap.yml是比 application.ym先加载的。 bootstrap yml优先级高于 application.yml
server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    # Config客户端配置
    config:
      label: master                     # 分支
      name: config                      # 配置文件名称
      profile: dev                      # 读取后缀名称
      uri: http://localhost:3344        # 配置中心地址

eureka:
  client:
    service-url: http://eureka7001.com:7001/eureka/
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientMain.class,args);
    }
}
@RestController
public class ConfigClientController
{
    @Value("${config.info}")
    private String configInfo;

    @GetMapping
    public String getConfigInfo(){
        return configInfo;
    }
}

Config客户端动态刷新

actuator监控
		<!-- actuator监控信息完善-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
暴露监控端点
# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"
@RefreshScope
@RestController
@RefreshScope
public class ConfigClientController
{
    @Value("${config.info}")
    public String configInfo;

    @GetMapping("/getConfigInfo")
    public String getConfigInfo(){
        return configInfo;
    }
}
POST方式通知刷新!!!
curl -X POST http://localhost:3355/actuator/refresh

在这里插入图片描述

  • 多个服务修改配置,便要发送多个刷新通知,广播一次,大范围刷新

SpringCloud Bus

  • SpringCloud Bus 配合SpringCloud Config 使用可以实现配置的动态刷线,Bus是用来将分布式系统的节点与轻量级消息系统链接起来的框架,它整合了Java的事件处理机制和消息中间件的功能。
  • 什么是总线
    在微服务架构的系统中,通常会使用轻量级的消息代理来构建一个共用的消息主题,并让系统中所有微服务实例都连接上来。由于该主题中产生的消息会被所有实例监听和消费,所以称它为消息总线。在总线上的各个实例,都可以方便地广播一些需要让其他连接在该主题上的实例都知道的消息。
  • 基本原理
    ConfigClient实例都监听MQ中同一个topic(默认是springCloud Bus)当一个服务刷新数据的时候,它会把这个消息放入到Topic中,这样其他监听同一Topic的服务就能得到通知,然后去更新自身的配置。
  • 分布式自动刷新配置功能(与Config结合使用)
  • 支持两种消息代理:RabbitMQ和Kafka
    在这里插入图片描述
  • Spring Cloud Bus能管理和传播分布式系统间的消息,就像一个分布式执行器,可用于广播状态更改、事件推送等,也可以当作微服务间的通信通道
    在这里插入图片描述

RibbitMQ环境配置

链接: https://pan.baidu.com/s/1grez5jbdLIAJFO5LkwMpfg 提取码: uwuw 复制这段内容后打开百度网盘手机App,操作更方便哦
–来自百度网盘超级会员v6的分享
在这里插入图片描述

  • 安装Erlang,下载地址
  • 安装RabbitMQ,下载地址
  • 或者直接看这位小哥的博客,软件包也是准备的妥妥的。
  • 有个很重要的一点,特么ribbitmq启动和没启动一个样子,记得先打开浏览器慢慢验证!
  • RobbitMQ地址:http://127.0.0.1:15672/
    在这里插入图片描述
  • 通知刷新地址:curl -X POST http://localhost:3355/actuator/refresh

动态刷新全局广播

  • 设计思想

利用消息总线触发一个服务端 Config Server的 /bus/refresh 端点,而刷新所有客户端的配置。

给3344服务端添加消息总线支持
<!--加消息总线 RabbitMQ-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
server:
  port: 3344
spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/swl356/springcloud-config.git
#          uri: https://github.com/huazhaolu/springcloud-config.git
#          uri: git@github.com:huazhaolu/springcloud-config.git
          # 搜索目录
          search-paths:
            - springcloud-config
          username: *************
          password: *************
      # 读取分支
      label: master
  # rabbits相关邮萱
  rabbitmq:
    host: localhost
    port: 15672
    username: guest
    password: guest

eureka:
  client:
    register-with-eureka: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/

# rabbitmq相关配置,暴漏bus刷新能置的端点
management:
  endpoints: #暴露bus刷新置的端点
    web:
      exposure:
        include: 'bus-refresh'
给3355,3366客户端添加消息总线支持
<!--加消息总线 RabbitMQ-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- actuator监控信息完善-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--热部署-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    # Config客户端配置
    config:
      label: master                     # 分支
      name: config                      # 配置文件名称
      profile: dev                      # 读取后缀名称
      uri: http://localhost:3344        # 配置中心地址
  # rabbits相关邮萱
  rabbitmq:
    host: localhost
    port: 15672
    username: guest
    password: guest

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

  • 3344启动报错:Correct the classpath of your application so that it contains a single, compatible version of org.springframework.integration.amqp.inbound.AmqpInboundChannelAdapter**加粗样式**
  • 百度一下,基本都是说版本不对啊,或者包冲突
  • 查看springcloud和springboot的版本对应关系
    在这里插入图片描述
  • 修改父工程spring cloud和springboot版本
  • 哼,徒劳,睡觉
  • 后面学nacos算了
  • 闹心

通知刷新地址:curl -x Post "http://localhost:3344/actuator/bus-refresh”

定点通知,只通知某一台客户端

http://localhost:3344/actuator/bus-refresh/(destination]
通过 destination参数类指定需要更新配置的服务或实例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

手揽回忆怎么睡

您的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值