/**
-
kafka工具
-
@author albert.ding
*/
@Component
public class KafkaUtil {@Autowired
private KafkaTemplate<String, byte[]> kafkaTemplate;/**
- 发送消息到kafka
- @author albert.ding
- @param topic
- @param key key值hash取模,根据模值决定写入哪个partition
- @param message
/
public void sendMsg(String topic, String key, byte[] message){
kafkaTemplate.send(topic, key, message);
}
/* - 发送消息到kafka
- @author albert.ding
- @param topic
- @param key key值hash取模,根据模值决定写入哪个partition
- @param message
/
public void sendMsg(String topic, String key, String message){
try {
sendMsg(topic, key, message.getBytes(Constant.DEFAULT_CHARSET));
} catch (UnsupportedEncodingException e) {
}
}
/* - 发送消息到kafka
- @author albert.ding
- @param topic
- @param key key值hash取模,根据模值决定写入哪个partition
- @param message
*/
public void sendMsg(String topic, String key, Object message){
String json = JSONObject.toJSONString(message);
try {
sendMsg(topic, key, json.getBytes(Constant.DEFAULT_CHARSET));
} catch (UnsupportedEncodingException e) {
}
}
}