spring-data-redis2.0以上配置redis連接


1.所需的maven依賴

     <dependency>
          <groupId>org.springframework.data</groupId>
          <artifactId>spring-data-redis</artifactId>
          <version>2.0.0.RELEASE</version>
      </dependency>
      <dependency>
          <groupId>redis.clients</groupId>
          <artifactId>jedis</artifactId>
          <version>2.9.0</version>
      </dependency>
   <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-xxx</artifactId>
    </dependency>

 

此處需要注意的是spring-data-redis2.0以上版本所需的jedis版本為2.9.0  目前最新的版本也為2.9.0  否則會報ClassNotFoundException或者NoSuchMethodError等異常

spring-xxx即為spring其他依賴的包,網上很多  找到粘貼吧

此外如果想查看工程用的包所依賴的包的版本可以去http://mvnrepository.com/ 網上輸入對應的包如spring-data-redis  

點擊后會羅列出maven倉庫里所包含的版本  點擊當前使用的版本如案例中的2.0.0  往下翻則會展示出所選包依賴的包

 

 這兒就會指明依賴jedis的包且最低版本為2.9.0

 

2.配置redis.properties

redis.host = 172.25.12.123
redis.port = 6379
redis.auth = admin
redis.maxTotal = 100
redis.maxIdle = 20
redis.maxWaitMillis = 10000
redis.testOnBorrow = true
redis.testOnReturn = true
redis.dbIndex = 3

3.配置spring-redis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
">

    <!--引入配置文件-->
    <context:property-placeholder location="redis.properties" ignore-unresolvable="true"></context:property-placeholder>
    <!--設置jedisPool鏈接池的配置-->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="${redis.maxTotal}"/>
        <property name="maxIdle" value="${redis.maxIdle}"/>
        <property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>
        <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
        <property name="testOnReturn" value="${redis.testOnReturn}"/>
    </bean>
    <!--redis鏈接密碼-->
    <bean id="redisPassword" class="org.springframework.data.redis.connection.RedisPassword">
        <constructor-arg name="thePassword" value="${redis.auth}"></constructor-arg>
    </bean>

    <!--spring-data-redis2.0以上的配置-->
    <bean id="redisStandaloneConfiguration" class="org.springframework.data.redis.connection.RedisStandaloneConfiguration">
        <property name="hostName" value="${redis.host}"/>
        <property name="port" value="${redis.port}"/>
        <property name="password" ref="redisPassword" />
        <property name="database" value="${redis.dbIndex}"/>
    </bean>
    <!--配置jedis鏈接工廠 spring-data-redis2.0中
        建議改為構造器傳入一個RedisStandaloneConfiguration  單機
                            RedisSentinelConfiguration  主從復制
                            RedisClusterConfiguration  集群-->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <!--注銷掉的部分為spring-data-redis2.0以下的版本配置的方式-->
      <!--  <property name="hostName" value="${redis.host}"/>
        <property name="port" value="${redis.port}"/>
        <property name="poolConfig" ref="jedisPoolConfig"/>
        <property name="password" value="${redis.auth}" />
        <property name="database" value="${redis.dbIndex}"/>-->
        <!--spring-data-redis2.0以上建議獲取的方式-->
        <constructor-arg name="standaloneConfig" ref="redisStandaloneConfiguration"></constructor-arg>
    </bean>

    <!--手動設置 key  與 value的序列化方式-->
    <bean id="keySerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
    <bean id="valueSerializer" class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer"/>

    <!--配置jedis模板  -->
    <bean id = "redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory" />
        <property name="keySerializer" ref="keySerializer" />
        <property name="valueSerializer" ref="valueSerializer" />
        <property name="hashKeySerializer" ref="keySerializer" />
        <property name="hashValueSerializer" ref="valueSerializer" />
    </bean>

    <!--也可以StringRedisTemplate  專注於String的操作  -->
    <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <!--<property name="connectionFactory" ref="jedisConnectionFactory"></property>-->
        <!--在StringRedisTemplate與redisTemplate不同,可以直接造構造器中傳入ConnectionFactory-->
        <constructor-arg name="connectionFactory" ref="jedisConnectionFactory"></constructor-arg>
        <property name="keySerializer" ref="keySerializer" />
        <property name="valueSerializer" ref="valueSerializer" />
        <property name="hashKeySerializer" ref="keySerializer" />
        <property name="hashValueSerializer" ref="valueSerializer" />

    </bean>
</beans>

上面需要注意一點的是

在spring-data-redis2.0以上的版本中在配置密碼的時候不能像老版本直接設置密碼值,需要注入一個RedisPassword的bean  在RedisPassword的構造方法中設置密碼

 

3.最后進行測試測試

package com.springactul.splx.nosql.redis;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Created by zhoum on 2018-06-22.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:spring-*.xml"})
public class RedisTest {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Test
    public void test1(){
        ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set("first","hellow word");
        System.out.println(valueOperations.get("first"));
    }

    @Test
    public void test2(){
        ValueOperations<String, String> stringStringValueOperations = stringRedisTemplate.opsForValue();
        stringStringValueOperations.set("secound","hello everyone");
        System.out.println(stringStringValueOperations.get("secound"));
    }
}

 

注意:在本篇博客中只講了RedisStandalongConfiguration的配置方式  ,並且測試本着以能連通redis的目的進行非常簡單的測試,如果想要了解更詳細的使用或者配置

可以參考  https://gitee.com/wjtree/codes/itcdnko9f5zqwr8u4gejs95 這篇文章


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM