-
pom.xml
cloud-iaas com.xiaoge 1.0-SNAPSHOT 4.0.0 config-server ego 商城的配置文件中 org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.cloud spring-cloud-config-server org.springframework.cloud spring-cloud-starter-bus-amqp org.springframework.boot spring-boot-maven-plugin -
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 @RestController public class ConfigController { @Value("${server.port}") private String port; @Autowired private RestTemplate restTemplate; @PostMapping("bus-refresh") public ResponseEntityrefresh() { //组装 url String url = "http://localhost:" + port + "/actuator/bus-refresh"; // 创建请求头 HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.add("content-type", "application/json;charset=utf-8"); // 创建request HttpEntity httpEntity = new HttpEntity<>(httpHeaders); // 发送post请求 ResponseEntity 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; @SpringBootApplication @EnableEurekaClient // 开启eureka客户端 @EnableConfigServer //开启配置文件中心 public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } @Bean public RestTemplate restTemplate() { return new RestTemplate(); } } -
在对应的gitee创建对应的仓库
-
通过网络接口发起post请求刷新
对配置中心发生刷新请求http://localhost:8888/actuator/refresh 发起post请求,以刷新微服务的配置信息。发生post请求之后,就会触发bus的广播效应,Bus本质还是利用了消息中间件做发布订阅,从而实现广播效果。结果就像下面的图一样(网络图片):



