SpringCloud Config配置中心、SpringCloud Bus消息总线

一、SpringCloud Config

1、前言
  • 微服务意味着要将单体应用中的业务拆分成一个个子服务, 每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。
  • SpringCloud提供了ConfigServer来解决这个问题, 我们每-个微服务自己带着一个application.yml, 上百个配置文件的管理…/(ToT)/~~
2、是什么?
  • SpringCloud Config为微服务架构中的微服务提供集化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了-个中心化的外部配置。
3、怎么玩?
  • SpringCloud Config分为服务端和客户端两部分。
  • 服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口
  • 客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,组可以通过git客户端工具来方便的管理和访问配置内容
4、能干嘛?
  • 集中管理配置文件
  • 不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/releasco
  • 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
  • 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
  • 将配置信息以REST接口的形式暴露
5、与GitHub整合配置
  • 由于SpringCloud Config默认使用Git来存储配置文件(也有其它方式,比如支持SVN和本地文件),但最推荐的还是Git,而且使用的是http/https访问的形式
6、Config服务端配置与测试
  • pom
 <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-config-server</artifactId>
 </dependency>
  • yml
server:
  port: 3344
spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          uri: https://github.com/rwnb/springcloud-config.git
          #uri: git@github.com:rwnb/springcloud-config.git
          search-paths:
            - springcloud-config
          skip-ssl-validation: true #记得一定要写 要不会报SSL异常的

      label: master
eureka:
  client:
    service-url:
      defaultZone:  http://localhost:7001/eureka
  • 启动类
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigCenterMain3344 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigCenterMain3344.class,args);
    }
}
7、读取配置规则

label:分支(branch)
name :服务名
profiles:环境(dev/test/prod)

  • /{label}/{application}-{profile}.yml
    • master分支
      • http://config-3344.com:3344/master/config-dev.yml
      • http://config-3344.com:3344/master/config-test.yml
      • http://config-3344.com:3344/master/config-prod.yml
    • dev分支
      • http://config-3344.com:3344/dev/config-dev.ymI
      • http://config-3344.com:3344/dev/config-test.ymI
      • http://config-3344.com:3344/dev/config-prod.ymI
  • /{application}-{profile}.yml
    • http://config-3344.com:3344/config-dev.yml
    • http://config-3344.com:3344/config-test.yml
    • http://config-3344.com:3344/config-prod.yml
  • /{application}-{profile}/{label}
    • http://config-3344.com:3344/config/dev/master
    • http://config-3344.com:3344/config/test/master
    • http://config-3344.com:3344/config/prod/master
8、Config客户端配置与测试
(1)bootstrap.yml介绍
  • applicaiton. yml是用户级的资源配置项
  • bootstrap. yml是系统级的,优先级更加高
  • Spring Cloud会创建一个"Bootstrap Context",作为Spring应用的Application Context的父上下文。初始化的时候,BootstrapContext’负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment。
  • Bootstrap’属性有高优先级,默认情况下,它们不会被本地配置覆盖。Bootstrap context’ 和Application Context有着不同的约定所以新增了-个bootstrap.yml文件,保证Bootstrap Context和Application Context配置的分离。
  • 要将Client模块下的application.yml文件改为bootstrap.yml,这是很关键的,
  • 因为bootstrap.ym是比application.yml先加载的。bootstrapyml优先级高于application.yml
(2)pom
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
  • bootstrap.yml
server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master #分支名称
      name: config #配置文件名称a
      profile: dev #上述三个分支config-dev.yml的配置文件将被读取:http://localhost:3344/master/config-dev.yml
      uri: http://localhost:3344
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka
  • 启动类
package com.rw.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientMain3355.class,args);
    }
}
  • 存在问题:修改了GitHub上的配置信息 客户端3344可以跟随动态刷新了 但是3355需要重启后才能刷新–>噩梦
9、config客户端之动态刷新
  • 修改pom引入actuator监控
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
  • 控制层加自动刷新注解
@RefreshScope
  • 光凭借这些是不能实现动态刷新的,此时需要修改配置后 发送post请求获取最新配置

  • curl -X POST “http:// loca I host : 3355/ actuator/refresh”

  • 问题,如果有很多台机器的话,逐个发送post请求是不是很不方便,当然可以写脚本来实现,但是是否可以一出广播,针对接收自动刷新呢?抱歉 config做不到,所以引入了消息总线↓↓↓↓↓↓↓

消息总线

二、SpringCloud Bus

1、概述
  • 分布式自动刷新配置功能
  • SpringCloud Bus配合SprignCloud Config使用可以实现配置的动态刷新
2、是什么?
  • Bus支持两种消息代理:RabbitMQ和Kafka
  • Spring Cloud Bus是用来将分布式系统的节点与轻量级消息系统链接起来的框架,它整合了Java的事件处理机制和消息中间件的功能。
3、能干嘛?
  • Spring Cloud Bus能管理和传播分布式系统间的消息,就像一 个分布式执行器,可用于广播状态更改、事件推送等, 也可以当作微服务间的通信通道
4、为何被称为总线
  • 什么是总线
    • 在微服务架构的系统中,通常会使用轻量级的消息代理来构建一 个共用的消息主题,并让系统中所有微服务实例都连接上来。由于该主题中产生的消息会被所有实例监听和消费,所以称它为消息总线。在总线上的各个实例,都可以方便地广播- -些需要让其他连接在该主题上的实例都知道的消息。
  • 基本原理
    • ConfigClient实例都监听MQ中同一 个topic(默认是springCloudBus)。 当- 个服务刷新数据的时候,它会把这个信息放入到Topic中,这样其它监听同一Topic的服务就能得到通知,然后去更新自身的配置。
      https://www. bilili.com/video/av55976700?from=search&seid=15010075915728605208
5、RabbitMQ环境配置
  • 安装Erlang,下载地址: http://erlang.org/download/otp_win64_21.3.exe
  • 安装RabbitMQ:https://dl.bintray.com/rabbitmq/all/rabbitmq-server/3.7.14/rabbitmq-server-3.7.14.exe
  • 进入sbin目录以管理员身份打开cmd 运行rabbitmq-plugins enable rabbitmq_ management
  • 启动完成之后访问:http://localhost:15672/
  • 默认密码:guest guest
6、SpringCloud Bus 动态刷新全局广播
  • 采用通知消息总线,总线分发消息的形式实现动态刷新
  • 服务端加入pom
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
  • yml
server:
  port: 3344
spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          uri: https://github.com/rwnb/springcloud-config.git
          #uri: git@github.com:rwnb/springcloud-config.git
          search-paths:
            - springcloud-config
          skip-ssl-validation: true
      label: master
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
eureka:
  client:
    service-url:
      defaultZone:  http://localhost:7001/eureka

# rabbitmq相关的
management:
  endpoints:
    web:
      exposure:
        include: 'bus-refresh'
  • 客户端 也引入pom
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
  • 客户端 bootstrap.yml修改
server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master #分支名称
      name: config #配置文件名称a
      profile: dev #上述三个分支config-dev.yml的配置文件将被读取:http://localhost:3344/master/config-dev.yml
      uri: http://localhost:3344
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka

# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"
  • 最终效果:更改GitHub上配置文件 然后去给3344发送广播:curl -X POST “http://localhost:3344/actuator/bus-refresh”
  • 就能做到移除广播处出接收修改
7、SpringCloud Bus 动态刷新定点通知
  • 不想全部通知,只想定点通知: 只通知3355不通知3366
  • curl -X POST “http://localhost:3344/actuator/bus-refresh/config-client:3355”
  • curl -X POST “http://localhost:服务中心/actuator/bus-refresh/微服务名:端口”
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值