简体   繁体   English

是否可以将环境变量作为 Kafka kafkaProducer.properties 和 KafkaConsumer.properties 文件中的属性值引用

[英]is it possible to refer environmental variable as values for properties in the kafkaProducer.properties and KafkaConsumer.properties files of Kafka

Im have a producer and consumer java code and Im trying to upgrade it to connect with the Kafka which is secured with SSL.我有一个生产者和消费者的 Java 代码,我试图升级它以与使用 SSL 保护的 Kafka 连接。 I'm in a situation that the ssl related passwords should be given only via environmental variables.我的情况是 ssl 相关密码只能通过环境变量提供。 So is it possible to directly refer to the values refered by Environmental variables in the KafkaProducer.properties and KafkaConsumer.properties files那么是否可以直接引用KafkaProducer.properties和KafkaConsumer.properties文件中环境变量引用的值

For Example: I declare an environmental variable in linux system SSL_KEY_PASSWORD=password例如:我在linux系统中声明了一个环境变量 SSL_KEY_PASSWORD=password

And inside the KafkaProducer/Consumer properties, I declare as, ''' ssl.key.password=${SSL_KEY_PASSWORD} '''在 KafkaProducer/Consumer 属性中,我声明为,''' ssl.key.password=${SSL_KEY_PASSWORD} '''

Sample KAFKA Consumer/Producer property file config may look like,示例 KAFKA 消费者/生产者属性文件配置可能如下所示,

# For SSL
security.protocol=SSL
ssl.truststore.location=/var/private/ssl/client.truststore.jks
ssl.truststore.password=${TRUSTSTORE_PASS_ENV_VARIABLE}
# For SSL auth
ssl.keystore.location=/var/private/ssl/client.keystore.jks
ssl.keystore.password=${KEYSTORE_PASS_ENV_VARIABLE}
ssl.key.password=${KEY_PASS_ENV_VARIABLE}

No, they cannot.不,他们不能。

Unclear how you are using the files or Kafka clients.不清楚您如何使用文件或 Kafka 客户端。 If from the shell commands, you should create a bash wrapper around the command you're running that uses sed or other templating scripts that generates the files before running the final command.如果来自 shell 命令,您应该围绕您正在运行的命令创建一个 bash 包装器,它使用sed或在运行最终命令之前生成文件的其他模板脚本。

If you are actually writing Java code, then build the Properties from the environment there, and not using files.如果您实际上是在编写 Java 代码,则从那里的环境构建属性,而不是使用文件。

Don't think properties file values are interpolated but probably you can test it once.不要认为属性文件值是内插的,但您可能可以测试一次。 Alternatively you can also remove these lines from property file and do it from code something like below...或者,您也可以从属性文件中删除这些行,并从如下代码中执行此操作...

   final Properties properties = new Properties();
   final FileInputStream input = new FileInputStream(yourExistingFile);
   
   properties.load(input); 
   properties.put("ssl.key.password",System.getenv("SSL_KEY_PASSWORD"));//this is additional property

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

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