隨着服務的擴展,配置文件也越來越多,這時候就需要一個集中修改配置文件的地方來管理配置文件
在自己的git上新建個項目用來測試
https://github.com/chill-zx/springcloud-config.git
新建三個yml文件用於測試:
在本地創建一個文件夾用於存放clone下來的yml文件
新建module:
添加pom
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.zx.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
編寫yml
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: https://github.com/chill-zx/springcloud-config.git #填寫你自己的github路徑
search-paths:
- springcloud-config
label: master
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka
編寫主啓動類
修改hosts文件
在dev的環境添加信息用於測試並上傳git:
啓動服務並訪問:
可以看到是能夠讀取到的
config分爲服務端和客服端,測試的3344就是服務端
新建一個客戶端3355:
添加pom依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>com.zx.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
config的客戶端用的是bootstrap.yml
application.yml是用戶級的系統資源
bootstrap.yml是系統級的,優先級更高
配置bootstrap.yml
server:
port: 3355
spring:
application:
name: config-client
cloud:
config:
label: master
name: config
profile: dev
uri: http://localhost:3344
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka
編寫主啓動類:
編寫controller並讀取配置:
啓動訪問:能看到已經讀取到了信息
動態刷新配置:
修改一下版本,把1.0改爲2.0
可以看到已經更改爲了2.0
訪問服務端:版本號改變了
再訪問3355客戶端發現沒有改變,還是1.0
1 要實現動態加載首先需要添加監控的依賴
2 修改yml,暴露監控的端口
在controller類添加一個刷新的註解
再從新訪問就是2.0,說明動態配置設置成功了
配置過後動態訪問還是不能夠成功,所以需要”激活一下“
再訪問: