java、spring、springboot中整合Redis的詳細(xì)講解
java整合Redis
1、引入依賴或者導(dǎo)入jar包
<dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
2、代碼實現(xiàn)
public class JedisTest {
public static void main(String[] args) {
//連接redis
//Jedis jedis=new Jedis("192.168.65.128",6379);
//使用Jedis連接池
Jedis jedis=getJedis();
//操作redis
jedis.set("name","小白");
jedis.set("age","19");
System.out.println("操作成功!");
jedis.close();
}
public static Jedis getJedis(){
//創(chuàng)建連接池配置對象
JedisPoolConfig config=new JedisPoolConfig();
config.setMaxIdle(10);
config.setMinIdle(5);
config.setMaxTotal(100);
//需要redis的服務(wù)密碼時,使用第一種創(chuàng)建方式
//JedisPool jedisPool=new JedisPool(config,"192.168.65.128",6379,10000,"root");
JedisPool jedisPool=new JedisPool(config,"192.168.65.128",6379,10000);
return jedisPool.getResource();
}
}
Spring整合Redis
1、添加依賴
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>2.1.8.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
2、redis配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--1、配置redis連接池對象--> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!--最大空閑數(shù)--> <property name="maxIdle" value="50"/> <!--最大連接數(shù)--> <property name="maxTotal" value="100"/> <!--最大等待時間--> <property name="maxWaitMillis" value="60000"/> </bean> <!--2、配置redis連接工廠--> <bean id="factory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <!--連接池的配置--> <property name="poolConfig" ref="poolConfig"/> <!--連接主機--> <property name="hostName" value="192.168.65.128"/> <!--端口--> <property name="port" value="6379"/> <!--密碼--> <!-- 當(dāng)出現(xiàn)以下錯誤時,說明并不需要設(shè)置密碼 Caused by: redis.clients.jedis.exceptions.JedisDataException: ERR Client sent AUTH, but no password is set --> <!--<property name="password" value="root"/>--> </bean> <!--3、配置redis模板對象--> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <!--配置連接工廠--> <property name="connectionFactory" ref="factory"/> </bean> </beans>
3、注入模板對象
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:application-redis.xml")
public class RedisTest {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void test(){
//redisTemplate.opsForValue().set("name","小黑");
Object name = redisTemplate.opsForValue().get("name");
System.out.println(name);
System.out.println("操作完成");
}
}
注意:使用Spring(SpringBoot)整合redis后,RedisTemplate對象會自帶key和value的序列化功能。在redis的客戶端操作時,獲取的key是被序列化后的key.
思考:為什么Spring要提供一個序列化的功能? 為了開發(fā)者方便將對象存入redis中??稍趚ml中配置自定義的序列化類型。
<!--3、配置redis模板對象--> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <!--配置連接工廠--> <property name="connectionFactory" ref="factory"/> <!--配置String類型的key value序列化方式 當(dāng)存儲的key是String類型時,則vlaue也是String類型,且key和value不被序列化--> <property name="keySerializer" ref="stringRedisSerializer"/> <property name="valueSerializer" ref="stringRedisSerializer"/> </bean> <!--自定義序列化類型--> <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/> <!--默認(rèn)的jdk序列化--> <bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
springboot整合Redis
1、添加依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
2、配置application.yml

3、注入模板對象
@RunWith(SpringRunner.class)
@SpringBootTest
class SpringbootRedisApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@PostConstruct
public void init(){
//配置String類型的key value序列化方式
redisTemplate.setStringSerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
}
@Test
void contextLoads() {
redisTemplate.opsForValue().set("age",12);
Object age = redisTemplate.opsForValue().get("age");
System.out.println(age);
System.out.println("操作成功");
}
//獲取幾種數(shù)據(jù)結(jié)構(gòu)的對象
@Test
public void getRedisType(){
//1、操作字符串?dāng)?shù)據(jù)類型
redisTemplate.opsForValue();
//2、操作hash的數(shù)據(jù)類型
redisTemplate.opsForHash();
//3、操作List的數(shù)據(jù)類型
redisTemplate.opsForList();
//4、操作Set的數(shù)據(jù)類型
redisTemplate.opsForSet();
//5、操作hSet的數(shù)據(jù)類型
redisTemplate.opsForZSet();
//6、操作基數(shù)的數(shù)據(jù)類型
redisTemplate.opsForHyperLogLog();
}
}
注意:不能在yml配置文件中配置自定義序列化,可以在springboot啟動類或者測試類中,通過@PostConstruct注解來觸發(fā)執(zhí)行方法,從而達(dá)到配置自定義序列化的效果。
補充:
1.@PostConstruct說明
被@PostConstruct修飾的方法會在服務(wù)器加載Servlet的時候運行,并且只會被服務(wù)器調(diào)用一次,類似于Serclet的inti()方法。被@PostConstruct修飾的方法會在構(gòu)造函數(shù)之后,init()方法之前運行。
2.@PreDestroy說明
被@PreDestroy修飾的方法會在服務(wù)器卸載Servlet的時候運行,并且只會被服務(wù)器調(diào)用一次,類似于Servlet的destroy()方法。被@PreDestroy修飾的方法會在destroy()方法之后運行,在Servlet被徹底卸載之前。
總結(jié)
1、當(dāng)項目報以下錯誤:Caused by: redis.clients.jedis.exceptions.JedisDataException: ERR Client sent AUTH, but no password is set
報錯的原因:是redis服務(wù)沒設(shè)置密碼,而項目配置文件中寫了有redis密碼
解決方案:
1)把項目配置文件中的密碼password設(shè)置為空或者不設(shè)置。
2)設(shè)置redis服務(wù)密碼
——可通過直接修改redis.conf配置文件中的requirepass屬性方式,如果修改不生效,可通過命令方式修改,進入redis的客戶端
redis 127.0.0.1:6379> CONFIG SET requirepass “root” OK redis 127.0.0.1:6379> AUTH root Ok
然后重啟項目就可以連接本機的redis服務(wù)了。
到此這篇關(guān)于java、spring、springboot中整合Redis的詳細(xì)講解的文章就介紹到這了,更多相關(guān)java springboot整合Redis內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解析java基本數(shù)據(jù)類型傳遞與引用傳遞區(qū)別
這篇文章主要介紹了java基本數(shù)據(jù)類型傳遞與引用傳遞區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Java輕松實現(xiàn)批量插入或刪除Excel行列操作
在職場生活中,對Excel工作表的行和列進行操作是非常普遍的需求,下面小編就來和大家介紹一下如何在Java中完成批量插入、刪除行和列的操作吧2023-10-10
Java?數(shù)據(jù)結(jié)構(gòu)深入理解ArrayList與順序表
ArrayList?類是一個可以動態(tài)修改的數(shù)組,與普通數(shù)組的區(qū)別就是它是沒有固定大小的限制,我們可以添加或刪除元素。ArrayList?繼承了?AbstractList?,并實現(xiàn)了?List?接口,順序表是將元素順序地存放在一塊連續(xù)的存儲區(qū)里,元素間的順序關(guān)系由它們的存儲順序自然表示2022-04-04
SpringCloud zuul 網(wǎng)關(guān)如何解決跨域問題
這篇文章主要介紹了SpringCloud zuul網(wǎng)關(guān)解決跨域問題的具體實現(xiàn)方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06

