码迷,mamicode.com
首页 > 其他好文 > 详细

RocketMq-简单示例(九)

时间:2019-11-28 14:55:07      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:one   ons   enable   int   info   prope   sage   设置   auto   

操作步骤

1.搭建rocketmq服务
2.生产者-微服务(发送-队列消息)
3.消费者-微服务(处理-队列消息)

1.搭建rocketmq服务

下载: https://www.apache.org/dyn/closer.cgi?path=rocketmq/4.5.1/rocketmq-all-4.5.1-bin-release.zip

# 解压至: /usr/local/rocketmq-all-4.5.1-bin-release
# 启动/关闭服务: 
    rocketmq
        # 启动:
            /usr/local/rocketmq-all-4.5.1-bin-release/bin/mqnamesrv -c /usr/local/rocketmq-all-4.5.1-bin-release/conf/namesrv.properties -n "192.168.1.180:6500" &
            # 设置自定义端口: 6500
            -n /usr/local/rocketmq-all-4.5.1-bin-release/conf/namesrv.properties
            # 文件内容是: 
                listenPort=6600
        # 关闭: 
            /usr/local/rocketmq-all-4.5.1-bin-release/bin/mqshutdown namesrv

    mqbroker:
        # 启动:  autoCreateTopicEnable=true(允许微服务创建topic)
            /usr/local/rocketmq-all-4.5.1-bin-release/bin/mqbroker -n "192.168.1.180:6500" autoCreateTopicEnable=true &
        # 关闭: 
        /usr/local/rocketmq-all-4.5.1-bin-release/bin/mqshutdown broker

2.生产者-微服务(发送-队列消息)

# 消息体-对象:
@Data
@Builder
@ToString
public class MemberMoneyMessage {
    int memberId;
    String event;
    double money;
}

# 发送消息:
@Autowired
RocketMQTemplate rocketMQTemplate;
....
rocketMQTemplate.convertAndSend(
            "member-money-act",
            MemberMoneyMessage.builder()
                    .event("pay-order")
                    .memberId(50)
                    .money(100)
                    .build()
    );
....

配置rocket服务端:
rocketmq:
name-server: tsca.cc:6600
producer:
    group: tsca-group

3.消费者-微服务(处理-队列消息)

# 监听处理:
@Service
@RocketMQMessageListener(consumerGroup = "tsca-group",topic = "member-money-act")
@Slf4j
public class MemberMoneyMqListener implements RocketMQListener<MemberMoneyMessage> {
    @Override
    public void onMessage(MemberMoneyMessage memberMoneyMessage) {
        log.info("收到-用户余额变动");
        log.info(JSON.toJSONString(memberMoneyMessage));
    }
}

# 配置:
rocketmq:
    name-server: tsca.cc:6600
    producer:
        group: tsca-group

RocketMq-简单示例(九)

标签:one   ons   enable   int   info   prope   sage   设置   auto   

原文地址:https://blog.51cto.com/3168834/2454292

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!