spring cloud config
什么是springcloud config
Spring Cloud Config为分布式系统外部化配置提供了服务器端和客户端的支持,它包括Config Server和Config Client两部分。由于Config Server和Config Client都实现了对Spring Environment和PropertySource抽象的映射,因此,Spring Cloud Config非常适合Spring应用程序,当然也可与任何其他语言编写的应用程序配合使用
Config Server是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环 境下的配置,默认使用Git存储配置内容(也可使用Subversion、本地文件系统或Vault存储 配置),因此可以方便的实现对配置的版本控制与内容审计。 Config Client 是Config Server的客户端,用于操作存储在Config Server中的配置属性
为什么要使用springcloud config![在这里插入图片描述](https://img-blog.csdnimg.cn/20210317170046703.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2FsbGNvdmV0YWxsbG9zZQ==,size_16,color_FFFFFF,t_70)
1、集中管理配置。一个使用微服务架构的应用系统可能会包含成百上千个微服务,因此集中管理配置是非常有必要的
2、不同环境,不同配置。例如,数据源配置在不同的环境(开发、测试、预发布、生产等)中是不同的;(测试、生产、开发、联调、灰度)
3、运行期间可动态调整。例如,我们可根据各个微服务的负载情况,动态调整数据源连接池大小或熔断阈值,并且在调整配置时不停止微服务
4、配置修改后可自动更新。如配置内容发生变化,微服务能够自动更新配置。
怎么把config 集成到自己的项目中
1、第一步在自己的git上创建仓库
2、模仿自己是一个运维人员,在本地创建一个文件夹:D:\config\springcloud\ms-config
3、然后进入该文件夹D:\config\springcloud\ms-config)点击git bash here,然后输入命令git clone http://localhost/git/ms-config.git
4、然后在远git文件中创建application.yml配置文件
server:
port: 9100
spring:
application:
name: ms-cfg-server
cloud:
config:
server:
git:
uri: http://localhost/git/ms-config.git
username: root
password: 123456
5、执行git命令把该文件推送到远端git仓库
6、通过config server来访问获取远程地址
搭建config server工程
加入jar包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
写application.yml的配置文件
server:
port: 9100
spring:
application:
name: ms-cfg-server
cloud:
config:
server:
git:
uri: http://localhost/git/ms-config.git
username: root
password: 123456
写注解
@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication .class, args);
}
}
访问规则
通过application-{profiles}.yml来访问
http://localhost:9100/application-test.yml
http://localhost:9100/application-prod.yml
通过/application/{profiles}/{lable}
http://localhost:9100/application/test/master
http://localhost:9100/application/prod/master
通过/lable/application-{profiles}.yml
http://localhost:9100/master/application-prod.yml
http://localhost:9100/master/application-test.yml
7、创建config 的客户端(也就是我们常用的微服务)
导入jar包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
写配置
定义一个bootstrap.yml
spring:
application:
name: MsConfigClient # 对应config server所获取的配置文件的{application}
cloud:
config:
uri: http://localhost:9100/
profile: dev # profile对应config server所获取的配置文件中的{profile}
label: master # 指定Git仓库的分支,对应config server所获取的配置文件的{label}
application.yml(可写可不写)bootstrap.yml的配置文件的优先级会覆盖他的
测试验证http://localhost:8088/getConfig4Remote
在项目中是如何使用的
低级用法(需要重新启动机器)
我的工程中依赖busi.ops,远程git的原来的值为Y,后来新上的业务逻辑有缺陷,不要走原来的老逻辑分支,运维人员把本地的Y改为N ,然后推送到远程分支上,重启生产上的工程
高级用法(手动刷新,不需要从新启动物理机)
1、第一步工程中加入依赖actuator的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2、第二步:开启refresh的监控端点
management:
endpoints:
web:
exposure:
include: "*" 开启所有的端点
3、第三步:在读取配置文件中的类上加入@RefreshScope
@RestController
@RequestMapping("/order")
@Slf4j
@RefreshScope
public class OrderController {
@Value("${busi.ops}")
private String isNewLogic;
}
4、测试访问接口:http://localhost:8003/order/testManualRefresh
5、模拟运维工程师把配置文件中的busi.ops改为Y 然后push到远程
6、调用刷新接口(通过post的方式调用)http://localhost:8003/actuator/refresh
7、然后在测试下变量的值http://localhost:8003/order/testManualRefresh
顶级用法
通过消息队列自定义通知
config server
加入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>
application.yml配置文件
server:
port: 9100
spring:
application:
name: ms-cfg-auto-refresh-server
cloud:
config:
server:
git:
uri: http://localhost/git/ms-config.git
username: root
password: 123456
rabbitmq:
host: 47.104.128.12
port: 5672
virtual-host: cofighost
username: guest
password: guest
connection-timeout: 10000
template:
mandatory: true
写注解
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication .class, args);
}
}
提供一个全局刷新接口,往mq broker上发送刷新消息http://loclahost:9100/autoRefresh
@RestController
public class AutoRefreshController {
@Autowired
private RabbitTemplate rabbitTemplate;
@RequestMapping("/autoRefresh")
public Object autoRefresh() {
Map<String,Object> sendMap = new HashMap<String,Object>();
sendMap.put("isNew","Y");
rabbitTemplate.convertAndSend("auto.refresh.topic","auto.refresh.all", JSON.toJSONString(sendMap));
return "OK";
}
}
config client
依赖的jar包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
application.yml的配置文件
spring:
rabbitmq:
host: 47.104.128.12
port: 5672
virtual-host: cofighost
username: guest
password: guest
connection-timeout: 10000
template:
mandatory: true
eureka:
instance:
prefer-ip-address: true
instance-id: ms-provider-order-8002
client:
service-url:
defaultZone: http://www.eureka9000.com:9000/eureka/
#配置mybatis
mybatis:
configuration:
map-underscore-to-camel-case: true
management:
endpoints:
web:
exposure:
include: "*"
提供一个消息消费监听
@Component
@Slf4j
public class AutoRefresh {
@Autowired
private RestTemplate restTemplate;
@Value("${server.port}")
private Integer port;
@RabbitListener(bindings = {
@QueueBinding(value = @Queue("auto.refresh.8002"),exchange = @Exchange(value = "auto.refresh.topic",type = "topic"),key = "auto.refresh.#")
})
public void consumerTopicMsg(Message message) {
log.info("端口:{},获取到刷新通知",port);
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> requestEntity = new HttpEntity<String>(null, requestHeaders);
restTemplate.postForEntity("http://localhost:"+port+"/actuator/refresh",requestEntity,Object.class);
}
}
开启/actuator/refresh刷新端点
通过消息总线bus来实现
config server
加入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
application.yml配置文件
spring:
rabbitmq:
host: 47.104.128.12
port: 5672
virtual-host: Confighost
username: guest
password: guest
connection-timeout: 10000
template:
mandatory: true
eureka:
instance:
prefer-ip-address: true
instance-id: ms-order-8002
client:
service-url:
defaultZone: http://www.eureka9000.com:9000/eureka/
#配置mybatis
mybatis:
configuration:
map-underscore-to-camel-case: true
management:
endpoints:
web:
exposure:
include: "*"
config client
依赖的jar包
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
application.yml的配置文件
spring:
rabbitmq:
host: 47.104.128.12
port: 5672
virtual-host: cofighost
username: guest
password: guest
connection-timeout: 10000
template:
mandatory: true
eureka:
instance:
prefer-ip-address: true
instance-id: ms-provider-order-8002
client:
service-url:
defaultZone: http://www.eureka9000.com:9000/eureka/
#配置mybatis
mybatis:
configuration:
map-underscore-to-camel-case: true
management:
endpoints:
web:
exposure:
include: "*"
使用JCE进行密文存储
1、第一步:去oracle官网下载最新的jce的jar包
Java 6 JCE地址:https://www.oracle.com/technetwork/java/javase/downloads/jce-6-download-429243.html
http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html
http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html
2、把下载的jar包下的包copy到你的java_home/jre/security/目录下
3、创建一个config server9100的工程,测试jce是否起作用:http://localhost:9100/encrypt/status
测试加密端点,必须通过post的方式调用http://localhost:9100/encrypt
测试解密端点:http://localhost:9100/decrypt
测试使用jce加密数据库密码
1、创建一个ms-provider-order-enc.yml文件提交到git上(数据库密码使用jce加密后的)
server:
port: 8002
spring:
datasource:
druid:
username: root
password: '{cipher}ae02ac56207ed2b2ea906d12b96fdbe107a74a7b361234a4909039450ac7f0c7'
jdbcUrl: jdbc:mysql://47.104.128.12:3306/yinz
driver-class-name: com.mysql.jdbc.Driver
profiles: dev
application:
name: ms-provider-enc
2、通过config server访问ms-provider-order-enc.yml文件http://localhost:9100/ms-provider-order-enc-dev.yml
正常请求下config server 需要加入security来进行登陆认证
加入依赖
<!--加入安全配置依赖的Jar包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
开启安全配置
spring.security.basic.enable=true
spring.security.user.name=root
spring.security.user.password=123456
config client端的bootstrap.yml配置security的登陆账号密码
spring:
application:
name: ms-provider-order-enc
cloud:
config:
uri: http://localhost:9100
profile: dev
label: master
username: root
password: 123456
本地/C:/Users/zhuwei/AppData/Local/Temp/config-repo-938262522502485995/ms-provider-order-enc.yml存储密码是密文的
4、创建一个config client 来连接server测试看是否能链接上数据库