redis和spring集成

Posted never-say-no

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis和spring集成相关的知识,希望对你有一定的参考价值。

redis和spring框架的整合

我这里创建的是maven工程,通过maven锁定版本号,管理jar包之间的依赖

1.在pom文件中,引入spring和redis的jar包的坐标:

<properties>
        <junit.version>4.12</junit.version>
        <spring.version>4.2.4.RELEASE</spring.version>
        <jedis.version>2.7.2</jedis.version>
        
    </properties>


<!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <!-- Redis客户端 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>

2.将redis的基本数据类型操作封装成工具类

定义接口JedisClient:

package nyist.e3.utils.redis;

public interface JedisClient {

    String set(String key, String value);
    String get(String key);
    Boolean exists(String key);
    Long expire(String key, int seconds);
    Long ttl(String key);
    Long incr(String key);
    Long hset(String key, String field, String value);
    String hget(String key, String field);
    Long hdel(String key, String... field);
}

单机版实现该接口的操作:

package nyist.e3.utils.redis;

import org.springframework.beans.factory.annotation.Autowired;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class JedisClientPool implements JedisClient {
    
    private JedisPool jedisPool;
    
    public JedisPool getJedisPool() {
        return jedisPool;
    }

    public void setJedisPool(JedisPool jedisPool) {
        this.jedisPool = jedisPool;
    }

    @Override
    public String set(String key, String value) {
        Jedis jedis = jedisPool.getResource();
        String result = jedis.set(key, value);
        jedis.close();
        return result;
    }

    @Override
    public String get(String key) {
        Jedis jedis = jedisPool.getResource();
        String result = jedis.get(key);
        jedis.close();
        return result;
    }

    @Override
    public Boolean exists(String key) {
        Jedis jedis = jedisPool.getResource();
        Boolean result = jedis.exists(key);
        jedis.close();
        return result;
    }

    @Override
    public Long expire(String key, int seconds) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.expire(key, seconds);
        jedis.close();
        return result;
    }

    @Override
    public Long ttl(String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.ttl(key);
        jedis.close();
        return result;
    }

    @Override
    public Long incr(String key) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.incr(key);
        jedis.close();
        return result;
    }

    @Override
    public Long hset(String key, String field, String value) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.hset(key, field, value);
        jedis.close();
        return result;
    }

    @Override
    public String hget(String key, String field) {
        Jedis jedis = jedisPool.getResource();
        String result = jedis.hget(key, field);
        jedis.close();
        return result;
    }

    @Override
    public Long hdel(String key, String... field) {
        Jedis jedis = jedisPool.getResource();
        Long result = jedis.hdel(key, field);
        jedis.close();
        return result;
    }

}

集群实现该接口:

package nyist.e3.utils.redis;



import redis.clients.jedis.JedisCluster;

public class JedisClientCluster implements JedisClient {
    
    private JedisCluster jedisCluster;
    
    public JedisCluster getJedisCluster() {
        return jedisCluster;
    }

    public void setJedisCluster(JedisCluster jedisCluster) {
        this.jedisCluster = jedisCluster;
    }

    @Override
    public String set(String key, String value) {
        return jedisCluster.set(key, value);
    }

    @Override
    public String get(String key) {
        return jedisCluster.get(key);
    }

    @Override
    public Boolean exists(String key) {
        return jedisCluster.exists(key);
    }

    @Override
    public Long expire(String key, int seconds) {
        return jedisCluster.expire(key, seconds);
    }

    @Override
    public Long ttl(String key) {
        return jedisCluster.ttl(key);
    }

    @Override
    public Long incr(String key) {
        return jedisCluster.incr(key);
    }

    @Override
    public Long hset(String key, String field, String value) {
        return jedisCluster.hset(key, field, value);
    }

    @Override
    public String hget(String key, String field) {
        return jedisCluster.hget(key, field);
    }

    @Override
    public Long hdel(String key, String... field) {
        return jedisCluster.hdel(key, field);
    }

}

3.在spring中配置单机版的连接池

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" 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-4.2.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">

    <!-- 将redis对象的创建交给spring容器完成 -->
    <!-- 注册单机版的redis,创建jedisPool,需要通过构造注入host和port -->
    <!-- 连接redis单机版 -->
    <!--注册单机版的实现类-->
    <bean id="jedisClientPool" class="nyist.e3.utils.redis.JedisClientPool">
        <property name="jedisPool" ref="jedisPool"></property>
    </bean>
    <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
        <constructor-arg name="host" value="ip" />
        <constructor-arg name="port" value="6379" />
    </bean>
</beans>

4.redis集群在spring中的配置

    <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
        <!-- 通过构造注入节点列表,然后在列表中创建HostAndPort对象,通过构造注入host和port -->
        <constructor-arg>
            <set>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="ip"></constructor-arg>
                    <constructor-arg name="port" value="xxxx1"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="ip"></constructor-arg>
                    <constructor-arg name="port" value="xxxx2"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="ip"></constructor-arg>
                    <constructor-arg name="port" value="xxxx3"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="ip"></constructor-arg>
                    <constructor-arg name="port" value="xxxx4"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="ip"></constructor-arg>
                    <constructor-arg name="port" value="xxxx5"></constructor-arg>
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg name="host" value="ip"></constructor-arg>
                    <constructor-arg name="port" value="xxxx6"></constructor-arg>
                </bean>
            </set>
        </constructor-arg>
    </bean>
    <!-- 配置redis集群的实现类 -->
    <bean id="jedisClientCluster" class="nyist.e3.utils.redis.JedisClientCluster">
        <property name="jedisCluster" ref="jedisCluster"></property>
    </bean>

需要注意的是:单机版的配置和集群版的配置不能同时存在,如果使用集群版,将单机版的注释,使用单机版,注释集群版的配置。总之,只用配置一个。

6.话不多说,直接上测试的代码:

技术分享图片
@Test
    public void testPool() {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-redis.xml");
        JedisClientPool jedisClientPool = (JedisClientPool) context.getBean("jedisClientPool");
        jedisClientPool.set("school", "nyist");
        String value = jedisClientPool.get("school");
        System.out.println(value);
    }
View Code

 

以上是关于redis和spring集成的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot (24) 使用Spring Cache集成Redis

Spring + Jedis集成Redis

redis和spring集成

redis和spring集成(注解实现,方便,快捷)

Spring Boot集成Spring Cache 和 Redis

redis和spring的集成