1. 程式人生 > 實用技巧 >SpringCloud配置中心本地多檔案配置

SpringCloud配置中心本地多檔案配置

本地配置方式:

1.配置中心啟動類必須要有@EnableConfigServer註解

2.本地配置方式包括本地類路徑和檔案系統兩種方式

首先設定spring.profiles.active為native
然後spring.cloud.config.server.native.searchLocations 指向目標資料夾
這樣Config Server啟動時,
才能載入到對應的配置檔案。

2.1檔案系統方式

需要注意的是,當本地配置有多個資料夾時,file需要指定多個目錄,

例:當configFile是根目錄時,本地若同git方式一樣指向該目錄,會有問題,

用http地址訪問時,需要加上子級目錄

+yml檔案 例:http://localhost:9999/config/answer-service/answer-service-dev.yml

所以若要跟git方式一樣,則採用多個檔案配置方式。以英文 , 逗號隔開

search-locations: file:H:\lianyou\project\honda-shop\configFile\,file:H:\lianyou\project\honda-shop\configFile\eureka\

server:
  port: 9090
  servlet:
    context-path: /config
spring:
  application:
    name: config
-server profiles: active: native cloud: config: server: native: search-locations: file:H:\lianyou\project\honda-shop\configFile\,file:H:\lianyou\project\honda-shop\configFile\eureka\,file:H:\lianyou\project\honda-shop\configFile\gateway\,file:H:\lianyou\project\honda-shop\configFile\wechat-service\,file:H:\lianyou\project\honda-shop\configFile\datebase\,file:H:\lianyou\project\honda-shop\configFile\redis\,file:H:\lianyou\project\honda-shop\configFile\system\

2.2類路徑方式

通過在search-locations中指定classpath下的路徑:

類路徑也可同文件方式配置

spring:
  application:
    name: config-server
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/config

3.啟動好後,其他微服務uri指向配置中心

spring:
  cloud:
    config:
    label: develop
    uri: http://shop-config.default.svc.cluster.local:9090/config
    name: wechat-service,eureka-client,redis,datebase-shop,common,feign
   profile: test

name:為yml檔名前部分,

profile:dev test pro 等等,為yml檔案後部分

例:answer-service-dev.yml

配置好後先啟動配置中心config-service,再啟動其他微服務wechat-service即可檢視,

也可只需啟動配置中心,http訪問對應路徑也可檢視