天天看點

Spring 整合Redis5.0叢集【jedisCluster】

1.第一步:引入響應的jar包。

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

2.第二步:Spring配置檔案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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.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/jee 
        http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

	 <context:property-placeholder location="classpath:redis.properties" />
	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxIdle" value="${redis.maxIdle}" />
		<property name="maxTotal" value="${redis.maxActive}" />
		<property name="maxWaitMillis" value="${redis.maxWait}" />
		<property name="testOnBorrow" value="${redis.testOnBorrow}" />
	</bean>

	<bean id="hostport1" class="redis.clients.jedis.HostAndPort">
		<constructor-arg name="host" value="172.16.1.84" />
		<constructor-arg name="port" value="7001" />
	</bean>

	<bean id="hostport2" class="redis.clients.jedis.HostAndPort">
		<constructor-arg name="host" value="172.16.1.84" />
		<constructor-arg name="port" value="7002" />
	</bean>

	<bean id="hostport3" class="redis.clients.jedis.HostAndPort">
		<constructor-arg name="host" value="172.16.1.84" />
		<constructor-arg name="port" value="7003" />
	</bean>

	<bean id="hostport4" class="redis.clients.jedis.HostAndPort">
		<constructor-arg name="host" value="172.16.1.84" />
		<constructor-arg name="port" value="7004" />
	</bean>

	<bean id="hostport5" class="redis.clients.jedis.HostAndPort">
		<constructor-arg name="host" value="172.16.1.84" />
		<constructor-arg name="port" value="7005" />
	</bean>

	<bean id="hostport6" class="redis.clients.jedis.HostAndPort">
		<constructor-arg name="host" value="172.16.1.84" />
		<constructor-arg name="port" value="7006" />
	</bean>

	<bean id="redisCluster" class="redis.clients.jedis.JedisCluster">
		<constructor-arg name="jedisClusterNode">
			<set>
				<ref bean="hostport1" />
				<ref bean="hostport2" />
				<ref bean="hostport3" />
				<ref bean="hostport4" />
				<ref bean="hostport5" />
				<ref bean="hostport6" />
			</set>
		</constructor-arg>
		<constructor-arg name="connectionTimeout" value="6000" />
		<constructor-arg name="soTimeout" value="2000" />
		<constructor-arg name="maxAttempts" value="3" />
		<constructor-arg name="poolConfig">
			<ref bean="jedisPoolConfig" />
		</constructor-arg>
	</bean> 
           

3.編寫redis.properties配置檔案

redis.maxTotal=1000
redis.maxIdle=10
redis.maxActive=1000
redis.maxWait=30000
redis.testOnBorrow=true
           

4.編寫測試代碼【使用的jedisCluster】

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import redis.clients.jedis.JedisCluster;

public class RedisTest {
	@Test
	public void redisTest(){
		// 獲得spring上下文,
		ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-redis.xml");
		 JedisCluster jc = ctx.getBean(JedisCluster.class);
		 System.out.println(jc.get("Hello"));
	}
           

5.如何引入stringRedisTemplate,修改spring配置檔案

<context:property-placeholder
		ignore-unresolvable="true" location="classpath:redis.properties" />

	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxTotal" value="${redis.maxTotal}" />
		<property name="maxIdle" value="${redis.maxIdle}" />
		<property name="numTestsPerEvictionRun" value="1024" />
		<property name="timeBetweenEvictionRunsMillis" value="30000" />
		<property name="minEvictableIdleTimeMillis" value="1800000" />
		<property name="softMinEvictableIdleTimeMillis" value="10000" />
		<property name="maxWaitMillis" value="1500" />
		<property name="testOnBorrow" value="true" />
		<property name="testWhileIdle" value="true" />
		<property name="blockWhenExhausted" value="false" />
	</bean>

	<bean id="redisClusterConfiguration"
		class="org.springframework.data.redis.connection.RedisClusterConfiguration">
		<property name="maxRedirects" value="5"></property>
		<property name="clusterNodes">
			<set>
				<bean class="org.springframework.data.redis.connection.RedisNode">
					<constructor-arg name="host" value="172.16.1.84" />
					<constructor-arg name="port" value="7001" />
				</bean>
				<bean class="org.springframework.data.redis.connection.RedisNode">
					<constructor-arg name="host" value="172.16.1.84" />
					<constructor-arg name="port" value="7002" />
				</bean>
				<bean class="org.springframework.data.redis.connection.RedisNode ">
					<constructor-arg name="host" value="172.16.1.84" />
					<constructor-arg name="port" value="7003" />
				</bean>
				<bean class="org.springframework.data.redis.connection.RedisNode">
					<constructor-arg name="host" value="172.16.1.84" />
					<constructor-arg name="port" value="7004" />
				</bean>
				<bean class="org.springframework.data.redis.connection.RedisNode">
					<constructor-arg name="host" value="172.16.1.84" />
					<constructor-arg name="port" value="7005" />
				</bean>
				<bean class="org.springframework.data.redis.connection.RedisNode">
					<constructor-arg name="host" value="172.16.1.84" />
					<constructor-arg name="port" value="7006" />
				</bean>
			</set>
		</property>
	</bean>
	<bean id="jedisConnectionFactory"
		class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
		<constructor-arg name="poolConfig" ref="jedisPoolConfig" />
		<constructor-arg name="clusterConfig" ref="redisClusterConfiguration" />
	</bean>
	<bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
		<property name="connectionFactory" ref="jedisConnectionFactory" />
		<property name="keySerializer">
			<bean
				class="org.springframework.data.redis.serializer.StringRedisSerializer" />
		</property>
	</bean>
	<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
		<constructor-arg index="0" ref="stringRedisTemplate" />
	</bean>
           

6.編寫測試代碼【使用stringRedisTemplate】

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import redis.clients.jedis.JedisCluster;
public class RedisTest {
	
	@Test
	public void redisTest(){
		// 獲得spring上下文,
		ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-redis.xml");
	StringRedisTemplate stringRedisTemplate = ctx.getBean(StringRedisTemplate.class);
	System.out.println(stringRedisTemplate.opsForValue().get("Hello"));
	}
           
Spring 整合Redis5.0叢集【jedisCluster】

繼續閱讀