[Redis]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"));
	}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Crypto Ascetic

您的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值