1

구성 서버에서 spring-cloud를 사용하고 있습니다. 다시 시작하지 않고도 앱의 구성을 새로 고침하고 싶습니다. application.yml에서경로 새로 고침 내에서 구성 새로 고침이 작동하지 않습니다.

1) 단일 구성, 클라이언트 및 주석 @RefreshScope자식

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

2) 액추에이터에 저장 :

내 시나리오입니다 컨트롤러에서. 봄과 통합

@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) 하나의 흐름 :

@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) I 구성을 업데이트하고

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

5 이눔 아 밀어)를 실행 새로 고침

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

컨트롤러에 대한 나머지 호출에서 everyth 구성이 업데이트되었습니다.

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

그러나 통합 흐름에 나머지 통화에서

의 설정은 업데이트되지 않은 :
[LOGGING DEMO] My ID is 123 

구성을 새로 방해하는 빈의 일부 특정 행동이있다?

감사합니다.

답변

3

@RefreshScope@Configuration 클래스를 추가하면 해당 범위에 선언 된 빈을 넣을 수 있다고 생각하지 않습니다.

또한 IntegrationFlow@Bean은 내부적으로 여러 개의 빈을 생성하며 해당 범위에 들어 가지 않습니다. 통합 플로우를 "새로 고"시도하면 안되기 때문에 런타임 문제가 발생할 수 있습니다.

대신에 흐름과 다른 클래스에 속성을 넣고 그 값을 DemoHandler@Bean에 삽입해야합니다.

+0

멋진 설명에 감사드립니다. 게리. –

+1

또한'job.demo' 키를 전용'@ ConfigurationProperties' 클래스에 넣으면'@ Value'에'@ RefreshScope'가 필요 없습니다. – spencergibb