future.png
珠玑随笔

联系:

future.png

版权 © 2022 by Hsu

技术: Gatsby

样式: Bulma

许可: CC BY NC SA 4.0

Spring Cloud 搭建 Config Server

2022年01月15日

Hsu

1分钟

背景

在 Spring Cloud 微服务架构中, Config Server 的作用是提供一个配置中心, 对所有微服务的配置集中管理, 减少重复工作.

Config Server

创建一个空项目, 引入以下依赖.

  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
  </dependency>

启动类添加 @EnableConfigServer

@SpringBootApplication
@EnableEurekaServer
public class ConfigServerApplication {

  public static void main(String[] args) {
    SpringApplication.run(ConfigServerApplication.class, args);
  }
}

配置

spring.application.name=config-server
server.port=8888

# 远程仓库
spring.cloud.config.server.git.uri: https://github.com/spring-cloud-samples/config-repo

# 本地文件夹
spring.profiles.active=native
spring.cloud.config.server.native.search-locations=file:///本地路径

在远程仓库或本地文件夹中,保存了 服务名-版本.yml 或 .properties 的文件, 他们的优先级高于服务内的配置文件.

Config Client

每个项目, 引入以下依赖.

  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
  </dependency>

配置

spring.config.import=optional:configserver:http://localhost:8888

注意

每当 Config Server 配置文件更改, Config Client 需要重启才能生效.

如果需要实时推送,我们需要配合 Spring Cloud Bus.

https://cloud.spring.io/spring-cloud-config/reference/html/#_push_notifications_and_spring_cloud_bus

https://spring.io/blog/2021/10/20/spring-tips-refreshable-configuration-with-spring-cloud-bus-and-the-spring-cloud-config-monitor

https://medium.com/swlh/spring-cloud-config-bus-auto-refresh-properties-for-clients-d18fa4c036cb

https://medium.com/trendyol-tech/automating-dynamic-config-management-with-spring-cloud-config-bus-a34e15397b15

https://www.cnblogs.com/wdzhz/p/10431038.html

https://blog.csdn.net/weixin_40703574/article/details/105972109

https://blog.csdn.net/qq_38423105/article/details/88080464

https://blog.csdn.net/yangliuhbhd/article/details/80526525

https://blog.csdn.net/qq_34083066/article/details/86517905

https://blog.csdn.net/a2267378/article/details/84334428

https://www.jianshu.com/p/2f566358b28a

参考

https://spring.io/guides/gs/centralized-configuration/

https://cloud.spring.io/spring-cloud-config/reference/html/

https://docs.spring.io/spring-cloud-bus/docs/current/reference/html/