微服務系列文章導航
源碼地址
工程說明
- 特點: spring cloud config 是將配置文件動態進行管理的工程
- 角色:spring cloud config server 、 spring cloud config client
- 角色功能:
- spring cloud config server :微服務配置中心,關聯git項目上的配置文件,對配置文件進行統一管理
- spring cloud config client:微服務配置客戶端,從微服務配置中心獲取配置文件信息,并且配置中心的配置一經改變,用post請求
/refresh
方法,則會動態刷新當前工程的配置信息
工程搭建
1、添加pom依賴
<!-- 導入spring config依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- 加入該依賴,使用可以使用refresh動態刷新配置文件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2、添加配置
server:
port: 50200
spring:
# 解決中文亂碼
http:
encoding:
charset: UTF-8
enabled: true
force: true
application:
name: govern-config
cloud:
config:
server:
git:
uri: https://gitee.com/tanxingsong/micro-service-config.git
# 根據項目名去尋找對應文件夾下的配置文件
searchPaths: '{application}'
eureka:
client:
service-url:
defaultZone: http://localhost:50101/eureka/
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
appname: govern-config
3、添加啟動注解類
@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
4、啟動測試(暫時只進行url請求測試)
訪問Eureka注冊中心,出現紅色方框內的服務,則說明啟動成功:
image.png
測試地址: http://localhost:50200/application-test.yml
返回以下內容,則說明配置成功:
image.png