我正在设置 Kafka 消费者配置,但该配置在类路径上找不到密钥库或信任库:
@EnableKafka
@Configuration
public class KafkaConfig {
@Value("${kafka.ssl.keystore}")
private String keyStorePath;
@Value("${kafka.ssl.truststore}")
private String trustStorePath;
@Bean
public ConsumerFactory<String, String> getConsumerFactory() {
Map<String, Object> properties = new HashMap<>();
properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"my-bootstrap.mydomain.com:443");
properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
properties.put(ConsumerConfig.GROUP_ID_CONFIG, "group1");
properties.put(ConsumerConfig.CLIENT_ID_CONFIG, "client1");
properties.put("enable.auto.commit", "true");
properties.put("auto.commit.interval.ms", "500");
properties.put("session.timeout.ms", "30000");
properties.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, "SSL");
properties.put(SslConfigs.SSL_KEYSTORE_LOCATION_CONFIG, keyStorePath);
properties.put(SslConfigs.SSL_KEYSTORE_PASSWORD_CONFIG, "password");
properties.put(SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG, trustStorePath);
properties.put(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG, "password");
properties.put(SslConfigs.SSL_KEY_PASSWORD_CONFIG, "password");
return new DefaultKafkaConsumerFactory<>(properties);
}
src/main/resources/ssl密钥库和信任库都位于与配置类相同的 Maven 模块中的目录中。
我在 application.yml 中设置了占位符,如下所示:
kafka:
ssl:
keystore: classpath:ssl/kafka-keystore.jks
truststore: classpath:ssl/kafka-truststore.jks
但是,应用程序无法启动,并出现以下异常:
"org.apache.kafka.common.KafkaException: java.io.FileNotFoundException: classpath:ssl/kafka-keystore.jks (No such file or directory)"
我的理解是,使用@Value可以使用classpath:前缀来解析类路径(请参阅此链接) https://www.baeldung.com/spring-classpath-file-access
此外,该@Value技术可以很好地解析同一应用程序中反应式 WebClient 配置的密钥库和信任库。
我需要做什么来解析 Kafka 配置的类路径?我在这里错过了什么吗?
Qyouu
GCT1015
相关分类