Spring Boot中除了对经常使用的关系型数据库提供了优秀的自动化支持以外,对于不少NoSQL数据库同样提供了自动化配置的支持,包括:Redis, MongoDB, Elasticsearch, Solr和Cassandra。html
使用Redis
Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库。git
引入依赖
Spring Boot提供的数据访问框架Spring Data Redis基于Jedis。能够经过引入spring-boot-starter-redis
来配置依赖关系。redis
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-redis</artifactId> </dependency>
|
参数配置
按照惯例在application.properties
中加入Redis服务端的相关配置,具体说明以下:spring
# REDIS (RedisProperties) # Redis数据库索引(默认为0) spring.redis.database=0 # Redis服务器地址 spring.redis.host=localhost # Redis服务器链接端口 spring.redis.port=6379 # Redis服务器链接密码(默认为空) 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
|
其中spring.redis.database的配置一般使用0便可,Redis在配置的时候能够设置数据库数量,默认为16,能够理解为数据库的schema数据库
测试访问
经过编写测试用例,举例说明如何访问Redis。服务器
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(Application.class) public class ApplicationTests {
@Autowired private StringRedisTemplate stringRedisTemplate;
@Test public void test() throws Exception {
|
经过上面这段极为简单的测试案例演示了如何经过自动配置的StringRedisTemplate
对象进行Redis的读写操做,该对象从命名中就可注意到支持的是String类型。若是有使用过spring-data-redis的开发者必定熟悉RedisTemplate<K, V>
接口,StringRedisTemplate
就至关于RedisTemplate<String, String>
的实现。网络
除了String类型,实战中咱们还常常会在Redis中存储对象,这时候咱们就会想是否可使用相似RedisTemplate<String, User>
来初始化并进行操做。可是Spring Boot并不支持直接使用,须要咱们本身实现RedisSerializer<T>
接口来对传入对象进行序列化和反序列化,下面咱们经过一个实例来完成对象的读写操做。app
public class User implements Serializable {
private static final long serialVersionUID = -1L;
private String username; private Integer age;
public User(String username, Integer age) { this.username = username; this.age = age; }
|
public class RedisObjectSerializer implements RedisSerializer<Object> {
private Converter<Object, byte[]> serializer = new SerializingConverter(); private Converter<byte[], Object> deserializer = new DeserializingConverter();
static final byte[] EMPTY_ARRAY = new byte[0];
public Object deserialize(byte[] bytes) { if (isEmpty(bytes)) { return null; }
try { return deserializer.convert(bytes); } catch (Exception ex) { throw new SerializationException("Cannot deserialize", ex); } }
public byte[] serialize(Object object) { if (object == null) { return EMPTY_ARRAY; }
try { return serializer.convert(object); } catch (Exception ex) { return EMPTY_ARRAY; } }
private boolean isEmpty(byte[] data) { return (data == null || data.length == 0); } }
|
@Configuration public class RedisConfig {
@Bean JedisConnectionFactory jedisConnectionFactory() { return new JedisConnectionFactory(); }
@Bean public RedisTemplate<String, User> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, User> template = new RedisTemplate<String, User>(); template.setConnectionFactory(jedisConnectionFactory()); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new RedisObjectSerializer()); return template; }
}
|
@RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(Application.class) public class ApplicationTests {
@Autowired private RedisTemplate<String, User> redisTemplate;
@Test public void test() throws Exception {
|
固然spring-data-redis中提供的数据操做远不止这些,本文仅做为在Spring Boot中使用redis时的配置参考,更多对于redis的操做使用,请参考Spring-data-redis Reference。框架