我通过定义以下两个类来创建自定义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;
}
}
这节课:
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);
}
}
在我的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": []
}
]
我还将服务定义文件添加到META-INF文件夹中。
的src /主/资源/ META-INF /服务/ org.springframework.cloud.cloudfoundry.CloudFoundryServiceInfoCreator
内容:
path.to.my.file.KafkaServiceInfoCreator
我现在希望在cloud.services.kafka。*路径中看到其getter使用@ServiceProperty注释的属性。但是,他们没有出现。相反,我只有以下两个条目:
"cloud.services.kafka.id": "kafka",
"cloud.services.null.id": "kafka",
我想知道这里出了什么问题以及为什么我的第二个条目之间有“null”。
任何想法在这里可能是错的?因为我得到了上面创建者类中定义的日志消息,所以似乎找到了这些类。
问候,Lars
答案 0 :(得分:1)
id | PROJECT NAME| REQUEST| DATE
2 ----- bla -------- 3 ---- 4-2-17
5 ----- bla -------- 5 ---- 6-2-17
7 ----- ble -------- 1 ---- 9-2-17
9 ----- ble -------- 3 ---- 6-3-17
注释预计会提供@ServiceProperty
或category
属性。这些值用于构建放置在name
映射中的键。如果未提供cloud.services
或category
属性,则name
注释不会导致该属性显示在地图中。
典型用法是@ServiceProperty
,其名称使用bean命名规则默认为属性的名称。在您的情况下,添加@ServiceProperty(category="connection")
属性应该会导致
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"],
条目的来源。如果在将属性添加到"cloud.services.null.id": "kafka"
注释后仍然显示,请告诉我。
答案 1 :(得分:0)
在深入挖掘之后,我发现“理由”解释为here。
云代工厂java buildpack包含自动重新配置库,该库本身包含org.springframework.cloud命名空间的副本。此副本不考虑任何自定义ServiceInfo类。
由于也是这种自动重新配置将cloud.services。*属性暴露给环境,我的个人属性也不会被提取和暴露。因此,我将关闭此自动重新配置并手动配置所需的内容。
Spring云连接器文档也存在误导,因为cloud.service。*中的属性仅通过java自动重新配置添加到环境中。