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