阅读量:0
import org.apache.kafka.clients.admin.AdminClientConfig; import org.apache.kafka.clients.admin.NewTopic; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.kafka.core.KafkaAdmin; import java.util.Collections; import java.util.HashMap; import java.util.Map; @Configuration public class KafkaTopicConfig { @Value("${kafka.topic.message}") private String topic; @Value("${spring.kafka.bootstrap-servers}") private String serverAddr; @Bean public NewTopic topicWithThreePartitions() { NewTopic newTopic = new NewTopic(topic, 2,(short)1); return newTopic; } @Bean public KafkaAdmin kafkaAdmin() { Map<String, Object> configs = new HashMap<>(); configs.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG,serverAddr); return new KafkaAdmin(configs); } }
声明一个配置文件就可以,这是设置了两个分区,一个副本