SpringCloudAlibaba:6.3SpringBoot接入RocketMQ

依赖

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>java_sc_alibaba</artifactId>
        <groupId>jkw.life</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>test-rocketmq8009</artifactId>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- rocketmq-->
        <dependency>
            <groupId>org.apache.rocketmq</groupId>
            <artifactId>rocketmq-spring-boot-starter</artifactId>
            <version>2.2.3</version>
        </dependency>
        <!-- SpringMVC-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

application.yml

复制代码
server:
  port: 8009
spring:
  application:
    name: test-rocketmq8009
rocketmq:
  # nameserver地址
  name-server: 192.168.66.101:9876
  producer:
    # 生产组
    group: my-group1
    # 发送消息超时时间
    send-message-timeout: 300000
demo:
  rocketmq:
    topic: testtopic
    consumer: test_customer

启动类

复制代码
package jkw;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@Slf4j
@SpringBootApplication
public class Main8009 {
    public static void main(String[] args) {
        SpringApplication.run(Main8009.class, args);
        log.info("************** 服务提供者 8009 启动成功 ************");
    }
}

RocketMQ SpringBoot 3.0不兼容解决方案

我们要在resources文件夹中,新建META-INF/spring文件夹,在里面新建一个叫 org.springframework.boot.autoconfigure.AutoConfiguration.imports 的文件里面填入 org.apache.rocketmq.spring.autoconfigure.RocketMQAutoConfiguration

创建topic

复制代码
mqadmin updateTopic -n localhost:9876 -c DefaultCluster -t testtopic

生产者服务

复制代码
package jkw.service;

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.Service;
@Service
public class MessageProduce {
    @Autowired
    private RocketMQTemplate rocketMQTemplate; // 直接注入生产者
    @Value("${demo.rocketmq.topic}")
    private String topic;
    /**
     * 发送消息
     *
     * @param message
     * @return
     */
    public SendResult sendMessage(String message) {
        return rocketMQTemplate.syncSend(topic, message);
    }
}

生产者控制器

复制代码
package jkw.controller;

import jkw.service.MessageProduce;
import org.apache.rocketmq.client.producer.SendResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RocketmqProduceCon {
    @Autowired
    private MessageProduce messageProduce;
    /**
     * 发送消息
     *
     * @param message
     * @return
     */
    @GetMapping("/send")
    public SendResult sendMessage(String message) {
        return messageProduce.sendMessage(message);
    }
}

消费者服务

复制代码
package jkw.service;


import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.annotation.SelectorType;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;

/**
 * 消费者主要使用RocketMQMessageListener接口进行监听配置
 */
@Service
@RocketMQMessageListener(
        //主题
        topic = "${demo.rocketmq.topic}",
        //消费组
        consumerGroup = "${demo.rocketmq.consumer}",
        // 过滤方式:默认为Tag过滤
        selectorType = SelectorType.TAG,
        // 过滤值:默认为全部消费,不过滤【*】
        selectorExpression = "*",
        // 消费模式:顺序消费ORDERLY,并发消费CONCURRENTLY
        consumeMode = ConsumeMode.ORDERLY,
        // 消息模式:有集群消费CLUSTERING,广播消费
        messageModel = MessageModel.CLUSTERING

)
public class MessageConsumer implements RocketMQListener<String> {
    @Override
    public void onMessage(String s) {
        System.out.println(s);
    }
}

测试

1.发送消息:http://localhost:8009/send?message=test

2.idea的控制台查看监控后输出的内容

相关推荐
陌殇殇3 小时前
SpringBoot整合SpringCache缓存
spring boot·redis·缓存
小林学习编程5 小时前
Springboot + vue + uni-app小程序web端全套家具商场
前端·vue.js·spring boot
ladymorgana5 小时前
【Spring boot】tomcat Jetty Undertow对比,以及应用场景
spring boot·tomcat·jetty
IT_10246 小时前
Spring Boot项目开发实战销售管理系统——系统设计!
大数据·spring boot·后端
DCTANT6 小时前
【原创】国产化适配-全量迁移MySQL数据到OpenGauss数据库
java·数据库·spring boot·mysql·opengauss
Touper.7 小时前
SpringBoot -- 自动配置原理
java·spring boot·后端
喜欢敲代码的程序员8 小时前
SpringBoot+Mybatis+MySQL+Vue+ElementUI前后端分离版:项目搭建(一)
spring boot·mysql·elementui·vue·mybatis
华子w9089258599 小时前
基于 SpringBoot+Vue.js+ElementUI 的 “花开富贵“ 花园管理系统设计与实现7000字论文
vue.js·spring boot·elementui
小时候的阳光10 小时前
SpringBoot3 spring.factories 自动配置功能不生效?
spring boot·spring·失效·factories·imports
大只鹅11 小时前
Springboot3整合ehcache3缓存--XML配置和编程式配置
spring boot·缓存