1

A project I'm working on is utilizing Spring Cloud Config server to handle property update/refresh.

One question that has repeatedly come up is how to reference/serve plain text from the config server.

I know that the server supports serving plain-text. What I'm trying to figure out is that if I have a reference /foo/default/master/logj42.xml.

How would I reference this in an "agnostic" way such that if I were to put:

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

The reference {configserver} would be expanded.

Additionally, when using "discovery", if I inject the reference to the "resource" as above, the default mechanism will attempt to use java.net.URLConnection to load the content. I do not think it will resolve the 'discovery' host.

Thanks in advance.

Dave G
  • 9,639
  • 36
  • 41

2 Answers2

3

It also can be resolved using Customizing Bootstrap Configuration without aspects by creating custom property source and set configserver uri after locating from discovery. I had similar issue, more details in this stackoverflow post

nmyk
  • 1,582
  • 1
  • 8
  • 20
  • This definitely works, however in the environment I'm working in we have a large number of apps that would need to consume this change. By putting it on the app embedding the server, all apps can leverage it transparently – Dave G Apr 18 '18 at 12:16
0

I found a way to do this that is minimally invasive but "pierces the veil" of where the config server actually resides.

On the primary application class, the annotation @EnableDiscoveryClient needs to be added.

I created an aspect to add a property source with a key that indicates the actual URI of the server handling the request:

@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;
    }
}

By doing this, I expose a key configserver.instance.uri which can then be referenced from within a property value and interpolated/resolved on the client.

This has some ramifications with regard to exposing the actual configuration server, but for resolving resources that do not necessarily utilize the discovery client this can be utilized.

Dave G
  • 9,639
  • 36
  • 41