【问题标题】:Is there a symbolic item to reference spring-cloud config server?是否有引用 spring-cloud 配置服务器的象征性项目?
【发布时间】:2018-03-29 14:33:37
【问题描述】:

我正在做的一个项目是利用 Spring Cloud Config 服务器来处理属性更新/刷新。

一个反复出现的问题是如何从配置服务器引用/提供纯文本。

我知道服务器支持serving plain-text。我想弄清楚的是,如果我有一个参考 /foo/default/master/logj42.xml。

我将如何以“不可知论”的方式引用它,如果我要说:

{configserver}/foo/default/master/log4j2.xml in the config file

引用 {configserver} 将被扩展。

另外,当使用“发现”时,如果我像上面那样注入对“资源”的引用,默认机制将尝试使用 java.net.URLConnection 来加载内容。我认为它不会解决“发现”主机。

提前致谢。

【问题讨论】:

    标签: spring-cloud-config


    【解决方案1】:

    它也可以使用Customizing Bootstrap Configuration 来解决,方法是创建自定义属性源并在从发现中定位后设置 configserver uri。 我有类似的问题,更多细节在this stackoverflow post

    【讨论】:

    • 这绝对有效,但是在我工作的环境中,我们有大量的应用程序需要使用此更改。通过将其放在嵌入服务器的应用程序上,所有应用程序都可以透明地利用它
    【解决方案2】:

    我找到了一种方法来做到这一点,它的侵入性微乎其微,但“揭开了配置服务器实际驻留位置的面纱”。

    在主应用类上,需要添加注解@EnableDiscoveryClient。

    我创建了一个方面来添加一个属性源,其键指示处理请求的服务器的实际 URI:

    @Component
    @Aspect
    public class ResolverAspect {
        @Autowired
        private DiscoveryClient discoveryClient;
    
        @Pointcut("execution(org.springframework.cloud.config.environment.Environment org.springframework.cloud.config.server.environment.EnvironmentController.*(..))
        private void environmentControllerResolve();
    
        @Around("environmentControllerResolve()")
        public Object environmentControllerResolveServer(final ProceedingJoinPoint pjp) throws Throwable {
            final Environment pjpReturn = (Environment)pjp.proceed();
            final ServiceInstance localSErviceInstance = discoveryClient.getLocalServiceInstance();
            final PropertySource instancePropertySource =
                new PropertySource("cloud-instance", Collections.singletonMap("configserver.instance.uri", localServiceInstance.getUri().toString()));
            pjpReturn.addFirst(instancePropertySource);
            return pjpReturn;
        }
    }
    

    通过这样做,我公开了一个键 configserver.instance.uri,然后可以从属性值中引用它并在客户端上插值/解析。

    这对暴露实际配置服务器有一些影响,但对于解决不一定使用发现客户端的资源,可以使用它。

    【讨论】:

      猜你喜欢
      • 2017-09-07
      • 2019-02-10
      • 2016-02-15
      • 2017-07-25
      • 2023-03-24
      • 1970-01-01
      • 2015-01-03
      • 2018-08-24
      • 2016-09-12
      相关资源
      最近更新 更多