spring-cloud / spring-cloud-consul

Spring Cloud Consul
http://cloud.spring.io/spring-cloud-consul/
Apache License 2.0
812 stars 541 forks source link

Path configuration ability #711

Closed ns3777k closed 3 years ago

ns3777k commented 3 years ago

Is your feature request related to a problem? Please describe. I've a project that has multiple components(nginx, grafana, prometheus, consul, ...) deployed with docker-compose. Security requirement is to accept https only on single a port so the only entrypoint is nginx. Access to consul api is rewritten so opening https://my-host.local/consul/v1/... . The problem is that I can't specify the path for consul discovery.

Describe the solution you'd like I did a little investigation and found out that ConsulRawClient already supports setting a custom path but the ConsulClient does not. Imo it'd much better to specify custom path in the properties (like host and port) like this:

spring.cloud.consul.port=10000
spring.cloud.consul.host=localhost
spring.cloud.consul.path=/consul

It only requires modifying ConsulProperties and ConsulAutoConfiguration + docs.

Describe alternatives you've considered The alternative way is to write my own ConsulClient building it from the raw client like this:

@Bean
public ConsulClient consulClient(ConsulProperties consulProperties) {
    final int agentPort = consulProperties.getPort();
    final String agentHost = !StringUtils.isEmpty(consulProperties.getScheme())
            ? consulProperties.getScheme() + "://" + consulProperties.getHost() : consulProperties.getHost();

    var consulRawClient = ConsulRawClient.Builder.builder()
            .setHost(agentHost)
            .setPort(agentPort);

    if (consulProperties.getTls() != null) {
        ConsulProperties.TLSConfig tls = consulProperties.getTls();
        TLSConfig tlsConfig = new TLSConfig(tls.getKeyStoreInstanceType(), tls.getCertificatePath(),
                tls.getCertificatePassword(), tls.getKeyStorePath(), tls.getKeyStorePassword());

        consulRawClient.setTlsConfig(tlsConfig);
    }

    consulRawClient.setPath("/consul"); // custom path example

    return new ConsulClient(consulRawClient.build());
}

That works fine.

Additional context As I said, I can reach what I want without any PRs, but the way is kinda ugly. I'd define my own property like spring.cloud.consul.path and inject it manually into the configuration to set the path from it. Imo it'd be much more consistent to allow setting path from properties. I can make a PR if you're ok with the proposal. Thanks in advance!

spencergibb commented 3 years ago

You've done most of the work, fancy to submit a PR?

ns3777k commented 3 years ago

Sure, Will do today later