spring-xml-配置redis

文章目錄

前言

  • 這是從爲知筆記的遷移的文章~~

單機

<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
  <property name="maxIdle" value="300" />
  <property name="maxWaitMillis" value="3000" />
  <property name="testOnBorrow" value="true" />
</bean>

<!-- jedis 連接工廠 -->
<bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
  <property name="hostName" value="192.168.174.128" />
  <property name="port" value="6379" />
  <property name="poolConfig" ref="poolConfig" />
</bean>

<!-- Jedis模板配置,序列化方式不同-->
<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
  <property name="connectionFactory" ref="redisConnectionFactory" />
  <property name="keySerializer">
      <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
  </property>
  <property name="valueSerializer">
      <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer">
      </bean>
  </property>
</bean>


<!-- Jedis模板配置,序列化方式不同-->
<bean id="redisTemplateString"  class="org.springframework.data.redis.core.StringRedisTemplate">
  <property name="connectionFactory" ref="connectionFactory" />
  <property name="keySerializer">
      <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
  </property>
  <property name="valueSerializer">
      <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
  </property>
</bean>

集羣

<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
     xmlns:mvc="http://www.springframework.org/schema/mvc"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
                         http://www.springframework.org/schema/beans/spring-beans.xsd
                         http://www.springframework.org/schema/mvc
                         http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                         http://www.springframework.org/schema/context
                         http://www.springframework.org/schema/context/spring-context.xsd">

  <!-- 配置jedis連接池 -->
  <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
      <constructor-arg name="host" value="單機IP" />
      <constructor-arg name="port" value="單機端口號" />
  </bean>
  <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
      <!-- 配置集羣信息 -->
      <constructor-arg name="nodes">
          <set>
              <bean class="redis.clients.jedis.HostAndPort">
                  <constructor-arg name="host" value="集羣IP1}" />
                  <constructor-arg name="port" value="集羣端口號1}" />
              </bean>
              <bean class="redis.clients.jedis.HostAndPort">
                  <constructor-arg name="host" value="集羣IP2}" />
                  <constructor-arg name="port" value="集羣端口號2}" />
              </bean>
              <bean class="redis.clients.jedis.HostAndPort">
                  <constructor-arg name="host" value="集羣IP3}" />
                  <constructor-arg name="port" value="集羣端口號3}" />
              </bean>
              <bean class="redis.clients.jedis.HostAndPort">
                  <constructor-arg name="host" value="集羣IP4}" />
                  <constructor-arg name="port" value="集羣端口號4}" />
              </bean>
              <bean class="redis.clients.jedis.HostAndPort">
                  <constructor-arg name="host" value="集羣IP5}" />
                  <constructor-arg name="port" value="集羣端口號5}" />
              </bean>
              <bean class="redis.clients.jedis.HostAndPort">
                  <constructor-arg name="host" value="集羣IP6}" />
                  <constructor-arg name="port" value="集羣端口號6}" />
              </bean>
          </set>
      </constructor-arg>
  </bean>
  <!-- 配置操作單機版redis的實現類 -->
  <bean id="poolJedisClient" class="com.taotao.manager.service.RedisPool" />
  <!-- 配置操作集羣版實現類 -->
  <bean id="clusterJedisClient" class="com.taotao.manager.service.RedisPool" />
</beans>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章