Skip to content

Commit 2472cd3

Browse files
committed
add springRedis queue
1 parent 0db55bd commit 2472cd3

9 files changed

Lines changed: 422 additions & 0 deletions

File tree

springboot-redis-queue/pom.xml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>cn.abel</groupId>
8+
<artifactId>springboot-redis-queue</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<parent>
11+
<groupId>org.springframework.boot</groupId>
12+
<artifactId>spring-boot-starter-parent</artifactId>
13+
<version>2.0.4.RELEASE</version>
14+
<relativePath/> <!-- lookup parent from repository -->
15+
</parent>
16+
17+
<dependencies>
18+
<!--springboot-->
19+
<dependency>
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-starter-web</artifactId>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.springframework.boot</groupId>
25+
<artifactId>spring-boot-starter-test</artifactId>
26+
<scope>test</scope>
27+
</dependency>
28+
<!--redis-->
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-data-redis</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.apache.commons</groupId>
35+
<artifactId>commons-pool2</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-autoconfigure</artifactId>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>redis.clients</groupId>
44+
<artifactId>jedis</artifactId>
45+
<version>3.0.1</version>
46+
</dependency>
47+
48+
49+
</dependencies>
50+
<build>
51+
<plugins>
52+
<plugin>
53+
<groupId>org.springframework.boot</groupId>
54+
<artifactId>spring-boot-maven-plugin</artifactId>
55+
</plugin>
56+
<plugin>
57+
<groupId>org.apache.maven.plugins</groupId>
58+
<artifactId>maven-compiler-plugin</artifactId>
59+
</plugin>
60+
<plugin>
61+
<groupId>org.apache.maven.plugins</groupId>
62+
<artifactId>maven-surefire-plugin</artifactId>
63+
<version>2.18.1</version>
64+
<configuration>
65+
<skipTests>true</skipTests>
66+
</configuration>
67+
</plugin>
68+
</plugins>
69+
</build>
70+
</project>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cn.abel.queue;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* @author yyb
8+
* @time 2019/8/13
9+
*/
10+
@SpringBootApplication
11+
public class Application {
12+
public static void main(String[] args) {
13+
SpringApplication.run(Application.class, args);
14+
}
15+
16+
}
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
package cn.abel.queue.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 org.apache.commons.pool2.impl.GenericObjectPoolConfig;
7+
import org.slf4j.Logger;
8+
import org.slf4j.LoggerFactory;
9+
import org.springframework.beans.factory.annotation.Value;
10+
import org.springframework.cache.annotation.CachingConfigurerSupport;
11+
import org.springframework.cache.annotation.EnableCaching;
12+
import org.springframework.cache.interceptor.KeyGenerator;
13+
import org.springframework.context.annotation.Bean;
14+
import org.springframework.context.annotation.Configuration;
15+
import org.springframework.data.redis.connection.RedisConnectionFactory;
16+
import org.springframework.data.redis.connection.RedisPassword;
17+
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
18+
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
19+
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
20+
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
21+
import org.springframework.data.redis.core.RedisTemplate;
22+
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
23+
import org.springframework.data.redis.serializer.StringRedisSerializer;
24+
25+
import java.time.Duration;
26+
27+
/**
28+
* @author yyb
29+
* @time 2019/8/13
30+
*/
31+
@Configuration
32+
@EnableCaching
33+
public class RedisConfig extends CachingConfigurerSupport {
34+
/**
35+
* logger
36+
*/
37+
private static final Logger logger = LoggerFactory.getLogger(RedisConfig.class);
38+
@Value("${spring.redis.database}")
39+
private Integer database;
40+
@Value("${spring.redis.host}")
41+
private String host;
42+
@Value("${spring.redis.port}")
43+
private Integer port;
44+
@Value("${spring.redis.password}")
45+
private String password;
46+
@Value("${spring.redis.lettuce.pool.max-active}")
47+
private Integer maxActive;
48+
@Value("${spring.redis.lettuce.pool.max-wait}")
49+
private Integer maxWait;
50+
@Value("${spring.redis.lettuce.pool.max-idle}")
51+
private Integer maxIdle;
52+
@Value("${spring.redis.lettuce.pool.min-idle}")
53+
private Integer minIdle;
54+
@Value("${spring.redis.lettuce.shutdown-timeout}")
55+
private Integer timeout;
56+
57+
/**
58+
* 在使用@Cacheable时,如果不指定key,则使用这个默认的key生成器生成的key
59+
*
60+
* @return
61+
*/
62+
@Override
63+
@Bean
64+
public KeyGenerator keyGenerator() {
65+
return (target, method, params) -> {
66+
StringBuilder sb = new StringBuilder();
67+
sb.append(target.getClass().getName());
68+
sb.append(method.getName());
69+
for (Object obj : params) {
70+
sb.append(obj.toString());
71+
}
72+
return sb.toString();
73+
};
74+
}
75+
76+
@Bean(name = "redisTemplate")
77+
public RedisTemplate<Object, Object> redisTemplate() {
78+
return getTemplate(redisConnectionFactory());
79+
}
80+
81+
private RedisConnectionFactory redisConnectionFactory() {
82+
return connectionFactory(maxActive, maxIdle, minIdle, maxWait, host, password, timeout, port, database);
83+
}
84+
85+
86+
/**
87+
* 创建连接
88+
*
89+
* @param maxActive
90+
* @param maxIdle
91+
* @param minIdle
92+
* @param maxWait
93+
* @param host
94+
* @param password
95+
* @param timeout
96+
* @param port
97+
* @param database
98+
* @return
99+
*/
100+
private RedisConnectionFactory connectionFactory(Integer maxActive,
101+
Integer maxIdle,
102+
Integer minIdle,
103+
Integer maxWait,
104+
String host,
105+
String password,
106+
Integer timeout,
107+
Integer port,
108+
Integer database) {
109+
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
110+
redisStandaloneConfiguration.setHostName(host);
111+
redisStandaloneConfiguration.setPort(port);
112+
redisStandaloneConfiguration.setDatabase(database);
113+
redisStandaloneConfiguration.setPassword(RedisPassword.of(password));
114+
115+
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
116+
poolConfig.setMaxTotal(maxActive);
117+
poolConfig.setMinIdle(minIdle);
118+
poolConfig.setMaxIdle(maxIdle);
119+
poolConfig.setMaxWaitMillis(maxWait);
120+
LettuceClientConfiguration lettucePoolingConfig = LettucePoolingClientConfiguration.builder()
121+
.poolConfig(poolConfig).shutdownTimeout(Duration.ofMillis(timeout)).build();
122+
LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(redisStandaloneConfiguration,
123+
lettucePoolingConfig);
124+
connectionFactory.afterPropertiesSet();
125+
126+
return connectionFactory;
127+
}
128+
129+
/**
130+
* 创建 RedisTemplate 连接类型,此处为hash
131+
*
132+
* @param factory
133+
* @return
134+
*/
135+
private RedisTemplate<Object, Object> getTemplate(RedisConnectionFactory factory) {
136+
RedisTemplate<Object, Object> template = new RedisTemplate<>();
137+
template.setConnectionFactory(factory);
138+
template.setValueSerializer(jackson2JsonRedisSerializer(new ObjectMapper()));
139+
template.setKeySerializer(new StringRedisSerializer());
140+
template.setHashKeySerializer(new StringRedisSerializer());
141+
template.setHashValueSerializer(jackson2JsonRedisSerializer(new ObjectMapper()));
142+
143+
template.afterPropertiesSet();
144+
return template;
145+
}
146+
147+
/**
148+
* 对value 进行序列化
149+
*
150+
* @param objectMapper
151+
* @return
152+
*/
153+
private Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer(ObjectMapper objectMapper) {
154+
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer =
155+
new Jackson2JsonRedisSerializer<>(Object.class);
156+
jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
157+
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
158+
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
159+
return jackson2JsonRedisSerializer;
160+
}
161+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package cn.abel.queue.controller;
2+
3+
import cn.abel.queue.service.ProducerService;
4+
import cn.abel.queue.service.ReceiverService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.PathVariable;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.bind.annotation.RequestMethod;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
/**
12+
* @author yyb
13+
* @time 2019/8/13
14+
*/
15+
16+
@RestController
17+
@RequestMapping("/publisher")
18+
public class PublisherController {
19+
@Autowired
20+
private ProducerService producerService;
21+
@Autowired
22+
private ReceiverService receiverService;
23+
24+
@RequestMapping(value = "{name}",method = RequestMethod.GET)
25+
public String sendMessage(@PathVariable("name") String name) {
26+
return producerService.sendMessage(name);
27+
}
28+
29+
30+
@RequestMapping(value = "/get",method =RequestMethod.GET )
31+
public String getMessage() {
32+
return receiverService.getMessage();
33+
}
34+
35+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cn.abel.queue.service;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.data.redis.core.StringRedisTemplate;
5+
import org.springframework.stereotype.Service;
6+
7+
8+
/**
9+
* @author yyb
10+
* @time 2019/8/13
11+
*/
12+
13+
@Service
14+
public class ProducerService {
15+
@Autowired
16+
private StringRedisTemplate redisTemplate;
17+
18+
public String sendMessage(String name) {
19+
try {
20+
redisTemplate.opsForList().leftPush("quere", name);
21+
return "消息发送成功了";
22+
23+
} catch (Exception e) {
24+
e.printStackTrace();
25+
return "消息发送失败了";
26+
}
27+
}
28+
29+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cn.abel.queue.service;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.data.redis.core.StringRedisTemplate;
5+
import org.springframework.stereotype.Service;
6+
7+
/**
8+
* @author yyb
9+
* @time 2019/8/13
10+
*/
11+
@Service
12+
public class ReceiverService {
13+
@Autowired
14+
private StringRedisTemplate redisTemplate;
15+
16+
public String getMessage() {
17+
String value = redisTemplate.opsForList().rightPop("quere");
18+
return value;
19+
}
20+
}
21+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
## tomcat\u914D\u7F6E
2+
server.port=9666
3+
#server.tomcat.maxHttpHeaderSize=8192
4+
server.tomcat.uri-encoding=UTF-8
5+
spring.http.encoding.charset=UTF-8
6+
spring.http.encoding.enabled=true
7+
spring.http.encoding.force=true
8+
spring.messages.encoding=UTF-8
9+
# tomcat\u6700\u5927\u7EBF\u7A0B\u6570\uFF0C\u9ED8\u8BA4\u4E3A200
10+
server.tomcat.max-threads=800
11+
# session\u6700\u5927\u8D85\u65F6\u65F6\u95F4(\u5206\u949F)\uFF0C\u9ED8\u8BA4\u4E3A30
12+
server.session-timeout=60
13+
14+
## spring \u914D\u7F6E
15+
spring.application.name=redis-queue
16+
application.main=cn.abel.queue.Application
17+
18+
## LOG
19+
logging.file=./logs/redis-queue.log
20+
21+
22+
## spring cache
23+
#\u7F13\u5B58\u7684\u540D\u79F0\u96C6\u5408\uFF0C\u591A\u4E2A\u91C7\u7528\u9017\u53F7\u5206\u5272
24+
#spring.cache.cache-names=admin,role
25+
#\u7F13\u5B58\u7684\u7C7B\u578B\uFF0C\u5B98\u65B9\u63D0\u4F9B\u4E86\u5F88\u591A\uFF0C\u8FD9\u91CC\u6211\u4EEC\u586B\u5199redis
26+
spring.cache.type=redis
27+
#\u662F\u5426\u7F13\u5B58null\u6570\u636E\uFF0C\u9ED8\u8BA4\u662Ffalse
28+
#spring.cache.redis.cache-null-values=false
29+
#redis\u4E2D\u7F13\u5B58\u8D85\u65F6\u7684\u65F6\u95F4\uFF0C\u9ED8\u8BA460000ms
30+
#spring.cache.redis.time-to-live=60000
31+
#\u7F13\u5B58\u6570\u636Ekey\u662F\u5426\u4F7F\u7528\u524D\u7F00\uFF0C\u9ED8\u8BA4\u662Ftrue
32+
#spring.cache.redis.use-key-prefix=true
33+
#\u7F13\u5B58\u6570\u636Ekey\u7684\u524D\u7F00\uFF0C\u5728\u4E0A\u9762\u7684\u914D\u7F6E\u4E3Atrue\u65F6\u6709\u6548\uFF0C
34+
35+
# Redis\u6570\u636E\u5E93\u7D22\u5F15\uFF08\u9ED8\u8BA4\u5E93\uFF09
36+
spring.redis.database=2
37+
# Redis\u670D\u52A1\u5668\u5730\u5740
38+
spring.redis.host=127.0.0.1
39+
# Redis\u670D\u52A1\u5668\u8FDE\u63A5\u7AEF\u53E3
40+
spring.redis.port=6379
41+
# Redis\u670D\u52A1\u5668\u8FDE\u63A5\u5BC6\u7801\uFF08\u9ED8\u8BA4\u4E3A\u7A7A\uFF09
42+
spring.redis.password=
43+
# \u8FDE\u63A5\u6C60\u6700\u5927\u8FDE\u63A5\u6570\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09
44+
spring.redis.lettuce.pool.max-active=100
45+
# \u8FDE\u63A5\u6C60\u6700\u5927\u963B\u585E\u7B49\u5F85\u65F6\u95F4\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09
46+
spring.redis.lettuce.pool.max-wait=1000
47+
# \u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5927\u7A7A\u95F2\u8FDE\u63A5
48+
spring.redis.lettuce.pool.max-idle=50
49+
# \u8FDE\u63A5\u6C60\u4E2D\u7684\u6700\u5C0F\u7A7A\u95F2\u8FDE\u63A5
50+
spring.redis.lettuce.pool.min-idle=0
51+
# \u8FDE\u63A5\u8D85\u65F6\u65F6\u95F4\uFF08\u6BEB\u79D2\uFF09
52+
spring.redis.lettuce.shutdown-timeout=0

0 commit comments

Comments
 (0)