相關知識
Redis 簡介
Redis 是一個開源的,基於內存中的,高性能的數據存儲系統,它可以用作數據庫、緩存和消息中間件。
Redis 支持多種類型的數據結構,如:string、hashes、lists、sets、sortedSets等。
Redis 內置了複製(replication)、LUA腳本(Lua scripting)、事務(transactions)、磁盤持久化(persistence)、LRU驅動事件(LRU eviction)等功能。
Redis 可以通過哨兵(Sentinel)以及集羣(Cluster)提供高可用性
Lettuce 和 Jedis
Lettuce 和 Jedis 都是連接 Redis Server 的客戶端程序,
SpringBoot2.x 之前默認使用 Jedis 作爲與 Redis 進行交互的組件,SpringBoot2.x 則換成了 Lettuce(生菜)。
Jedis 在實現上是直連 redis server,多線程環境下非線程安全,除非使用連接池,爲每個 Jedis 實例增加物理連接。
Lettuce 基於 Netty 的連接實例(StatefulRedisConnection),可以在多個線程間併發訪問,且線程安全,滿足多線程環境下的併發訪問,
同時它是可伸縮的設計,一個連接實例不夠的情況也可以按需增加連接實例。
目標
整合 Redis 實現對 redis 的增刪查改
準備工作
安裝 Redis
介紹使用 Docker 方式安裝,Docker 安裝可以參考 https://blog.csdn.net/gongm24/article/details/86357866
下載鏡像
docker pull redis
運行鏡像
docker run --name redis \
-p 6379:6379 \
-itd redis --requirepass "123456"
檢查是否安裝成功,使用客戶端登入容器,執行 redis-cli -a 123456
命令,進行連接
docker exec -it redis /bin/bash
操作步驟
添加依賴
引入 Spring Boot Starter 父工程
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
添加 spring-boot-starter-data-redis
的依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
添加後的整體依賴如下
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
配置
spring:
redis:
host: 127.0.0.1
password: 123456
# 連接超時時間(毫秒)
timeout: 10000
# Redis默認情況下有16個分片,這裏配置具體使用的分片,默認是0
database: 0
lettuce:
pool:
# 連接池最大連接數(使用負值表示沒有限制) 默認 8
max-active: 8
# 連接池最大阻塞等待時間(使用負值表示沒有限制) 默認 -1
max-wait: -1
# 連接池中的最大空閒連接 默認 8
max-idle: 8
# 連接池中的最小空閒連接 默認 0
min-idle: 0
編碼
@Slf4j
@RunWith(SpringRunner.class)
@WebAppConfiguration
@SpringBootTest(classes = Application.class)
public class RedisTest {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
public void testString() throws Exception {
stringRedisTemplate.opsForValue().set("welcome", "hello redis");
String welcome = stringRedisTemplate.opsForValue().get("welcome");
log.info("存儲字符串: {}", welcome);
}
}
編碼(使用自定義序列化)
定義對象
@NoArgsConstructor 一定要記得加,反序列化時會調用無參構造函數進行對象實例化。
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
String username;
String password;
}
註冊自定義 RedisTemplate
值的序列化選擇了 jackson
@Configuration
@AutoConfigureAfter(value = RedisAutoConfiguration.class)
public class CustomConfig {
@Bean(name = "jsonRedisTemplate")
public RedisTemplate<String, Object> jsonRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setConnectionFactory(redisConnectionFactory);
return template;
}
}
測試
@Slf4j
@RunWith(SpringRunner.class)
@WebAppConfiguration
@SpringBootTest(classes = Application.class)
public class RedisTest {
@Autowired
@Qualifier("jsonRedisTemplate")
private RedisTemplate<String, Object> jsonRedisTemplate;
@Test
public void testCustomerSerializer() throws Exception {
jsonRedisTemplate.opsForValue().set("user:1", new User("lili", "123456"));
User user = (User) jsonRedisTemplate.opsForValue().get("user:1");
log.info("存儲對象: {}", user);
}
}
源碼地址
本章源碼 : https://gitee.com/gongm_24/spring-boot-tutorial.git
結束語
下列的就是Redis其它類型所對應的操作方式
- opsForValue: 對應 String(字符串)
- opsForZSet: 對應 ZSet(有序集合)
- opsForHash: 對應 Hash(哈希)
- opsForList: 對應 List(列表)
- opsForSet: 對應 Set(集合)
- opsForGeo: 對應 GEO(地理位置)
參考
擴展
Redis 相關資料
spring-data-redis文檔: https://docs.spring.io/spring-data/redis/docs/2.0.1.RELEASE/reference/html/#new-in-2.0.0
Redis 文檔: https://redis.io/documentation
Redis 中文文檔: http://www.redis.cn/commands.html