spring cloud之配置中心

Config 统一配置中心(*)

1.简介
# 统一配置中心
- 官网:https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.3.RELEASE/reference/html/#_spring_cloud_config_server

- config 分为 config server 和 config client。用来统一管理所有微服务的配置
  • 统一配置中心流程图
    在这里插入图片描述
2.config server 开发
  • 引入依赖
<!-- config server -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</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>
<!-- consul 注册中心 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
  • 配置文件
server.port=8555
# 服务名
spring.application.name=CONFIG-SERVER

# consul 注册中心地址
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500

# gitee 仓库地址
spring.cloud.config.server.git.uri=https://gitee.com/lwwby/config-center.git
# 指定默认拉取配置信息的分支名
spring.cloud.config.server.default-label=master
# 如果仓库是私有的,需要配置用户名和密码
#spring.cloud.config.username=
#spring.cloud.config.password=
  • 启动类添加注解@EnableConfigServer
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigServerApplication {

}
3.config client 开发

config client也就是一个个具有实际业务含义的微服务,例如order服务,user服务

  • 引入依赖
 <!-- config client -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-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>
<!-- consul 注册中心 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
  • 以gitee作为仓库,在gitee上新建仓库config-center

分别新建 configclient.properties、 configclient-dev.properties、 configclient-test.properties。

在实际开发中,建议以服务名命名,例如order-dev.properties/order-dev.yml
在这里插入图片描述
在这里插入图片描述

  • 项目中配置文件需要命名为bootstrap.properties/bootstrap.yml,容器启动时bootstrap.properties/bootstrap.yml命名会比application.properties/application.yml命名优先加载(先要去配置中心拉去配置到本地缓存)。

    bootstrap.properties 项目本地配置

    # 开启配置发现
    spring.cloud.config.discovery.enabled=true
    # 指定配置中心服务ID,去注册中心获取
    spring.cloud.config.discovery.service-id=CONFIG-SERVER
    
    # consul 注册中心地址
    spring.cloud.consul.host=localhost
    spring.cloud.consul.port=8500
    
    # 指定拉取配置文件的分支
    spring.cloud.config.label=master
    # 指定拉取配置文件的名称
    spring.cloud.config.name=configclient
    # 指定拉取配置文件的环境
    spring.cloud.config.profile=test
    

    测试代码

    @RestController
    public class ConfigController {
    
        @Value("${name}")
        private String name;
    
        @GetMapping("/demo")
        public String demo() {
            return name;
        }
    }
    

    如果命名为application.properties/application.yml,config client启动会报错,ConfigController无法注入name属性(配置文件信息还未加载到本地)

  • 启动类,引入config-client依赖即可,无需加额外注解

@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientApplication {

}

config server启动后,可以通过地址获取配置信息,例如:http://localhost:8555/configclient-dev.properties
在这里插入图片描述

配置信息= configclient.properties(默认会获取到) + configclient-dev.properties

4.手动配置刷新
- 当远端git仓库中配置发生变化时,不需要重启微服务,就可以读取到修改之后的最新配置信息
  • 1.在需要刷新的类上添加注解@RefreshScope
// 添加注解@RefreshScope,仅作用于当前类ConfigController的配置刷新
@RestController
@RefreshScope
public class ConfigController {

    @Value("${name}")
    private String name;

    @GetMapping("/demo")
    public String demo() {
        return name;
    }
}
  • 2.开启刷新端点
# 注意properties格式为*,yml格式为"*"
# /actuator/refresh端点默认是不开启的。"*"代表开启所有端点
management.endpoints.web.exposure.include=*
  • 3.修改完远端git仓库配置后,向需要刷新配置的微服务发送一个POST请求
- 使用post
http://localhost:8655/actuator/refresh

- 命令行终端
curl -X POST http://localhost:8655/actuator/refresh

在这里插入图片描述

5.Bus组件
- 官方文档: https://spring.io/projects/spring-cloud-bus

- spring cloud bus使用轻量级消息代理将分布式系统的节点连接起来。然后可以使用它来广播状态更改(例如配置更改)或其他管理命令。AMQP和kafka broker(中间件)实现包含在项目中。或者,在类路径上找到任何spring cloud stream绑定器都可以作为传输使用。

- bus称之为spring cloud中消息总线,主要用来在微服务系统中实现远端配置更新时,通过广播形式通知所有客户端刷新配置信息,避免手动重启服务来刷新配置
  • 实现配置刷新原理
    在这里插入图片描述

  • 安装RabbitMQ(todo)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

coo_lw

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值