是什么
Spring Cloud Config为分布式系统中的外部化配置提供服务器和客户端支持。使用Config Server,您可以集中管理所有环境中应用程序的外部属性
干嘛用
统一管理各个微服务的配置文件
怎么用
引入jar
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
在程序的入口Application类加上@EnableConfigServer注解开启配置服务器。
@SpringBootApplication
@EnableConfigServer
public class ConfigServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServiceApplication.class, args);
}
}
application.yml 配置
server:
port: 7000
spring:
application:
name: config-servie
cloud:
config:
server:
git:
uri: https://git.oschina.net/jamen/blife-config-centor.git
search-paths: blife-config-centor
在git中放 yml文件
config-test-dev.yml
spring:
application:
name: config-test
启动config-service
请求
http://10.10.8.101:7000/config-test/dev
返回
{"name":"config-test","profiles":["dev"],"label":null,"version":null,"state":null,"propertySources":[{"name":"https://git.oschina.net/jamen/blife-config-centor.git/config-test-dev.yml","source":{"spring.application.name":"config-test"}}]}
Dockerfile
FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD config-service-1.0-SNAPSHOT.jar app.jar
RUN sh -c 'touch /app.jar'
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
EXPOSE 7000
config 客户端的使用
引入jar
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
boostrapt.yml
spring:
cloud:
config:
allow-override: false
#label: master
profile: dev
uri: http://10.10.8.101:7000 #配置中心地址