spring cloud config 讀取配置中心

spring cloud 讀取配置中心

一、配置中心服務:

  1. pom文件添加依賴:
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
  1. 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 	##配置中心的端口
  1. 啓動類添加註解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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章