org.springframework.boot
spring-boot-starter-jdbc
org.springframework.cloud
spring-cloud-config-server
org.springframework.cloud
spring-cloud-starter-eureka
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter-test
test
添加 @EnableConfigServer
@EnableConfigServer
@SpringBootApplication
public class ConfigServerJdbcApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerJdbcApplication.class, args);
}
}
application.properties
management.security.enabled=false
#config默认支持git模式,但是同时也提供了svn,vault,jdbc三种配置模式
spring.profiles.active=jdbc
#数据库配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/qinwei?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driverClassName=com.mysql.jdbc.Driver
#config配置
spring.cloud.config.label=master
spring.cloud.config.server.jdbc=true
#重写SQL,因为KEY为mysql关键字,在mysql需要使用反引号来表示其为字段
spring.cloud.config.server.jdbc.sql=SELECT `KEY`, `VALUE` from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?
application.yml
server:
port: 8090
eureka:
client:
service-url:
defaultZone: http://localhost:8081/eureka-server/eureka/
spring:
application:
name: config-server-jdbc
mysql数据库中创建 properties 表
CREATE TABLE `properties` (
`id` int(11) NOT NULL,
`key` varchar(50) DEFAULT NULL,
`value` varchar(500) DEFAULT NULL,
`application` varchar(50) DEFAULT NULL,
`label` varchar(50) DEFAULT NULL,
`profile` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

bootstrap.properties
server.port=8088
eureka.client.service-url.defaultZone=http://localhost:8081/eureka-server/eureka/
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=config-server-jdbc
#设为true,如果无法连接config server,启动时会抛异常,并停止服务
spring.cloud.config.failFast=true
#关闭权限验证
management.security.enabled=false
#显示每个服务实例发送的所有事件和所有的ack
spring.cloud.bus.trace.enabled=true
// 必须在使用的地方加上 @RefreshScope 注解,否则无效
@RefreshScope
@RestController
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
@Value("${msg}")
String msg;
@Value("${name}")
String name;
@Value("${version}")
String version;
@RequestMapping("/hello")
public String getConfig() {
return "msg = " + msg + "; name = " + name + "; version = " + version;
}
}
POST 请求调用 http://localhost:8088/refresh ,可以看到配置刷新了

如果使用消息总线,可以通过POST 请求调用 http://localhost:8088/bus/refresh ,刷新整个 client 集群

http://localhost:8088/bus/refresh 可以添加 destination 参数刷新指定的服务,如/bus/refreshdestination=config-client:8089

**注意:**我总共启了3个client,端口分别是8087、8089、8089,如果请求http://localhost:8088/bus/refresh destination=config-client:8089,则8088和8089会刷新配置,而8087不会。也就是说你调用哪个服务的POST请求,那个服务肯定会刷新。
destination参数的值指的是 服务的ApplicationContext ID,默认情况下,ApplicationContext ID是spring.application.name:server.port