1. 程式人生 > >系列6-springCloud微服務-config配置中心

系列6-springCloud微服務-config配置中心

config配置中心分為服務端和客戶端,服務端根據檔案儲存位置分為三種設定方式:git\githupSVN本地儲存其中git\githup,SVN的設定方式基本一樣。此處僅記錄git的設定,githup與git的設定完全一樣。1.config Server基於git步驟1:本文件預設為已經有git伺服器並且有建立文件的許可權。在(不限於)D盤的根目錄下新建一個資料夾:springCloudConfig,在springCloudConfig目錄下執行git命令:
git init
步驟2:通過步驟1已經將目錄設定為git可提交目錄。通過git管理員添加當前專案目錄的許可權配置,在此目錄下建立需要管理的配置檔案,application.yml(檔名稱不限,但是字尾一定是yml或者是properties)新增如下內容:
spring:
profiles: active: - dev---spring: profiles: dev application: name: springCloud-dev---spring: profiles: beta application: name: springCloud-beta
步驟3:通過git將檔案上傳至伺服器,命令如下或者通過git的視覺化工具將檔案提交到伺服器:
#將更新新增到本地暫存倉庫git add .#將本地暫存倉庫的檔案提交到本地庫git commit -m "add application.yml file"#同步推送到伺服器端git push origin master
步驟4:建立一個【springCloud-config-git】的專案作為config Server的git讀取專案。修改pom檔案,新增對config Server的依賴引用。
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
步驟5:修改【springCloud-config-git】配置檔案application.yml新增如下內容:
server:
port: 8300spring: application: name: springCloud-config #配置中心的應用名稱 cloud: config: server: git: uri: [email protected]:springCloudConfig.git #遠端git的地址 username: yourname #遠端git認證的賬號 password: yourpass #遠端git認證的密碼 (此處在我的電腦上已經進行過ssh配置,所有我並未設定)
步驟6:修改【springCloud-config-git】新增啟動類,在啟動類上新增@EnableConfigServer註解
@SpringBootApplication@EnableConfigServerpublic class ConfigApplication { public static void main(String[] args) { SpringApplication.run(ConfigApplication.class, args); }}
步驟7:啟動配置中心服務端,進行測試:此處需要額外記錄一下,config讀取配置檔案時的幾種路徑格式:
1./{application}/{profile}[/{lable}]2./{application}-{profile}.yml3./{lable}/{application}-{profile}.yml
解釋下這幾個變數:{application}:是在服務端設定的配置檔案的檔名字{profile}:指的是配置檔案中設定的profile的值{lable}:通常使用git或者是SVN才用到,所以是可選項,指代的是git中的分支名字2.config Server基於本地檔案步驟1:新建【springCloud-config-native】專案,修改pom檔案,新增如下依賴。
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
步驟2:修改【springCloud-config-native】,在src/main/resources路徑下新建config資料夾,新建配置檔案idaaCloud.yml新增如下內容:
spring: profiles: active: - dev---spring: profiles: dev application: name: springCloud-dev---spring: profiles: beta application: name: springCloud-beta
步驟3:修改【springCloud-config-native】中的application.yml,
server: port: 8888spring: application: name: springCloud-config profiles: active: - native
此處未新增檢索路徑,是因為我們的配置檔案路徑正好在src/main/resources下,並且正好名字叫config,在config的預設原始碼中的預設路徑包含此路徑,如果想要自定義路徑則需要新增上如下程式碼:
cloud: config: server: native: search-locations: file:./config,classpath:configg
多個路徑用“,”分隔,上面有兩種設定方式,一種是file:,一種是classpath:這兩種方式都可以實現本地載入配置檔案,file是直接通過本地檔案系統並相對於本專案的路徑載入讀取,classpath是直接從專案的src/main/resources下進行載入讀取配置檔案。此處的search-locations設定方式目前只能採用逗號分隔的方式設定,從yaml的官網上提供的資訊顯示是可以通過如下形式進行設定的,但是目前測試並不能搜尋到檔案。原因待查:
cloud: config: server: native: search-locations: - file:./config - classpath:configg
步驟4:修改【springCloud-config-native】新增啟動類
@SpringBootApplication@EnableConfigServerpublic class ConfigApplication { public static void main(String[] args) { SpringApplication.run(ConfigApplication.class, args); }}
步驟5:啟動測試:參照git服務的啟動測試進行3.config client呼叫服務端配置檔案。步驟1:新建【springCloud-configClient-8803】專案,pom檔案裡新增對config的依賴:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
步驟2:修改【springCloud-configClient-8803】,新建application.yml檔案,新增如下內容:
spring: application: name: springCloud-configClient-service
步驟3:修改【springCloud-configClient-8803】新建bootstrap.yml檔案,新增如下內容:
spring: cloud: config: name: springCloud-configClient #讀取配置檔案的檔名字 profile: beta #讀取配置檔案的段 uri: http://localhost:8888/ #配置中心的地址,此處如果是負載均衡,需要用“,”分隔 #username: 如果配置中心設定了安全認證,在此處設定認證賬號 #password: 如果配置中心設定了安全認證,在此處設定認證密碼
此處需要特別說明一下,bootstrap.yml和application.yml檔案在執行上是有優先順序的,bootstrap的優先順序高於application的優先順序,官方的說法是,bootstrap是系統級的設定,application是應用級設定。步驟4:修改【springCloud-configClient-8803】新建cotroller進行測試。讀取配置檔案。
@RestControllerpublic class UltraServiceController { @Value("${spring.application.name}") private String application_name ; @RequestMapping("/get") public Object getSessionId() { return "獲取到的引用名稱:"+application_name; }}
步驟5:修改【springCloud-configClient-8803】新增啟動類
@SpringBootApplicationpublic class UtralServiceApplication { public static void main(String[] args) { SpringApplication.run(UtralServiceApplication.class, args); }}
步驟6:啟動配置中心,啟動客戶端,通過客戶端進行連線測試:4.config的高可用機制配置中心的高可用機制是有兩種實現方式的:方式一:基於一些軟負載工具建立叢集,在客戶端直接訪問代理伺服器從而實現配置中心服務的高可用,此種方式是以配置中心為整個叢集的中心構建的方式二:通過Eureka實現高可用,建立多個配置中心註冊到Eureka中,客戶端通過服務名稱獲取配置中心的服務,此種方式是以Eureka為整個叢集的中心進行構建的,將這個的配置中心作為一個微服務在註冊中心進行註冊並管理。搭建方式與其他的微服務搭建方式專案,不在此詳述。