spring-cloud / spring-cloud-consul

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

Any changes with metadatas (tags) for Consul in Spring Cloud 2020 #695

Closed HaojunRen closed 3 years ago

HaojunRen commented 3 years ago

Hello Team,

It seems that the followings config of tags can't work in 2020 version, but it works fine in old Spring Cloud version

spring.cloud.consul.discovery.tags=version=1.0,region=dev,env=env1,zone=zone1

Would you please tell me how to set tags in Spring Cloud 2020? Thanks a lot

spencergibb commented 3 years ago

Duplicates #682

HaojunRen commented 3 years ago

Yeah, it works fine with ConsulServiceInstance

ConsulServiceInstance consulServer = (ConsulServiceInstance) server;

Map<String, String> metadata = new HashMap<String, String>();
List<String> tags = consulServer.getTags();
for (String tag : tags) {
    String[] tagArray = StringUtils.splitByWholeSeparator(tag, "=");
    metadata.put(tagArray[0], tagArray[1]);
}

but i want to get metadata of local service just like following code:

    @Autowired
    protected Registration registration;

    public Map<String, String> getMetadata() {
        return registration.getMetadata();
    }

it can't work

HaojunRen commented 3 years ago

BTW, i think it is better to convert tag list to tag map in consulServer.getMetadata method

spencergibb commented 3 years ago

I don't think it is. Tags as metadata was a workaround.

spencergibb commented 3 years ago

ConsulServiceRegistration has access to a NewService object which has access to tags

HaojunRen commented 3 years ago

Now current solution is not so friendly and compatible to customers if they upgrades version from low Spring Cloud version

registration.getMetadata() -> not return customer's metadata consulServer.getMetadata() -> not return customer's metadata

spencergibb commented 3 years ago

The previous version had a flag to allow migration to metadata from tags

HaojunRen commented 3 years ago

I want to keep the same behavior with EurekaServiceInstance and ZookeeperServiceInstance. Why getMetadata is work fine in EurekaServiceInstance and ZookeeperServiceInstance, but failed in ConsulServiceInstance?

spencergibb commented 3 years ago

Eureka already had a map as metadata. Zookeeper serialized anything we wanted so we made it a map. Consul, early on only had tags, then it got real metadata. So as part of our breaking changes in our major release we migrated off of the hack.

HaojunRen commented 3 years ago

It works with following formats input: spring.cloud.consul.discovery.metadata.version=1.0 spring.cloud.consul.discovery.metadata.region=dev spring.cloud.consul.discovery.metadata.env=env1

But i still suggest that new spring cloud consul can be compatible with old format input: spring.cloud.consul.discovery.tags=version=1.0,region=dev,env=env1