Spring Boot入门十:集成Sentinel Redis

前言

上一篇文章介绍了spring boot集成单点的redis,然而实际生产环境使用单点的redis风险很高,一旦宕机整个服务将无法使用,这篇文章介绍如何使用基于sentinel的redis高可用方案。

哨兵sentinel的地址如下:

192.168.12.194:26379
192.168.12.194:36379
192.168.12.194:46379

Redis的地址如下:

192.168.12.194:6379
192.168.12.194:6380
192.168.12.194:6381

实现:

properties配置文件中添加配置信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
spring.redis.database=0
spring.redis.password=123456

# pool settings ...池配置
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1

#哨兵监听redis server名称
spring.redis.sentinel.master=mymaster
#哨兵的配置列表
spring.redis.sentinel.nodes=192.168.12.194:26379,192.168.12.194:36379,192.168.12.194:46379

创建RedisComponent类

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
package com.woniu.RedisComponent;

import java.io.UnsupportedEncodingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.woniu.bean.User;


@Component
public class RedisComponent {

@Autowired
//操作字符串的template,StringRedisTemplate是RedisTemplate的一个子集
private StringRedisTemplate stringRedisTemplate;

@Autowired
// RedisTemplate,可以进行所有的操作
private RedisTemplate<Object,Object> redisTemplate;

public void set(String key, String value){
ValueOperations<String, String> ops = this.stringRedisTemplate.opsForValue();
boolean bExistent = this.stringRedisTemplate.hasKey(key);
if (bExistent) {
System.out.println("this key is bExistent!");
}else{
ops.set(key, value);
}
}

public String get(String key){
return this.stringRedisTemplate.opsForValue().get(key);
}

public void del(String key){
this.stringRedisTemplate.delete(key);
}

public void sentinelSet(User user){
String key = null;
try {
key = new String(user.getId().getBytes("gbk"),"utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.out.println(key);
redisTemplate.opsForValue().set(key, user.toString());
}

public String sentinelGet(String key){
return stringRedisTemplate.opsForValue().get(key);
}
}

添加测试类的测试代码

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
package com.woniu;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.woniu.RedisComponent.RedisComponent;
import com.woniu.bean.User;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootSentinelredisApplicationTests {

@Autowired
private RedisComponent redisComponet;

@Test
public void sentinelSet(){
User user = new User();
user.setId("001");
user.setAge("30");
user.setName("wangpengfei");

redisComponet.sentinelSet(user);
}

@Test
public void sentinelGet(){
String str = redisComponet.sentinelGet("001");
System.out.println(str);
}
}

工程springboot_sentinelredis源码下载地址:点击打开链接

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×