Spring Cloud Config

分布式配置中心组件Config

  • Spring Cloud Config 构建

创建spring-cloud-06-config-server 项目导入pom.xml依赖

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
 </dependency>
  • 在主入口Application类中加入@@EnableConfigServer注解
  • 在application.properties中配置
spring.application.name=config-server
server.port=4001
#指远程git仓库地址
spring.cloud.config.server.git.uri=https://github.com/yexiaofei123/Learning
# 相对路径 对应 Learning下得子目录
#spring.cloud.config.server.git.search-paths=

# 指定本地仓库地址
spring.cloud.config.server.git.basedir=/data/temp
  • 远程Git仓库:用来存储配置文件得地方
  • config.server:构建分布式配置中心,指定了要连接得Git仓库位置等连接信息
  • 本地Git仓库:在Config Server中,每次客户端请求获取配置信息时,Config Server从Git仓库中获取最新配置到本地,然后在本地Git仓库中读取并返回,当远程仓库无法获取时,直接将本地内容返回

创建spring-cloud-06-config-client 项目导入pom.xml依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId> spring-boot-starter-actuator</artifactId>
</dependency>
  • 在bootstrap.properties中配置
spring.application.name=temp
server.port=5500
#配置中心地址
spring.cloud.config.uri=http://localhost:4001
spring.cloud.config.profile=dev
#不写默认是master
spring.cloud.config.label=master

#验证关掉
management.security.enabled=false
  • 切记client中spring.application.name的名称要和远程Git中得文件一致 我远程是叫temp-dev.prrperties
  • 创建请求类
@RefreshScope
@RestController
public class ConfigC {

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

    @RequestMapping(value = "/from")
    public String from(){
        System.out.println("from"+from);
        return this.from;
    }
}

  • client端请求时是默认需要安全保护得 不然http://localhost:5500/refresh刷新刷新不到 如不需要management.security.enabled=false关掉
  • 安全保护可以通过Spring Security来实现
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
</dependency>
默认会生成一个名为User得用户,会生成一个随机密码
在server端配置
security.user.name=user
security.user.password=123456
在client端配置
spring.cloud.config.username=user
spring.cloud.config.password=123456
这样client端启动得时候需要输入username和password
  • 服务化配置 将Config Server 注册到服务中心,并通过服务发现来访问Config Server并获取Git仓库中得配置信息
  • 在Server端和Client端加入
  <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
  </dependency>
  #注册到服务中心
  eureka.client.service-url.defaultZone=http://eureka1:8008/eureka/
  • 更改Client端 bootstrap.properties中内容
#开启服务
spring.cloud.config.discovery.enabled=true
#指定Config Server注册得服务名
spring.cloud.config.discovery.service-id=config-server
spring.cloud.config.profile=dev
spring.cloud.config.label=master
  • 借用spring-cloud-05-gateway-service项目 启动服务service端
  • 启动Server client端访问http://localhost:5500/from 就可以访问到

失败快速响应与重试

当Config Service没启动时,Client端连接时如果没有配置spring.cloud.config.fail-fast=true 会加载很多内容,配置后加载得内容会少很多,会快速报错
Config提供了自动重试功能,在pom.xml中配置
<dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>
#失败快速响应 设置为true时,当config-server没启用时会很快报错
spring.cloud.config.fail-fast=true
#初始重试间隔时间 默认为1000毫秒
spring.cloud.config.retry.multiplier=1000
#下一间隔得乘数默认为1.1 最初间隔为1000 下次失败后间隔为1100
spring.cloud.config.retry.initial-interval=1.1
#最大间隔时间 默认为2000
spring.cloud.config.retry.max-interval=2000
#最大重试次数  默认为6次
spring.cloud.config.retry.max-attempts=6
  • 有个问题,当Git提交变化时,就给对应得配置主机发送/refresh请求来实现配置信息得实时更新。但是,当系统变大时,维护刷新就麻烦了。后续开始Spring Cloud Bus实现消息总线方式进行配置变更通知

  • GitHub地址

  • 下节Spring Cloud Bus

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值