1

我正在使用来自 spring-cloud 的配置服务器。我希望刷新应用程序的配置而不必重新启动它。

这是我的场景:

1)application.yml中的单一配置,存储在git中

job:
 demo:
  testMessage: 'My ID is 123'

2)客户端中的Actuator和控制器中的注解@RefreshScope

@RefreshScope
@Component
@RestController
public class DemoController {

  @Value("${job.demo.testMessage}")
  String testMessage;

  @RequestMapping(value = "/", produces = "application/json")
  public List<String> index() {
      List<String> env = Arrays.asList(
            "config 1 is: " + testMessage
      );
      return env;
  }
}

3) Spring Integration 的一个流程:

@RefreshScope
@Slf4j
@Setter
@Component
@ConfigurationProperties(prefix = "job.demo")
public class DemoFlow {

    private String testMessage;

    @Bean
    public IntegrationFlow putDemoModelFlow() {
        return IntegrationFlows.from(Http.inboundChannelAdapter("/demoFlow"))
            .handle(new DemoHandler())
            .handle(m -> log.info("[LOGGING DEMO] {}" , m.getPayload()))
            .get();
    }

    private class DemoHandler implements GenericHandler {

        @Override
        public String handle(Object payload, Map headers) {
            return new StringBuilder().append(testMessage)
                .append(" ").toString();
        }
    }
}

4)我更新配置并推送到git

job:
 demo:
  testMessage: 'My ID is 789'

5)运行刷新

curl -d{} http://localhost:8002/refresh

在对控制器的其余调用中,一切正常,配置已更新。

["config 1 is: My ID is 789"]

但是在对集成流程的其余调用中,配置没有更新:

[LOGGING DEMO] My ID is 123

bean 的某些特殊行为会阻止刷新配置?

谢谢。

4

1 回答 1

3

我不相信将@Configuration类放入@RefreshScope会将其中声明的 bean 放入该范围。

此外,在IntegrationFlow @Bean内部生成了许多 bean,它们肯定不会放在那个范围内。您不应尝试“刷新”集成流,这可能会导致运行时问题。

相反,您应该将该属性放在与流不同的类中,并将其注入到您的DemoHandler @Bean.

于 2016-08-22T15:07:37.097 回答