1

I couldn't find any example to swtich between kafka cluster .

Anyone has implmeneted this class ABSwitchCluster from Spring Kafka.

https://docs.spring.io/spring-kafka/reference/html/

I tried with below code, but its not switching cluster.

@RestController
public class ApacheKafkaWebController {

    @Autowired
    ConsumerKakfaConfiguration configuration;

    @Autowired
    private KafkaListenerEndpointRegistry registry;

    @Autowired
    private ABSwitchCluster switcher;

    @GetMapping(value = "/switch")
    public String producer() {

        registry.stop();
        switcher.secondary();
        registry.start();
        return "switched!";
    }

}

and swticher bean here:

 @Bean
    public ABSwitchCluster switcher() {
        return new ABSwitchCluster("127.0.0.1:9095", "127.0.0.1:9096");
    }

Could you please tell me am I missing anything here?, still its running in 9095 port.

Ram
  • 37
  • 4

1 Answers1

0

See this answer and this test.

Basically, you switch the cluster and reset the connections by stopping and starting listener containers and resetting the producer factory.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • Just defining it as a `@Bean` does nothing; you have to add it to the consumer and/or producer factory. See `setBootstrapServersSupplier()`. – Gary Russell May 14 '21 at 13:27
  • Since you are using Spring Boot; if you are using its auto-configured factories, add them as parameters to your `switcher` bean definition method and call the setter(s) before returning the switcher. e.g. `... switcher(ConsumerFctory, ?> cf) {...}`. – Gary Russell May 14 '21 at 13:38
  • Your suggestion helped me! – Ram May 15 '21 at 17:07