RocketMq整合springboot,demo示例

RocketMq整合springboot,demo示例,第1张

RocketMq整合springboot,demo示例 一、window安装rocketmq: 1.1地址:http://rocketmq.apache.org/release_notes/release-notes-4.2.0/ 1.2选择‘Binary’进行下载 1.3解压已下载工程

2. 配置 2.1 系统环境变量配置

        变量名:ROCKETMQ_HOME

        变量值:MQ解压路径MQ文件夹

2.2重启服务器 3. 启动 3.1 启动NAMESERVER

        Cmd命令框执行进入至‘MQ文件夹bin’下,然后执行‘start mqnamesrv.cmd’,启动NAMESERVER。成功后会d出提示框,此框勿关闭。

3.2 启动BROKER

        Cmd命令框执行进入至‘MQ文件夹bin’下,然后执行‘start mqbroker.cmd -n 127.0.0.1:9876   -c ../conf/broker.conf  autoCreateTopicEnable=true’,启动BROKER。成功后会d出提示框,此框勿关闭。


二、代码示例: 2.1 pom依赖:
 
        org.apache.rocketmq
        rocketmq-spring-boot-starter
        2.2.1
2.2代码:

   创建topic

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;

public class RocketMQMgr {
    public static void main(String[] args) {
        DefaultMQProducer producer=new DefaultMQProducer("jason");

        producer.setNamesrvAddr("127.0.0.1:9876");

        try {
            producer.start();

            
            producer.createTopic("broker-a","test-topic",4);

            System.out.println("test-topic  创建成功!");

            producer.shutdown();
        } catch (MQClientException e) {
            e.printStackTrace();
        }

    }
}
2.3 springboot代码:

application.properties增加:

rocketmq:
  name-server: localhost:9876
  producer:
    group: ${spring.application.name}
    send-message-timeout: 30000
    access-key: rocketmq2
    secret-key: 12345678
    enable-msg-trace: true

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

@SpringBootApplication
public class BackendApplicationBootstrap {

    public static void main(String[] args) {
        SpringApplication.run(BackendApplicationBootstrap.class);
    }
}




import com.sep.tech.inf.service.DubboTestService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @DubboReference(version = "1.0.0",providedBy = "dubbo-web")
    private DubboTestService dubboTestService;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;


    @GetMapping("dubboTest")
    public String dubboTest() {
        return dubboTestService.say("Hello world");
    }

    @GetMapping("push")
    public void push() {
        rocketMQTemplate.syncSend("test-topic", MessageBuilder.withPayload("test-topic"));
    }
}
 2.4 测试:

 日志:

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5121589.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-17
下一篇 2022-11-17

发表评论

登录后才能评论

评论列表(0条)

保存