feat:() 新增MQ生产者 和 修改kafka生产者的信息

dev.processing
晨哀 2024-10-04 10:18:16 +08:00
parent 0e391451b0
commit d02813f7e4
1 changed files with 40 additions and 3 deletions

View File

@ -1,12 +1,14 @@
package com.muyu.processing.controller; package com.muyu.processing.controller;
import com.alibaba.fastjson.JSONObject; import cn.hutool.json.JSONObject;
import com.muyu.common.core.constant.KafkaConstants; import com.muyu.common.core.constant.KafkaConstants;
import com.muyu.common.core.utils.uuid.UUID;
import com.muyu.common.kafka.config.KafkaProducerConfig; import com.muyu.common.kafka.config.KafkaProducerConfig;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.apache.kafka.clients.producer.KafkaProducer; import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord; import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.protocol.types.Field; import org.apache.kafka.common.protocol.types.Field;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@ -14,6 +16,7 @@ import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
/** /**
* -
* @Author * @Author
* @Packagecom.muyu.processing.controller * @Packagecom.muyu.processing.controller
* @Projectcar-cloud-server * @Projectcar-cloud-server
@ -29,10 +32,44 @@ public class TestKafka {
@Resource @Resource
private KafkaProducer<String, String> kafkaProducer; private KafkaProducer<String, String> kafkaProducer;
@Resource
private RabbitTemplate rabbitTemplate;
/**
* Kafka
* @return String
*/
@GetMapping("/send") @GetMapping("/send")
public void sendMsg(){ public String sendMsg(){
ProducerRecord<String, String> producerRecord = new ProducerRecord<>("zeshi", "你好啊"); JSONObject entries = new JSONObject();
entries.set("vin","vin123468");
entries.set("name","宝马");
String entriesString = entries.toString();
ProducerRecord<String, String> producerRecord = new ProducerRecord<>("zeshi", entriesString);
kafkaProducer.send(producerRecord); kafkaProducer.send(producerRecord);
return "OK";
} }
/**
* MQ
* @return String
*/
@GetMapping("/sendMQ")
public String sendMQ(){
rabbitTemplate.convertAndSend("long_time_no_see","晨哀,好久不见",message -> {
message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
return message;
});
return "OK";
}
/**
* MQ
* @return String
*/
@GetMapping("/sendDui")
public String sedDui() {
rabbitTemplate.convertAndSend("myExchange","Im.fine","");
return "OK";
}
} }