------------恢復內容開始------------
SpringCloud Config 配置中心
Config 配置中心
Spring Cloud Config為分布式系統中的外部化配置提供服務器端和客戶端支持。使用Config Server,您可以在中心位置管理所有環境中應用程序的外部屬性。客戶端和服務器上的概念都與Spring
Environment
和PropertySource
抽象映射相同,因此它們非常適合Spring應用程序,但可以與以任何語言運行的任何應用程序一起使用。在應用程序從開發人員到測試人員再到生產人員的整個部署過程中,您可以管理這些環境之間的配置,並確保應用程序具有它們遷移時所需的一切。服務器存儲后端的默認實現使用git,因此它輕松支持帶標簽的配置環境版本,並且可以通過各種工具來訪問這些內容來管理內容。添加替代實現並將其插入Spring配置很容易。
Config配置中心實例
Config-Server
1.新建一個配置中心服務 springcloud-config-6001,並添加依賴
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> <version>2.1.0.RELEASE</version> </dependency> <!--配置中心服務需要添加進注冊中心,所以要加eureka-client依賴--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>1.4.6.RELEASE</version> </dependency> </dependencies>
2.創建配置文件 application.yml
server: port: 6001 spring: application: name: springcloud-config cloud: config: server: git: uri: https://gitee.com/little_gofy/config-server.git #git倉庫的html地址 #如果配置文件放在子目類下,則需在search-paths配置添加子目錄路徑 search-paths: #如果倉庫為私有,則需在username和password配置添加用戶名和密碼 username: password: eureka: client: service-url: defaultZone: http://localhost:7001/eureka/
3.
@SpringBootApplication @EnableConfigServer public class ConfigServer { public static void main(String[] args) { SpringApplication.run(ConfigServer.class, args); } }
4.在git倉庫創建配置文件application.yml,隨便添加點配置
spring: profiles: dev application: name: config-dev my: name: gofy-dev --- spring: profiles: test application: name: config-test my: name: gofy-test
開啟注冊中心服務和配置中心服務, 訪問 localhost:6001/application-dev.yml 和
HTTP服務具有以下形式的資源:
# application:配置文件名, profile:配置的模式, label:配置文件所在分支 /{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties
Config-Client
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> <version>2.0.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>1.4.6.RELEASE</version> </dependency> </dependencies>
2.
spring: cloud: config: discovery: service-id: springcloud-config #指定配置中心,對應配置中心服務名 enabled: true #開啟配置信息發現 label: master #配置文件所在分支, 默認為master profile: dev #配置的模式, 多個用逗號分隔 eureka: client: service-url: defaultZone: http://server1:7001/eureka/ register-with-eureka: false
server: port: 80 spring: application: name: config-client
@RestController public class ConfigController { @Value("${my.name}") //注入配置中心里的配置文件的my.name值 private String myName; @RequestMapping("/my") public String getMyName(){ return myName; } }
4.創建啟動類
@SpringBootApplication @EnableEurekaClient public class ConfigClient { public static void main(String[] args) { SpringApplication.run(ConfigClient.class, args); } }
當我們把bootstrap.yml里的profile值改為test, 則返回的是test模式配置的值gofy-test
.
如果報以下異常, 是因為配置中心服務還沒有注冊到注冊中心, 等一會再啟動客戶端就行了. 如果還報錯, 請檢查配置是否和上面的配置一致.
java.lang.IllegalStateException: No instances found of configserver (springcloud-config)
修改Config-Server本地倉庫位置
使用基於VCS的后端(git,svn),文件被檢出並克隆到本地文件系統。默認情況下,它們以config-repo-
為前綴放在系統臨時目錄中。例如,在Linux上,它可能是/tmp/config-repo-
,在Window上,它是/Temp/config-repo-
。一些操作系統通常會清除臨時目錄。這可能導致意外行為,例如缺少屬性。
為避免此問題,請通過將spring.cloud.config.server.git.basedir
或spring.cloud.config.server.svn.basedir
設置為不在系統臨時結構中的目錄來更改Config Server使用的目錄。
spring:
cloud:
config:
server:
git:
basedir: E:/local-config-repo