为什么使用配置中心
每个微服务都有各种环境的配置,例如开发、测试、预生产、生产环境,通过配置中心可以集中进行管理与维护,同时还可以保证配置信息的安全性。
配置中心的特点
Spring Cloud Config分为服务端与客户端两部分:
- 服务端:也称为分布式配置中心,是一个独立的微服务应用,用来连接配置仓库并为客户端提供获取配置信息的接口。
- 客户端:是微服务架构中各微服务应用,启动时先从配置中心加载配置信息。
config支持的存储方式
- Git
- FileSystem
- Vault
- JDBC
- Redis
- Aws S3
- CredHub
本文搭建基于数据库存储的config配置中心
1、config server 服务端搭建
Application启动类增加注解 @EnableConfigServer
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerDemoApplication.class, args);
}
}
pom引入maven依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/>
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--配置中心server依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>