自动刷新配置客户端在 Spring Boot 2 中不起作用
Automatic refresh of Config Clients not working in Spring Boot 2
我正在尝试将 Spring Cloud Config Server
和 RabbitMQ
的简单示例代码迁移为 Spring Cloud Bus
(基于 Spring Boot 1.5.22.RELEASE
和 Spring Cloud Brixton.SR7
)到 Spring Boot 2.2.6.RELEASE
和 Spring Cloud Hoxton.SR3
。该示例由配置服务器、配置客户端、GitLab
作为 SCM 和 RabbitMQ
(3.8 - Erlang 22.1.5) 组成。代码正在编译、启动、推送 webhook 被触发,也可以在服务器和客户端的日志中看到。
问题是在Git中更新的属性没有在客户端更新。在 Spring Boot 1.5.22.RELEASE
和 Spring Cloud Brixton.SR7
的基础上,它工作可靠。
但是如果我手动执行 curl -X POST http://localhost:8889/actuator/bus-refresh
,属性 将被更新。
可能是什么问题或者我忘记配置哪个 属性?
这是我的 configuration/code:
Git实验室(作为 Docker 容器启动)
推送 WebHook:http://user:password@localhost:8889/monitor
RabbitMQ(作为 Docker 容器启动)
无特殊配置
pom.xml
Config服务器和客户端的根模块:
<?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">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>spring-config-server</module>
<module>spring-config-client</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.myorg</groupId>
<artifactId>spring-config-mgmt</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud-dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<properties>
<spring-cloud-dependencies.version>Hoxton.SR3</spring-cloud-dependencies.version>
</properties>
</project>
配置服务器的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>spring-config-mgmt</artifactId>
<groupId>com.myorg</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-config-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-monitor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置客户端的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>spring-config-mgmt</artifactId>
<groupId>com.myorg</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-config-client</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
配置服务器:
server:
port: 8889
spring:
cloud:
config:
server:
git:
uri: git@localhost:root/springcloudconfig.git
clone-on-start: true
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
logging:
level:
org.springframework: DEBUG
配置客户端的application.yml
:
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
logging:
level:
org.springframework: TRACE
配置客户端的bootstrap.properties
:
spring.application.name=config-client
spring.profiles.active=development
spring.cloud.config.uri=http://localhost:8889
spring.cloud.config.fail-fast=true
management.security.enabled=false
配置服务器:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
配置客户端:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.scheduling.annotation.Scheduled;
@SpringBootApplication
@RefreshScope
@EnableScheduling
public class Application {
@Value("${myProperty}")
private String myProperty;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Scheduled(fixedDelay =1000)
public void printProperty() {
System.out.println("Value of property \"myProperty\": " + myProperty);
}
}
非常感谢
在 bootstrap.properties
中设置 属性 spring.cloud.bus.id
解决了问题:
https://github.com/spring-cloud/spring-cloud-bus/issues/124#issuecomment-423960553
不是很漂亮,但很管用。
首先你应该设置你的配置服务器名称(例如:spring.application.name= configServer
)
在配置服务器集(示例:spring.cloud.bus.id = configServer:9000
)中,端口当然应该是您的配置服务器的端口。
我正在尝试将 Spring Cloud Config Server
和 RabbitMQ
的简单示例代码迁移为 Spring Cloud Bus
(基于 Spring Boot 1.5.22.RELEASE
和 Spring Cloud Brixton.SR7
)到 Spring Boot 2.2.6.RELEASE
和 Spring Cloud Hoxton.SR3
。该示例由配置服务器、配置客户端、GitLab
作为 SCM 和 RabbitMQ
(3.8 - Erlang 22.1.5) 组成。代码正在编译、启动、推送 webhook 被触发,也可以在服务器和客户端的日志中看到。
问题是在Git中更新的属性没有在客户端更新。在 Spring Boot 1.5.22.RELEASE
和 Spring Cloud Brixton.SR7
的基础上,它工作可靠。
但是如果我手动执行 curl -X POST http://localhost:8889/actuator/bus-refresh
,属性 将被更新。
可能是什么问题或者我忘记配置哪个 属性?
这是我的 configuration/code:
Git实验室(作为 Docker 容器启动) 推送 WebHook:http://user:password@localhost:8889/monitor
RabbitMQ(作为 Docker 容器启动) 无特殊配置
pom.xml
Config服务器和客户端的根模块:
<?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">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>spring-config-server</module>
<module>spring-config-client</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.myorg</groupId>
<artifactId>spring-config-mgmt</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud-dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<properties>
<spring-cloud-dependencies.version>Hoxton.SR3</spring-cloud-dependencies.version>
</properties>
</project>
配置服务器的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>spring-config-mgmt</artifactId>
<groupId>com.myorg</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-config-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-monitor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置客户端的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>spring-config-mgmt</artifactId>
<groupId>com.myorg</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-config-client</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
配置服务器:
server:
port: 8889
spring:
cloud:
config:
server:
git:
uri: git@localhost:root/springcloudconfig.git
clone-on-start: true
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
logging:
level:
org.springframework: DEBUG
配置客户端的application.yml
:
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
logging:
level:
org.springframework: TRACE
配置客户端的bootstrap.properties
:
spring.application.name=config-client
spring.profiles.active=development
spring.cloud.config.uri=http://localhost:8889
spring.cloud.config.fail-fast=true
management.security.enabled=false
配置服务器:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
配置客户端:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.scheduling.annotation.Scheduled;
@SpringBootApplication
@RefreshScope
@EnableScheduling
public class Application {
@Value("${myProperty}")
private String myProperty;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Scheduled(fixedDelay =1000)
public void printProperty() {
System.out.println("Value of property \"myProperty\": " + myProperty);
}
}
非常感谢
在 bootstrap.properties
中设置 属性 spring.cloud.bus.id
解决了问题:
https://github.com/spring-cloud/spring-cloud-bus/issues/124#issuecomment-423960553
不是很漂亮,但很管用。
首先你应该设置你的配置服务器名称(例如:spring.application.name= configServer
)
在配置服务器集(示例:spring.cloud.bus.id = configServer:9000
)中,端口当然应该是您的配置服务器的端口。