當服務比較多的時候,每一個服務都有本身的配置文件;當配置文件須要變動的時候,須要重啓服務才能使配置文件生效,不能作到實時更新。springcloud有本身的配置中心config,該組件支持將配置文件放置於本地也支持將配置文件放置於git。java
新建一個項目,引入配置依賴,其做爲配置中心服務端,也能夠當作一個微服務註冊到註冊中心,當服務比較多的時候就能夠啓用多個服務,實現高可用。git
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>XiaYuApplication</artifactId> <groupId>com.xiayu</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>configcenter</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <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> </dependencies> </project>
加入@EnableConfigServer註解github
package com.xiayu.config.center; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer @EnableEurekaClient public class ConfigCenterApplication { public static void main(String[] args) { SpringApplication.run(ConfigCenterApplication.class); } }
配置中心配置文件web
spring: application: name: configcenter cloud: config: server: git: uri: https://gitee.com/niruoanh/xiayuapplication #uri表示git公共倉庫地址 私有倉庫地址須要設置用戶名及密碼, #因爲github.com訪問很是慢,換成國內的gitee search-paths: /config # /config 配置文件的所在目錄 # username: root # password: root label: master #指定分支 server: port: 8087 eureka: client: service-url: defaultZone: http://localhost:8083/eureka/
配置文件的文件名:服務名+profiles.yml
如:CommonIntegration-dev.yml
配置文件和路徑映射spring
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties
引入依賴數據庫
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
不須要開啓配置客戶端註解,直接在bootstrap.yml文件中加入apache
spring: application: name: CommonIntegration cloud: config: label: master #對應配置中心的label # uri: http://localhost:8087 #配置中心服務地址 profile: dev #profile discovery: enabled: true #設置從配置中心獲取數據 service-id: configcenter #配置中心服務名稱 server: port: 8091 eureka: client: service-url: defaultZone: http://localhost:8083/eureka/ #註冊中心url
啓動項目,首先會去配置中心,獲取該服務的配置文件bootstrap
(在搭建過程當中,遇到java.lang.IllegalStateException: No instances found of configserver (configserver)錯誤
,解決方法將application.yml文件中的註冊中心的配置移到bootstrap.yml文件中便可。)app
A.若是配置文件進行了修改,而後提交到git上了,應用修改後的配置文件就須要將服務從新啓動,這麼作很是麻煩,springcloud提供了一種自動更新配置的方法。maven
B.配置文件中有不少保密性的字段,如數據庫用戶名和密碼,對象存儲的key及祕鑰,發送短信key及祕鑰等等。因此須要對配置文件進行加密,對配置文件加密有對稱加密及非對稱加密。
詳見下篇springcloud教程之消息總線