|
在微服务架构中,动则成百上千个微服务,所以如果微服务中配置一旦修改了,则需要成百上千个微服务需要去修改配置文件,重启应用,比较麻烦,所以需要一个共享配置服务来管理微服务的配置文件,引入出了springcloud config这个概念

config server的资源中心可以用git,snv等管理,接下来是根据git来讲的
1、创建git仓库,存放配置文件
首先,我在github上创建一个springCloudConfigTest一个仓库,git路径为:
git@github.com:beiduofen2011/springCloudConfigTest.git
里面创建了一个application.yml文件(也可以创建properties文件,注意yml文件跟properties文件的区别),文件内容为:
spring:
profiles:
active:
- dev
---
spring:
profiles: dev
application:
name: mymicroconfig
---
spring:
profiles: pro
application:
name: mymicroconfig
---
spring:
profiles: test
application:
name: mymicroconfig
1、spring.profiles.active:dev 表示的是默认环境的配置,当然还有default这个配置
2、每个环境配置项是根据 '---' 这个分隔符隔离开的(对,你没看错,刚开始我也觉得不可思议)
3、在每个环境的配置项中可以自定义配置(不一定都是spring开头的配置)
如果用properties就要创建三个文件:application-pro.properties,application-dev.properties,application-test.properties三个文件
2、配置本机ssh到远程的git仓库(这个我就跳过不讲了,一定要配置)
3、创建config server程序
我这里是基于springboot,用maven管理的话,在pom.xml加入以下依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
启动程序中添加 @EnableConfigServer注解:
@SpringBootApplication
@EnableConfigServer
public class SpringCloudStuApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudStuApplication.class, args);
}
}
application.yml配置文件配置git仓库地址:
server:
port: 7101
spring:
application:
name: microcloud-test
cloud:
config:
server:
git:
uri: git@github.com:beiduofen2011/springCloudConfigTest.git
这里要注意的事,之前我一直用https的git地址,一直报错,请求不了,可能要添加认证信息,用了git地址及配置ssh之后,就不会了,我也不太懂,还请的懂的同学请留言哈
4、启动程序并且 请求服务
启动程序后,请求服务有以下规则:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
1、application: 表示微服务名称,即配置的spring.application.name
2、profile: 表示当前的环境,local、feature、dev、test、prod
3、label: 表示git仓库分支,feature、develop、test、master,当然默认的话是master
但是据我测试,文件名称也可以:这里省略
比如上述:
/{application}/{profile}[/{label}]格式的请求为:
http://localhost:7101/mymicroconfig/pro
http://localhost:7101/mymicroconfig/dev
http://localhost:7101/mymicroconfig/test
带label-----------------------------------------------------
http://localhost:7101/mymicroconfig/pro/master
http://localhost:7101/mymicroconfig/dev/master
http://localhost:7101/mymicroconfig/test/master
返回结果:
{"name":"mymicroconfig","profiles":["pro"],"label":"master","version":"3be24ff69c078bc78a2fbe6426d3b2f244959154","state":null,"propertySources":[{"name":"git@github.com:beiduofen2011/springCloudConfigTest.git/application.yml (document #2)","source":{"spring.profiles":"pro","spring.application.name":"microconfig-test-pro","spring.datasource.url":"jdbc:pro","spring.datasource.username":"pro","spring.datasource.password":123456}},{"name":"git@github.com:beiduofen2011/springCloudConfigTest.git/application.yml (document #0)","source":{"spring.profiles.active[0]":"dev"}}]}
/{application}-{profile}.yml格式的请求为:
http://localhost:7101/mymicroconfig-pro.yml
http://localhost:7101/mymicroconfig-dev.yml
http://localhost:7101/mymicroconfig-test.yml
返回结果为:
spring:
application:
name: microconfig-test-pro
datasource:
password: 123456
url: jdbc:pro
username: pro
profiles:
active:
- dev
可见两种方式返回结果的格式不太一样 |