Config-Server搭建及配置文件的动态刷新
-
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>cloud-iaas</artifactId> <groupId>com.xiaoge</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>config-server</artifactId> <description>ego 商城的配置文件中</description> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--添加config--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!-- 添加 bus 和 rabbitmq 的集成依赖 实现动态刷新--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> </dependencies> <!-- maven 的打包插件--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
-
application.yml
server: port: ${APP_PORT:8888} spring: application: name: config-server cloud: config: discovery: enabled: true server: #开启配置文件发现功能 git: #配置远端配置文件地址 用户名和密码以及文件夹名称 gitee地址 uri: https://gitee.com/xiaogectwbq/ego-shop-config.git username: xiaogectwbq password: ******* search-paths: /ego-shop-config/** # 放配置文件的地址 #配置 rabbitmq rabbitmq: addresses: 172.16.66.248 username: admin password: 123456 port: 5672 virtual-host: / eureka: client: service-url: # 设置eureka服务器地址 defaultZone: ${EUREKA_SERVER:http://admin:admin@localhost:8761/eureka} fetch-registry: true #要不要去注册中心获取其他服务的地址 register-with-eureka: true #自己就是注册中心,注册自己 instance: hostname: ${APP_HOST:localhost} # eureka服务器的地址 应用实例主机名 # 客户端在注册时使用自己的IP而不是主机名,缺省:false # prefer-ip-address设置为true以IP地址注册到服务中心,相互注册使用IP地址,访问注册中心页面DS Replicas模块中有一个实例为localhost prefer-ip-address: true instance-id: ${spring.application.name}:${server.port} # 设置注册进eureka服务器中的id lease-expiration-duration-in-seconds: 30 # 30s 客户端没发心跳就剔除 lease-renewal-interval-in-seconds: 10 #客户端向服务端发送心跳的时间 management: endpoints: web: exposure: include: '*' #暴露的端点放行,例如健康监测的
-
ConfigController.java(实现动态刷新)
package com.xiaoge.com.xiaoge; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.*; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import org.springframework.web.servlet.function.ServerRequest; /** * @Classname ConfigController * @Date 2022/3/20 下午4:10 * @Created by xiaoge * @Description TODO */ @RestController public class ConfigController { @Value("${server.port}") private String port; @Autowired private RestTemplate restTemplate; /** * 钩子函数会触发我们这个接口,发请求刷新所有的配置文件 * * 因为gitee发请求到我们这来 * 我们发一个请求到config-server里面 * @return */ @PostMapping("bus-refresh") public ResponseEntity<String> refresh() { //组装 url String url = "http://localhost:" + port + "/actuator/bus-refresh"; // 创建请求头 HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.add("content-type", "application/json;charset=utf-8"); // 创建request HttpEntity<String> httpEntity = new HttpEntity<>(httpHeaders); // 发送post请求 ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class); if (responseEntity.getStatusCode().equals(HttpStatus.NO_CONTENT)) { return ResponseEntity.ok("刷新配置文件成功!"); } return ResponseEntity.badRequest().body("配置文件刷新失败!"); } }
-
启动类
package com.xiaoge; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; /** * @Classname ConfigServerApplication * @Date 2022/3/20 下午4:02 * @Created by xiaoge * @Description TODO */ @SpringBootApplication @EnableEurekaClient // 开启eureka客户端 @EnableConfigServer //开启配置文件中心 public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } /** * 远程调用工具 * @return */ @Bean public RestTemplate restTemplate() { return new RestTemplate(); } }
-
在对应的gitee创建对应的仓库
-
通过网络接口发起post请求刷新
对配置中心发生刷新请求http://localhost:8888/actuator/refresh 发起post请求,以刷新微服务的配置信息。发生post请求之后,就会触发bus的广播效应,Bus本质还是利用了消息中间件做发布订阅,从而实现广播效果。结果就像下面的图一样(网络图片):