# spring-cloud-hoxton **Repository Path**: wolfxz/spring-cloud-hoxton ## Basic Information - **Project Name**: spring-cloud-hoxton - **Description**: Demo for Spring Cloud hoxton - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 4 - **Forks**: 0 - **Created**: 2021-05-09 - **Last Updated**: 2021-12-10 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Spring Cloud 演示项目使用指南 ## 组件及工具使用版本说明 *组件*| *版本*|*必要性* ---|---|--- JDK|11.0.11|✓ Maven|3.8.1|✓ Spring Cloud|Hoxton SR11|✓ Redis|6.2.3|✓ RabbitMQ|3.7.19|✓ Percona|8.0.23-14|✓ Docker|20.10.6| Docker-compose|1.29.1| ## 开发环境说明 - 开发设备需求 - 内存 16G+ - CPU 4C+ (低于4C容器环境运行缓慢) - Ubuntu 20.04+ - MacOS - Windows WSL2 + Ubuntu 20.04+ - 开发环境必须具备以下基本工具依赖 - JDK及其命令行工具([JDK环境管理工具](https://sdkman.io/)) - JDK的[JCE配置](https://www.oracle.com/technetwork/cn/java/javase/downloads/jce8-download-2133166-zhs.html) - Maven命令行工具([阿里镜像源配置](https://developer.aliyun.com/mirror/maven?spm=a2c6h.13651102.0.0.53322f70zNTCrh)) - IDE(推荐[Jetbrains IDEA](https://www.jetbrains.com/)) - [Docker](https://www.docker.com/products/docker-desktop) 和 [Docker-compose](https://docs.docker.com/compose/)(如果不准备使用,那么基于Docker的一键开发依赖组件配置就无法使用,需要手工提供开发依赖环境,并修改项目的相关配置参数) - [Springboot CLI](https://docs.spring.io/spring-boot/docs/2.3.11.RELEASE/reference/html/getting-started.html#getting-started-installing-the-cli) - 开发环境包管理工具(可选) - [SDKMAN](https://sdkman.io/) ## 项目目录 ## 使用步骤 ## 特性说明 ### 配置加密服务 Config - 敏感属性加解密 1. 生成秘钥库 ```bash # 使用JDK自带keytool工具 keytool -genkeypair -alias encrptkey -keyalg RSA -dname "CN=www.newtouch.com, O=NEWTOUCH, C=CN" -keypass www.newtouch.com -keystore key.jks -storepass www.newtouch.com # 这步为可选步骤(转换为最新标准格式) keytool -importkeystore -srckeystore key.jks -destkeystore key.jks -deststoretype pkcs12 # 将产生的秘钥文件复制到配置服务的src/main/resources目录下 cp key.jks [config root]/src/main/resources ``` 2. 修改配置中心服务配置文件bootstrap.yml,添加下面的配置 ```yaml encrypt: key-store: location: "classpath:key.jks" # 秘钥文件所在路径 password: www.newtouch.com # 务必和执行命令行中的参数保持一致 alias: encrptkey # 务必和执行命令行中的参数保持一致 secret: www.newtouch.com # 务必和执行命令行中的参数保持一致 ``` 3. 使用命令行产生秘钥加密串 ```bash curl http://localhost:9999/encrypt -d 123456 # -d后的123456为需要加密的敏感数据,请自行替换 AQAGo/G3MiqK0xY/......略 ``` 4. 将原配置文件中需要加密的敏感数据替换成{cipher}...格式 - 原格式 ```yaml spring: rabbitmq: password: 123456 ``` - 加密格式 ```yaml spring: rabbitmq: password: "{cipher}AQAGo/G3MiqK0xY/......略" ``` - 配置属性动态刷新 1. Config Server添加下述依赖 ```xml org.springframework.cloud spring-cloud-config-monitor org.springframework.cloud spring-cloud-starter-bus-amqp ``` 2. Client端添加下述依赖 ```xml org.springframework.cloud spring-cloud-starter-bus-amqp ``` 3. 修改Client端ServiceHelloApplication.java,添加@RefreshScop注解 ```java @SpringBootApplication @EnableDiscoveryClient @RestController @RefreshScope public class ServiceHelloApplication { @Value("${hello.name}") private String helloName; @Autowired private DiscoveryClient discoveryClient; @Value("${server.port}") private int serverPort; public static void main(String[] args) { SpringApplication.run(ServiceHelloApplication.class, args); } @GetMapping("/") String sayHello() throws InterruptedException { TimeUnit.MILLISECONDS.sleep(1600); return String.format("Hello, current port is: %d", serverPort); } @GetMapping("/name") String getHelloName() { return helloName; } } ``` 4. 在更新属性文件内容后,向Config Server的端口/actuator/bus-refresh发起POST请求来发起属性更新 ```shell curl -X POST http://localhost:9999/actuator/bus-refresh/service-hello ``` ### 添加Spring Cloud Sleuth链路跟踪 1. 所有的服务pom.xml添加类库依赖 ```xml org.springframework.cloud spring-cloud-sleuth-zipkin org.springframework.cloud spring-cloud-starter-bus-amqp org.springframework.boot spring-boot-starter-amqp ```