我们之前学习了数据访问层的解决方案,包括关系数据库和非关系数据库,这一篇我们重点学校SpringBoot整合第三方技术,下面一起努力学习吧。
目录
缓存是在应用软件和数据库之间的一个数据临时存储介质,使用缓存可以有效地减少低速数据读取的次数(例如:磁盘IO),提高系统性能。缓存不仅可以用于提高永久性存储介质的数据读取效率,还可以提供临时的数据存储空间。

首先需要在pom.xml文件中导入缓存对应的坐标依赖,如下:

然后在启动类使用@EnableCaching注解启动缓存,如下:

在具体的操作中使用缓存,将相应的操作结果放入缓存中,具体如下:


1)浏览器传入手机号,返回6位验证码,将验证码存入缓存。
2)浏览器传入传入手机号和验证码,返回比对缓存中的验证码和传入的验证码结果是否一致。
首先配置坐标依赖,如下:
- "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 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.7.5version>
- <relativePath/>
- parent>
- <groupId>com.codegroupId>
- <artifactId>codeartifactId>
- <version>0.0.1-SNAPSHOTversion>
- <name>codename>
- <description>Demo project for Spring Bootdescription>
- <properties>
- <java.version>1.8java.version>
- properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starterartifactId>
- dependency>
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- <version>1.18.24version>
- <scope>providedscope>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-cacheartifactId>
- 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>
然后,创建实体类,使用lomok。
-
- import lombok.Data;
-
- @Data
- public class SMCode {
- private String tel ;
- private String code ;
- }
业务层,创建接口和实现类,用于将密码转化成验证码存入缓存,并对比下次传入的验证码和缓存中的验证码是否一致。
- import com.code.domain.SMCode;
-
- public interface SMService {
- public String sendToCodeSM(String tel) ;
- public boolean check(SMCode smCode) ;
- }
-
- import com.code.domain.SMCode;
- import com.code.utils.CodeUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.cache.annotation.CachePut;
- import org.springframework.stereotype.Service;
-
- @Service
- public class SMServiceImpl implements SMService {
-
- @Autowired
- private CodeUtils codeUtils ;
-
- @Override
- @CachePut(value = "smsCode", key = "#tel")
- public String sendToCodeSM(String tel) {
- String code = codeUtils.generator(tel) ;
- return code ;
- }
-
- @Override
- public boolean check(SMCode smCode) {
- String code = smCode.getCode() ;
- String smsCode = codeUtils.get(smCode.getTel()) ;
- return code.equals(smsCode);
- }
- }
业务层需要使用一个工具类,封装成bean,该工具类用于将密码转换成验证码以及根据手机号读取缓存中的验证码,如下:
- import org.springframework.cache.annotation.Cacheable;
- import org.springframework.stereotype.Component;
-
- @Component
- public class CodeUtils {
- static String [] arr = {"00000","0000","000","00","0",""} ;
-
- public String generator(String tel){
- int hash = tel.hashCode() ;
- int encryption = 20226666 ;
- long result = hash ^ encryption ;
- long time = System.currentTimeMillis() ;
- result = result ^ time ;
- long code = result % 100000 ;
- code = code < 0 ? -code : code ;
- return arr[String.valueOf(code).length()-1] + code ;
- }
-
- @Cacheable(value = "smsCode" , key = "#tel")
- public String get(String tel){
- return null ;
- }
-
- /* public static void main(String[] args) {
- System.out.println(new CodeUtils().generator("19825069651"));
- }*/
- }
在表现层调用业务层的接口,实现相应的请求和响应功能。
-
- import com.code.domain.SMCode;
- import com.code.service.SMService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
-
- @RestController
- @RequestMapping("/sms")
- public class SMController {
- @Autowired
- private SMService smService ;
-
- @GetMapping
- public String getCode(String tel){
- String code = smService.sendToCodeSM(tel) ;
- return code ;
- }
-
- @PostMapping
- public boolean checkCode(SMCode smCode){
- return smService.check(smCode) ;
- }
-
- }
最后需要在启动类加上启动缓存功能的注解EnableCaching,如下:
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cache.annotation.EnableCaching;
-
- @SpringBootApplication
- @EnableCaching
- public class CodeApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(CodeApplication.class, args);
- }
-
- }
首先需要在配置文件中加入坐标依赖,如下:
- <dependency>
- <groupId>net.sf.ehcachegroupId>
- <artifactId>ehcacheartifactId>
- dependency>
然后需要配置ehchache.xml,其中根据缓存名称加载缓存,可以设置缓存有效时间等。
- "1.0" encoding="UTF-8"?>
- <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:noNamespaceSchemaLocation="https://www.ehcache.org/ehcache.xsd"
- updateCheck="false">
- <diskStore path="D:\ehcache" />
-
-
-
-
-
-
-
-
-
- <defaultCache
- eternal="false"
- diskPersistent="false"
- maxElementsInMemory="1000"
- overflowToDisk="false"
- timeToIdleSeconds="60"
- timeToLiveSeconds="60"
- memoryStoreEvictionPolicy="LRU" />
-
- <cache
- name="smsCode"
- eternal="false"
- diskPersistent="false"
- maxElementsInMemory="1000"
- overflowToDisk="false"
- timeToIdleSeconds="60"
- timeToLiveSeconds="60"
- memoryStoreEvictionPolicy="LRU" />
-
- ehcache>
然后需要在.yml配置类中设置使用encache缓存即可,如下:
- spring:
- cache:
- type: ehcache
首先早pom.xml文件中导入redis的坐标依赖,如下:
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-data-redisartifactId>
- dependency>
然后配置redis在.yml文件中配置redis即可,如下:
- spring:
- redis:
- host: localhost
- port: 6379
- cache:
- type: redis
- redis:
- use-key-prefix: true
- key-prefix: sms_
- time-to-live: 20s #有效时长
- cache-null-values: false
首先下载memcahed,然后解压就可以,然后以管理员身份打开memcached,输入指令切换到memcahed解压的文件夹下,安装和启动memcached即可。

下面在SpringBoot中整合memcached缓存,首先在pom.xml文件中加载坐标依赖,具体如下:

在.yml文件中做该缓存的必要配置,如下:

然后,读取配置类,并封装成bean,如下:
创建客户端配置类,具体如下:

注入客户端对象,进行修改值和查询值,如下:


jetCache实现了对SpringCache进行了封装,在原有功能的基础之上实现了多级缓存、缓存统计、自动刷新、自动调用、数据报表等功能。
1)jetcache远程缓存
首先配置坐标依赖,需要把之前的redis坐标依赖删除,否则会导致循环依赖。
- "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 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.4.5version>
- <relativePath/>
- parent>
- <groupId>com.codegroupId>
- <artifactId>codeartifactId>
- <version>0.0.1-SNAPSHOTversion>
- <name>codename>
- <description>Demo project for Spring Bootdescription>
- <properties>
- <java.version>1.8java.version>
- properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starterartifactId>
- dependency>
- <dependency>
- <groupId>org.projectlombokgroupId>
- <artifactId>lombokartifactId>
- dependency>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
- <dependency>
- <groupId>com.alicp.jetcachegroupId>
- <artifactId>jetcache-starter-redisartifactId>
- <version>2.6.2version>
- 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>
然后再.yml文件中进行相关配置,如下:
- jetcache:
- remote:
- default:
- type: redis
- host: localhost
- port: 6379
- poolConfig:
- maxTotal: 50
使用缓存,如下所示。
- import com.alicp.jetcache.Cache;
- import com.alicp.jetcache.anno.CreateCache;
- import com.code.domain.SMCode;
- import com.code.utils.CodeUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import java.util.concurrent.TimeUnit;
-
- @Service
- public class SMServiceImpl implements SMService {
-
- @Autowired
- private CodeUtils codeUtils ;
-
- //设置缓存有效时长未20s
- @CreateCache(name = "jetCache", expire = 20, timeUnit = TimeUnit.SECONDS)
- public Cache
jetCache ; -
- @Override
- public String sendToCodeSM(String tel) {
- String code = codeUtils.generator(tel) ;
- jetCache.put(tel, code);
- return code ;
- }
-
- @Override
- public boolean check(SMCode smCode) {
- String code = smCode.getCode() ;
- String smsCode = jetCache.get(smCode.getTel()) ;
- return code.equals(smsCode);
- }
- }
需要在启动类加上启动注解,如下所示。
-
- @SpringBootApplication
- @EnableCreateCacheAnnotation
- public class CodeApplication {
-
- public static void main(String[] args) {
- ConfigurableApplicationContext context = SpringApplication.run(CodeApplication.class, args);
-
-
- }
-
- }
最后打开redis服务端,使用postman发送web请求测试即可。

2)jetcache本地缓存
本地方案和远程方案可以共存,可以设置选择使用哪一种方案,只需要在.yml文件中配置本地缓存
- jetcache:
- local:
- default:
- type: linkedhashmap
- keyConvertor: fastjson
然后在业务层使用缓存的时候,设置为使用本地缓存即可。
- import com.alicp.jetcache.Cache;
- import com.alicp.jetcache.anno.CacheType;
- import com.alicp.jetcache.anno.CreateCache;
- import com.code.domain.SMCode;
- import com.code.utils.CodeUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import java.util.concurrent.TimeUnit;
-
- @Service
- public class SMServiceImpl implements SMService {
-
- @Autowired
- private CodeUtils codeUtils ;
-
- //设置缓存有效时长未20s,设置为只用本地缓存
- @CreateCache(name = "jetCache1", expire = 20, timeUnit = TimeUnit.SECONDS, cacheType = CacheType.LOCAL)
- public Cache
jetCache ; -
- @Override
- public String sendToCodeSM(String tel) {
- String code = codeUtils.generator(tel) ;
- jetCache.put(tel, code);
- return code ;
- }
-
- @Override
- public boolean check(SMCode smCode) {
- String code = smCode.getCode() ;
- String smsCode = jetCache.get(smCode.getTel()) ;
- return code.equals(smsCode);
- }
- }
3)jetcache方法缓存
首先需要在启动类加上方法注解,开启方法缓存,方法所在的包需要指定。

下面就可以启用具体的缓存方法注解,查询的注解为@Cached,刷新的注解为@CacheRefresh,添加的注解为@CacheUpdate,删除的注解为@CacheInvalidate


因为需要保证缓存对象的序列化,才能操作方法缓存,所以需要对实体类实现序列化,并在配置类中设置序列化和反序列化的类型均为Java类型。

首先需要加入j2cache相关坐标,主要包含三个坐标,如下所示。

然后配置j2cache,设置j2cache配置文件的位置。

创建配置文件j2cache.properties,配置一级缓存和二级缓存以及一级缓存到二级缓存的发送方式。

将缓存对象进行注入,然后使用缓存对象进行缓存的set和get操作,如下:

1.2、任务我们在整合框架之前先看一下下面四个概念,工作和工作明细是比较容易理解的,触发器是触发工作的,调度器则是将工作和触发器绑定到一起,不同触发器触发不同的工作明细。

首先导入SpringBoot整合Quartz的坐标。
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-quartzartifactId>
- dependency>
定义具体要执行的任务。
-
- import org.quartz.JobExecutionContext;
- import org.quartz.JobExecutionException;
- import org.springframework.scheduling.quartz.QuartzJobBean;
-
- public class MyQuartz extends QuartzJobBean {
- @Override
- protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
- System.out.println("quartz take run...");
- }
- }
定义工作明细和触发器,并绑定它们之间的关系。
- import com.q.quartz.MyQuartz;
- import org.quartz.*;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
-
- @Configuration
- public class QuartzConfig {
-
- @Bean //绑定具体的工作
- public JobDetail printJobDetail(){
- return JobBuilder.newJob(MyQuartz.class).storeDurably().build() ;
- }
-
- @Bean //绑定对应的工作明细
- public Trigger printTrigger(){
- ScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/5 * * * * ?") ;
- return TriggerBuilder.newTrigger().forJob(printJobDetail()).withSchedule(scheduleBuilder).build() ;
- }
-
- }
首先在启动类中使用EnableScheduling开启定时任务功能,如下:
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.scheduling.annotation.EnableScheduling;
-
- @SpringBootApplication
- @EnableScheduling //开启定时任务功能
- public class QuartzApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(QuartzApplication.class, args);
- }
-
- }
然后,定义一个Bean,设置定时任务和定时周期就可以了,如下:
- import org.springframework.scheduling.annotation.Scheduled;
- import org.springframework.stereotype.Component;
-
- @Component
- public class MyTask {
- //设置定时执行的任务,并设置执行周期
- @Scheduled(cron = "0/5 * * * * ?")
- public void printLog(){
- System.out.println(Thread.currentThread().getName() + " is running");
- }
- }
我们首先来看一下电子邮件中的三个协议,学过计算机网络的对这个应该都比较熟悉了。

发送邮件的过程如下,首先导入坐标,具体如下:

需要配置javaMail,配置过程中邮件服务器供应商需要写清楚,邮箱账号和密码要写清楚,这个 密码是从邮件获取的授权码,点击如下开启,发送短信到指定账号即可获取授权码。
对于业务层,也就是邮件客户端需要怎么写呢,具体如下,首先需要定义发件人、收件人等信息,然后使用注入的JavaMailSender对象进行邮件发送。


这一部分只需要修改业务层的代码,实现邮件附件的发送,以及超链接的发送等。
具体就不展开了。
消息分为发送方和接收方,也称为生产者和消费者,消息又分为同步消息和异步消息,同步消息必须有响应,异步消息不需要有响应也可以的。
浏览器发送请求,业务系统会把请求先发送给MQ(消息队列),子业务系统在从MQ中获取相应的要执行的工作,这样的话就可以有效地降低业务系统的压力了,具体如下所示:

我们先了解一下JMS消息模型,它包含多种消息类型,我们常用的还是字节类型的消息,常用的模型是发布订阅模式。JMS是规范消息开发的API。

AMQP是规定消息传递的格式,是一种高级消息队列协议,如下所示:
我们在接下来主要学习四个消息队列的实现,分别是:ActiveMQ、RabbitMQ、RocketMQ、kafka.
1)ActiveMQ的下载与安装
首先在官网进行下载安装即可,官网地址如下:ActiveMQ

下载后解压缩,然后双击activemq.exe即可启动服务,通过地址可以访问ActiveMQ的服务器,如下:

进入到如下界面,说明ActiveMQ服务器启动成功。

2)SpringBoot整合ActiveMQ
首先导入mq坐标依赖,如下所示。
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-activemqartifactId>
- dependency>
然后在.yml文件中进行服务器的配置,如下:
-
- spring:
- activemq:
- broker-url: tcp://localhost:61616
- jms:
- template:
- default-destination: mq
- #pub-sub-domain: true 可以在这里设置发布订阅模式
使用注入的JmsMessageServiceTemplate进行生产消息和接收消息,为了使生产的消息可以立刻被消费,我们一般会设置监听器监听,然后立刻消费。
- import com.mq.service.MessageService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.jms.core.JmsMessagingTemplate;
- import org.springframework.stereotype.Service;
-
- @Service
- public class MessageServiceActivemqImpl implements MessageService {
-
-
- @Autowired
- private JmsMessagingTemplate jmsMessagingTemplate ;
-
- @Override
- public void sendMessage(String id) {
- System.out.println("待发送的消息已纳入处理队列:id" + id);
- jmsMessagingTemplate.convertAndSend("order.queue.id",id);
- }
-
- @Override
- public String doMessage() {
- String id = jmsMessagingTemplate.receiveAndConvert("order.queue.id" , String.class) ;
- System.out.println("已经完成发送的业务:id" + id);
- return id;
- }
- }
监听器的设置如下,用于监听 指定位置的消息。
- import org.springframework.jms.annotation.JmsListener;
- import org.springframework.messaging.handler.annotation.SendTo;
- import org.springframework.stereotype.Component;
-
- @Component
- public class MessageListener {
- @JmsListener(destination = "order.queue.id") //监听响应位置的消息,用于立刻消费、
- @SendTo("order.other.queue.id") //用于
- public String receive(String id){
- System.out.println("已完成短信业务发送:id" + id);
- return "new:" + id ;
- }
- }
控制层发送消息和接收消息的代码如下:
- import com.mq.service.OrderService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- @RestController
- @RequestMapping("/orders")
- public class OrderController {
-
- @Autowired
- private OrderService orderService ;
-
- @PostMapping("{id}")
- public void order(@PathVariable String id){
- orderService.order(id);
- }
- }
-
- import com.mq.service.MessageService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- @RestController
- @RequestMapping("/msgs")
- public class MessageController {
- @Autowired
- private MessageService messageService ;
-
-
- @GetMapping
- public String doMessage(){
- String id = messageService.doMessage() ;
- return id ;
- }
- }
使用postman发送和接收请求模拟得到的效果如下:

1)RabbitMQ的安装与配置
安装和路径配置的过程如下所示,参考这篇文章即可:我们一起来学RabbitMQ 四:RabbitMQ windows 安装 - 知乎
命令行看到如下消息,则说明安装配置 erlang 环境成功。

2)直联交换机模式下SpringBoot整合RabbitMQ
首先导入RabbitMQ的坐标依赖,如下所示:
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-amqpartifactId>
- dependency>
在.yml文件中配置mq,具体如下:
- spring:
- activemq:
- broker-url: tcp://localhost:61616
- jms:
- template:
- default-destination: mq
- #pub-sub-domain: true 可以在这里设置发布订阅模式
- rabbitmq:
- host: localhost
- port: 5672
定义消息队列和交换机,并实现队列和交换机进行绑定,具体如下:
-
-
- import org.springframework.amqp.core.Binding;
- import org.springframework.amqp.core.BindingBuilder;
- import org.springframework.amqp.core.DirectExchange;
- import org.springframework.amqp.core.Queue;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
-
- @Configuration
- public class RabbitConfigDirect {
- @Bean //消息队列的对象
- public Queue directQueue(){
- return new Queue("direct_queue") ;
- }
- @Bean //消息队列的对象
- public Queue directQueue2(){
- return new Queue("direct_queue2") ;
- }
- @Bean //交换机
- public DirectExchange directExchange(){
- return new DirectExchange("directExchange") ;
- }
- @Bean //将消息队列和交换机进行绑定
- public Binding bindingDirect(){
- return BindingBuilder.bind(directQueue()).to(directExchange()).with("direct") ;
- }
-
- @Bean //将消息队列和交换机进行绑定
- public Binding bindingDirect2(){
- return BindingBuilder.bind(directQueue2()).to(directExchange()).with("direct2") ;
- }
- }
业务层使用注入的AmqpTemplate进行生产消息,如下:
- import com.mq.service.MessageService;
- import org.springframework.amqp.core.AmqpTemplate;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- @Service
- public class MessageServiceRabbitmqDirectImpl implements MessageService {
-
- @Autowired
- private AmqpTemplate amqpTemplate ;
- @Override
- public void sendMessage(String id) { //使用直联交换机发送消息
- System.out.println("待发送的消息订单已经纳入处理队列(rabbitmq direct).id :" + id);
- amqpTemplate.convertAndSend("directExchange","direct",id);
- }
-
- @Override
- public String doMessage() {
- return null;
- }
- }
使用监听器进行监听,消费消息。
- import org.springframework.amqp.rabbit.annotation.RabbitListener;
- import org.springframework.stereotype.Component;
-
- @Component
- public class MessageListener {
- //监听消息队列,有消息就可以消费
- @RabbitListener(queues = "direct_queue")
- public void receive(String id){
- System.out.println("已完成短信发送业务(rabbitmq direct).id" + id);
- }
- }
使用postman发送消息请求,可以在服务器端看到生成的两个消息队列。

3)主题模式下的SpringBoot整合RabbitMQ
该模式和direct模式的区别在于匹配方式的不同,具体如下:

1)下载安装RocketMQ
现在官网下载安装并完成环境变量的配置,具体如下:

先启动命名服务器,再启动业务服务器,如下:

2)SpringBoot整合RocketMQ
首先在pom.xml文件中导入RocketMQ坐标依赖,具体如下:

需要在.yml文件中进行配置,先配置命名服务器,在给生产者配置一个组,如下:

生产消息,使用注入的RocketMQTemplate完成生产消息。

当然也可以使用异步生产消息方式进行生产消息,具体如下:
使用监听器的方式进行监听并消费消息,如下:
