配置刷新不在路由声明

时间:2016-08-22 14:17:58

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

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

这是我的情景:

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

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

2)客户端中的执行器和控制器中的注释 @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有一些特殊的行为阻止刷新配置吗?

感谢。

1 个答案:

答案 0 :(得分:3)

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

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

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

相关问题