Spring Cloud搭建手冊(2)——Spring Cloud Config

※在Dalston.SR2版本之後,均不能正常加密,若是必須使用此功能,須要降級到SR1或Camden SR7。css

一、首先須要建立一個config-server工程,做爲配置中心的服務器,用來與git、svn或者本地倉庫鏈接,從倉庫獲取配置文件html

① config-server工程的POM文件須要增長如下依賴:java

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-security</artifactId> </dependency> 

② 在Spring Boot入口類添加註解,以啓用配置中心服務器git

@EnableConfigServer

③ 一樣的使用application-peer1.properties和application-peer2.properties來進行配置github

spring.profiles.active=peer1 server.port=8001
spring.profiles.active=peer2 server.port=8002

將公用配置,配置在application.properties裏:spring

spring.application.name=config-server-service #disable security when testing management.security.enabled=false security.user.name=admin security.user.password=taoge1gb spring.cloud.config.server.git.uri=https://github.com/spring-cloud.git spring.cloud.config.server.git.searchPaths=spring-cloud-config-repo spring.cloud.config.server.git.username=taoge spring.cloud.config.server.git.password=taoge1gb eureka.client.serviceUrl.defaultZone=http://admin:taoge1gb@eureka-server:8361/eureka,http://admin:taoge1gb@eureka-server:8362/eureka 

④ 執行如下啓動命令,分別啓動激活peer1和peer2:bootstrap

java -jar config-server-1.0.0.jar --spring.profiles.active=peer1 java -jar config-server-1.0.0.jar --spring.profiles.active=peer2 

二、對於config-client來講,須要進行以下配置:瀏覽器

① 首先要在POM文件添加config-client的依賴:服務器

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> 

② 在bootstrap.properties配置config-server信息:oracle

eureka.client.serviceUrl.defaultZone=http://admin:taoge1gb@eureka-server:8361/eureka,http://admin:taoge1gb@eureka-server:8362/eureka spring.cloud.config.profile=dev spring.cloud.config.name=test spring.cloud.config.label=develop spring.cloud.config.username=admin spring.cloud.config.password=taoge1gb spring.cloud.config.discovery.enabled=true spring.cloud.config.discovery.serviceId=config-server-service 

※必須配置在bootstrap.properties裏,使該配置在程序啓動時就生效。

③ 開啓失敗重試功能(可選),須要在POM文件添加依賴:

<dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> 

再在bootstrap.properties配置文件中開啓重試功能:

spring.cloud.config.failFast=true

三、若是要啓用配置文件中密碼的加解密功能,Spring Cloud Config要求加密擴展無限強度限制,因此須要先下載JCE,替換原JDK裏的加密擴展包

JDK 7:

http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html

JDK 8:

http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html

下載完成解壓後,把local_policy.jar和US_export_policy.jar拷貝並覆蓋到$JAVA_HOME/jre/lib/security下便可。

四、可能遇到的問題:

①使用curl localhost:8888/encrypt -d mysecret獲取加密後密文,返回401,{"timestamp":1517811624176,"status":401,"error":"Unauthorized","message":"Full authentication is required to access this resource","path":"/encrypt"}

緣由:因爲spring cloud config server在啓動時會在控制檯打印出密碼,在訪問時,須要帶上默認的用戶名和密碼。

解決辦法:建議在配置application.properties時,加上指定的用戶名密碼:

security.user.name=admin security.user.password=taoge1gb

訪問時,使用curl http://admin:taoge1gb@localhost:8888/encrypt -d mysecret

或關閉驗證

management.security.enabled=false security.basic.enabled=false 

②訪問curl http://admin:taoge1gb@localhost:8888/encrypt -d mysecret時,返回404,{"description":"No key was installed for encryption service","status":"NO_KEY"}

緣由:沒有設置加密時使用的key。

解決辦法:在配置application.properties時,加上key的配置:

encrypt.key=sisterred

※在Dalston.SR2版本之後,均不能正常加密,若是必須使用此功能,須要降級到SR1或Camden SR7。

參考:https://github.com/spring-cloud/spring-cloud-config/issues/767

③ 配置的git倉庫沒法克隆和檢出

緣由:配置spring.cloud.config.server.git.uri的路徑,必須是指定到.git的url才能夠,能夠經過在瀏覽器上訪問,試驗所配置的路徑是否正確。

而在.git後的路徑,則須要配置在spring.cloud.config.server.git.searchPaths。

解決辦法:配置成如下形式

spring.cloud.config.server.git.uri=https://github.com/spring-cloud.git spring.cloud.config.server.git.searchPaths=spring-cloud-config-repo

④ 更新配置後,發送/bus/refresh請求,返回401

發送curl -X POST http://localhost:8088/bus/refresh,返回以下內容:

{"timestamp":1517974621306,"status":401,"error":"Unauthorized","message":"Full authentication is required to access this resource.","path":"/bus/refresh"}

緣由:因爲spring cloud config client在啓動時會在控制檯打印出密碼,在訪問時,須要帶上默認的用戶名和密碼。

解決辦法:建議在配置application.properties時,加上指定的用戶名密碼:

security.user.name=admin security.user.password=taoge1gb

訪問時,使用curl -X POST http://admin:taoge1gb@localhost:8088/bus/refresh

或關閉驗證

management.security.enabled=false security.basic.enabled=false
相關文章
相關標籤/搜索