1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
| import com.baidu.citool.common.kafka.bean.KafkaTopicEnum; import lombok.extern.slf4j.Slf4j; import net.sf.json.JSONObject; import org.apache.kafka.clients.admin.AdminClient; import org.apache.kafka.clients.admin.KafkaAdminClient; import org.apache.kafka.clients.admin.NewPartitions; import org.apache.kafka.clients.admin.NewTopic; import org.apache.kafka.clients.admin.TopicDescription; import org.apache.kafka.clients.producer.KafkaProducer; import org.apache.kafka.clients.producer.ProducerConfig; import org.apache.kafka.clients.producer.ProducerRecord; import org.apache.kafka.common.TopicPartitionInfo; import org.apache.kafka.common.serialization.StringSerializer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties;
@Slf4j @Component public class KafkaManager {
@Autowired private KafkaConfigProperties kafkaConfigProperties;
private static KafkaProducer<String, String> producer;
public static AdminClient adminClient;
@PostConstruct private void initKafkaManger() { try { Properties properties = new Properties(); properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaConfigProperties.getBootstrapServers()); properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class); properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class); properties.put(ProducerConfig.ACKS_CONFIG, kafkaConfigProperties.getAcks()); properties.put(ProducerConfig.RETRIES_CONFIG, kafkaConfigProperties.getRetries()); refreshTopics(properties); producer = new KafkaProducer<>(properties); } catch (Exception e) { log.error("创建KafkaProducer失败", e); } }
private void refreshTopics(Properties properties) { adminClient = KafkaAdminClient.create(properties); adminClient.listTopics().names() .whenComplete((topics, throwable) -> { List<String> newTopics = KafkaTopicEnum.toList(); log.info("当前已有主题:{}", topics); newTopics.removeIf(str -> !topics.isEmpty() && topics.contains(str)); if (!newTopics.isEmpty()) { log.info("创建新的主题:{}", newTopics); List<NewTopic> newTopicList = new ArrayList<>(); for (String ntp : newTopics) { NewTopic newTopic = new NewTopic(ntp, kafkaConfigProperties.getNumPartitions(), (short) kafkaConfigProperties.getReplicationFactor()); newTopicList.add(newTopic); } adminClient.createTopics(newTopicList); } adminClient.describeTopics(topics).all().whenComplete((descriptionMap, throwable1) -> { for (Map.Entry<String, TopicDescription> entry : descriptionMap.entrySet()) { List<TopicPartitionInfo> tps = entry.getValue().partitions(); log.info("主题:{},描述:{}", entry.getKey(), tps); log.info("当前主题分区数:{},即将修改的分区数:{}", tps.size(), kafkaConfigProperties.getNumPartitions()); List<String> allTopics = KafkaTopicEnum.toList(); log.info("所有主题:{}", allTopics); if (tps.size() < kafkaConfigProperties.getNumPartitions() && allTopics.contains(entry.getKey())) { log.info("主题扩展分区:{}", entry.getKey()); NewPartitions newPartitions = NewPartitions.increaseTo(kafkaConfigProperties.getNumPartitions()); Map<String, NewPartitions> partitionsMap = new HashMap<>(); partitionsMap.put(entry.getKey(), newPartitions); adminClient.createPartitions(partitionsMap); } } }); }); }
public static void sendMessage(String topicName, String jsonMessage) { producer.send(new ProducerRecord<>(topicName, jsonMessage)); }
public static void sendMessage(String topicName, String... jsonMessages) throws InterruptedException { for (String jsonMessage : jsonMessages) { producer.send(new ProducerRecord<>(topicName, jsonMessage)); } }
public static void sendMessage(String topicName, List<Map<Object, Object>> mapMessageToJSONForArray) { for (Map<Object, Object> mapMessageToJSON : mapMessageToJSONForArray) { String array = JSONObject.fromObject(mapMessageToJSON).toString(); producer.send(new ProducerRecord<>(topicName, array)); } }
public static void sendMessage(String topicName, Map<Object, Object> mapMessageToJSON) { String array = JSONObject.fromObject(mapMessageToJSON).toString(); producer.send(new ProducerRecord<>(topicName, array)); } }
|