您的位置:首页 > 数据库 > Redis

Spring 整合Redis5.0集群【jedisCluster】

2019-02-22 11:38 302 查看

1.第一步:引入响应的jar包。

[code]<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

[code]<?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配置文件

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

4.编写测试代码【使用的jedisCluster】

[code]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配置文件

[code]	<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】

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: