【问题标题】:Refresh Scope without config Server没有配置服务器的刷新范围
【发布时间】:2022-01-16 08:09:08
【问题描述】:

我想在运行时更新数据库时刷新 bean,所以想使用 /refresh 端点手动触发它并用新值刷新 bean。

但目前我们没有使用配置服务器 - 任何人都可以指导我如何只能使用刷新范围

我只想使用刷新范围而不需要配置服务器。

例如,我的 bean 从数据库中获取值

@Configuration
public class AppConfiguration {
    @Autowired
    MyRepository myRepo;

    @Bean
    @RefreshScope
    DemoWebClient getDemoWebClient() {
        String currentTime = new Date().getTime() + "";
        return new DemoWebClient(myRepo.getTenantId());
    }

}

尝试使用spring cloud starter

  • 但它正在尝试连接配置服务器
2022-01-16 13:25:10.733  INFO 17884 --- [-144.54.177.213] c.c.c.ConfigServicePropertySourceLocator : Connect Timeout Exception on Url - http://localhost:8888. Will be trying the next url if available
2022-01-16 13:25:10.733  WARN 17884 --- [-144.54.177.213] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: I/O error on GET request for "http://localhost:8888/application/default": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect
2022-01-16 13:25:13.836  INFO 17884 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
tried to disable config server lookup - didn't work

spring.cloud.config.enabled=false
spring.cloud.config.import-check.enabled=false

下面是我的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>RefreshScopeDemoApp</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>RefreshScopeDemoApp</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
        <spring-cloud.version>Hoxton.SR8</spring-cloud.version>
    </properties>
    <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>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>

</project>

【问题讨论】:

  • 使用 spring-cloud-starter 而不是 spring-cloud-starter-config
  • 是的,我在改用 spring-cloud-starter 后犯了错误。

标签: java spring spring-boot spring-cloud spring-cloud-config


【解决方案1】:

您需要在您的应用程序上启用执行器。然后你就可以调用你应用的端点/actuator/refresh来刷新需要刷新的bean了。

您必须了解的重要一点是,必须在配置服务器的客户端应用程序上启用执行器,即连接到配置服务器的应用程序而不是实际的配置服务器。

【讨论】:

    猜你喜欢
    • 2013-10-06
    • 2016-01-18
    • 2021-09-12
    • 1970-01-01
    • 1970-01-01
    • 2020-02-21
    • 2016-02-07
    • 2017-03-20
    • 1970-01-01
    相关资源
    最近更新 更多