SpringBoot中怎么整合LettuceRedis

SpringBoot中怎么整合Lettuceredis,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。

站在用戶的角度思考問題,與客戶深入溝通,找到內(nèi)鄉(xiāng)網(wǎng)站設(shè)計(jì)與內(nèi)鄉(xiāng)網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站設(shè)計(jì)、成都網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、空間域名、網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋內(nèi)鄉(xiāng)地區(qū)。

Redis介紹

Redis是一個(gè)開源的使用ANSI C語言編寫、支持網(wǎng)絡(luò)、可基于內(nèi)存亦可持久化的日志型、Key-Value數(shù)據(jù)庫(kù),并提供多種語言的API。相比Memcached它支持存儲(chǔ)的類型相對(duì)更多(字符、哈希、集合、有序集合、列表、GEO),同時(shí)Redis是線程安全的。2010年3月15日起,Redis的開發(fā)工作由VMware主持,2013年5月開始,Redis的開發(fā)由Pivotal贊助。

Lettuce

Lettuce 和 Jedis 的都是連接Redis Server的客戶端程序。Jedis在實(shí)現(xiàn)上是直連redis server,多線程環(huán)境下非線程安全,除非使用連接池,為每個(gè)Jedis實(shí)例增加物理連接。Lettuce基于Netty的連接實(shí)例(StatefulRedisConnection),可以在多個(gè)線程間并發(fā)訪問,且線程安全,滿足多線程環(huán)境下的并發(fā)訪問,同時(shí)它是可伸縮的設(shè)計(jì),一個(gè)連接實(shí)例不夠的情況也可以按需增加連接實(shí)例。

導(dǎo)入依賴

在 pom.xml 中spring-boot-starter-data-redis的依賴,Spring Boot2.x 后底層不在是Jedis如果做版本升級(jí)的朋友需要注意下

<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.apache.commons</groupId>  <artifactId>commons-pool2</artifactId></dependency><dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-test</artifactId>  <scope>test</scope></dependency>

屬性配置

在 application.properties 文件中配置如下內(nèi)容,由于Spring Boot2.x 的改動(dòng),連接池相關(guān)配置需要通過spring.redis.lettuce.pool或者 spring.redis.jedis.pool 進(jìn)行配置了

spring.redis.host=localhostspring.redis.password=battcn# 連接超時(shí)時(shí)間(毫秒)spring.redis.timeout=10000# Redis默認(rèn)情況下有16個(gè)分片,這里配置具體使用的分片,默認(rèn)是0spring.redis.database=0# 連接池最大連接數(shù)(使用負(fù)值表示沒有限制) 默認(rèn) 8spring.redis.lettuce.pool.max-active=8# 連接池最大阻塞等待時(shí)間(使用負(fù)值表示沒有限制) 默認(rèn) -1spring.redis.lettuce.pool.max-wait=-1# 連接池中的最大空閑連接 默認(rèn) 8spring.redis.lettuce.pool.max-idle=8# 連接池中的最小空閑連接 默認(rèn) 0spring.redis.lettuce.pool.min-idle=0

具體編碼

Spring Boot對(duì)Redis的支持已經(jīng)非常完善了,良好的序列化以及豐富的API足夠應(yīng)對(duì)日常開發(fā)

實(shí)體類

創(chuàng)建一個(gè)User類

package com.battcn.entity;import java.io.Serializable;/** * @author Levin * @since 2018/5/10 0007 */public class User implements Serializable {  private static final long serialVersionUID = 8655851615465363473L;  private Long id;  private String username;  private String password;  // TODO 省略get set}

自定義Template

默認(rèn)情況下的模板只能支持RedisTemplate<String, String>,也就是只能存入字符串,這在開發(fā)中是不友好的,所以自定義模板是很有必要的,當(dāng)自定義了模板又想使用String存儲(chǔ)這時(shí)候就可以使用StringRedisTemplate的方式,它們并不沖突…

package com.battcn.config;import org.springframework.boot.autoconfigure.AutoConfigureAfter;import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;import java.io.Serializable;/** * TODO 修改database * * @author Levin * @since 2018/5/10 0022 */@Configuration@AutoConfigureAfter(RedisAutoConfiguration.class)public class RedisCacheAutoConfiguration {  @Bean  public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {    RedisTemplate<String, Serializable> template = new RedisTemplate<>();    template.setKeySerializer(new StringRedisSerializer());    template.setValueSerializer(new GenericJackson2JsonRedisSerializer());    template.setConnectionFactory(redisConnectionFactory);    return template;  }}

測(cè)試

完成準(zhǔn)備事項(xiàng)后,編寫一個(gè)junit測(cè)試類來檢驗(yàn)代碼的正確性,有很多人質(zhì)疑過Redis線程安全性,故下面也提供了響應(yīng)的測(cè)試案例,如有疑問歡迎指正

package com.battcn;import com.battcn.entity.User;import org.junit.Test;import org.junit.runner.RunWith;import org.slf4j.Logger;import org.slf4j.LoggerFactory;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.StringRedisTemplate;import org.springframework.test.context.junit4.SpringRunner;import java.io.Serializable;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.stream.IntStream;/** * @author Levin * @since 2018/5/10 0010 */@RunWith(SpringRunner.class)@SpringBootTestpublic class Chapter8ApplicationTest {  private static final Logger log = LoggerFactory.getLogger(Chapter8ApplicationTest.class);  @Autowired  private StringRedisTemplate stringRedisTemplate;  @Autowired  private RedisTemplate<String, Serializable> redisCacheTemplate;  @Test  public void get() {    // TODO 測(cè)試線程安全    ExecutorService executorService = Executors.newFixedThreadPool(1000);    IntStream.range(0, 1000).forEach(i ->        executorService.execute(() -> stringRedisTemplate.opsForValue().increment("kk", 1))    );    stringRedisTemplate.opsForValue().set("k1", "v1");    final String k1 = stringRedisTemplate.opsForValue().get("k1");    log.info("[字符緩存結(jié)果] - [{}]", k1);    // TODO 以下只演示整合,具體Redis命令可以參考官方文檔,Spring Data Redis 只是改了個(gè)名字而已,Redis支持的命令它都支持    String key = "battcn:user:1";    redisCacheTemplate.opsForValue().set(key, new User(1L, "u1", "pa"));    // TODO 對(duì)應(yīng) String(字符串)    final User user = (User) redisCacheTemplate.opsForValue().get(key);    log.info("[對(duì)象緩存結(jié)果] - [{}]", user);  }}

其它類型

下列的就是Redis其它類型所對(duì)應(yīng)的操作方式

opsForValue: 對(duì)應(yīng) String(字符串)  opsForZSet: 對(duì)應(yīng) ZSet(有序集合)  opsForHash: 對(duì)應(yīng) Hash(哈希)  opsForList: 對(duì)應(yīng) List(列表)  opsForSet: 對(duì)應(yīng) Set(集合)  opsForGeo: 對(duì)應(yīng) GEO(地理位置)

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。

文章名稱:SpringBoot中怎么整合LettuceRedis
標(biāo)題鏈接:http://muchs.cn/article6/jpgpig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、關(guān)鍵詞優(yōu)化、做網(wǎng)站、服務(wù)器托管、靜態(tài)網(wǎng)站、App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化