Spring Cloud Config配置中心

本文介绍了配置中心,其用于集中管理不同环境和集群配置并实时更新。阐述了配置中心具备的功能,如Open API、配置生效监控等。详细说明了Config配合git的工作原理,还给出了服务端和客户端的依赖、配置及测试代码示例,最后介绍了手动刷新配置的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

配置中心

配置中心的由来

配置中心被用作集中管理不同环境和不同集群配置,以及在修改配置之后能将修改的配置即使的更新到系统。

配置中心具备的功能

  • Open API
  • 业务无关
  • 配置生效监控
  • 一致性K-V存储
  • 统一配置实时推送
  • 配合灰度与更新
  • 配置全局恢复,备份与历史
  • 高可用集群

Config配合git的工作原理

配置的客户端在启动的时候活像配置服务端发请求,服务端在接收到客户端的请求之后会在之前配置好的地址去git残酷拉去一份配置到本地,创建一个临时文件,这个目录就是一个git的本地仓目录,然后服务端读取本地文件返回给客户端。这样做的好处就是,当git服务器故障或者网路出现异常后仍然可以工作。

例子

服务端和客户端公共的依赖如下:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
复制代码

server端的依赖如下:

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
复制代码

服务端的配置:

spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/chendom/SpringCloudConfig.git
          username: xx
          password: xx
          search-paths: config
  application:
    name: config-server
server:
  port: 8098
复制代码

启动类:

@SpringBootApplication
@EnableConfigServer
public class ConfigGitApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigGitApplication.class,args);
    }
}
复制代码

客户端的依赖如下:

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-client</artifactId>
    </dependency>
复制代码

客户端配置如下:

server:
  port: 8099
spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      uri: http://localhost:8098
      name: client-config
      profile: dev
复制代码

测试类的代码

@RestController
@RequestMapping("/demo")
public class DemoController {

@Value("${springcloud.configserver}")
 private String value;

@RequestMapping("/getValue")
public String getValue(){
    return value;
}
}
复制代码

分别启动服务端和客户端

访问http://localhost:8098/client-config/dev/master

访问http://localhost:8099/demo/getValue

手动刷新

pom依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-client</artifactId>
    </dependency>
    <!--简单的安全和端点开放-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
</dependencies>
</project>
复制代码

bootstrap.yml配置文件

server:
  port: 8100
spring:
  application:
    name: refresh-config-client
  cloud:
    config:
      label: master
      uri: http://localhost:8098
      name: client-config
      profile: dev
management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always
复制代码

请求

@RestController
@RequestMapping("/demo")
@RefreshScope//刷新注解
public class DemoController {

@Value("${springcloud.configserver}")
 private String value;

@RequestMapping("/getValue")
public String getValue(){
    return value;
}
}
复制代码

我们知道之前的配置是这样的 springcloud: configserver: this is test dev

现在该成如下图所示的样子:

此时如果没有刷新还是访问http://localhost:8100/demo/getValue还是不会有变化

此时我们可以访问 Post:http://localhost:8100/actuator/refresh

访问 http://localhost:8100/demo/getValue

以上就完成了手动刷新的例子,但是微服务一般服务都很多,如果有个别的例子没有进行刷新就很可能出现错误,开锁所以就需要使用到自动刷新

转载于:https://juejin.im/post/5ce4a68ef265da1b833362e0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值