代码之家  ›  专栏  ›  技术社区  ›  katiex7

spring的refreshScope坏了还是我用错了?

  •  0
  • katiex7  · 技术社区  · 6 年前

    我有一个运行@enableconfigserver的spring配置服务器,它运行得很好

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

    然后我还有一个客户端,运行@springbootsapplication和@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;
       }
    }
    

    现在,我已经让配置服务器与我的私有github repo端点进行了对话,以检索配置属性,它做得非常好,例如,如果我命中了我的端点 http://localhost:8888/y/default ,它会把这个片段还给我

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

    这一切都很好,当我从客户端点击使用属性by@value标记的端点时,我得到@value(${some.property})的hi。

    问题是当我更新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})获得“hi” 即使我使用post请求从客户端点击/actuators/refresh端点

    这是我从客户端(springbootsapplication)得到的调试日志,在我看来,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 相反,但这似乎是一个不太可能的虫子从春天的作家。好像我做的一切都是对的,但不知怎的它还是坏了

    1 回复  |  直到 6 年前
        1
  •  1
  •   Darren Forsythe    6 年前

    您应该刷新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。