├── .gitignore
├── README.md
├── pom.xml
├── redis-mq-test
├── pom.xml
└── src
│ └── main
│ ├── java
│ └── org
│ │ └── example
│ │ └── redis
│ │ └── mq
│ │ └── test
│ │ ├── SpringRedisMq.java
│ │ ├── config
│ │ ├── RedisConfig.java
│ │ └── SwaggerConfig.java
│ │ ├── controller
│ │ └── TestController.java
│ │ ├── entity
│ │ └── Job.java
│ │ └── listener
│ │ └── RedisMQListener.java
│ └── resources
│ ├── application.yml
│ └── logback.xml
└── springboot-starter-redis-mq
├── pom.xml
└── src
└── main
├── java
└── com
│ └── jdragon
│ └── starter
│ └── redis
│ └── mq
│ ├── RedisMQListenerAutoConfig.java
│ ├── annotations
│ └── RedisListener.java
│ └── core
│ ├── RedisListenerAnnotationScanPostProcesser.java
│ ├── RedisMQSender.java
│ ├── RedisMessageQueueRegister.java
│ └── domain
│ ├── RedisListenerMethod.java
│ └── RedisMessage.java
└── resources
└── META-INF
└── spring.factories
/.gitignore:
--------------------------------------------------------------------------------
1 | # Created by .ignore support plugin (hsz.mobi)
2 | ### Java template
3 | # Compiled class file
4 | *.class
5 |
6 | # Log file
7 | *.log
8 |
9 | # BlueJ files
10 | *.ctxt
11 |
12 | # Mobile Tools for Java (J2ME)
13 | .mtj.tmp/
14 |
15 | # Package Files #
16 | *.jar
17 | *.war
18 | *.nar
19 | *.ear
20 | *.zip
21 | *.tar.gz
22 | *.rar
23 |
24 | *.iml
25 |
26 | .idea/
27 |
28 | target/
29 |
30 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
31 | hs_err_pid*
32 |
33 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## RedisMQ简介
2 |
3 | 在SpringBoot-Starter上,基于RedisTemplate的Redis Stream API实现的消息队列,注解驱动编程,快速上手。
4 |
5 |
6 |
7 | ## Quick Start
8 |
9 | #### 1、拉取编译
10 |
11 | ```shell
12 | git clone http://gitlab.tyu.wiki/jdragon/spring-redis-mq.git
13 |
14 | mvn install
15 | ```
16 |
17 |
18 |
19 | #### 2、依赖导入
20 |
21 | ```xml
22 |
23 | com.jdragon.starter
24 | springboot-starter-redis-mq
25 | 1.0-SNAPSHOT
26 |
27 | ```
28 |
29 |
30 |
31 | #### 3、添加配置
32 |
33 | 使用的是springboot自带的RedisTemplate,可通过spring自带参数设置redismq的配置
34 |
35 | ```yml
36 | spring:
37 | redis:
38 | host: 127.0.0.1
39 | port: 6379
40 | ```
41 |
42 |
43 |
44 | #### 4、生产消费示例
45 |
46 | ```java
47 | @Data
48 | public class Job {
49 | private Integer id;
50 |
51 | private Map param;
52 |
53 | @JsonFormat(pattern = "yyyy-MM-dd hh:mm:ss")
54 | private Date createDate;
55 |
56 | @JsonSerialize(using = LocalDateTimeSerializer.class)
57 | @JsonDeserialize(using = LocalDateTimeDeserializer.class)
58 | private LocalDateTime endDate;
59 | }
60 | ```
61 |
62 |
63 |
64 | ##### 1)生产者
65 |
66 | 创建一个TestController类,注入`RedisMQSender`
67 | PS:redis发送生产消息的方法`RedisMQSender.send(参数1,参数2)`:
68 |
69 | - 参数1是String类型的消息队列名称
70 | - 参数2是你想传递的任意数据。
71 |
72 | ```java
73 | @RestController
74 | @RequestMapping("test")
75 | public class TestController {
76 |
77 | @Autowired
78 | private RedisMQSender redisMQSender;
79 |
80 | @GetMapping("redisMQSender")
81 | public String redisMQSender() {
82 | String streamKey = "streamKey";
83 | redisMQSender.send(streamKey, "你好");
84 | return "成功";
85 | }
86 |
87 | @GetMapping("redisMQSender2")
88 | public String redisMQSender2() {
89 | String streamKey = "streamKey2";
90 |
91 | HashMap param = new HashMap<>();
92 | param.put("id", 1);
93 | param.put("name", "张三");
94 | param.put("paramInline", new HashMap<>());
95 |
96 | Job job = new Job();
97 | job.setId(1);
98 | job.setParam(param);
99 | job.setCreateDate(new Date());
100 | job.setEndDate(LocalDateTime.now());
101 | redisMQSender.send(streamKey, job);
102 | return "成功";
103 | }
104 | }
105 | ```
106 |
107 |
108 |
109 | ##### 2)消费者
110 |
111 | 创建一个RedisListenerContainer类用于定义redis队列消息监听处理方法。
112 |
113 | @RedisListener注解支持参数
114 |
115 | - queueName:监听队列名称,默认为default_queue,表示该方法你需要处理的哪个队列的消息。
116 | - group:消费者组,默认为default_group,消费者组内存在竞争关系。
117 | - customer:消费者名称,默认为consumer
118 |
119 |
120 |
121 | PS: 实现redis队列监听只需在Spring容器所管理的Bean中的方法上添加注解`@RedisListener(queueName,group,customer)`,注意被@RedisListener修饰的方法只能包含一个参数,这个参数的可以一个`RedisMessage`类型的参数,也可以是你需要传递的直接消息类型。
122 |
123 | ```java
124 | @Slf4j
125 | @Component
126 | public class RedisMQListener {
127 | @RedisListener(queueName = "streamKey")
128 | public void test(RedisMessage redisMessage) {
129 | log.info("redis message 接受到信息:{}", redisMessage.getData());
130 | }
131 |
132 | @RedisListener(queueName = "streamKey")
133 | public void test2(String redisMessage) {
134 | log.info("接受到信息:{}", redisMessage);
135 | }
136 |
137 | @RedisListener(queueName = "streamKey2")
138 | public void test3(RedisMessage job) {
139 | log.info("redis message 接受到信息:{}", job.getData());
140 | }
141 |
142 | @RedisListener(queueName = "streamKey2")
143 | public void test4(Job job) {
144 | log.info("接受到信息:{}", job);
145 | }
146 | }
147 | ```
148 |
149 |
150 |
151 | #### 3)启动结果
152 |
153 |
154 |
155 | 见以下日志打印,即为启动监听成功
156 |
157 | ```powershell
158 | 2021-12-17 17:08:26.787 [main] INFO c.j.s.r.m.c.RedisMessageQueueRegister [run 101] - 启动消息队列监听器:【streamKey2.default_group】
159 | 2021-12-17 17:08:26.871 [main] INFO c.j.s.r.m.c.RedisMessageQueueRegister [run 101] - 启动消息队列监听器:【streamKey.default_group】
160 | ```
161 |
162 |
163 |
164 | 请求`/test/redisMQSender`
165 |
166 | ```powershell
167 | 2021-12-17 17:11:14.633 [SimpleAsyncTaskExecutor-2] INFO c.j.s.r.m.c.RedisMessageQueueRegister [lambda$run$0 78] - stream message。messageId=1639732274611-0, stream=streamKey, body={"queueName":"streamKey","data":"你好","createTime":"2021-12-17 05:11:14"}
168 | 2021-12-17 17:11:14.634 [SimpleAsyncTaskExecutor-2] INFO o.e.r.m.t.listener.RedisMQListener [test 20] - redis message 接受到信息:你好
169 | 2021-12-17 17:11:14.635 [SimpleAsyncTaskExecutor-2] INFO o.e.r.m.t.listener.RedisMQListener [test3 30] - 接受到信息:你好
170 | ```
171 |
172 |
173 |
174 | 请求`/test/redisMQSender2`
175 |
176 | ```powershell
177 | 2021-12-17 17:11:20.547 [SimpleAsyncTaskExecutor-1] INFO c.j.s.r.m.c.RedisMessageQueueRegister [lambda$run$0 78] - stream message。messageId=1639732280628-0, stream=streamKey2, body={"queueName":"streamKey2","data":{"id":1,"param":{"paramInline":{},"name":"张三","id":1},"createDate":"2021-12-17 05:11:20","endDate":"2021-12-17T17:11:20.461"},"createTime":"2021-12-17 05:11:20"}
178 | 2021-12-17 17:11:20.547 [SimpleAsyncTaskExecutor-1] INFO o.e.r.m.t.listener.RedisMQListener [test2 25] - redis message 接受到信息:{id=1, param={paramInline={}, name=张三, id=1}, createDate=2021-12-17 05:11:20, endDate=2021-12-17T17:11:20.461}
179 | 2021-12-17 17:11:20.548 [SimpleAsyncTaskExecutor-1] INFO o.e.r.m.t.listener.RedisMQListener [test4 35] - 接受到信息:Job(id=1, param={paramInline={}, name=张三, id=1}, createDate=Fri Dec 17 05:11:20 CST 2021, endDate=2021-12-17T17:11:20.461)
180 | ```
181 |
182 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 | 4.0.0
6 |
7 | org.example.redis
8 | spring-redis-mq
9 | 1.0-SNAPSHOT
10 | pom
11 |
12 |
13 | redis-mq-test
14 | springboot-starter-redis-mq
15 |
16 |
17 |
--------------------------------------------------------------------------------
/redis-mq-test/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
5 | 4.0.0
6 |
7 | org.springframework.boot
8 | spring-boot-starter-parent
9 | 2.5.7
10 |
11 |
12 | org.example.redis.mq.test
13 | redis-mq-test
14 | 1.0-SNAPSHOT
15 | redis-mq-test
16 |
17 |
18 | UTF-8
19 | 1.8
20 | 1.8
21 |
22 |
23 |
24 |
25 | com.jdragon.starter
26 | springboot-starter-redis-mq
27 | 1.0-SNAPSHOT
28 |
29 |
30 |
31 | junit
32 | junit
33 | 4.11
34 | test
35 |
36 |
37 |
38 |
39 | org.springframework.boot
40 | spring-boot-starter-data-redis
41 |
42 |
43 |
44 | org.springframework.data
45 | spring-data-redis
46 |
47 |
48 |
49 | org.springframework.boot
50 | spring-boot-starter-web
51 |
52 |
53 |
54 | org.springframework.boot
55 | spring-boot-starter-test
56 | test
57 |
58 |
59 |
60 | org.projectlombok
61 | lombok
62 | 1.18.2
63 |
64 |
65 |
66 | io.springfox
67 | springfox-boot-starter
68 | 3.0.0
69 |
70 |
71 |
72 | com.github.xiaoymin
73 | knife4j-spring-boot-starter
74 | 3.0.2
75 |
76 |
77 | org.apache.commons
78 | commons-pool2
79 |
80 |
81 |
82 | com.fasterxml.jackson.datatype
83 | jackson-datatype-jsr310
84 |
85 |
86 |
87 |
88 |
89 | org.springframework.boot
90 | spring-boot-maven-plugin
91 |
92 |
93 |
94 |
95 |
--------------------------------------------------------------------------------
/redis-mq-test/src/main/java/org/example/redis/mq/test/SpringRedisMq.java:
--------------------------------------------------------------------------------
1 | package org.example.redis.mq.test;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | * Hello world!
8 | */
9 |
10 | @SpringBootApplication
11 | public class SpringRedisMq {
12 | public static void main(String[] args) {
13 | SpringApplication.run(SpringRedisMq.class);
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/redis-mq-test/src/main/java/org/example/redis/mq/test/config/RedisConfig.java:
--------------------------------------------------------------------------------
1 | package org.example.redis.mq.test.config;
2 |
3 | import com.fasterxml.jackson.annotation.JsonAutoDetect;
4 | import com.fasterxml.jackson.annotation.PropertyAccessor;
5 | import com.fasterxml.jackson.databind.ObjectMapper;
6 | import lombok.extern.slf4j.Slf4j;
7 | import org.springframework.cache.annotation.CachingConfigurerSupport;
8 | import org.springframework.context.annotation.Bean;
9 | import org.springframework.context.annotation.Configuration;
10 | import org.springframework.data.redis.connection.RedisConnectionFactory;
11 | import org.springframework.data.redis.core.RedisTemplate;
12 | import org.springframework.data.redis.core.StringRedisTemplate;
13 | import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
14 |
15 | @Slf4j
16 | @Configuration
17 | public class RedisConfig extends CachingConfigurerSupport {
18 |
19 | /**
20 | * 默认是JDK的序列化策略,这里配置redisTemplate采用的是Jackson2JsonRedisSerializer的序列化策略
21 | *
22 | * @param redisConnectionFactory
23 | * @return
24 | */
25 | @Bean
26 | public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
27 | //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用JDK的序列化方式)
28 | Jackson2JsonRedisSerializer