kafaka发送接收消息stream方式实例
来源:互联网 发布:linux httplib2 编辑:程序博客网 时间:2024/06/08 15:42
1.配置文件
input为接收,output为发送
如果发送接收在同一个程序中,则不需要加上consumer: headerMode:raw ,如果本程序仅是接收消息进行消费,需要加上consumer: headerMode:raw
spring: cloud: stream: bindings: input-collect: contentType: text/plain;charset=UTF-8 destination: ACCOUNT_COLLECT_NOTIFY group: account-dev input-order: consumer: headerMode: raw contentType: text/plain;charset=UTF-8 destination: ACCOUNT_ORDER_NOTIFY group: account-dev output-collect: contentType: text/plain;charset=UTF-8 destination: ACCOUNT_COLLECT_NOTIFY output-watch: contentType: text/plain;charset=UTF-8 destination: ACCOUNT_WATCH_NOTIFY kafka: binder: brokers: 192.168.1.158:9092,192.168.1.159:9092,192.168.1.160:9092 zkNodes: 192.168.1.158:2181,192.168.1.159:2181,192.168.1.160:2181
2.发送消息outPut分类Bean
public interface NotifyMessageChannel { String COLLECT_OUTPUT = "output-collect"; String WATCH_OUTPUT ="output-watch" ; @Output(NotifyMessageChannel.COLLECT_OUTPUT) MessageChannel collectOutPut(); @Output(NotifyMessageChannel.WATCH_OUTPUT) MessageChannel watchOutPut();}
3.发送消息service
NotifyMessageChannel中定义了2个发送MessageChannel,发送时可以直接.collectOutPut().send,选择不同的output进行发送
@Service@Slf4j@EnableBinding(NotifyMessageChannel.class)public class NotifyServiceImpl implements NotifyService { @Autowired private NotifyMessageChannel notifyMessageChannel; private ObjectMapper mapper = new ObjectMapper(); @Override public void sendUserCollectCourse(UserCollectCourseNotify userCollectCourseNotify) { try { Boolean result = notifyMessageChannel.collectOutPut().send(MessageBuilder.withPayload( mapper.writeValueAsString(userCollectCourseNotify)).build()) ; log.info("send result:"+result); } catch (Exception e) { log.error("Exception from create user UserCollectCourse.", e); } }}
4.接收消息input配置
public interface ReceiveMessageChannel { String COLLECT_INPUT = "input-collect"; String ORDER_INPUT ="input-order" ; @Input(ReceiveMessageChannel.COLLECT_INPUT) SubscribableChannel collectInput(); @Input(ReceiveMessageChannel.ORDER_INPUT) SubscribableChannel orderInput();}
5.监听接收到的消息,进行消费处理
@Service@Slf4j@EnableBinding(ReceiveMessageChannel.class)public class CollectListener { private UserCollectCourseClient userCollectCourseService; private ObjectMapper mapper = new ObjectMapper(); public CollectListener(UserCollectCourseClient userCollectCourseClient) { this.userCollectCourseService = userCollectCourseClient; } @StreamListener(ReceiveMessageChannel.COLLECT_INPUT) public void process(Message<String> message) { log.debug("Received Notify:[{}]",message.toString()); String content = message.getPayload(); UserCollectCourseNotify uccn; try { uccn = mapper.readValue(content,UserCollectCourseNotify.class); log.debug("Received Notify:[userId:{},courseId:{}]",uccn.getUserId(),uccn.getCourseId()); if(uccn!=null){ log.info("receive UserCollectCourseNotify:"+uccn);// userCollectCourseService.saveUserCollectCourse(ucci);// log.debug("Save Collect to Mongo:[userId:{},courseId:{}]",ucci.getUserId(),ucci.getCourseId()); } } catch (Exception e) { log.warn("RECEIVE Collect NOTIFY ERROR:[message_body:{},error:{}]",message.toString(),e.getLocalizedMessage()); } }}
阅读全文
0 0
- kafaka发送接收消息stream方式实例
- java调用rabbitmq消息队列发送和接收消息实例
- TCP网络通信实例--消息的发送与接收
- JMS消息发送和接收实例 - 点对点模式
- JMS消息发送和接收实例 - 点对点模式
- python、rabbitmq、pika、消息发送和接收实例
- Bluemix结合RabbitMq实现消息发送与接收实例
- Bluemix结合RabbitMq实现消息发送与接收实例
- Bluemix结合RabbitMq实现消息发送与接收实例
- activemq安装与简单消息发送接收实例
- activemq安装与简单消息发送接收实例
- Netty:实现同步发送并接收消息的一种方式
- AmqpTemplate-发送-接收-消息
- Socket 发送接收实例
- ActiveMQ 消息接收方式
- Udp消息发送接收示例
- 使用ActiveMQ 发送/接收消息
- ActiveMQ消息发送和接收
- 根据帕斯卡三角的一行求帕斯卡三角的下一行
- hibernate常用配置
- 深入理解C++中public、protected及private用法
- ThinkPHP3.2 Ajax分页
- PHP+Redis 消息队列 实现高并发下注册人数统计
- kafaka发送接收消息stream方式实例
- 百度地图的使用
- GMIC智行未来:听霍金隔空谈AI
- 60秒测量6项生理数据,联发科让手机秒变健康伴侣
- 每日一练_19
- 4.3(3)
- c++-串的模式匹配
- 你理想中的家是什么样子?AI与未来人居的碰撞
- AI为啥如此迷恋“棋”?