简体   繁体   English

自定义Spring Cloud Service连接器的属性未发布到cloud.services。*

[英]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: 我通过定义以下两个类创建了一个定制的Spring Cloud Service Connector:

@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: 在我的PCF实例上,我创建了一个用户提供的服务,该服务在VCAPS env变量中查找如下所示:

"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. 我还将服务定义文件添加到META-INF文件夹中。

src/main/resources/META-INF/services/org.springframework.cloud.cloudfoundry.CloudFoundryServiceInfoCreator SRC /主/资源/ META-INF /服务/ 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. 现在,我希望在cloud.services.kafka。*路径中看到使用@ServiceProperty注释其吸气剂的属性。 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. 我想知道这里出了什么问题,也为什么我要在第二个条目之间插入“ null”。

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. @ServiceProperty批注期望提供有categoryname属性。 These values are used to build the key that is placed into the cloud.services map. 这些值用于构建放置到cloud.services映射中的密钥。 If neither category or name attribute are provided, the @ServiceProperty annotation does not result in the property appearing in the map. 如果没有提供categoryname属性,则@ServiceProperty批注不会导致该属性出现在地图中。

A typical usage is @ServiceProperty(category="connection") , with the name defaulting to the name of the property using bean naming rules. 典型的用法是@ServiceProperty(category="connection") ,其名称使用Bean命名规则默认为属性的名称。 In your case, adding the category="connection" attribute should result in 就您而言,添加category="connection"属性应导致

"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. 我不确定"cloud.services.null.id": "kafka"条目来自何处。 Let me know if that still appears after adding the attributes to the @ServiceProperty annotation. 让我知道将属性添加到@ServiceProperty批注后是否仍然出现。

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. Cloud Foundry Java构建包包括自动重配置库,该库本身包含org.springframework.cloud命名空间的副本。 This copy does NOT consider any custom ServiceInfo classes. 此副本不考虑任何自定义ServiceInfo类。

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. An也是因为这种自动重新配置将cloud.services。*属性暴露给环境,所以我的个人属性也不会被提取和暴露。 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. Spring cloud连接器文档在这里也具有误导性,因为cloud.service。*中的属性仅通过java自动重新配置添加到环境中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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