首页 > 代码库 > Redis学习笔记8--spring集成redis

Redis学习笔记8--spring集成redis

spring配置单个redis

<dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.3.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.4.1</version>
        </dependency>
<?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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    xmlns:redis="http://www.springframework.org/schema/redis"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-34.0.xsd     
                            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
                            http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop.xsd
                            http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
                            http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
                            http://www.springframework.org/schema/redis http://www.springframework.org/schema/redis/spring-redis-1.0.xsd">

    <!-- <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="http://www.mamicode.com/300" />
        <property name="testOnBorrow" value="http://www.mamicode.com/true" />
    </bean> -->

    <bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="10.10.10.30"></property>
        <property name="port" value="6379"></property>
        <!-- 引入默认的连接池配置 -->
        <property name="usePool" value="true"></property>
        <!-- 引入自定义的poolConfig连接池配置 -->
        <!-- <property name="poolConfig" ref="poolConfig"></property> -->
    </bean>
    
    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="redisConnectionFactory"></property>
    </bean>

</beans>

spring配置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" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    xmlns:redis="http://www.springframework.org/schema/redis"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-34.0.xsd     
                            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
                            http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop.xsd
                            http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
                            http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
                            http://www.springframework.org/schema/redis http://www.springframework.org/schema/redis/spring-redis-1.0.xsd">

    <context:annotation-config>
        <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
            <property name="maxTotal" value="1000" />
            <property name="maxIdle" value="10" />
            <property name="minIdle" value="1" />
            <property name="maxWaitMillis" value="30000" />
            <property name="testOnBorrow" value="true" />
            <property name="testOnReturn" value="true" />
            <property name="testWhileIdle" value="true" />
            <!-- <property name="testWhileIdle" value="http://www.mamicode.com/true"/> -->
        </bean>

        <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool"
            scope="singleton">
            <constructor-arg ref="jedisPoolConfig" />
            <constructor-arg>
                <list>
                    <bean class="redis.clients.jedis.JedisShardInfo">
                        <constructor-arg name="host" value="10.10.10.20" />
                        <constructor-arg name="port" value="6379" />
                        <constructor-arg name="auth" value="Java0713!" />
                        <constructor-arg index="2" value="instance:01"/>
                    </bean>
                    <bean class="redis.clients.jedis.JedisShardInfo">
                        <constructor-arg name="host" value="10.10.10.30" />
                        <constructor-arg name="port" value="6379" />
                        <constructor-arg name="auth" value="Java0713!" />
                        <constructor-arg index="2" value="instance:02"/>
                    </bean>
                    <bean class="redis.clients.jedis.JedisShardInfo">
                        <constructor-arg name="host" value="10.10.10.40" />
                        <constructor-arg name="port" value="6379" />
                        <constructor-arg name="auth" value="Java0713!" />
                        <constructor-arg index="2" value="instance:03"/>
                    </bean>
                </list>
            </constructor-arg>
        </bean>
        <!--java帮我们同步sentinel的信息,将主从信息同步到客户端来 -->
        <bean class="redis.clients.jedis.JedisSentinelPool">
            <constructor-arg index="0" value="mymaster" />
            <constructor-arg index="1">
                <set>
                    <value>127.0.0.1:6379</value>
                </set>
            </constructor-arg>
            <constructor-arg index="2" ref="jedisPoolConfig" />
        </bean>
    </context:annotation-config>
</beans>

 

Redis学习笔记8--spring集成redis