-
Notifications
You must be signed in to change notification settings - Fork 415
/
redis.html
324 lines (250 loc) · 13.9 KB
/
redis.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>2.17. Spring boot with Redis</title><link rel="stylesheet" type="text/css" href="..//docbook.css" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><meta name="keywords" content="Spring, Spring Boot, Spring Cloud, Spring Eureka, Spring Config" /><link rel="home" href="../index.html" title="Netkiller Spring Cloud 手札" /><link rel="up" href="index.html" title="第 2 章 Spring Boot" /><link rel="prev" href="datasource.html" title="2.16. 数据源配置" /><link rel="next" href="cache.html" title="2.18. Spring boot with Caching" /></head><body><a xmlns="" href="//www.netkiller.cn/">Home</a> |
<a xmlns="" href="//netkiller.github.io/">简体中文</a> |
<a xmlns="" href="http://netkiller.sourceforge.net/">繁体中文</a> |
<a xmlns="" href="/journal/index.html">杂文</a> |
<a xmlns="" href="//www.netkiller.cn/home/donations.html">打赏(Donations)</a> |
<a xmlns="" href="https://yq.aliyun.com/u/netkiller/">云栖社区</a> |
<a xmlns="" href="http://my.oschina.net/neochen/">OSChina 博客</a> |
<a xmlns="" href="https://www.facebook.com/bg7nyt">Facebook</a> |
<a xmlns="" href="http://cn.linkedin.com/in/netkiller/">Linkedin</a> |
<a xmlns="" href="https://zhuanlan.zhihu.com/netkiller">知乎专栏</a> |
<a xmlns="" href="https://github.com/netkiller">Github</a> |
<a xmlns="" href="/search.html">Search</a> |
<a xmlns="" href="//www.netkiller.cn/home/about.html">About</a><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">2.17. Spring boot with Redis</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="datasource.html">上一页</a> </td><th width="60%" align="center">第 2 章 Spring Boot</th><td width="20%" align="right"> <a accesskey="n" href="cache.html">下一页</a></td></tr></table><hr /></div><table xmlns=""><tr><td><iframe src="//ghbtns.com/github-btn.html?user=netkiller&repo=netkiller.github.io&type=watch&count=true&size=large" height="30" width="170" frameborder="0" scrolling="0" style="width:170px; height: 30px;" allowTransparency="true"></iframe></td><td><iframe src="//ghbtns.com/github-btn.html?user=netkiller&repo=netkiller.github.io&type=fork&count=true&size=large" height="30" width="170" frameborder="0" scrolling="0" style="width:170px; height: 30px;" allowTransparency="true"></iframe></td><td><iframe src="//ghbtns.com/github-btn.html?user=netkiller&type=follow&count=true&size=large" height="30" width="240" frameborder="0" scrolling="0" style="width:240px; height: 30px;" allowTransparency="true"></iframe></td></tr></table><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="redis"></a>2.17. Spring boot with Redis</h3></div></div></div>
<div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="redis"></a>2.17.1. Spring boot with Redis</h4></div></div></div>
<div class="section"><div class="titlepage"><div><div><h5 class="title"><a id="idp58"></a>2.17.1.1. maven</h5></div></div></div>
<pre class="programlisting">
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</pre>
</div>
<div class="section"><div class="titlepage"><div><div><h5 class="title"><a id="idp59"></a>2.17.1.2. application.properties</h5></div></div></div>
<pre class="programlisting">
spring.redis.database=10
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.timeout=0
</pre>
</div>
<div class="section"><div class="titlepage"><div><div><h5 class="title"><a id="idp60"></a>2.17.1.3. JUnit</h5></div></div></div>
<pre class="programlisting">
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
public class ApplicationTests {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
public void test() throws Exception {
// 保存字符串
stringRedisTemplate.opsForValue().set("neo", "chen");
Assert.assertEquals("chen", stringRedisTemplate.opsForValue().get("neo"));
}
}
</pre>
</div>
<div class="section"><div class="titlepage"><div><div><h5 class="title"><a id="idp61"></a>2.17.1.4. Controller</h5></div></div></div>
<p>stringRedisTemplate模板用于存储key,value为字符串的数据</p>
<pre class="programlisting">
@Autowired
private StringRedisTemplate stringRedisTemplate;
@RequestMapping("/test")
@ResponseBody
public String test() {
String message = "";
stringRedisTemplate.opsForValue().set("hello", "world");
message = stringRedisTemplate.opsForValue().get("hello");
return message;
}
</pre>
<p>等同于</p>
<pre class="programlisting">
@Autowired
private RedisTemplate<String, String> redisTemplate;
</pre>
<p>ListOperations</p>
<pre class="programlisting">
public class Example {
// inject the actual template
@Autowired
private RedisTemplate<String, String> template;
// inject the template as ListOperations
// can also inject as Value, Set, ZSet, and HashOperations
@Resource(name="redisTemplate")
private ListOperations<String, String> listOps;
public void addLink(String userId, URL url) {
listOps.leftPush(userId, url.toExternalForm());
// or use template directly
redisTemplate.boundListOps(userId).leftPush(url.toExternalForm());
}
}
</pre>
<div class="example"><a id="idp334"></a><p class="title"><strong>例 2.2. RedisTemplate</strong></p><div class="example-contents">
<pre class="programlisting">
@Autowired
private RedisTemplate<String, String> redisTemplate;
public List<Protocol> getProtocol() {
List<Protocol> protocols = new ArrayList<Protocol>();
Gson gson = new Gson();
Type type = new TypeToken<List<Protocol>>(){}.getType();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
String cacheKey = String.format("%s:%s", this.getClass().getName(), Thread.currentThread().getStackTrace()[1].getMethodName());
long expireTime = 5;
if(redisTemplate.hasKey(cacheKey)){
String cacheValue = redisTemplate.opsForValue().get(cacheKey);
System.out.println(cacheValue);
protocols = gson.fromJson(cacheValue, type);
}else{
Protocol protocol = new Protocol();
protocol.setRequest(new Date().toString());
protocols.add(protocol);
String jsonString = gson.toJson(protocols, type);
System.out.println( jsonString );
redisTemplate.opsForValue().set(cacheKey, jsonString);
redisTemplate.expire(cacheKey, expireTime, TimeUnit.SECONDS);
}
return protocols;
}
</pre>
</div></div><br class="example-break" />
</div>
</div>
<div class="section"><div class="titlepage"><div><div><h4 class="title"><a id="redis.pubsub"></a>2.17.2. Redis Pub/Sub</h4></div></div></div>
<div class="section"><div class="titlepage"><div><div><h5 class="title"><a id="idp62"></a>2.17.2.1. Redis配置类</h5></div></div></div>
<pre class="programlisting">
package cn.netkiller.wallet.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import cn.netkiller.wallet.redis.RedisMessageSubscriber;
@Configuration
public class RedisConfig {
public RedisConfig() {
}
@Bean
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory connectionFactory) {
StringRedisTemplate redisTemplate = new StringRedisTemplate();
redisTemplate.setConnectionFactory(connectionFactory);
return redisTemplate;
}
@Bean
public MessageListenerAdapter messageListener() {
return new MessageListenerAdapter(new RedisMessageSubscriber());
}
@Bean
public ChannelTopic topic() {
return new ChannelTopic("demo");
}
@Bean
public RedisMessageListenerContainer redisContainer(RedisConnectionFactory connectionFactory, MessageListenerAdapter messageListener) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.addMessageListener(messageListener(), topic());
container.addMessageListener(messageListener(), new ChannelTopic("test"));
return container;
}
}
</pre>
</div>
<div class="section"><div class="titlepage"><div><div><h5 class="title"><a id="idp63"></a>2.17.2.2. 订阅和发布类</h5></div></div></div>
<pre class="programlisting">
package cn.netkiller.wallet.redis;
import java.nio.charset.StandardCharsets;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
public class RedisMessageSubscriber implements MessageListener {
public void onMessage(final Message message, final byte[] pattern) {
System.out.println("Topic : " + new String(message.getChannel(), StandardCharsets.UTF_8));
System.out.println("Message : " + message.toString());
}
}
</pre>
<pre class="programlisting">
package cn.netkiller.wallet.redis;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
public class RedisMessagePublisher {
private final StringRedisTemplate redisTemplate;
private final ChannelTopic topic;
public RedisMessagePublisher(StringRedisTemplate redisTemplate, ChannelTopic topic) {
this.redisTemplate = redisTemplate;
this.topic = topic;
}
public void publish(String message) {
redisTemplate.convertAndSend(topic.getTopic(), message);
}
}
</pre>
</div>
<div class="section"><div class="titlepage"><div><div><h5 class="title"><a id="idp64"></a>2.17.2.3. 消息发布演示</h5></div></div></div>
<pre class="programlisting">
@Autowired
private StringRedisTemplate stringRedisTemplate;
@GetMapping("/pub/demo")
public String pub() {
RedisMessagePublisher publisher = new RedisMessagePublisher(stringRedisTemplate, new ChannelTopic("demo"));
String message = "Message " + UUID.randomUUID();
publisher.publish(message);
return message;
}
@GetMapping("/pub/test")
public String pub(@RequestParam String message) {
RedisMessagePublisher publisher = new RedisMessagePublisher(stringRedisTemplate, new ChannelTopic("test"));
publisher.publish(message);
return message;
}
</pre>
</div>
</div>
</div><div xmlns="" id="disqus_thread"></div><script xmlns="">
var disqus_config = function () {
this.page.url = "http://www.netkiller.cn"; // Replace PAGE_URL with your page's canonical URL variable
this.page.identifier = 'netkiller'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
};
(function() { // DON'T EDIT BELOW THIS LINE
var d = document, s = d.createElement('script');
s.src = '//netkiller.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
</script><noscript xmlns="">Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript><br xmlns="" /><script xmlns="" type="text/javascript" id="clustrmaps" src="//cdn.clustrmaps.com/map_v2.js?u=r5HG&d=9mi5r_kkDC8uxG8HuY3p4-2qgeeVypAK9vMD-2P6BYM"></script><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="datasource.html">上一页</a> </td><td width="20%" align="center"><a accesskey="u" href="index.html">上一级</a></td><td width="40%" align="right"> <a accesskey="n" href="cache.html">下一页</a></td></tr><tr><td width="40%" align="left" valign="top">2.16. 数据源配置 </td><td width="20%" align="center"><a accesskey="h" href="../index.html">起始页</a></td><td width="40%" align="right" valign="top"> 2.18. Spring boot with Caching</td></tr></table></div><script xmlns="">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-11694057-1', 'auto');
ga('send', 'pageview');
</script><script xmlns="" async="async">
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?93967759a51cda79e49bf4e34d0b0f2c";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script><script xmlns="" async="async">
(function(){
var bp = document.createElement('script');
var curProtocol = window.location.protocol.split(':')[0];
if (curProtocol === 'https') {
bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
}
else {
bp.src = 'http://push.zhanzhang.baidu.com/push.js';
}
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(bp, s);
})();
</script><script xmlns="" type="text/javascript" src="/js/q.js" async="async"></script></body></html>