SpringMVC下Redis集群cluster的试水小测
博客专区 > Pig-man 的博客 > 博客详情
SpringMVC下Redis集群cluster的试水小测
Pig-man 发表于1年前
SpringMVC下Redis集群cluster的试水小测
  • 发表于 1年前
  • 阅读 121
  • 收藏 3
  • 点赞 2
  • 评论 0

华为云·免费上云实践>>>   

摘要: 之前有写通过RedisClient这种客户端的方式调用redis,基本功能都是可以实现的,效率也很客观,但是近期发现一个很严重的问题,就是我博客中的redisClient这种方式,在最后的释放资源时不能完全释放,导致无法从redis服务器获取资源,于是更改成了cluster的集群方式。客户端的方式为什么会报错,原因还在查找中。

第一步:resource.porperties文件写入cluster的参数

#redis
jedis_max_active=2048
jedis_max_idle=100
jedis_max_wait=10000
jedis_test_on_borrow=true


#cluster
cluster_url=192.168.7.60
cluster_port=6381
cluster_url2=192.168.7.60
cluster_port2=6380
cluster_url3=192.168.7.60
cluster_port3=6381
cluster_url4=192.168.7.60
cluster_port4=6379
cluster_url5=192.168.7.60
cluster_port5=6380
cluster_url6=192.168.7.60
cluster_port6=6383

第二步:封bean,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:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:aop="http://www.springframework.org/schema/aop"  
    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">  

    <context:property-placeholder location="classpath:resource.properties" />  

    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${jedis_max_idle}" />
        <property name="maxTotal" value="${jedis_max_active}" />
        <property name="maxWaitMillis" value="${jedis_max_wait}" />
        <property name="testOnBorrow" value="${jedis_test_on_borrow}" />
    </bean>

    <!-- 	 使用构造方法注入:public JedisCluster(Set<HostAndPort> nodes, int timeout, final GenericObjectPoolConfig poolConfig)  -->
    <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
        <constructor-arg index="0">
            <set>
                <bean class="redis.clients.jedis.HostAndPort">
                    <constructor-arg type="java.lang.String" value="${cluster_url}" />
                    <constructor-arg type="int" value="${cluster_port}" />
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">
                   <constructor-arg type="java.lang.String" value="${cluster_url2}" />
                   <constructor-arg type="int" value="${cluster_port2}" />
                </bean>-->
                <bean class="redis.clients.jedis.HostAndPort">-->
                    <constructor-arg type="java.lang.String" value="${cluster_url3}" />
                    <constructor-arg type="int" value="${cluster_port3}" />
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">-->
                    <constructor-arg type="java.lang.String" value="${cluster_url4}" />
                    <constructor-arg type="int" value="${cluster_port4}" />
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">-->
                    <constructor-arg type="java.lang.String" value="${cluster_url5}" />
                    <constructor-arg type="int" value="${cluster_port5}" />-->
                </bean>
                <bean class="redis.clients.jedis.HostAndPort">-->
                    <constructor-arg type="java.lang.String" value="${cluster_url6}" />
                    <constructor-arg type="int" value="${cluster_port6}" />
                </bean>
            </set>
        </constructor-arg>
        <constructor-arg index="1" value="60000" type="int"></constructor-arg>
        <constructor-arg index="2" ref="poolConfig"></constructor-arg>
    </bean>
</beans>

第三步 接口描述IRedisCommandService,及方法调用RedisCommandServiceImpl implements IRedisCommandService

package com.aneop.common.cluster;

import java.util.List;
import java.util.Map;
import java.util.Set;

public interface IRedisCommandService {
	//----------------String-------------------
	/**
	 * 给一个 KEY 设置一个 值 ,并且设置有效时间
	 * @param key 存储的KEY
	 * @param value 存储KEY的值
	 * @param expire 有效时间
	 * @return 操作成功完成时返回 OK
	 * @throws Exception 异常
	 */
	public String set(String key, Object value, int expire) throws Exception;
	/**
	 * 获取 KEY对应的值
	 * @param key 查询的KEY
	 * @param cls 返回的对象类型
	 * @return
	 * @throws Exception 异常
	 */
	public <T> T get(String key, Class<T> cls) throws Exception;

	
	//--------------Set-----------------------
	/**
	 * 返回Set中成员的数量,如果该Key并不存在,返回0。
	 * 
	 * @param key
	 * @return
	 * @throws Exception
	 */
	public Long scard(String key) throws Exception;

	/**
	 * 如果在插入的过程用,参数中有的成员在Set中已经存在,该成员将被忽略,而其它成员仍将会被正常插入。
	 * 如果执行该命令之前,该Key并不存在,该命令将会创建一个新的Set,此后再将参数中的成员陆续插入。
	 * 如果该Key的Value不是Set类型,该命令将返回相关的错误信息。
	 * 
	 * @param key
	 * @param members
	 * @return 本次操作实际插入的成员数量。
	 * @throws Exception 
	 */
	public Long sadd(String key, Object... members) throws Exception;

	/**
	 * 从与Key关联的Set中删除参数中指定的成员,不存在的参数成员将被忽略,如果该Key并不存在,将视为空Set处理。
	 * 
	 * @param key
	 * @param members
	 * @return 从Set中实际移除的成员数量,如果没有则返回0。
	 * @throws Exception 
	 */
	public Long srem(String key, Object... members) throws Exception;

	/**
	 * 查询set 里面的成员
	 * 
	 * @param key
	 * @throws Exception 
	 */
	public <T> Set<T> smembers(String key, Class<T> t) throws Exception;

	/**
	 * 删除 set key的数据
	 * @throws Exception 
	 */
	public Long sremove(String key) throws Exception;
	
	//--------------Map-----------------------
	/**
	 *  Map集合存储值(为MAP新增key-value)
	 * @param key map的key
	 * @param mapKey map里面value对应的key
	 * @param value 要存储的值(JSON字符串)
	 * @throws Exception 异常
	 */
	public void setMap(String key, String mapKey, String value) throws Exception;

	/**
	 *  Map存储List(map(key,List))
	 * @param key map的key
	 * @param mapKey map里面value对应的key
	 * @param value 要存储的值
	 * @throws Exception 异常
	 */
	public void setMapList(String key, String mapKey, List<?> list) throws Exception;
	
	/**
	 * 获取缓存中,map集合中mapkey存放的对象
	 * @param name  以对象形式存储的名字
	 * @param mapKey map中key值
	 * @param t 返回实体对象类型
	 * @throws Exception
	 */
	public <T> T getMapValue(String key, String mapKey, Class<T> t) throws Exception;

	public String getMapValue(String key, String string) throws Exception;
	/**
	 * 获取缓存中,map集合中的值
	 * @param name 以对象形式存储的名字
	 * @param t 返回实体对象类型
	 * @throws Exception
	 */
	public <T> List<T> getMapValues(String key, Class<T> t) throws Exception;
	
	/**
	 * 删除 map里面的某一个值
	 * @param key map的外层key
	 * @param valueKey 值对应的key
	 * @return
	 * @throws Exception
	 */
	public Long removeMap(String key, String valueKey) throws Exception;
	
	/**
	 * 获取 map里面 所有 key对应的 value
	 * @param name 以对象形式存储的key
	 * @throws Exception 
	 */
	public Map<String, String> getMaps(String key) throws Exception;
	
	//--------------List----------------------
	
	//--------------common--------------------
	/**
	 * 删除 KEY -----此处可以删除 任意数据类型的KEY数据
	 * @param key 要删除的KEY
	 * @return 影响的数据行
	 * @throws Exception 异常
	 */
	public Long remove(String key) throws Exception;
	/**
	 * 检测 KEY在缓存中是否存在
	 * @param key 检测的KEY
	 * @return
	 * @throws Exception
	 */
	public boolean exists(String key) throws Exception;
	/**
	 * 设置有效期
	 * @param key 有效期的key
	 * @param seconds 有效时间 秒
	 * @return 影响行
	 */
	public Long expire(String key, int seconds) throws Exception;
    
	/**
	 * 
	* @Title: type 
	* @Description: 返回 key 所储存的值的类型。
	* @param @param key
	* @param @return
	* @param @throws Exception 
	* @return String 	none (key不存在)
						string (字符串)
						list (列表)
						set (集合)
						zset (有序集)
						hash (哈希表)
	* @throws
	 */
	public String type(String key)throws Exception;
    
	
	public long rPush(String key, List<?> list);
	
	public String lPop(String key);
	
	public List<String> lrange(String key);
}
  
package com.aneop.common.cluster.impl;

import com.alibaba.fastjson.JSON;
import com.aneop.common.cluster.IRedisCommandService;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisCluster;

import javax.annotation.Resource;
import java.util.*;

@Service("redisCommandService")
public class RedisCommandServiceImpl implements IRedisCommandService {
	@Resource(name="jedisCluster")
	public JedisCluster jedisCluster;

	@Override
	public String set(String key, Object value, int expire) throws Exception {
		try {
			String result = jedisCluster.set(key, JSON.toJSONString(value));
			if (expire > 0)
				jedisCluster.expire(key, expire);
			return result;
		} catch (Exception e) {
			throw e;
		}

	}

	@Override
	public <T> T get(String key, Class<T> cls) throws Exception {
		try {
			String jsonValue = jedisCluster.get(key);
			return JSON.parseObject(jsonValue, cls);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public Long scard(String key) throws Exception {
		Long count = 0l;
		try {
			count = jedisCluster.scard(key);
		} catch (Exception e) {
			throw e;
		}

		return count;
	}

	@Override
	public Long sadd(String key, Object... members) throws Exception {
		Long rows = 0l;
		try {
			String[] item = new String[members.length];
			int i = 0;
			for (Object obj : members) {
				item[i] = JSON.toJSONString(obj);
				i++;
			}
			rows = jedisCluster.sadd(key, item);
		} catch (Exception e) {
			throw e;
		}

		return rows;
	}

	@Override
	public Long srem(String key, Object... members) throws Exception {
		Long rows = 0l;
		try {
			String[] item = new String[members.length];
			int i = 0;
			for (Object obj : members) {
				item[i] = JSON.toJSONString(obj);
				i++;
			}
			rows = jedisCluster.srem(key, item);
		} catch (Exception e) {
			throw e;
		}
		return rows;
	}

	@Override
	public <T> Set<T> smembers(String key, Class<T> t) throws Exception {
		try {
			Set<String> sItem = jedisCluster.smembers(key);

			Set<T> result = new HashSet<T>();
			Iterator<String> rt = sItem.iterator();
			while (rt.hasNext()) {
				result.add(JSON.parseObject(rt.next(), t));
			}
			return result;
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public Long sremove(String key) throws Exception {
		Long result = 0L;
		try {
			result = jedisCluster.del(key);
		} catch (Exception e) {
			throw e;
		}
		return result;
	}

	// -------------------map--------------------------
	@Override
	public void setMap(String key, String mapKey, String value)
			throws Exception {
		try {
//			String jsonValue = JSON.toJSONString(value);
			jedisCluster.hset(key, mapKey, value);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public void setMapList(String key, String mapKey, List<?> list)
			throws Exception {
		try {
			jedisCluster.hset(key, mapKey, JSON.toJSONString(list));
		} catch (Exception e) {
			throw e;
		}		
	}

	@Override
	public <T> T getMapValue(String key, String mapKey, Class<T> t)
			throws Exception {
		List<String> list = null;
		try {

			list = jedisCluster.hmget(key, mapKey);
		} catch (Exception e) {
			throw e;
		}
		if (list == null || list.size() == 0) {
			return null;
		}

		String value = list.get(0);
		if (value != null) {
			return JSON.parseObject(value, t);
		}
		return null;
	}

	
	
	@Override
	public String getMapValue(String key, String mapKey) throws Exception {
		// TODO Auto-generated method stub
		String value = null;
		try {
			value = jedisCluster.hget(key, mapKey);
		} catch (Exception e) {
			throw e;
		}
		return value;
	}

	@Override
	public <T> List<T> getMapValues(String key, Class<T> t) throws Exception {
		List<String> list = new ArrayList<String>();
		List<T> rList = null;

		try {

			list = jedisCluster.hvals(key);
		} catch (Exception e) {
			throw e;
		}

		if (list != null && list.size()>0) {
			rList = new ArrayList<T>();
			for (int i = 0; i < list.size(); i++) {
				rList.add(JSON.parseObject(list.get(i), t));
			}
		}

		return rList;
	}

	@Override
	public Long removeMap(String key, String valueKey) throws Exception {
		Long result = 0L;

		try {
			result = jedisCluster.hdel(key, valueKey);
		} catch (Exception e) {
			throw e;
		}
		return result;
	}

	@Override
	public Map<String, String> getMaps(String key) throws Exception {

		Map<String, String> rList = new HashMap<String, String>();
		try {

			rList = jedisCluster.hgetAll(key);
		} catch (Exception e) {
			throw new Exception(e);
		}
		return rList;
	}

	// -------------------common--------------------------
	@Override
	public Long remove(String name) throws Exception {
		Long result = 0L;

		try {

			result = jedisCluster.del(name);
		} catch (Exception e) {
			throw e;
		}
		return result;
	}

	@Override
	public boolean exists(String key) throws Exception {

		try {

			return jedisCluster.exists(key);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public Long expire(String key, int seconds) throws Exception {
		try {
			return jedisCluster.expire(key, seconds);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public String type(String key) throws Exception {
		try {
			return jedisCluster.type(key);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public long rPush(String key, List<?> list) {
		try {
			return jedisCluster.rpush(key, JSON.toJSONString(list));
		} catch (Exception e) {
			//throw e;
			return 0l;
		}
	}

	@Override
	public String lPop(String key) {
		try {
			return jedisCluster.lpop(key);
		} catch (Exception e) {
			//throw e;
			return null;
		}
	}

	@Override
	public List<String> lrange(String key) {
		return jedisCluster.lrange(key, 0, -1);
	}

}

第四部,数据的吞吐实例

 IRedisCommandService redisCommandService = (IRedisCommandService) ApplicationContextUtil.getBean("redisCommandService");
        String reParams = this.turnToExpParams(tmsOrder);
        try {
            redisCommandService.setMap("redis_test", map.key(), map.value();
        } catch (Exception e) {
            e.printStackTrace();
        }
  IRedisCommandService redisCommandService = (IRedisCommandService) ApplicationContextUtil.getBean("redisCommandService");
        Map<String, String> newMap = null;//从redis中取出数据
        try {
           newMap = redisCommandService.getMaps("redis_test");
        } catch (Exception e) {
            e.printStackTrace();
        }

 

标签: Redis cluster SpringMvc
共有 人打赏支持
粉丝 19
博文 57
码字总数 43860
×
Pig-man
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: