【SpringCloud】Config配置中心
一、介绍二、使用
2.1 创建远程仓库2.2 创建Config Server项目2.3 创建Config Client项目2.4 加载远程配置文件中的自定义配置信息 三、配置动态更新
【SpringCloud】Config配置中心 一、介绍Spring Cloud Config项目是一个解决分布式系统的配置管理方案。它包含了Client和Server两个部分,server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client通过接口获取数据、并依据此数据初始化自己的应用。
在Client项目中,因为需要使用Server项目中的信息,并远程加载配置文件,将其下载到本地,所以我们的配置信息不能写在application.properties中,因为这个时候无法确保是配置文件的加载
此时引入一个新的文件:bootstrap.properties
bootstrap.properties比application.properties的优先级要高;
bootstrap.properties是系统级的资源配置文件,是用在程序引导执行时更加早期配置信息读取;
application.properties是用户级的资源配置文件,是用来后续的一些配置所需要的公共参数。
但是,在SpringCloud 2020.* 版本把bootstrap禁用了,导致在读取文件的时候读取不到而报错,所以我们只要把bootstrap从新导入进来就会生效了。
二、使用 2.1 创建远程仓库org.springframework.cloud spring-cloud-starter-bootstrap
创建远程仓库中的配置文件
2.2 创建Config Server项目前提
需要一个eureka注册中心,将Config Server注册到注册中心
创建项目
配置文件
#项目名 spring.application.name=spring-cloud-config-service #erueka默认端口号:8761 server.port=8085 # 使用注册中心的地址 eureka.client.service-url.defaultZone=http://localhost:8761/eureka/ # 远程仓库的地址,建议使用gitee,github容易出现连接问题 spring.cloud.config.server.git.uri= # 远程仓库的用户名 spring.cloud.config.server.git.username= # 远程仓库的密码 spring.cloud.config.server.git.password=
启动类增加@EnableConfigServer注解
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@SpringBootApplication
public class SpringCloudConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudConfigServerApplication.class, args);
}
}
测试
访问地址:localhost:8085/master/spring-cloud-eureka-provider-a.properties
前提
需要一个eureka注册中心,将Config Client注册到注册中心,并且从注册中心获取Config Server的信息,并且获取配置信息
创建项目
依赖信息
4.0.0 org.springframework.boot spring-boot-starter-parent 2.6.4 com.example SpringCloudOrderProvider 0.0.1-SNAPSHOT SpringCloudOrderProvider Demo project for Spring Boot 1.8 2021.0.1 org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-config org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-starter-bootstrap org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin
配置文件
bootstrap.properties
spring.application.name= spring-cloud-eureka-provider # 允许发现配置中心 spring.cloud.config.discovery.enabled=true # 配置中心的service id spring.cloud.config.discovery.service-id=spring-cloud-config-service # 使用的是注册中心的地址 eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
在Server附中中的远程仓库中,Client会默认查找配置文件名和当前项目名一致的配置文件,并加载到本地,然后启动项目
错误提示
此时可能是忘记添加bootstrap依赖,参考第一部分的介绍处理
启动效果
此时在application配置文件中并没有添加相关的端口号,启动之后的端口号是远程的端口号
前提:在配置文件中编写自定义配置内容
创建Controller类
package com.example.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderController {
//加载端口号
@Value("${server.port}")
private String port;
@GetMapping("get")
public String getPort(){
return port;
}
}
访问controller地址:
三、配置动态更新在第二部分中的配置文件可以实现加载,但是如果远程仓库修改了配置文件,已经启动的项目无法更新,如何实现配置文件的动态更新,是需要解决的问题
// ToDo



