Spring的@RefreshScope是否损坏,或者我使用的是错误的?

时间:2018-07-14 01:24:12

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

我有一个运行@EnableConfigServer的Spring Config Server,它运行正常

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

然后我也有运行@SpringBootApplication和@RefreshScope的客户端

@SpringBootApplication
@RefreshScope
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

以及我的RestController

@RestController
@RefreshScope
public class LicenseServiceController {
   @Value("${some.property}")
   String someProperty;

   @GetMapping("/someProperty")
   public String getSomeProperty() {
      return someProperty;
   }
}

现在,我已经使Config Server与我的私有github repo端点进行对话以获取配置属性,并且它做得非常好,例如,如果我命中了端点http://localhost:8888/y/default,它将把这个代码片段还给我

propertySources: [
{
      name: "https://github.com/me/config/x/y.yml",
      source: {
         some.property: "hi"
      }
}
]

一切正常,当我通过@Value标签点击使用该属性的端点时,从客户端获取@Value($ {some.property})

的喜好

问题是当我更新git repo并将some.property更改为“ bye”时。如果我再次从客户端访问端点,我将再次为@Value($ {some.property})返回“ hi”,而不是“ bye”。

然后在配置服务器端点http://localhost:8888/y/default中,我得到

propertySources: [
{
      name: "https://github.com/me/config/x/y.yml",
      source: {
         some.property: "bye"
      }
}
]

在客户端时,我仍然对@Value($ {some.property})表示“喜” 即使我使用POST请求从客户端点击/ actuators / refresh端点

这是我从客户端(SpringBootApplication)获得的调试日志,在我看来这像POST请求一样有效,我也从curl命令获得了正确的响应,至少我认为

 INFO 13493 --- [nio-8080-exec-2] trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$b4dd7d61] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
 INFO 13493 --- [nio-8080-exec-2] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888 
DEBUG 13493 --- [nio-8080-exec-2] o.s.web.client.RestTemplate              : Created GET request for "http://localhost:8888/y/default"
DEBUG 13493 --- [nio-8080-exec-2] o.s.web.client.RestTemplate              : Setting request Accept header to [application/json, application/*+json]
DEBUG 13493 --- [nio-8080-exec-2] o.s.web.client.RestTemplate              : GET request for "http://localhost:8888/y/default" resulted in 200 (null)
DEBUG 13493 --- [nio-8080-exec-2] o.s.web.client.RestTemplate              : Reading [class org.springframework.cloud.config.environment.Environment] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@2e68f713]
 INFO 13493 --- [nio-8080-exec-2] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=y, profiles=[default], label=null, version=948c431771g06325k3520x67k70z74xa2829ts33, state=null
 INFO 13493 --- [nio-8080-exec-2] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://github.com/me/tmp-config/x/y.yml'}]}
 INFO 13493 --- [nio-8080-exec-2] o.s.boot.SpringApplication               : The following profiles are active: default
 INFO 13493 --- [nio-8080-exec-2] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@6a81dcff: startup date [Fri Jul 13 21:02:29 EDT 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@7ab876b9
 INFO 13493 --- [nio-8080-exec-2] o.s.boot.SpringApplication               : Started application in 0.202 seconds (JVM running for 24.359)
DEBUG 13493 --- [nio-8080-exec-2] o.s.w.c.s.StandardServletEnvironment     : Replacing PropertySource 'bootstrapProperties' with 'bootstrapProperties' 
DEBUG 13493 --- [nio-8080-exec-2] o.s.w.c.s.StandardServletEnvironment     : Replacing PropertySource 'random' with 'random'
DEBUG 13493 --- [nio-8080-exec-2] o.s.w.c.s.StandardServletEnvironment     : Replacing PropertySource 'applicationConfig: [classpath:/application.yml]' with 'applicationConfig: [classpath:/application.yml]'
DEBUG 13493 --- [nio-8080-exec-2] o.s.w.c.s.StandardServletEnvironment     : Replacing PropertySource 'springCloudClientHostInfo' with 'springCloudClientHostInfo'
DEBUG 13493 --- [nio-8080-exec-2] o.s.w.c.s.StandardServletEnvironment     : Replacing PropertySource 'applicationConfig: [classpath:/bootstrap.yml]' with 'applicationConfig: [classpath:/bootstrap.yml]'
DEBUG 13493 --- [nio-8080-exec-2] o.s.w.c.s.StandardServletEnvironment     : Replacing PropertySource 'defaultProperties' with 'defaultProperties
DEBUG 13493 --- [nio-8080-exec-2] o.s.w.c.s.StandardServletEnvironment     : Replacing PropertySource 'defaultProperties' with 'defaultProperties'
 INFO 13493 --- [nio-8080-exec-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@6a81dcff: startup date [Fri Jul 13 21:02:29 EDT 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@7ab876b9
 INFO 13493 --- [nio-8080-exec-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@7ab876b9: startup date [Fri Jul 13 21:02:29 EDT 2018]; root of context hierarchy
DEBUG 13493 --- [nio-8080-exec-2] m.m.a.RequestResponseBodyMethodProcessor : Written [[config.client.version, some.property]] as "application/vnd.spring-boot.actuator.v2+json" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@67770b37]
DEBUG 13493 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
DEBUG 13493 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Successfully completed request

现在,我唯一能得出的结论是端点URL错误,因为在日志中它说“ https://github.com/me/config/x/y.yml

,但是在提供404的浏览器中,因为它应该改为“ https://github.com/me/config/blob/master/x/y.yml”,但这似乎是Spring编写者不太可能的错误。似乎我做对了所有事情,但还是不知所措

1 个答案:

答案 0 :(得分:1)

您应该刷新Bean,而不是尝试刷新整个应用程序以及它加载的所有配置> Bean。

从Spring文档中,

https://cloud.spring.io/spring-cloud-static/docs/1.0.x/spring-cloud.html#_refresh_scope

  

@RefreshScope(技术上)在@Configuration类上工作,但可能会导致令人惊讶的行为:这并不意味着该类中定义的所有@Beans本身都是@RefreshScope。具体来说,依赖于那些Bean的任何事物都不能依赖于刷新启动时对其进行更新,除非它本身在@RefreshScope中(在刷新中将其重建并重新注入其依赖项,此时它们将被刷新)从刷新的@Configuration重新初始化。)

您应该做的是在将值注入到任何地方时刷新bean。