我需要使用信任商店在Google Cloud Dataflow中建立SSL Kafka连接。我可以从存储桶中提供此信息,还是可以将其存储在“本地文件系统”中?
答案 0 :(得分:3)
您可以使用KafkaIO.Read.withConsumerFactoryFn提供将被调用以创建Kafka使用者的工厂函数。在该功能中,您可以自由地做任何您喜欢的事情,例如:您可以从GCS存储桶下载信任存储文件(我建议使用GcsUtil)并将其保存到本地磁盘上的临时文件中 - AFAIK Kafka本身仅支持在本地磁盘上存储此文件。然后手动创建KafkaConsumer
并将其指向文件。
答案 1 :(得分:1)
感谢@jkff提供解决方案,这是一个实现示例:
样本ConsumerFactoryFn实现:
private static class ConsumerFactoryFn
implements SerializableFunction<Map<String, Object>, Consumer<byte[], byte[]>>
{
public Consumer<byte[], byte[]> apply(Map<String, Object> config)
{
try
{
Storage storage = StorageOptions.newBuilder()
.setProjectId("prj-id-of-your-bucket")
.setCredentials(GoogleCredentials.getApplicationDefault())
.build()
.getService();
Blob blob = storage.get("your-bucket-name", "pth.to.your.kafka.client.truststore.jks");
ReadChannel readChannel = blob.reader();
FileOutputStream fileOuputStream;
fileOuputStream = new FileOutputStream("/tmp/kafka.client.truststore.jks"); //path where the jks file will be stored
fileOuputStream.getChannel().transferFrom(readChannel, 0, Long.MAX_VALUE);
fileOuputStream.close();
File f = new File("/tmp/kafka.client.truststore.jks"); //assuring the store file exists
if (f.exists())
{
LOG.debug("key exists");
}
else
{
LOG.error("key does not exist");
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
LOG.error( e.getMessage());
} catch (IOException e) {
// TODO Auto-generated catch block
LOG.error( e.getMessage());
}
config.put("ssl.truststore.location",(Object) "/tmp/kafka.client.truststore.jks" );
return new KafkaConsumer<byte[], byte[]>(config);
}
}
并且不要忘记在您的KafkaIO.read()调用中使用.withConsumerFactoryFn,应该类似于:
Map<String, Object> configMap = new HashMap<String, Object>();
configMap.put("security.protocol", (Object) "SSL");
configMap.put("ssl.truststore.password", (Object) "clientpass");
p.apply("ReadFromKafka", KafkaIO.<String, String>read()
.withBootstrapServers("ip:9093")
.withTopic("pageviews")
.withKeyDeserializer(StringDeserializer.class)
.withValueDeserializer(StringDeserializer.class)
.updateConsumerProperties(configMap)
.withConsumerFactoryFn(new ConsumerFactoryFn()) ... etc.