1.简介

Springcloud Bus 将分布式的节点用轻量的消息代理连接起来。可以用来广播配置文件的更改或者服务之间的通讯,也可以用于监控。我们需要安装rabbitMq,rabbitMq的安装可以参考https://blog.csdn.net/lu10052...。rabbitMq安装好后,默认用户名及密码为guest/guest。

参考:https://blog.csdn.net/forezp/...

image.png

2.使用消息总线自动更新配置文件

在微服务pom.xml文件中引入依赖

<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> //该依赖为Springboot自带的监控项目

在application.yml文件添加rabbitMq的设置及消息配置

spring:
    rabbitmq:
        host: localhost
        port: 5672
        username: guest
        password: guest
    cloud:
        bus:
          enabled: true
          trace:
            enabled: true
management:
    endpoints:
        web:
          exposure:
             include: bus-refresh

使用到配置文件中的值的类上上加入@RefreshScope注解

@RestController  
@RefreshScope  
public class PortController {  
  
    @Value("${xiayu.feature.mail}")  
    private String mail;
}

3.更新配置

配置更新提交后,向任意一个服务,发送post请求 http://localhost:8083/actuator/bus-refresh 8083代表一个服务的端口,所有的服务都将更新配置,每个服务都有相应的业务逻辑,可以将上述依赖及配置加入到配置中心服务中,每次发送post请求到配置中心,配置中心通过消息总线发送消息,每个服务从消息总线获取消息,获取配置文件更新。

发送请求方式:

手动请求:可以使用curl -X post http://example.com/actuator/b...,也可以使用postman发送请求

通过webhook: 在代码update之后,通过webhook,发送请求

image.png

集成到jenkins: 以后研究。。。

4.配置加密

A.对称加密:

前提:必须替换本地jdk jre lib下的文件下面俩个文件
https://www.oracle.com/techne...

image.png

通过设置entry.key进行加密
(必须在bootstrap.yml上设置)

encrypt:  
 key: sunliqian

加密:curl -X post http://localhost:8083/encrypt -d wewerriiooo

结果:13b243305084d3efcb62f4915351d24363fa03d7d5a39e21829ae3ac1217ad07

解密:curl -X POST http://localhost:8087/decrypt -d cb2d3a481ab97be5ace975e9040dd88f887b005c5b9bd71bcd2e37f07edb70ea

结果:wewerriiooo

B.非对称加密

参考:https://blog.csdn.net/wtdm_16...

1.生成jks文件,并放置在配置中心的classpath目录下

rsa算法

$ keytool -genkeypair -alias mytestkey -keyalg RSA -dname "CN=Web Server,OU=Unit,O=Organization,L=City,S=State,C=US" -keypass changeme -keystore server.jks -storepass letmein

2.bootstrap.yml文件中加入配置

encrypt:
  keyStore:
    location: classpath:/server.jks        #生成在jks文件路径
    password: letmein            #key store 秘钥
    alias: mytestkey            #别名
    secret: changeme            #私钥

加密及解密同对称加密相同。


你若安好便是晴天
82 声望10 粉丝