首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Spring 2中不工作的Config客户端的自动刷新

在Spring 2中不工作的Config客户端的自动刷新
EN

Stack Overflow用户
提问于 2020-04-06 15:50:19
回答 2查看 5.7K关注 0票数 2

我正在尝试将Spring Cloud Config ServerRabbitMQ作为Spring Cloud Bus (基于Spring Boot 1.5.22.RELEASESpring Cloud Brixton.SR7)的简单示例代码迁移到Spring Boot 2.2.6.RELEASESpring Cloud Hoxton.SR3。该示例由Config服务器、Config客户端、GitLab作为SCM和RabbitMQ(3.8-Erlang22.1.5)组成。代码正在编译、启动、推送网络钩子被触发,也可以在服务器和客户端的日志中看到。

问题是,在Git中更新的属性在客户端中没有更新。在Spring Boot 1.5.22.RELEASESpring Cloud Brixton.SR7的基础上,工作可靠。但是如果我手动执行curl -X POST http://localhost:8889/actuator/bus-refresh,则该属性将被更新。

问题是什么,或者我忘记配置哪些属性?

下面是我的配置/代码:

GitLab (从码头容器开始) Push WebHook:http://user:password@localhost:8889/monitor

RabbitMQ (启动为Docker容器)无特定配置

Config服务器和客户端的pom.xml 根模块:

代码语言:javascript
复制
<?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>

Config服务器的pom.xml

代码语言:javascript
复制
<?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>

Config客户端的pom.xml

代码语言:javascript
复制
<?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>

Config服务器的application.yml

代码语言:javascript
复制
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

Config客户端的application.yml

代码语言:javascript
复制
spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

logging:
  level:
    org.springframework: TRACE

Config客户端的bootstrap.properties

代码语言:javascript
复制
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

Config Server:

代码语言:javascript
复制
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);
    }
}

Config客户端:

代码语言:javascript
复制
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);
    }
}

事先非常感谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-07 12:42:32

spring.cloud.bus.id中设置属性bootstrap.properties修复了问题:https://github.com/spring-cloud/spring-cloud-bus/issues/124#issuecomment-423960553

不太漂亮但很管用。

票数 0
EN

Stack Overflow用户

发布于 2021-03-28 17:45:45

首先,您应该设置配置服务器名称(例如:spring.application.name= configServer)和配置服务器集(例如:spring.cloud.bus.id = configServer:9000),当然端口应该是配置服务器的端口。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61063717

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档