Skip to content

Commit

Permalink
๐Ÿ”ง Producer ์„ค์ • ํŒŒ์ผ ์ถ”๊ฐ€ (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
jun108059 committed Aug 14, 2022
1 parent 669751d commit 176ef49
Showing 1 changed file with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package hexagonal.socket.kafka.config;

import hexagonal.socket.dto.Message;
import lombok.RequiredArgsConstructor;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;
import org.springframework.kafka.support.serializer.JsonSerializer;

import java.util.HashMap;
import java.util.Map;

/**
* KafkaTemplate - Topic ์œผ๋กœ ๋ฐ์ดํ„ฐ ๋ณด๋‚ด๋Š” ํŽธ์˜ ๊ธฐ๋Šฅ ์„ค์ •(By Producer)
*/
@EnableKafka
@Configuration
@RequiredArgsConstructor
public class KafkaProducerConfig {

private final KafkaConstants kafkaConstants;

@Bean
public KafkaTemplate<String, Message> kafkaTemplate() {
return new KafkaTemplate<>(producerFactory());
}

@Bean
public ProducerFactory<String, Message> producerFactory() {
return new DefaultKafkaProducerFactory<>(producerConfigurations());
}

@Bean
public Map<String, Object> producerConfigurations() {
Map<String, Object> configurations = new HashMap<>();
configurations.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaConstants.getProducerBootStrapServers());
configurations.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
configurations.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);
return configurations;
}

}

0 comments on commit 176ef49

Please sign in to comment.