【问题标题】:@RefreshScope not working with external config files@RefreshScope 不适用于外部配置文件
【发布时间】:2020-05-15 15:32:42
【问题描述】:

我在 pom 中添加了以下依赖项,以便可以使用 Spring cloud

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter</artifactId>
    </dependency>

  <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>

将 spring-cloud.version 添加为 Finchley.RELEASE

这是我打算刷新的bean:

@Component
@ConfigurationProperties(ignoreUnknownFields = true,prefix="global")
@PropertySource(value = "${spring.config.location}")
@Getter
@Setter
@Validated
@RefreshScope
public class GeneralProperties {
    private String mode;
}

在我的控制器中访问 bean:

@RestController
@RequestMapping
@RefreshScope
public class AppController {

    private static final AnalyticsLogger LOGGER = AnalyticsLoggerFactory
            .getLogger(AppController.class);

    @Autowired
    SimulatorRequestProcessor simulatorRequestProcessor;

    @Autowired
    GeneralProperties generalProperties;

    @ResponseBody
    @PostMapping(value = "/api/mock-getdata", produces = "application/json")
    public String fetchResponseBasedOnMode(@RequestHeader HttpHeaders headers,
            @RequestBody String request) {
        String response = null;
        LOGGER.info("color: "+color);
        switch (generalProperties.getMode()) {
         //code

在应用程序配置中设置的属性:

 spring.config.location=file:///var/lib/data/demo/config/generalConfig.properties
 management.endpoints.web.exposure.include=refresh

无法刷新模式的值。有人可以指出此代码中的错误。我的属性文件位于 git 中未提交的文件夹中,因此我没有使用 spring-cloud-config。

【问题讨论】:

  • Finchley 不再受支持,你能用 Hoxton.SR1 再试一次吗?
  • 配置属性 bean 也不需要 PropertySource 和 RefreshScope。控制器上也不需要刷新范围
  • 我正在使用 Spring-boot 2.0.1 RELEASE,所以兼容的 Spring cloud 版本是 Finchley
  • 从那以后的 11 个版本中可能已经修复了一些问题。
  • Boot 2.0.x 也不再支持

标签: spring spring-cloud spring-cloud-config


【解决方案1】:

把这个放到src/main/resources/application.properties

management.endpoints.web.exposure.include=*

用这个参数--spring.config.additional-location=/tmp/application.properties运行这个程序

package com.example.demorefreshscopeexternalfile;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.core.style.ToStringCreator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
@EnableConfigurationProperties(DemorefreshscopeexternalfileApplication.MyProps.class)
public class DemorefreshscopeexternalfileApplication {

    @Autowired
    MyProps props;

    @RequestMapping
    public String message() {
        return props.getMessage();
    }

    public static void main(String[] args) {
        SpringApplication.run(DemorefreshscopeexternalfileApplication.class, args);
    }

    @ConfigurationProperties("my")
    public static class MyProps {
        private String message;

        public String getMessage() {
            return this.message;
        }

        public void setMessage(String message) {
            this.message = message;
        }

        @Override
        public String toString() {
            return new ToStringCreator(this)
                    .append("message", message)
                    .toString();

        }
    }
}

/tmp/application.properties的内容

my.message=message from /tmp

查看http://localhost:8080

/tmp/application.properties 可以更新,发帖到/actuator/refresh 并再次查看8080,您将看到更新后的值。

使用 boot 2.2.4 和 Hoxton.SR1 构建

请参阅https://docs.spring.io/spring-boot/docs/2.2.4.RELEASE/reference/html/appendix-application-properties.html#common-application-properties 了解spring.config.* 及其含义。

【讨论】:

  • /tmp/application.properties 是否放在类路径下?因为我的要求是使用外部文件,而这种方法不适用于此。
  • 不,它不在类路径中。这是一个Linux临时目录
猜你喜欢
  • 2017-03-06
  • 1970-01-01
  • 2015-03-11
  • 2014-10-08
  • 2013-06-15
  • 1970-01-01
  • 1970-01-01
  • 2019-05-27
  • 2023-03-29
相关资源
最近更新 更多