注:本教程均是参考"程序猿DD"大佬的SpringCloud教程:http://blog.didispace.com/spring-cloud-learning/实践结合自己的理解和总结编写
分布式配置中心
有三种角色组成。1.配置中心文件;2.配置中心服务;3.配置中心客户端。
配置中心文件:
demo:https://github.com/SpringStudent/config-client
配置中心服务:
连接github的配置仓库
a.启动类:
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
b.maven坐标
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
c.application.properties
spring.application.name=config-server
server.port=1202
spring.cloud.config.server.git.uri=https://github.com/SpringStudent/config-client
#用于加密,比如数据库密码等敏感信息
encrypt.key=zhouning
eureka.client.service-url.defaultZone=http://localhost:1001/eureka/
d. 验证服务是否可用
地址:GET http://ip:port/{application}/{profile}/{label}
{application}代表仓库名称,{profile}表示环境比如dev开发default默认,{label}代表git分支
如:GET http://192.168.3.94:1202/config-client/dev/master
成功后应该返回如下:
{
"name": "config-client",
"profiles": [
"dev"
],
"label": "master",
"version": null,
"state": null,
"propertySources": [
{
"name": "https://github.com/SpringStudent/config-client/config-client-dev.properties",
"source": {
"spring.datasource.driver-class-name": "com.mysql.jdbc.Driver",
"spring.datasource.username": "root",
"info.profile": "dev",
"spring.datasource.url": "jdbc:mysql://localhost:3306/test",
"from": "springstudent",
"spring.datasource.password": "root"
}
},
{
"name": "https://github.com/SpringStudent/config-client/config-client.properties",
"source": {
"info.profile": "default"
}
}
]
}
值得说明的是我用了用yml格式的配置文件报错:
Caused by: org.yaml.snakeyaml.parser.ParserException: while parsing MappingNode
通过网上查找资料发现都是格式的问题,但是自己查了很久没有发现问题,把配置文件改为properties的格式,将问题解决
配置中心客户端:
和其他的客户端不同的是需要引入Maven坐标spring-cloud-starter-config,并且必须使用bootstrap.properties配置文件,config-server配置才能被加载。
spring.application.name=config-client
server.port=2001
#用来配置该监控项是否属于敏感信息,访问敏感信息需要用户具有ACTUATOR角色权限,或者使用以下配置关闭安全限制
management.security.enabled=false
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
#通过服务访问configServer
spring.cloud.config.discovery.enabled=true
#spring.cloud.config.uri=http://localhost:1201/
spring.cloud.config.discovery.service-id=config-server
spring.cloud.config.profile=dev
spring.cloud.config.label=master
配置成功后通过
GET http://ip:port/info
可以看到如下信息
{
"profile":"default"
}
配置文件的加密与解密
配置中心服务,提供了相应的端点如下
a./encrypt/status #查看加密功能状态
在不配置encrypt.key=zhouning的状况下返回为:
{
"description":"No key was installed for encryption service",
"status":"NO_KEY"
}
在application.properites中添加了配置encrypt.key=zhouning
{
"status":"OK"
}
b./key 查看秘钥的端点
c./encrypt 对请求的body内容进行加密
如:curl http://ip:port/encrypt -d 带加密字符串
d//decrypt 对请求的body内容进行解密
如:curl http://ip:port/encrypt -d 带解密字符串
对于配置文件通过在属性值前使用{cipher}前缀来标注该内容是一个加密值如
spring.datasource.password={cipher}加密的字符串
配置的动态刷新
1.配置中心客户端添加spring-boot-starter-actuator
2.添加management.security.enabled=false到bootstrap.properties文件
3.在需要刷新的bean上添加@RefreshScope注解
4.修改配置中心文件
5.调用GET http://ip:port/refresh接口