SSM整合Redis Cluster(Redis集群)

4 篇文章 0 订阅
1 篇文章 0 订阅

1、导入依赖

<!-- Redis客户端:Jedis -->
		<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>2.9.0</version>
		</dependency>

		<!-- Spring整合redis -->
		<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-redis</artifactId>
			<version>1.8.4.RELEASE</version>
		</dependency>

建议就导入这两个版本的依赖,其他可能会版本会造成jar包冲突。

2、redis.properties

#redis
redis.pool.maxTotal=30
redis.pool.maxIdle=10
redis.pool.numTestsPerEvictionRun=1024
redis.pool.timeBetweenEvictionRunsMillis=30000
redis.pool.minEvictableIdleTimeMillis=1800000
redis.pool.softMinEvictableIdleTimeMillis=10000
redis.pool.maxWaitMillis=1500
redis.pool.testOnBorrow=true
redis.pool.testWhileIdle=true
redis.pool.blockWhenExhausted=false
redis.maxRedirects=3
redis.host1=192.168.110.131
redis.port1=7001
redis.host2=192.168.110.131
redis.port2=7002
redis.host3=192.168.110.131
redis.port3=7003
redis.host4=192.168.110.131
redis.port4=7004
redis.host5=192.168.110.131
redis.port5=7005
redis.host6=192.168.110.131
redis.port6=7006

3、beans-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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:cache="http://www.springframework.org/schema/cache"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/cache
		http://www.springframework.org/schema/cache/spring-cache.xsd">
		<!-- 使用.properties文件 -->
	<bean id="propertyConfigurer"			class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="classpath:redis.properties" />
	</bean>
	<!-- Jedis配置 -->
	<bean id="jedisPoolConfig"
		class="redis.clients.jedis.JedisPoolConfig">
		<!-- 最大连接数 -->
		<property name="maxTotal" value="30" />
		<!-- 最大空闲连接数 -->
		<property name="MaxIdle" value="10" />
		<!-- 每次释放连接的最大数目 -->
		<property name="numTestsPerEvictionRun" value="1024" />
		<!-- 释放连接的扫描间隔(毫秒) -->
		<property name="timeBetweenEvictionRunsMillis" value="30000" />
		<!-- 连接最小空闲时间 -->
		<property name="minEvictableIdleTimeMillis" value="1800000" />
		<!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
		<property name="softMinEvictableIdleTimeMillis" value="10000" />
		<!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
		<property name="maxWaitMillis" value="1500" />
		<!-- 在获取连接的时候检查有效性, 默认false -->
		<property name="testOnBorrow"
			value="${redis.pool.testOnBorrow}" />
		<!-- 在空闲时检查有效性, 默认false -->
		<property name="testWhileIdle"
			value="${redis.pool.testWhileIdle}" />
		<!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
		<property name="blockWhenExhausted"
			value="${redis.pool.blockWhenExhausted}" />
	</bean>

	<!-- 配置Cluster -->
	<bean id="redisClusterConfiguration"
		class="org.springframework.data.redis.connection.RedisClusterConfiguration">
		<property name="maxRedirects" value="3"></property>
		<!-- 节点配置 -->
		<property name="clusterNodes">
			<set>
				<bean
					class="org.springframework.data.redis.connection.RedisClusterNode">
					<constructor-arg name="host" value="${redis.host1}"></constructor-arg>
					<constructor-arg name="port" value="${redis.port1}"></constructor-arg>
				</bean>
				<bean
					class="org.springframework.data.redis.connection.RedisClusterNode">
					<constructor-arg name="host" value="${redis.host2}"></constructor-arg>
					<constructor-arg name="port" value="${redis.port2}"></constructor-arg>
				</bean>
				<bean
					class="org.springframework.data.redis.connection.RedisClusterNode">
					<constructor-arg name="host" value="${redis.host3}"></constructor-arg>
					<constructor-arg name="port" value="${redis.port3}"></constructor-arg>
				</bean>
				<bean
					class="org.springframework.data.redis.connection.RedisClusterNode">
					<constructor-arg name="host" value="${redis.host4}"></constructor-arg>
					<constructor-arg name="port" value="${redis.port4}"></constructor-arg>
				</bean>
				<bean
					class="org.springframework.data.redis.connection.RedisClusterNode">
					<constructor-arg name="host" value="${redis.host5}"></constructor-arg>
					<constructor-arg name="port" value="${redis.port5}"></constructor-arg>
				</bean>
				<bean
					class="org.springframework.data.redis.connection.RedisClusterNode">
					<constructor-arg name="host" value="${redis.host6}"></constructor-arg>
					<constructor-arg name="port" value="${redis.port6}"></constructor-arg>
				</bean>
			</set>
		</property>
	</bean>
	<!-- 配置jedis连接工厂 -->
	<bean id="jeidsConnectionFactory"
		class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
		<constructor-arg ref="redisClusterConfiguration" />
		<constructor-arg ref="jedisPoolConfig" />
	</bean>
	<!-- 配置RedisTemplate -->
	<bean id="redisTemplate"
		class="org.springframework.data.redis.core.RedisTemplate">
		<property name="connectionFactory"
			ref="jeidsConnectionFactory" />
		<!-- String -->
		<property name="keySerializer">
			<bean
				class="org.springframework.data.redis.serializer.StringRedisSerializer" />
		</property>
		<property name="valueSerializer">
			<bean
				class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
		</property>
		<!-- hash -->
		<property name="hashKeySerializer">
			<bean
				class="org.springframework.data.redis.serializer.StringRedisSerializer" />
		</property>
		<property name="hashValueSerializer">
			<bean
				class="org.springframework.data.redis.serializer.StringRedisSerializer" />
		</property>
	</bean>
	<!-- 定义缓存管理器redisCacheManager。 注意:cache-manager默认值是cacheManager,你的缓存管理器id要是命名是cacheManager,这里可以省略 
		1.使用注解驱动 -->
	<cache:annotation-driven
		cache-manager="redisCacheManager" />
	<!-- 2.定义缓存管理器 -->
	<bean id="redisCacheManager"
		class="org.springframework.data.redis.cache.RedisCacheManager">
		<!-- 通过构造方法注入redisTemplate -->
		<constructor-arg index="0" ref="redisTemplate" />
		<!-- 定义超时时间,单位秒 -->
		<property name="defaultExpiration" value="5000"></property>
		<!-- 设置缓存器名称 -->
		<property name="cacheNames">
			<list>
				<value>redisCacheManager</value>
			</list>
		</property>
	</bean>

</beans>

4、注解使用

在BeanServiceImpl.java文件中对方法进行注解添加
@CachePut注解:
如果缓存需要更新,且不干扰方法的执行,可以使用注解@CachePut。@CachePut标注的方法在执行前不会去检查缓存中是否存在之前执行过的结果,而是每次都会执行该方法,并将执行结果以键值对的形式存入指定的缓存中。

@Override
@CachePut(value="redisCacheManager",key="'insertP'")
public void insertProduct(Product product,CommonsMultipartFile file, HttpServletRequest request){
    service.insertP(product);
}
// key对应的是mapper中的方法,并不是BeanServiceImpl.java方法

@Cacheable注解:
用来声明方法是可缓存的。将结果存储到缓存中以便后续使用相同参数调用时不需执行实际的方法。直接从缓存中取值。最简单的格式需要制定缓存名称。

@Cacheable(value="redisCacheManager",key="'findProduct'")
public List<Product> findProduct(Integer pageNo){
	service.findProduct(product);}

@CacheEvict是用来标注在需要清除缓存元素的方法或类上的。当标记在一个类上时表示其中所有的方法的执行都会触发缓存的清除操作。
PS:
value的值是redis配置文件对应的缓存管理器的id值
key="‘insertProduct’":是要缓存到redis中的值,下次spring会直接通过key获取结果。注意key的值中还有个单引号。
@Cacheable:适合查询,有返回值的方法上。因为,每次请求在进入方法之前,spring会先从缓存服务器中查找对应的key,如果有,就直接返回。否则再去查询数据库,在将结果通过key保存到缓存中。
@CachePut:适合添加,修改,void方法上。因为spring不会事先去缓存服务器中查询数据,而是直接执行方法,然后将结果通过key保存到缓存中。这样是防止数据不一致。

配置完成后重启服务器,可能会报错

redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

查看Redis集群踩的坑及报错记录并解决

一、Redis服务器搭建与学习
二、Redis Cluster(Redis 集群)的搭建
四、Redis集群踩的坑及报错记录并解决

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值