变量名:ROCKETMQ_HOME
变量值:MQ解压路径MQ文件夹名
2.2重启服务器 3. 启动 3.1 启动NAMESERVERCmd命令框执行进入至‘MQ文件夹bin’下,然后执行‘start mqnamesrv.cmd’,启动NAMESERVER。成功后会d出提示框,此框勿关闭。
3.2 启动BROKERCmd命令框执行进入至‘MQ文件夹bin’下,然后执行‘start mqbroker.cmd -n 127.0.0.1:9876 -c ../conf/broker.conf autoCreateTopicEnable=true’,启动BROKER。成功后会d出提示框,此框勿关闭。
二、代码示例: 2.1 pom依赖:
2.2代码:org.apache.rocketmq rocketmq-spring-boot-starter2.2.1
创建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 测试:
日志:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)