簡體   English   中英

Quarkus:Smallrye Kafka 使用不同的 KEYS 和 PASSWORD 為不同的引導服務器配置通道

[英]Quarkus: Smallrye Kafka configure channels for distinct bootstrap servers using diferent KEYS and PASSWORD

我的目標是使用使用 SASL_SSL 的 jaas 配置使用不同的引導服務器在 2 個不同的通道中生成事件,但我無法設置通道以在引導服務器上正確進行身份驗證。

我嘗試了以下設置

mp.messaging.outgoing.channel1.bootstrap.servers=${KAFKA1}
mp.messaging.outgoing.channel1.ssl.endpoint-identification-algorithm=https
mp.messaging.outgoing.channel1.security.protocol=SASL_SSL
mp.messaging.outgoing.channel1.sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="${KEY1}" password="${PWD1}";
mp.messaging.outgoing.channel1.sasl.mechanism=PLAIN

mp.messaging.outgoing.channel2.bootstrap.servers=${KAFKA2}
mp.messaging.outgoing.channel2.ssl.endpoint-identification-algorithm=https
mp.messaging.outgoing.channel2.security.protocol=SASL_SSL
mp.messaging.outgoing.channel2.sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="${KEY2}" password="${PWD2}";
mp.messaging.outgoing.channel2.sasl.mechanism=PLAIN

使用此設置,我在通道初始化時收到錯誤。

2023-01-18 13:57:10 13:57:10.445 ERROR [Application] (main) Failed to start application (with profile prod): java.lang.IllegalArgumentException: Could not find a 'KafkaClient' entry in the JAAS configuration. System property 'java.security.auth.login.config' is not set
2023-01-18 13:57:10     at org.apache.kafka.common.security.JaasContext.defaultContext(JaasContext.java:131)
2023-01-18 13:57:10     at org.apache.kafka.common.security.JaasContext.load(JaasContext.java:96)
2023-01-18 13:57:10     at org.apache.kafka.common.security.JaasContext.loadClientContext(JaasContext.java:82)
2023-01-18 13:57:10     at org.apache.kafka.common.network.ChannelBuilders.create(ChannelBuilders.java:167)
2023-01-18 13:57:10     at org.apache.kafka.common.network.ChannelBuilders.clientChannelBuilder(ChannelBuilders.java:81)
2023-01-18 13:57:10     at org.apache.kafka.clients.ClientUtils.createChannelBuilder(ClientUtils.java:105)

初始設置使用默認的引導程序設置,並且在將 KAFKA 引入等式之前一直運行良好。

kafka.bootstrap.servers='${KAFKA1}'
kafka.ssl.endpoint-identification-algorithm=https
kafka.security.protocol=SASL_SSL
kafka.sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="${Key1}" password="${PWD1}";
kafka.sasl.mechanism=PLAIN

我已經嘗試了這個問題上描述的方法,但我無法弄清楚如何配置通道以對 2 個不同的引導服務器進行身份驗證。

如錯誤所述,您需要在 JVM 系統屬性中設置 JAAS conf

-Djava.security.auth.login.config=/path/to/kafka-jaas.conf

閱讀文檔后 [https://quarkus.io/guides/kafka] 選項 1

kafka-configuration允許 config1

因此解決方案是實現一個提供者 bean

@ApplicationScoped
@Slf4j
public class KafkaConfigBean {

    @Produces
    @Identifier("kafka1")
    public Map<String, Object> kafkaConfig() {
        HashMap<String, Object> config = new HashMap<>();

        config.put("security.protocol", "SASL_SSL");
        config.put("sasl.mechanism", "PLAIN");

        String saslConfig = String.format("org.apache.kafka.common.security.plain.PlainLoginModule required username=\"%S\" password=\"%s\";",
                System.getenv("KEY1"), System.getenv("PWD1"));
        config.put("sasl.jaas.config", saslConfig);

        log.info("Initialized Kafka 1 config");

        return config;
    }

    @Produces
    @Identifier("kafka2")
    public Map<String, Object> kafkaConfigPTT() {
        HashMap<String, Object> config = new HashMap<>();

        config.put("security.protocol", "SASL_SSL");
        config.put("sasl.mechanism", "PLAIN");

        String saslConfig = String.format("org.apache.kafka.common.security.plain.PlainLoginModule required username=\"%S\" password=\"%s\";",
                System.getenv("KEY2"), System.getenv("PWD2"));
        config.put("sasl.jaas.config", saslConfig);

        log.info("Initialized Kafka 2 config");

        return config;
    }
}

從而導致以下配置文件

mp.messaging.outgoing.channel1.bootstrap.servers=\${KAFKA1}
mp.messaging.outgoing.channel1.kafka-configuration=kafka1
 
mp.messaging.outgoing.channel2.bootstrap.servers=\${KAFKA2}
mp.messaging.outgoing.channel2.kafka-configuration=kafka2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM