1、导入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
2、使用@EnableConfigServer开启配置中心服务端支持
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer //开启配置中心服务端支持
public class ConfigServerApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(ConfigServerApplication.class).web(true).run(args);
}
}
3、创建git仓库项目并上传配置文件
4、在application.yml文件中配置git远程仓库信息
security:
basic:
enabled: false
spring:
application:
name: mima-cloud-config-server
cloud:
consul:
discovery:
instanceId: ${spring.application.name}:${server.port}
host: localhost
port: 8500
config:
enabled: true #falseu7981u7528Consulu914du7f6euff0cu9ed8u8ba4true
format: YAML # u8868u793aconsulu4e0au9762u6587u4ef6u7684u683cu5f0f u6709u56dbu79cd YAML PROPERTIES KEY-VALUE FILES
#data-key: configuration #u8868u793aconsulu4e0au9762u7684KEYu503c(u6216u8005u8bf4u6587u4ef6u7684u540du5b57) u9ed8u8ba4u662fdata
data-key: data #u8868u793aconsulu4e0au9762u7684KEYu503c(u6216u8005u8bf4u6587u4ef6u7684u540du5b57) u9ed8u8ba4u662fdata
#prefixu8bbeu7f6eu914du7f6eu503cu7684u57fau672cu6587u4ef6u5939
#defaultContextu8bbeu7f6eu6240u6709u5e94u7528u7a0bu5e8fu4f7fu7528u7684u6587u4ef6u5939u540du79f0
#profileSeparatoru8bbeu7f6eu7528u4e8eu4f7fu7528u914du7f6eu6587u4ef6u5728u5c5eu6027u6e90u4e2du5206u9694u914du7f6eu6587u4ef6u540du79f0u7684u5206u9694u7b26u7684u503c
config:
server:
git:
#git远程地址
uri: https://gitee.com/lynch168/config-file
#搜索目录
search-paths: /**
#git默认分支
default-label: master
#如果为私有仓库,需要设置用户名、密码
#username:
#password:
#是否开启配置中心
enabled: true
#默认的分支
label: master
server:
port: 6063
启动config-server服务。
5、浏览器上访问配置文件
http://localhost:6063/application-prod.properties