亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Spring Boot 中的 Kafka 配置類未找到密鑰庫或信任庫

Spring Boot 中的 Kafka 配置類未找到密鑰庫或信任庫

哈士奇WWW 2023-07-28 10:16:58
我正在設置 Kafka 消費者配置,但該配置在類路徑上找不到密鑰庫或信任庫:@EnableKafka@Configurationpublic 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 配置的類路徑?我在這里錯過了什么嗎?
查看完整描述

2 回答

?
Qyouu

TA貢獻1786條經驗 獲得超11個贊

您注入一個字符串,它將保留“classpath:”在字符串值中并將其作為屬性提供給 DefaultKafkaConsumerFactory,嘗試注入到 spring 資源中,例如:


import org.springframework.core.io.Resource;


@Value("classpath:path/to/file/in/classpath")

Resource resourceFile;

然后你可以訪問該文件,你可以獲得絕對路徑,如下所示:


resourceFile.getFile().getAbsolutePath()


這個想法是你可以提供 DefaultKafkaConsumerFactory 的絕對路徑


但是您也可以嘗試刪除“classpath:”并像當前代碼一樣注入為 String ,這可能取決于 DefaultKafkaConsumerFactory 如何處理該屬性。但我不明白為什么上面的絕對路徑不起作用。


查看完整回答
反對 回復 2023-07-28
?
GCT1015

TA貢獻1827條經驗 獲得超4個贊

對于像我這樣使用 Spring Boot 和 Spring Kafka 并且不重寫 DefaultKafkaConsumerFactory 的人(僅使用屬性進行配置),您可以實現一個BeanPostProcessor類。它提供了兩種方法:

postProcessAfterInitializationpostProcessBeforeInitialization

工廠鉤子允許對新 bean 實例進行自定義修改 - 例如,檢查標記接口或使用代理包裝 bean。通常,通過標記接口等填充 Bean 的后處理器將實現 postProcessBeforeInitialization(java.lang.Object, java.lang.String),而用代理包裝 Bean 的后處理器通常將實現 postProcessAfterInitialization(java.lang.Object) ,java.lang.String)。

我將 Spring Boot 與 Spring Kafka 一起使用,我只想更改本地配置文件。

在我的代碼示例中,我使用它來覆蓋 Kafka Location 屬性,因為對于 SSL,它不會從類路徑讀取。

這就是代碼:

import io.confluent.kafka.schemaregistry.client.SchemaRegistryClientConfig;

import java.io.IOException;

import java.util.Arrays;

import lombok.RequiredArgsConstructor;

import lombok.SneakyThrows;

import org.apache.kafka.common.config.SslConfigs;

import org.springframework.beans.BeansException;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.beans.factory.config.BeanPostProcessor;

import org.springframework.boot.autoconfigure.kafka.KafkaProperties;

import org.springframework.context.annotation.Configuration;

import org.springframework.core.env.Environment;

import org.springframework.core.io.FileSystemResource;

import org.springframework.core.io.Resource;


@Configuration

@RequiredArgsConstructor

public class KafkaConfiguration implements BeanPostProcessor {


? @Value("${spring.kafka.ssl.key-store-location:}")

? private Resource keyStoreResource;

? @Value("${spring.kafka.properties.schema.registry.ssl.truststore.location:}")

? private Resource trustStoreResource;

? private final Environment environment;


? @SneakyThrows

? @Override

? public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

? ? if (bean instanceof KafkaProperties) {

? ? ? KafkaProperties kafkaProperties = (KafkaProperties) bean;

? ? ? if(isLocalProfileActive()) {

? ? ? ? configureStoreLocation(kafkaProperties);

? ? ? }

? ? }

? ? return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);

? }


? private boolean isLocalProfileActive() {

? ? return Arrays.stream(environment.getActiveProfiles()).anyMatch(profile -> "local".equals(profile));

? }


? private void configureStoreLocation(KafkaProperties kafkaProperties) throws IOException {

? ? kafkaProperties.getSsl().setKeyStoreLocation(new FileSystemResource(keyStoreResource.getFile().getAbsolutePath()));

? ? kafkaProperties.getProperties().put(SchemaRegistryClientConfig.CLIENT_NAMESPACE + SslConfigs.SSL_KEYSTORE_LOCATION_CONFIG, keyStoreResource.getFile().getAbsolutePath());

? ? kafkaProperties.getSsl().setTrustStoreLocation(new FileSystemResource(trustStoreResource.getFile().getAbsolutePath()));

? ? kafkaProperties.getProperties().put(SchemaRegistryClientConfig.CLIENT_NAMESPACE + SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG, trustStoreResource.getFile().getAbsolutePath());

? }


}

這樣我就可以在我的屬性文件中添加:


spring.kafka.ssl.key-store-location=classpath:mykeystore.jks


代碼將從中獲取絕對路徑并設置它。它還可以根據配置文件進行過濾。


值得一提的是,BeanPostProcessor 會針對每個bean 運行,因此請確保您過濾了您想要的內容。


查看完整回答
反對 回復 2023-07-28
  • 2 回答
  • 0 關注
  • 271 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號