Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In micronaut-kafka, How can I use JAAS config to two different consumers from one application?

I have tried the approach mentioned on official documentations to override the bootstrap servers in kafka client config.

https://micronaut-projects.github.io/micronaut-kafka/latest/guide/#kafkaClient

However, in my case, I am getting one JaaS config from other bean dependency and other one is available in secret path. Having a custom configuration just load one JaaS config and other consumer get disconnected.

For example,

kafka:
  sasl:
    mechanism: PLAIN
    jaas:
      config: >-
        org.apache.kafka.common.security.plain.PlainLoginModule required
        username="$ConnectionString"
        password="%s";
  security:
    protocol: SASL_SSL
  consumers:
    abc-consumer-client:
      sasl:
        mechanism: PLAIN
        jaas:
          config: >-
            org.apache.kafka.common.security.plain.PlainLoginModule required
            username="$ConnectionString"
            password="%s";
      security:
        protocol: SASL_SSL
    xyz-client:
      sasl:
        mechanism: PLAIN
        jaas:
          config: >-
            org.apache.kafka.common.security.plain.PlainLoginModule required
            username="$ConnectionString"
            password="%s";
      security:
        protocol: SASL_SSL

I related the micronaut-kafka with spring kafka implementation. It seems like I will have to override the beans to achieve this but getting stuck on which order I should override and in which sequence to achieve this.

Similar Spring Kafka reference- spring-kafka application.properties configuration for JAAS/SASL not working

I have tried the options listed on micronaut documentation but in my case, other jaas config is coming from other bean depedency which makes a GRPC call to fetch bootstrap URL and JaaS config.

What am I looking for here is the order in which I should override the micronaut-kafka beans to achieve two consumer connecting to two different bootstrap servers with each having its own JaasConfig and other JaasConfig is dependent on other service call.

like image 974
Dharmendra Vishwakarma Avatar asked Dec 10 '25 11:12

Dharmendra Vishwakarma


1 Answers

Finally, I was able to figure out this problem myself.

In order to solve this problem, I had to override the KafkaDefaultConfiguration bean and include the properties in the environment variable based on groupID.

You can actually override properties at runtime during the bean instantiation phase. For instance,

environment.addPropertySource(PropertySource.of("overrides",
                Map.of("kafka.consumers." + GroupId + ".bootstrap.servers", baseUrl,
                        "kafka.consumers." + GroupId + ".sasl.jaas.config", newJaasConfig )));
like image 164
Dharmendra Vishwakarma Avatar answered Dec 14 '25 12:12

Dharmendra Vishwakarma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!