Spring Cloud Config為分佈式系統中的外部化配置提供服務器端和客戶端支持。 使用config組件只需要以下幾步:
1. 引入依賴
在springboot項目pom文件中添加如下依賴
<code>
<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>
/<code>
2. 配置application.yml文件
<code># 配置服務端口
server:
port: 20010
# 配置註冊中心參數 (如果是使用eureka作為註冊中心管理項目,不是不需要引入)
eureka:
port: 8761
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${eureka.port}/eureka/
# 配置服務名稱
spring:
application:
name: config
# 配置git倉庫地址 賬號密碼(這裡使用碼雲平臺作為實驗)
cloud:
config:
server:
git:
username: test
password: test
uri: https://gitee.com/test/study-project-repertory
/<code>
3. 在項目啟動類上添加註解
<code>@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient // 如果是使用eureka作為註冊中心管理項目,不是不需要引入
public class ApesConfigServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ApesConfigServiceApplication.class, args);
}
}
/<code>
4. 測試訪問遠程倉庫中的配置文件
- 啟動項目
- 遠程倉庫存放配置文件eureka-service-dev.yml
<code>server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
/<code>
- 打開瀏覽器地址欄輸入:http://localhost:20010
閱讀更多 高集攻城獅 的文章