首页 新闻 会员 周边

RMQ在spirngCloud集成在哪里比较好,还是单独作为一个模块接口?

0
[已解决问题] 解决于 2018-11-02 17:27

RMQ在SpirngCloud集成在哪里比较好,还是单独作为一个模块接口
在搭建一个SpringCloud全家桶的框架,做到消息队列的时候想用RMQ不过以前没有用过RMQ,想请问一下各位大佬RMQ应该怎么布置?

玄月白的主页 玄月白 | 初学一级 | 园豆:6
提问于:2018-11-02 11:00
< >
分享
最佳答案
0

一个基础的消息类Message,定义一个未定义的队列名;
具体业务实现具体的Message,定义具体的队列名;
一个通用的生产者Producer,用于生产这些具体业务产生的Message;
基于业务的消费者Listener,基于业务分别处理消息。

奖励园豆:5
Rekent | 初学一级 |园豆:91 | 2018-11-02 15:16

能否介绍一个案例
创建一个生产者、消费者如果只是创建一个类好像没什么效果
我的想法是按照redis集成在springcloud一样,打包成为一个公共的model-common接口

玄月白 | 园豆:6 (初学一级) | 2018-11-02 17:15

@玄月白:

一个公用的生产者接口,并实现他:

public interface MessageProducer<M extends Message> {

void produce(M message);

void produce(M message, String delay);

}

一个基础的消息类:

public interface Message extends Serializable{

public static final String CONTENT_TYPE_BYTES = "application/octet-stream";

public static final String CONTENT_TYPE_TEXT_PLAIN = "text/plain";

public static final String CONTENT_TYPE_PROTO = "application/proto";

public static final String CONTENT_TYPE_JSON = "application/json";

public static final String CONTENT_TYPE_XML = "application/xml";

public static final String DEFAULT_ENCODING = "UTF-8";

public static final Integer DEFAULT_PRIORITY = Integer.valueOf(0);

public static final Integer DELIVERYMODE_PERSISTENT = Integer.valueOf(2);

/**
 * 消息队列名
 * @return
 */
String getQueueName();

/**
 * 消息内容类型(序列化方式)
 * @return
 */
String getContentType();

/**
 * 消息的JavaType
 * @return 消息Class完整name 
 */
String getType();

}

根据业务继承Message实现它:
public class CustomerBatchExportMessage extends AbstractMessage {

private static final long serialVersionUID = 1L;

public static final String QUEUE_NAME = "cloud.customerExprot";

private Long customerId;    //潜在客户Id

public Long getCustomerId() {
    return customerId;
}

public void setCustomerId(Long customerId) {
    this.customerId = customerId;
}

public CustomerBatchExportMessage() {
    super(CustomerBatchExportMessage.class);
}

@Override
public String getQueueName() {
    return QUEUE_NAME;
}

}
根据业务建立Listener监听相应消息并做处理:
@Service
@RabbitListener(queues = {CustomerBatchExportMessage.QUEUE_NAME})
public class CustomerMessageListener {
@RabbitHandler
public void handle(CustomerBatchExportMessage bathMessage) {
.....
}

}

Rekent | 园豆:91 (初学一级) | 2018-11-02 17:26

@玄月白: 在需要用到的地方直接autowire引入 MessageProducer 发送相应的消息,并在Listener中处理即可

Rekent | 园豆:91 (初学一级) | 2018-11-02 17:27

@Rekent: 非常感谢,以前我没有做过消息队列,做接口的时候是非常犹豫,现在好多了

玄月白 | 园豆:6 (初学一级) | 2018-11-02 17:29
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册