一、導入依賴git
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
二、使用@EnableConfigServer開啓配置中心服務端支持web
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); } }
三、建立git倉庫項目並上傳配置文件
spring
四、在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 #false\u7981\u7528Consul\u914d\u7f6e\uff0c\u9ed8\u8ba4true format: YAML # \u8868\u793aconsul\u4e0a\u9762\u6587\u4ef6\u7684\u683c\u5f0f \u6709\u56db\u79cd YAML PROPERTIES KEY-VALUE FILES #data-key: configuration #\u8868\u793aconsul\u4e0a\u9762\u7684KEY\u503c(\u6216\u8005\u8bf4\u6587\u4ef6\u7684\u540d\u5b57) \u9ed8\u8ba4\u662fdata data-key: data #\u8868\u793aconsul\u4e0a\u9762\u7684KEY\u503c(\u6216\u8005\u8bf4\u6587\u4ef6\u7684\u540d\u5b57) \u9ed8\u8ba4\u662fdata #prefix\u8bbe\u7f6e\u914d\u7f6e\u503c\u7684\u57fa\u672c\u6587\u4ef6\u5939 #defaultContext\u8bbe\u7f6e\u6240\u6709\u5e94\u7528\u7a0b\u5e8f\u4f7f\u7528\u7684\u6587\u4ef6\u5939\u540d\u79f0 #profileSeparator\u8bbe\u7f6e\u7528\u4e8e\u4f7f\u7528\u914d\u7f6e\u6587\u4ef6\u5728\u5c5e\u6027\u6e90\u4e2d\u5206\u9694\u914d\u7f6e\u6587\u4ef6\u540d\u79f0\u7684\u5206\u9694\u7b26\u7684\u503c 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服務。app
五、瀏覽器上訪問配置文件
http://localhost:6063/application-prod.properties
ui