【问题标题】:Is Spring's @RefreshScope broken or am I using it wrong?Spring 的 @RefreshScope 是坏了还是我用错了?
【发布时间】:2018-12-22 09:15:18
【问题描述】:

我有一个使用 @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,它会返回这个 sn-p

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

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

问题是当我更新我的 git 存储库并将 some.property 更改为“再见”时。如果我再次从客户端点击端点,我将再次为 @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 编写者不太可能出现的错误。似乎我做的一切都正确,但不知何故它仍然坏了

【问题讨论】:

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


    【解决方案1】:

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

    来自 Spring 文档,

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

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

    你应该做的是在值被注入的地方刷新 bean。

    【讨论】:

    • 抱歉,我忘了补充一点,我确实将@RefreshScope 也放在了我的 RestController Bean 上,但没有用
    • 你能添加你试图刷新属性的类和配置吗?
    • 好吧,看看我的历史,看来我可能在 Controller 类上注释掉了 RefreshScope,取消注释后忘记测试它,因为通过尝试不同的东西还有许多其他 cmets/未注释的注释.但是谢谢,我会接受这个作为控制器类需要用 RefreshScope 注释的解决方案
    猜你喜欢
    • 1970-01-01
    • 2011-07-21
    • 1970-01-01
    • 2010-12-31
    • 2022-09-30
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多