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

※在Dalston.SR2版本以后,均不能正常加密,如果必须使用此功能,需要降级到SR1或Camden SR7。

1、首先需要创建一个config-server工程,作为配置中心的服务器,用来与git、svn或者本地仓库连接,从仓库获取配置文件

① config-server工程的POM文件需要增加以下依赖:

<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入口类添加注解,以启用配置中心服务器

@EnableConfigServer

③ 同样的使用application-peer1.properties和application-peer2.properties来进行配置

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

将公用配置,配置在application.properties里:

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:

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

2、对于config-client来说,需要进行如下配置:

① 首先要在POM文件添加config-client的依赖:

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

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

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

3、如果要启用配置文件中密码的加解密功能,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下即可。

4、可能遇到的问题:

①使用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
posted @ 2018-03-13 11:12  GordonDicaprio  阅读(763)  评论(0编辑  收藏  举报