解决的问题:该技术解决了github上内容修改,不能同步到微服务的问题,这就需要Config手动动态刷新的技术。
1、pom文件
<?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>springcloud</artifactId>
<groupId>com.springcloud</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>config-client3355</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
2、主配置文件(文件名为bootstrap.yml)
#指定服务的端口号
server:
port: 3355
#指定微服务的名称
spring:
application:
name: config-client
#config客户端配置
cloud:
config:
#分支名称
label: master
#配置文件名称
name: config
#读取后缀名称 上述3个综合:master分支上config-dec.yml的配置文件被读取http://localhost:3344/master/config-dev.yml
profile: dev
#配置中心地址
uri: http://localhost:3344
#服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
#暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
3、控制器
package com.springcloud.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.ws.rs.GET;
/**
* @author dc
* @date 2020/8/6 - 14:43
*/
@RestController
@RefreshScope //刷新控制器的注解
public class ConfigClientController {
@Value("${result}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo() {
return configInfo;
}
}
4、主启动类
package com.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author dc
* @date 2020/8/6 - 11:01
*/
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class, args);
}
}
微服务启动完成后,在命令行发送请求来刷新控制器(前提安装cur):
如果不发送,那么就看不到效果!!!
遗留问题:假如有多个微服务的客户端(上百个),那么每个微服务都需要执行一次post请求来手动刷新。(这就需要广播,一次通知,用到消息总线技术)。
手动刷新命令(命令行的方式):
curl -X POST “http://localhost:3355(微服务端口号)/actuator(固定的)/refresh(固定的)”