简体   繁体   中英

Properties of custom Spring Cloud Service Connector not published to cloud.services.*

I created a custom Spring Cloud Service Connector by defining the following two classes:

@ServiceLabel("kafka") 
public class KafkaServiceInfo extends BaseServiceInfo {

    private static Logger logger = Logger.getLogger(KafkaServiceInfo.class.getName());

    public static final String BROKERS = "brokers";

    public static final String REGISTRY = "schemaregistry";

    protected List<String> brokers;

    protected String registry;

    public KafkaServiceInfo(String id, List<String> brokers, String registry) {
        super(id);
        this.brokers = brokers;
        this.registry = registry;
    }

    @ServiceProperty
    public String getRegistry() {
        return registry;
    }

    @ServiceProperty
    public List<String> getBrokers() {
        return brokers;
    }
}

And this class:

public class KafkaServiceInfoCreator extends CloudFoundryServiceInfoCreator<KafkaServiceInfo> {

    private static Logger logger = Logger.getLogger(KafkaServiceInfoCreator.class.getName());

    public static final String USER_PROVIDED_SERVICE_NAME = "kafka";

    public KafkaServiceInfoCreator() {
        super(new Tags(USER_PROVIDED_SERVICE_NAME), null);
    }

    public KafkaServiceInfo createServiceInfo(Map<String, Object> serviceData) {
        String id = getId(serviceData);
        Map<String, Object> credentials = getCredentials(serviceData);

        List<String> brokers = (List<String>) credentials.get(KafkaServiceInfo.BROKERS);
        String registry = (String) credentials.get(KafkaServiceInfo.REGISTRY);

        logger.info("KafkaServiceInfo created for Cloud Foundry Service \"" + id + "\"");
        logger.info("Kafka Brokers configured for Cloud Foundry Service: " + brokers.toString());
        logger.info("Schema Registry configured for Cloud Foundry Service: " + registry);

        return new KafkaServiceInfo(id, brokers, registry);
    }

    @Override
    public boolean accept(Map<String, Object> serviceData) {
        return getId(serviceData).contains(USER_PROVIDED_SERVICE_NAME);
    }
}

On my PCF instance, I created a user-provided service that looks in the VCAPS env variables like this:

"user-provided": [
  {
    "credentials": {
      "brokers": [
        "<some-ip-here>:29092"
      ],
      "schemaregistry": "http://<some-ip-here>:8081"
    },
    "label": "user-provided",
    "name": "kafka",
    "syslog_drain_url": "",
    "tags": [],
    "volume_mounts": []
  }
]

I also added the service definition file into the META-INF folder.

src/main/resources/META-INF/services/org.springframework.cloud.cloudfoundry.CloudFoundryServiceInfoCreator

with the content:

path.to.my.file.KafkaServiceInfoCreator

I would now expect to see the properties whose getters are annotated with @ServiceProperty in the cloud.services.kafka.* path. However, they do not show up. Instead, I just have the following 2 entries:

"cloud.services.kafka.id": "kafka",
"cloud.services.null.id": "kafka",

I am wondering what is going wrong here and also why I have the second entry with the "null" in between.

Any ideas what could be wrong here? The classes somehow seem to be found since I get the log messages defined in the creator class above.

Regards, Lars

The @ServiceProperty annotation expects to be provided with either a category or name attribute. These values are used to build the key that is placed into the cloud.services map. If neither category or name attribute are provided, the @ServiceProperty annotation does not result in the property appearing in the map.

A typical usage is @ServiceProperty(category="connection") , with the name defaulting to the name of the property using bean naming rules. In your case, adding the category="connection" attribute should result in

"cloud.services.kafka.id": "kafka", "cloud.services.kafka.connection.registry": "http://<some-ip-here>:8081", "cloud.services.kafka.connection.brokers": ["<some-ip-here>:29092"],

I'm not sure yet where the "cloud.services.null.id": "kafka" entry is coming from. Let me know if that still appears after adding the attributes to the @ServiceProperty annotation.

After digging quite a bit deeper, I found the "reason" explained here .

The cloud foundry java buildpack includes the auto-reconfiguration library which by itself contains a copy of the org.springframework.cloud namespace. This copy does NOT consider any custom ServiceInfo classes.

An since it is also this auto-reconfiguration that exposes the cloud.services.* properties to the environment, my personal ones are not picked-up and exposed as well. So I will switch this auto-reconfiguration off and configure the required stuff manually.

The spring cloud connector documentation is misleading here as well, since the properties in cloud.service.* are only added by java auto reconfiguration to the environment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM