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。框架