说明

本文主要介绍使用spring-boot集成jedis的方式来操作redis。

至于jedis的单独使用就不必多说了。

此处的集成方式有两种:

  • 手动配置集成jedis
  • 使用spring-boot-starter-data-redis集成

1 手动配置集成jedis

1.1 jar依赖

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
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--此处并不是直接使用spring提供的redis-starter-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
</dependencies>

1.2 配置

1.2.1 外部配置文件

  • application.yaml
1
2
3
4
5
6
7
8
9
jedis:
host: 127.0.0.1
port: 6379
pool:
max-idle: 300
min-idle: 10
max-total: 600
max-wait: 1000
block-when-exhausted: true

1.2.2 java配置类(代替传统的xml配置)

  • RedisConfig.java
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
package cn.hylexus.app.config;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
public class RedisConfig {
@Bean("jedis.config")
public JedisPoolConfig jedisPoolConfig(//
@Value("${jedis.pool.min-idle}") int minIdle, //
@Value("${jedis.pool.max-idle}") int maxIdle, //
@Value("${jedis.pool.max-wait}") int maxWaitMillis, //
@Value("${jedis.pool.block-when-exhausted}") boolean blockWhenExhausted, //
@Value("${jedis.pool.max-total}") int maxTotal) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMinIdle(minIdle);
config.setMaxIdle(maxIdle);
config.setMaxWaitMillis(maxWaitMillis);
config.setMaxTotal(maxTotal);
// 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true
config.setBlockWhenExhausted(blockWhenExhausted);
// 是否启用pool的jmx管理功能, 默认true
config.setJmxEnabled(true);
return config;
}
@Bean
public JedisPool jedisPool(//
@Qualifier("jedis.config") JedisPoolConfig config, //
@Value("${jedis.host}") String host, //
@Value("${jedis.port}") int port) {
return new JedisPool(config, host, port);
}
}

1.3 使用示例

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
package cn.hylexus.app.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
@Service
public class RedisServiceImpl implements RedisService {
// 此处直接注入即可
@Autowired
private JedisPool jedisPool;
@Override
public String get(String key) {
Jedis jedis = this.jedisPool.getResource();
String ret;
try {
ret = jedis.get(key);
} finally {
if (jedis != null)
jedis.close();
}
return ret;
}
@Override
public boolean set(String key, String val) {
Jedis jedis = this.jedisPool.getResource();
try {
return "OK".equals(jedis.set(key, val));
} finally {
if (jedis != null)
jedis.close();
}
}
}

1.4 简单测试

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
package cn.hylexus.app.service;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import cn.hylexus.app.SpringBootRedisJedispoolApplicationTests;
public class RedisServiceImplTest extends SpringBootRedisJedispoolApplicationTests {
@Autowired
private RedisService redisService;
@Test
public void testGet() {
// test set
boolean status = this.redisService.set("foo", "bar");
Assert.assertTrue(status);
// test get
String str = this.redisService.get("foo");
Assert.assertEquals("bar", str);
}
}

1.5 源码地址

https://github.com/hylexus/blog-src/tree/master/spring-boot-redis-jedispool

2 使用spring-boot-starter-data-redis

2.1 jar依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<dependencies>
<!--此处使用spring提供的针对于redis的starter-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

2.2 配置

2.2.1 外部配置文件

  • application.yaml
1
2
3
4
5
6
7
8
9
10
11
spring:
redis:
host: 127.0.0.1
port: 6379
password: null
pool:
max-idle: 300
min-idle: 10
max-active: 600
max-wait: 1000
timeout: 0

2.2.2 java配置类

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
package cn.hylexus.app.config;
import org.springframework.beans.factory.annotation.Autowired;
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.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Autowired
private RedisConnectionFactory redisConnectionFactory;
/**
* 实例化 RedisTemplate 对象
*
*/
@Bean
public RedisTemplate<String, Object> functionDomainRedisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
this.initRedisTemplate(redisTemplate, redisConnectionFactory);
return redisTemplate;
}
/**
* 序列化设置
*/
private void initRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) {
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.setConnectionFactory(factory);
}
@Bean
public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForHash();
}
@Bean
public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForValue();
}
@Bean
public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForList();
}
@Bean
public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForSet();
}
@Bean
public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
return redisTemplate.opsForZSet();
}
}

2.3 简单测试

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
package cn.hylexus.app;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
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.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootRedisApplicationTests {
@Autowired
private ValueOperations<String, Object> valueOperations;
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Test
public void contextLoads() {
}
@Test
public void testStringOps() {
this.valueOperations.set("k1", "spring-redis");
Boolean hasKey = this.redisTemplate.hasKey("k1");
assertEquals(true, hasKey);
Object str = this.valueOperations.get("k1");
assertNotNull(str);
assertEquals("spring-redis", str.toString());
}
}

2.4 源码地址

https://github.com/hylexus/blog-src/tree/master/spring-boot-redis-boot-starter