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")); }
相关文章推荐
- spring整合redis集群遇到的问题及MyEclipse下Maven的安装配置
- dubbo2.5-spring4-mybastis3.2-springmvc4-mongodb3.4-redis3.2整合(十一)之 spring中定时器quartz的整合续集(Quartz集群配置)
- redis学习系列(二)--spring boot整合Redis集群
- Redis集群与spring的整合
- redis集群之哨兵模式高可用整合spring的配置使用及spring缓存机制和redis的结合
- Spring整合Redis集群并使用Java简单操作
- 【Spring系列】Spring mvc整合redis(非集群)
- Redis-6. Redis集群与Spring的整合
- SpringBoot整合Redis(单机+集群) —— 含Spring版附件文档
- 与Spring整合连接redis集群
- spring整合redis集群的相关配置(senlient模式)
- jedis(redis)整合spring,包括jedis客户端单机版,jedis集群版配置 ,连接池配置
- 分布式缓存技术redis学习系列(七)——spring整合jediscluster
- Spring Boot2.0之 整合Redis集群
- redis集群搭建以及和spring整合
- Spring整合Redis-Cluster集群实现04
- redis 集群介绍就,安装详细步骤,和spring 整合
- spring和redis集群的整合
- spring集成 JedisCluster 联接 redis3.0 集群
- SpringBoot整合Redis集群