kafka工具

/**

  • 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) {
      }
      }
      }

猜你喜欢

转载自blog.csdn.net/weixin_43524361/article/details/104820547