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
Spring Cloud搭建手册(2)——Spring Cloud Config的更多相关文章
- 一步一步深入spring(1)--搭建和测试spring的开发环境
1.引用jar包 到spring的网站上下载spring的jar包(本文是2.5.6),解压缩后找到 使用spring必须引用的jar包 spring.jar commons-logging.jar ...
- Spring第二弹—–搭建与测试Spring的开发环境
PS:Spring既可以使用在javaSE中,也可以使用在javaWeb中. 使用Spring需要的jar 下载spring(我下载的是2.5.6版本),然后进行解压缩,在解压目录中找到下面jar文件 ...
- Spring Cloud(3):配置服务(Config)
Spring Cloud Config的目标是在在大量的微服务中,将服务配置信息和和服务的实际物理部署分离,且服务配置服务不应与服务实例一起部署.配置信息应该作为环境变量传递给正在启动的服务,或者在服 ...
- spring cloud 专题二(spring cloud 入门搭建 之 微服务搭建和注册)
一.前言 本文为spring cloud 微服务框架专题的第二篇,主要讲解如何快速搭建微服务以及如何注册. 本文理论不多,主要是傻瓜式的环境搭建,适合新手快速入门. 为了更好的懂得原理,大家可以下载& ...
- 使用Spring Cloud搭建高可用服务注册中心
我们需要的,不仅仅是一个服务注册中心而已,而是一个高可用服务注册中心. 上篇博客[使用Spring Cloud搭建服务注册中心]中我们介绍了如何使用Spring Cloud搭建一个服务注册中心,但是搭 ...
- 使用Spring Cloud搭建服务注册中心
我们在之前的博客中已经介绍过阿里的分布式服务框架dubbo[Linux上安装Zookeeper以及一些注意事项][一个简单的案例带你入门Dubbo分布式框架],但是小伙伴们应该也看到了,阿里的dubb ...
- maven 聚合工程 用spring boot 搭建 spring cloud 微服务 模块式开发项目
项目的简单介绍: 项目采用maven聚合工程 用spring boot 搭建 spring cloud的微服务 模块式开发 项目的截图: 搭建开始: 能上图 我少打字 1.首先搭建maven的聚合工程 ...
- 【微服务】使用spring cloud搭建微服务框架,整理学习资料
写在前面 使用spring cloud搭建微服务框架,是我最近最主要的工作之一,一开始我使用bubbo加zookeeper制作了一个基于dubbo的微服务框架,然后被架构师否了,架构师曰:此物过时.随 ...
- spring boot 2.0.3+spring cloud (Finchley)2、搭建负载均衡Ribbon (Eureka+Ribbon+RestTemplate)
Ribbon是Netflix公司开源的一个负载均衡组件,将负载均衡逻辑封装在客户端中,运行在客户端的进程里. 本例子是在搭建好eureka的基础上进行的,可参考spring boot 2.0.3+sp ...
随机推荐
- 解决Myeclipse中导入自定义的配色方案后,JSP中的js代码块为白色背景的问题
捣鼓了大半个上午,终于搞定.这样设置就可以了: 点击MyEclipse上方的菜单栏中的window菜单.选择Preferences菜单项.在弹出的窗口的左侧树形菜单依次选择:MyEclipse.Fil ...
- Oracle connect by 层级结构查询
公司组织架构表:organization 一.查询子级 select * from organizationstart with id='1'connect by prior id = parent_ ...
- struts2:在Action中使用Servlet的API,设置、读取各种内置对象的属性
有两种方式可以实现在Action中使用Servlet的API.一种是使用org.apache.struts2.ServletActionContext类,另一种是使用com.opensymphony. ...
- 基于node-webkit的web项目打包方案
下载node-webkit https://github.com/rogerwang/node-webkit 找到Downloads这一小节,然后下载对应平台的node-webkit预编译包.(为了介 ...
- HTML5学习笔记(二十):JavaScript中的标准对象
这里提到的标准对象指ECMAScript中定义的对象,无论JavaScript运行那种环境(浏览器.Node.js)下都存在的对象. typeof 在JavaScript的世界里,一切都是对象. 但是 ...
- 菜鸟学Java(二十)——你知道long和Long有什么区别吗?
Java中数据类型分两种: 1.基本类型:long,int,byte,float,double 2.对象类型:Long,Integer,Byte,Float,Double其它一切java提供的,或者你 ...
- flink Job提交过程
https://www.jianshu.com/p/0cdfa2a05ebd http://vinoyang.com/2017/04/02/flink-runtime-client-submit-jo ...
- Window 分布式 学习2----好文收藏
概述 我们在上一篇Windows平台分布式架构实践 - 负载均衡中讨论了Windows平台下通过NLB(Network Load Balancer) 来实现网站的负载均衡,并且通过压力测试演示了它的效 ...
- webpack的css处理
webpack打包处理css的时候需要两个loader: style-loader 和css-loader 安装: npm install style-loader css-loader --save ...
- 如何去掉文件里的^M
起因 csv文件用Python处理之后,有的地方跟着一个^M,特别好奇,以为是处理过程中产生的,后来想了想不是. 解决办法 尝试使用replace替换掉,但是失败了 查询原因,谷歌一番,发现是Wind ...