spring cloud 读取配置中心
一、配置中心服务:
- pom文件添加依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
- yml配置文件:
spring:
application:
name: config-center
cloud:
config:
server:
git:
uri: http://10.0.0.000:10000/root/config.git ## 配置中心的git地址,存放各种配置文件
username: root ## 该git项目的账户
password: 123456 ## 该git项目的密码
profiles:
active: pro
server:
port: 1111 ##配置中心的端口
- 启动类添加注解EnableConfigServer,表明该服务是配置中心:
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(ConfigCenterApplication.class);
}
}
二、客户端服务
1、pom文件添加依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
2、yml配置文件:`
spring:
application:
name: demo ##客户端服务名称
cloud:
config:
label: master ## git 分支
enabled: true ## 使用配置中心
discovery: ## 配置发现配置
service-id: CONFIG-CENTER ## 配置中心服务名称
enabled: true ## 从配置中心读取文件
fail-fast: true
retry:
initial-interval: 2000
profile: pro
三、读取文件的路径(均为get请求)
http://10.0.0.000:10000/root/config.git 设该git项目的文件目录结构为:
+++ clientJson文件夹
++++++ innercase.json
+++ outcasse.json
+++ demo-pro.properties (spring.applicaton.name + “-”+spring.cloud.config.profile)
1、读取properties文件:
http://配置中心的ip:配置中心的port/spring.application.name的值/spring.cloud.config.profile的值
以上述代码为例: http://10.0.0.000:1111/demo/pro
2、读取json文件:
http://配置中心的ip:配置中心的port/任意字符串/任意字符串/分支/文件名
以上述代码为例: http://10.0.0.000:1111/files/string/master/outcase.json
3、读取clientJson文件夹下面的case.json文件:
http://配置中心的ip:配置中心的port/任意字符串/任意字符串/分支/文件夹名称/文件名
以上述代码为例: http://10.0.0.000:1111/files/string/master/clientJson/innercase.json