依赖声明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.14.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.11.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
spring-boot-starter-data-redis:Redis 启动器。
jackson-databind:用于以 JSON 的形式序列化存储到 Redis 上的值。

Spring Boot 配置文件:

# src/main/resources/application.yml(单点模式)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
spring:
redis:
host: 10.10.10.127
port: 6379
database: 0
timeout: 5000
pool:
max-active: 8
max-idle: 8
min-idle: 4
max-wait: 30000
logging:
level:
root: warn

redis 配置项说明可参考:Redis参数配置

# src/main/resources/application.yml(哨兵模式)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
spring:
redis:
host: 10.10.10.127
port: 6379
database: 0
timeout: 5000
pool:
max-active: 8
max-idle: 8
min-idle: 4
max-wait: 30000
sentinel:
master: mymaster
nodes: 10.10.10.127:26379,10.10.10.128:26379,10.10.10.129:26379
logging:
level:
root: warn

哨兵模式比单点模式仅是多了sentinel部分的配置。

RedisTemplate 配置:

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
package org.fanlychie.config;
import com.fasterxml.jackson.databind.ObjectMapper;
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.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class ApplicationConfig {
@Bean
public RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<?, ?> template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
// 字符串序列化
RedisSerializer stringRedisSerializer = new StringRedisSerializer();
// 对象转为JSON串序列化
RedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer(new ObjectMapper());
template.setKeySerializer(stringRedisSerializer);
template.setValueSerializer(jsonRedisSerializer);
template.setHashKeySerializer(stringRedisSerializer);
template.setHashValueSerializer(jsonRedisSerializer);
return template;
}
}
@Configuration:效果等同于 Spring XML 配置方式。该注解用于 Java 编码形式配置。

启动类:

1
2
3
4
5
6
7
8
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

单元测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTest {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void doTest() {
if (redisTemplate.hasKey("infomsg")) {
System.out.println("=====> " + redisTemplate.opsForValue().get("infomsg"));
} else {
Map<String, Object> map = new HashMap<>();
map.put("name", "fanlychie");
map.put("mail", "fanlychie@yeah.net");
redisTemplate.opsForValue().set("infomsg", map);
}
}
}

示例项目开发环境:Java-8、Maven-3、IntelliJ IDEA-2018、Spring Boot-1.5.14.RELEASE
完整示例项目链接:spring-boot-redis-samplespring-boot-redis-sentinel-sample