rocketMQ系列(三): springboot整合rocketMQ

在第二篇我们已经部署好了一个服务  mqbroker.cmd -n localhost:9876 ,在此基础上,我们来做一个springboot的demo示例

网上很多有整合rocketmq的,但是部分没有使用官方rocketmq-spring-boot-starter,因为官方start是近几年出的,使用起来很简单,但是要注意版本之间的对应关系,好了,下面直接介绍整合步骤吧。

一、建立maven工程

不多说了,看看作者的工程目录

rocketMQ系列(三): springboot整合rocketMQ_第1张图片

二、在pom文件里面添加依赖


  4.0.0
  com.wx
  rocketmq01
  0.0.1-SNAPSHOT
  
  rocketmq测试

	 
	
	    3.1.1
		1.8
		1.2.1
		1.0.28
		1.2.31
		4.12
	
	

    
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.12.RELEASE
	
	
	
	
	   
		
		   org.apache.rocketmq
		   rocketmq-client
		   4.5.2
		
	   
	   
           org.apache.rocketmq
           rocketmq-spring-boot-starter
           2.0.3
       
       
		
		
			junit
			junit
			${junit.version}
			test
		

		
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		

        
          
            org.springframework.boot  
            spring-boot-configuration-processor  
            true  
        
        
		
			org.springframework
			spring-context-support
		

		
		
			com.alibaba
			fastjson
			${fastjson.version}
		

		
		
			org.apache.commons
			commons-lang3
			3.6
		
		
			commons-configuration
			commons-configuration
			1.10
		
		
			commons-io
			commons-io
			2.5
		
		
			org.apache.httpcomponents
			httpclient
			4.5.9
		
		
			org.springframework
			spring-context-support
		



		
		
			commons-collections
			commons-collections
			3.2.2
		


		
		
			commons-httpclient
			commons-httpclient
			3.1
		


	

	
		
			public
			aliyun nexus
			http://maven.aliyun.com/nexus/content/groups/public/
			
				true
			
		
	
	
		${project.artifactId}${project.version}
		
			
			
				org.apache.maven.plugins
				maven-compiler-plugin
				
					${java.version}
					${java.version}
					utf-8
				
			
			
				org.apache.maven.plugins
				maven-resources-plugin
				
					UTF-8
				
			
			
			
				org.springframework.boot
				spring-boot-maven-plugin
				
				    
					false
					true
				
			

		
	

这里关键的依赖是


           org.apache.rocketmq
           rocketmq-client
           4.5.2
        

       
      
           org.apache.rocketmq
           rocketmq-spring-boot-starter
           2.0.3
       

注意版本号,随便写启动是会报错的

三、配置application.yml

server:
  #tomcat端口号
  port: 8080

rocketmq:
  name-server: localhost:9876
  producer:
    group: wx-group
    
# 禁用spring boot权限控制系统
security:
  basic:
    enabled: false
    
spring:
  #自带json解析工具配置
  jackson:
    time-zone: GMT+8
    date-format: yyyy-MM-dd HH:mm:ss
    serialization:
      fail-on-empty-beans: false
  

注意rockketmq里面的配置,localhost:9876  是我们部署后启动的broker地址和端口

四、编写消息实体类

package com.wx.entitys;

import java.io.Serializable;

/**
 * 消息对象
 * @author wx
 */
public class MsgEntity implements Serializable{

	private static final long serialVersionUID = 1L;
	
	private String msgName;
	private String msgContent;
	
	public MsgEntity() {
	}
	
	public MsgEntity(String msgName, String msgContent) {
		super();
		this.msgName = msgName;
		this.msgContent = msgContent;
	}
	
	public void setMsgContent(String msgContent) {
		this.msgContent = msgContent;
	}
	public String getMsgContent() {
		return msgContent;
	}
	public void setMsgName(String msgName) {
		this.msgName = msgName;
	}
	public String getMsgName() {
		return msgName;
	}
}

注意要实现Serializable接口近序列化

四、编写生产者接口及实现类

package com.wx.service;

import com.wx.entitys.MsgEntity;

/**
 * 生产者发布消息接口
 * @author wx
 */
public interface SendMsgService {
	
	boolean sendMsg(MsgEntity msg);
}
package com.wx.service;

import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Service;

import com.wx.entitys.MsgEntity;

/**
 * 生产者发布消息实现类
 * @author wx
 */
@Service
public class SendMsgServiceImpl implements SendMsgService{

	@Autowired
    private RocketMQTemplate rocketMQTemplate;
	
	public boolean sendMsg(MsgEntity msg) {
		try {
            //指定消息主题发送消息
			rocketMQTemplate.convertAndSend("wx-topic", msg);
		} catch (MessagingException e) {
			e.printStackTrace();
		}
        return true;
	}
	
}

五、编写订阅消息的监听(消费者)

package com.wx.listener;

import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

import com.wx.entitys.MsgEntity;

/**
 * 消费者订阅消息的监听器
 * @author wx
 */
@Component
@RocketMQMessageListener(topic = "wx-topic",consumerGroup="mm-group")
public class ConsumerListener implements RocketMQListener{
	
	public void onMessage(MsgEntity msg) {
		System.out.println("消息标题:"+msg.getMsgName());
		System.out.println("消息内容:"+msg.getMsgContent());
		//此处可接上数据库调用,对消息持久化存起来或进行处理
	}
}

六:编写前端控制器模拟消息发送

package com.wx.controller;

import ch.qos.logback.classic.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.wx.entitys.MsgEntity;
import com.wx.service.SendMsgService;

import java.util.*;

/**
 * 模拟消息发送控制器
 * @author wx
 * Version 1.0
 */
@RestController
@RequestMapping("/rocket")
public class RocketController {

	private static Logger logger = (Logger) LoggerFactory.getLogger(RocketController.class);
	
	@Autowired
	private SendMsgService sendMsg;
	
	/**
	 * 模拟消息发送
	 * @param msgName 消息标题
	 * @param content 消息内容
	 * @return
	 */
	@RequestMapping("/send.do")
	public String send(String msgName,String content){
        MsgEntity msg=new MsgEntity(msgName, content);
        if(sendMsg.sendMsg(msg)){
        	return "消息发送成功";
        }else{
        	return "消息发送失败";
        }
	}
	
	
}

七、启动类

package com.wx;



import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class RocketApplication {
	
	public static void main(String[] args) {
		SpringApplication.run(RocketApplication.class, args);
		System.out.println("启动成功!");
	}
	
	
}

八、启动,测试

浏览器执行http://localhost:8080/rocket/send.do?msgName=mazi&content=wellcome to rocket's world

rocketMQ系列(三): springboot整合rocketMQ_第2张图片

查看控制台:

rocketMQ系列(三): springboot整合rocketMQ_第3张图片

消息成功接收了,ok,大功告成!

你可能感兴趣的:(消息中间件,springboot,rocketMQ)