springboot 整合rocketmq

centos7安装rocketmq

在rocketmq官网下载压缩包
官网地址:https://www.apache.org/dyn/closer.cgi?path=rocketmq/4.7.1/rocketmq-all-4.7.1-bin-release.zip
springboot 整合rocketmq_第1张图片

下载到本地后上传至centos7,我的上传路径为/usr/rocektmq
,然后解压

unzip rocketmq-all-4.7.1-bin-release.zip 

解压后进入目录

cd rocketmq-all-4.7.1-bin-release

创建logs目录用于存放日志文件

mkdir logs

修改nameserver和broker默认内存为2g

vi bin/runserver.sh 

springboot 整合rocketmq_第2张图片

vi bin/runbroker.sh

springboot 整合rocketmq_第3张图片
nameserver后台挂载启动

nohup sh bin/mqnamesrv > /usr/rocektmq/rocketmq-all-4.7.1-bin-release/logs/namesrv.log 2>&1 &

修改broker.conf

vi broker.conf 

加一行brokerIP1 = 192.168.130.128,指定ip

springboot 整合rocketmq_第4张图片
启动mqbroker

nohup sh bin/mqbroker -c conf/broker.conf -n localhost:9876 > /usr/rocektmq/rocketmq-all-4.7.1-bin-release/logs/broker.log 2>&1 &

关闭broker和nameserver

./bin/mqshutdown broker

./bin/mqshutdown namesrv

创建一个topic

./bin/mqadmin updateTopic -n '192.168.130.128:9876' -c DefaultCluster -t TopicTest

查看集群状态

./bin/mqadmin clusterList -n localhost:9876

在这里插入图片描述
至此rocketmq的安装已经讲解完毕了。
下面通过springboot来集成rocketmq

springboot整合rocketmq

pom文件


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0modelVersion>
	<parent>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-parentartifactId>
		<version>2.3.3.RELEASEversion>
		<relativePath/>
	parent>
	<groupId>com.sunyuqigroupId>
	<artifactId>demoartifactId>
	<version>0.0.1-SNAPSHOTversion>
	<name>demoname>
	<description>Demo project for Spring Bootdescription>

	<properties>
		<java.version>1.8java.version>
	properties>

	<dependencies>
		<dependency>
			<groupId>org.apache.rocketmqgroupId>
			<artifactId>rocketmq-spring-boot-starterartifactId>
			<version>2.1.0version>
		dependency>

		<dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-testartifactId>
			<scope>testscope>
		dependency>
	dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.bootgroupId>
				<artifactId>spring-boot-maven-pluginartifactId>
			plugin>
		plugins>
	build>
project>

配置文件

rocketmq:
  name-server: 192.168.130.128:9876
  producer:
    group: my-group

myrocketmq-config:
  my-topic: my_topic
  my-consumer-group: my_consumer_group

引导类

package com.sunyuqi;

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

@SpringBootApplication
public class DemoApplication {
     

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

消费者
需要继承RocketMQListener接口,此处泛型为String,如果需要监听对象,请填写自定义对象名

package com.sunyuqi.consumer;

import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(consumerGroup = "${myrocketmq-config.my-consumer-group}", topic = "${myrocketmq-config.my-topic}")
public class Consumer implements RocketMQListener<String> {
     

    @Override
    public void onMessage(String message) {
     
        System.out.println("收到消息: "+message);
    }
}

消息发送者

package com.sunyuqi.produce;

import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


@Component
public class RocketProduce {
     

    @Value("${myrocketmq-config.my-topic}")
    private String my_topic;

    @Value("${myrocketmq-config.my-consumer-group}")
    private String my_consumer_group;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    //同步发送
    public void sync(){
     
        String msg = "hello world";
        SendResult sendResult = rocketMQTemplate.syncSend(my_topic, msg);
        System.out.println("同步发送字符串: " + msg + "至topic: "+my_topic+",发送结果: " + sendResult);
    }

    //异步发送
    public void async(){
     

        rocketMQTemplate.asyncSend("test", "Hello world!", new SendCallback() {
     
            @Override
            public void onSuccess(SendResult var1) {
     
                System.out.println("异步发送成功: "+ var1);
            }
            @Override
            public void onException(Throwable var1) {
     
                System.out.println("异步发送失败: "+ var1);
            }
        });
    }

    //单向发送
    public void oneway(){
     
        rocketMQTemplate.sendOneWay("test", "Hello world!");
        System.out.println("单向发送");
    }
}

rocketmq有三种消息发送方式,分别为同步发送,异步发送,单发
同步发送:只有在消息完全发送完成之后才返回结果,此方式存在需要同步等待发送结果的时间代价。
异步发送:消息发送后立刻返回,当消息完全完成发送后,会调用回调函数sendCallback来告知发送者本次发送是成功或者失败。
单发:发送完消息后会立即返回,不会等待来自broker的ack来告知本次消息发送是否完全完成发送。

你可能感兴趣的:(消息中间件,java,rocketmq,消息中间件,spring,boot)