1. 程式人生 > 其它 >動態獲取github中的配置檔案

動態獲取github中的配置檔案

技術標籤:SpringBoot微服務

首先我們先在github上建立兩個測試用的配置檔案:

  • config-consumer-dev.yml
info:
  profile: dev 
  
name: Saul
words: 'God bless me'
  • config-consumer-prod.yml
info:
  profile: prod 
  
name: Paul
words: 'God bless you'

然後修改專案程式碼

  • pom
<dependencies>
    <dependency>
        <groupId>
org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies>
  • 啟動類application
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

    public static void main(String[] args) {
        new
SpringApplicationBuilder(ConfigServerApplication.class) .web(WebApplicationType.SERVLET) .run(args); } }
  • 配置檔案
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/xxx/project.git
          force-pull
: true # 非公開專案需要填入username和password # username: # password: server: port: 60000

然後啟動專案,訪問http://localhost:60000/config-consumer/dev

這裡請求地址為http://localhost:60000/{application}/{profile}/{label},如果不指定{label}的話預設用master

在這裡插入圖片描述
可以看到source欄位內返回了我們的配置資訊

還有一種請求方式為:http://localhost:60000/{label}/{application}-{profile}.yml

它會直接以.yml的格式返回資料
在這裡插入圖片描述
我們也可以將.yml修改為.properties.json,都會返回對應的格式
在這裡插入圖片描述