SpringBoot整合RocketMQ之环境搭建以及Producer发送消息

https://github.com/apache/rocketmq-spring/releases/tag/2.0.0

https://github.com/apache/rocketmq-spring/archive/2.0.0.zip
#源码地址(或者使用资料中的源码)
https://github.com/apache/rocketmq-spring
#进入源码目录,执行如下命令
mvn clean install


    4.0.0

    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.0.RELEASE
    

    cn.learn.rocketmq
    learn-rocketmq
    1.0-SNAPSHOT

    
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.apache.rocketmq
            rocketmq-spring-boot-starter
            2.0.0
        
        
            org.apache.rocketmq
            rocketmq-client
            4.3.2
        
    

    
        
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.2
                
                    1.8
                    1.8
                    UTF-8
                
            
        
    

# Spring boot application
spring.application.name = learn-rocketmq

spring.rocketmq.nameServer=localhost:9876
spring.rocketmq.producer.group=my-group
package cn.learn.rocketmq.spring;

import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SpringProducer {

    // 注入rocketMQ的模板
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * 发送消息
     *
     * @param topic
     * @param msg
     */
    public void sendMsg(String topic, String msg) {
        this.rocketMQTemplate.convertAndSend(topic, msg);
    }

}
    public void convertAndSend(D destination, Object payload) throws MessagingException {
        this.convertAndSend(destination, payload, (Map)null);
    }
package cn.learn.rocketmq.spring;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestSpringRocketMQ {

    @Autowired
    private SpringProducer springProducer;

    @Test
    public void testSendMsg(){
        String msg = "我的第2个SpringRocketMQ消息!";
        this.springProducer.sendMsg("spring-my-topic", msg);
        System.out.println("发送成功");
    }

}
package cn.learn.rocketmq.spring;

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

@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

 

你可能感兴趣的:(RocketMQ)