SpringCloud--config

目录

一、概述

1、简介

2、作用

3、使用

二、Config服务端配置与测试

1、配置github

2、创建Config服务端的模块

三、Config客户端的配置与测试

1、建模块

2、pom

3、bootstrap.yml

4、主启动

5、controller

6、测试

 四、Config客户端的动态刷新

1、到入actuator监控

2、修改yml,暴露监控端口

3、修改controller

4、测试

五、SpringCloudBus

1、概述

2、SpringCloud Bus动态刷新全局广播

3、定点通知

4、总结


一、概述

微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。

如果我们每一个微服务自己带着一个application.yml,那么上百个配置文件的管理是很恐怖的,springCloud提供了ConfigService来管理这些文件

1、简介

  • 是什么

SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。

  • 怎么玩

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

2、作用

  1. 集中管理配置文件
  2. 不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release
  3. 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
  4. 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
  5. 将配置信息以REST接口的形式暴露

3、使用

通常是与github整合,将配置文件存放到github中,ConfigServce到GitHubs进行拉取,而ConfgClient到ConfigService上进行获取配置

二、Config服务端配置与测试

1、配置github

git的使用请查看笔记

  • 在本地仓库中创建SpringCloud文件夹再创建一个yml文件,config-dev.yml
  • 在GitHub上创建一个仓库springcloud
  • 将本地仓库push到GitHub上

2、创建Config服务端的模块

  • 建模块

新建一个cloud-config-center3344模块

  • pom
<dependencies>
    <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-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
  • yml

由于我的ssh出现了错误所以

server:
  port: 3344
spring:
  application:
    name: config-center
  cloud:
    config:
      server:
        git:
          #仓库地址
          uri: https://github.com/xzjyy/springcloud-config.git
          search-paths:
            - springcloud-config  #要扫描的文件
          username: xzjyy #github的用户名
          #gitHub的令牌
          password: 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
      label: master #分支

eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。
    #单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetch-registry: true

    service-url:
      defaultZone: http://eureka7002.com:7002/eureka/
  • 主启动类
@SpringBootApplication
@EnableConfigServer
public class ConfigCenter3344 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigCenter3344.class,args);
    }
}
  • 测试  

  • 几种访问方式

/{application}-{profile}.yml

http://config-3344.com:3344/config-dev.yml

http://config-3344.com:3344/config-test.yml


/{application}/{profile}[/{label}

http://config-3344.com:3344/config/dev/master

http://config-3344.com:3344/config/test/master

三、Config客户端的配置与测试

1、建模块

新建cloud-config-client3355模块

2、pom

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-client</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-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

3、bootstrap.yml

  • bootstrap.yml是什么

applicaiton.yml是用户级的资源配置项
bootstrap.yml是系统级的,优先级更加高
 
Spring Cloud会创建一个“Bootstrap Context”,作为Spring应用的`Application Context`的父上下文。初始化的时候,`Bootstrap Context`负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的`Environment`。
 
`Bootstrap`属性有高优先级,默认情况下,它们不会被本地配置覆盖。 `Bootstrap context`和`Application Context`有着不同的约定,所以新增了一个`bootstrap.yml`文件,保证`Bootstrap Context`和`Application Context`配置的分离。
 
要将Client模块下的application.yml文件改为bootstrap.yml,这是很关键的,
因为bootstrap.yml是比application.yml先加载的。bootstrap.yml优先级高于application.yml

  • 内容
server:
  port: 3355
spring:
  application:
    name: config-client
  cloud:
    config:
      #分支名称
      label: master
      name: config  #配置文件名称
      profile: dev  #读取后缀名称
      uri: http://localhost:3344 #配置中心地址k
      #上述3个综合:master分支上config-dev.yml的配置文件被读取
      #http://config-3344.com:3344/master/config-dev.yml
eureka:
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://eureka7002.com:7002/eureka/
management:
  endpoints:
    web:
      exposure:
        include: "*"

4、主启动

@EnableEurekaClient
@SpringBootApplication
public class ConfigClient3355 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClient3355.class,args);
    }
}

5、controller

@RestController
@RefreshScope
public class ClientController {
    //读取配置文件的数据
    @Value("${config.info}")
    private String configInfo;

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

}

6、测试

 四、Config客户端的动态刷新

1、到入actuator监控

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

2、修改yml,暴露监控端口

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

3、修改controller

添加@RefreshScope注解

@RestController
@RefreshScope
public class ClientController {
    //读取配置文件的数据
    @Value("${config.info}")
    private String configInfo;

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

}

4、测试

  • 需要向客户端发送post请求动态刷新才能生效

curl -X POST "http://localhost:3355/actuator/refresh"

五、SpringCloudBus

什么是总线
在微服务架构的系统中,通常会使用轻量级的消息代理来构建一个共用的消息主题,并让系统中所有微服务实例都连接上来。由于该主题中产生的消息会被所有实例监听和消费,所以称它为消息总线。在总线上的各个实例,都可以方便地广播一些需要让其他连接在该主题上的实例都知道的消息。
基本原理
ConfigClient实例都监听MQ中同一个topic(默认是springCloudBus)。当一个服务刷新数据的时候,它会把这个信息放入到Topic中,这样其它监听同一Topic的服务就能得到通知,然后去更新自身的配置。

1、概述

Spring Cloud Bus 配合 Spring Clou

Spring Cloud Bus是用来将分布式系统的节点与轻量级消息系统链接起来的框架,
它整合了Java的事件处理机制和消息中间件的功能。
Spring Clud Bus目前支持RabbitMQ和Kafka。

  • 作用

 

Spring Cloud Bus能管理和传播分布式系统间的消息,就像一个分布式执行器,可用于广播状态更改、事件推送等,也可以当作微服务间的通信通道。

2、SpringCloud Bus动态刷新全局广播

需先配置Rabbitmq(不做说明)

1、根据3355的模板在配置一个3366,只需改一下yml的port和主启动类

2、设计思想

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

3、给cloud-config-center-3344配置中心服务端添加消息总线支持

  • 添加以下pom坐标
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
  • 更改yml
server:
  port: 3344
spring:
  application:
    name: config-center
  cloud:
    config:
      server:
        git:
          #仓库地址
          uri: https://github.com/xzjyy/springcloud-config.git
          search-paths:
            - springcloud-config  #要扫描的文件
          username: xzjyy #github的用户名
          #gitHub的令牌
          password: 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
      label: master #分支
     #rabbitmq的配置
  rabbitmq:
    host: 192.168.195.132
    port: 5672
    username: xiaojiang
    password: 123
##rabbitmq相关配置,暴露bus刷新配置的端点
management:
  endpoints: #暴露bus刷新配置的端点
    web:
      exposure:
        include: 'bus-refresh'
eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。
    #单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetch-registry: true

    service-url:
      defaultZone: http://eureka7002.com:7002/eureka/

4、给cloud-config-client-3355客户端添加消息总线支持 

  • pom
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
  • yml
server:
  port: 3355
spring:
  application:
    name: config-client
  cloud:
    config:
      #分支名称
      label: master
      name: config  #配置文件名称
      profile: dev  #读取后缀名称
      uri: http://localhost:3344 #配置中心地址k
      #上述3个综合:master分支上config-dev.yml的配置文件被读取
      #http://config-3344.com:3344/master/config-dev.yml
    #rabbitmq的配置
  rabbitmq:
    host: 192.168.195.132
    port: 5672
    username: xiaojiang
    password: 123
eureka:
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://eureka7002.com:7002/eureka/

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

6、给cloud-config-client-3366客户端添加消息总线支持

添加的配置与3355一致

7、测试

在github中改变配置config-dev.yml文件

curl -X POST "http://localhost:3344/actuator/bus-refresh"

3、定点通知

不想全部通知,只想定点通知

用法

公式:http://localhost:配置中心的端口号/actuator/bus-refresh/{destination}

/bus/refresh请求不再发送到具体的服务实例上,而是发给config server并
通过destination参数类指定需要更新配置的服务或实例

示列

我们这里以刷新运行在3355端口上的config-client为例

curl -X POST "http://localhost:3344/actuator/bus-refresh/config-client:3355"

4、总结

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值