How to run springboot without kafka server

2 answers

I have a Spring boot project that has dependencies that use kafka for logging. I can't get rid of said dependency because my logic requires code from that dependency

I want to start said application locally without running kafka so I can test my simple db lookup code.

I've tried disabling autoconfiguration


I've tried setting missingTopicsFatal to false via bean configuration

@Bean(name = "kafkaListenerContainerFactory") public ConcurrentKafkaListenerContainerFactory<?, ?> kafkaListenerContainerFactory(     ConsumerFactory<Object, Object> kafkaConsumerFactor,     ConcurrentKafkaListenerContainerFactoryConfigurer configurer) {    ConcurrentKafkaListenerContainerFactory<Object, Object> factory =       new ConcurrentKafkaListenerContainerFactory<>();   configurer.configure(factory, kafkaConsumerFactor);    ContainerProperties containerProperties = factory.getContainerProperties();   containerProperties.setMissingTopicsFatal(false);    ...   return factory; } 

But evidently the setting is already false as my application runs without failing, just that tomcat doesn't want to open the listen port

All other solutions I've encountered involves changing the code where kafka is used; in this case, the dependency I am using. But I have no access to changing said dependency.

The application starts but spring refuses to open a listen port and keeps looping with the error

Connection to node -1 (localhost/ could not be established. Broker may not be available. 

The application runs fine with kafka running locally except now the mere act of typing is slow thanks to kafka's resource usage. Please help.

All answers to this question, which has the identifier 61224895

The best answer:

Seems to me your main problem is the resource usage of your local kafka installation. Maybe first take a look at its configuration and data. Maybe start from fresh and purge all local data?

Alternatively, you could run KafkaServer in-process, something like this in your main class:

    Properties kafkaProperties = new Properties();     kafkaProperties.setProperty("", "1");     kafkaProperties.setProperty("", "localhost");     kafkaProperties.setProperty("listeners", "PLAINTEXT://localhost:" + kafkaPort);     kafkaProperties.setProperty("log.dir", Files.createTempDir().getAbsolutePath());     KafkaConfig kafkaBrokerConfig = new KafkaConfig(kafkaBrokerConfig);     KafkaServer kafkaServer = new KafkaServer(kafkaBrokerConfig, Time.SYSTEM);     kafkaServer.startup(); 

Or you could run Kafka from Docker. See e.g. the docker-compose file

You'll probably only need the zookeeper and broker:


version: '2' services:   zookeeper:     image: confluentinc/cp-zookeeper:5.4.1     hostname: zookeeper     container_name: zookeeper     ports:       - "2181:2181"     environment:       ZOOKEEPER_CLIENT_PORT: 2181       ZOOKEEPER_TICK_TIME: 2000    broker:     image: confluentinc/cp-kafka:5.4.1     hostname: broker     container_name: broker     depends_on:       - zookeeper     ports:       - "29092:29092"       - "9092:9092"     environment:       KAFKA_BROKER_ID: 1       KAFKA_ZOOKEEPER_CONNECT: 'zookeeper:2181'       KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT       KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker:29092,PLAINTEXT_HOST://localhost:9092       KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1       KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0 

You can disable the whole kafkaListenerContainerFactory bean via a Spring profile:

@Profile("!nokafka") @Bean(name = "kafkaListenerContainerFactory") public ConcurrentKafkaListenerContainerFactory<?, ?> kafkaListenerContainerFactory(){...} 

And run your application with spring profile 'nokafka'.

E.g. using the latest spring-boot maven plugin:

mvn spring-boot:run 

Or via java system properties: 

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type is referenced as an interface from”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data