SpringMvc集成使用redisCluster

来源:互联网 发布:火车头采集本地数据 编辑:程序博客网 时间:2024/06/05 11:36

最近在项目中引入了RedisCluster集群部署,下面就和大家分享一下引入的方式:

首先,在pom.xml中引入jedis包

<dependency>    <groupId>redis.clients</groupId>    <artifactId>jedis</artifactId>    <version>2.9.0</version></dependency>

然后使用配置文件配置:

<?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"    xsi:schemaLocation="http://www.springframework.org/schema/beans                        http://www.springframework.org/schema/beans/spring-beans-4.1.xsd                          http://www.springframework.org/schema/context                        http://www.springframework.org/schema/context/spring-context-4.0.xsd">   <bean id="jedisPoolConfig" class="org.apache.commons.pool2.impl.GenericObjectPoolConfig">        <property name="maxIdle" value="${redis.maxIdle}" />        <property name="minIdle" value="${redis.minIdle}" />        <property name="maxTotal" value="${redis.maxTotal}" />        <property name="testOnBorrow" value="true" />    </bean>    <bean id="jedisCluster" class="com.**.redis.config.JedisClusterFactory">        <property name="useable" value="${redis.useable}"></property>        <property name="hostNameAndPort" value="${redis.service.location}" />        <property name="timeout" value="${redis.service.timeout}" />        <property name="socketTimeout" value="${redis.service.socketTimeout}" />        <property name="maxRedirections" value="${redis.service.maxRedirections}" />        <property name="authPassword" value="${redis.service.authPassword}"></property>        <property name="GenericObjectPoolConfig" ref="jedisPoolConfig" />    </bean></beans>

其中参数配置,在文件redis.properties中:

#加了个开关,是否使用redis,考虑到有的项目单机的话,就可以把redis关掉redis.useable = true#redis连接池参数redis.maxTotal = 10redis.minIdle = 2redis.maxIdle = 10redis.maxWait = 600000#cluster地址,ip和端口之间使用冒号,然后使用逗号分隔redis.service.location = ip1:port1,ip2:port2,ip3:posrt3#如果redis开启了密码验证,需要配置密码redis.service.authPassword = ****redis.service.timeout = 300000redis.service.socketTimeout = 60000redis.service.maxRedirections = 2

以下是JedisClusterFactory的实现:

import java.util.HashSet;import java.util.Set;import org.apache.commons.pool2.impl.GenericObjectPoolConfig;import org.springframework.beans.factory.FactoryBean;import org.springframework.beans.factory.InitializingBean;import redis.clients.jedis.HostAndPort;import redis.clients.jedis.JedisCluster;public class JedisClusterFactory implements FactoryBean<JedisCluster>,InitializingBean{    private JedisCluster jedisCluster;    private GenericObjectPoolConfig poolConfig;    private int timeout;    private String hostNameAndPort;    private int maxRedirections;    private int socketTimeout;    private String authPassword;    private boolean useable;    public JedisCluster getObject() throws Exception {        return jedisCluster;    }    public Class<?> getObjectType() {        return (this.jedisCluster != null ? this.jedisCluster.getClass() : JedisCluster.class);    }    public boolean isSingleton() {        return true;    }    public void setGenericObjectPoolConfig(GenericObjectPoolConfig config) {        this.poolConfig = config;    }    public void setHostNameAndPort(String hostNameAndPort) {        this.hostNameAndPort = hostNameAndPort;    }    public void setTimeout(int timeout) {        this.timeout = timeout;    }    public void setMaxRedirections(int maxRedirections) {        this.maxRedirections = maxRedirections;    }    public GenericObjectPoolConfig getPoolConfig() {        return poolConfig;    }    public void setPoolConfig(GenericObjectPoolConfig poolConfig) {        this.poolConfig = poolConfig;    }    public int getTimeout() {        return timeout;    }    public String getHostNameAndPort() {        return hostNameAndPort;    }    public int getMaxRedirections() {        return maxRedirections;    }    public int getSocketTimeout() {        return socketTimeout;    }    public void setSocketTimeout(int socketTimeout) {        this.socketTimeout = socketTimeout;    }    public String getAuthPassword() {        return authPassword;    }    public void setAuthPassword(String authPassword) {        this.authPassword = authPassword;    }    public void setUseable(String useable) {        this.useable = Boolean.valueOf(useable);    }    //cluster的地址初始化    private Set<HostAndPort> parseHostAndPort() throws Exception {        Set<HostAndPort> haps = new HashSet<HostAndPort>();        String[] hostAndPorts = hostNameAndPort.split(",");        for (String hostAndPort : hostAndPorts) {            String host = hostAndPort.split(":")[0];            int port = Integer.valueOf(hostAndPort.split(":")[1]);            HostAndPort hap = new HostAndPort(host, port);            haps.add(hap);        }        return haps;    }    //项目启动自动执行,配置redis    public void afterPropertiesSet() throws Exception {        if(useable) {            Set<HostAndPort> haps = parseHostAndPort();                     jedisCluster = new JedisCluster(haps, timeout,socketTimeout, maxRedirections,authPassword,poolConfig);        }    }}

使用方式:

…………………………//注入@Autowiredprivate JedisCluster jedisCluster;……//直接使用jedisCluster中的方法jedisCluster.get(key);jedisCluster.set(key,value);……
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 安卓app与设备不兼容怎么办 手机卡坏了收不到快递短信怎么办 pph手术后钛钉没有脱落怎么办 右侧附件囊状透明声可怎么办 刚刚出生小孩睾丸没有掉下来怎么办 汽车被依非法营运之由扣押怎么办 老婆结婚小孩两个又上夜场该怎么办 对于不断无理要求赔偿的房东怎么办 欠的钱越来越多不知道怎么办了 手机壳的开机按键不好按怎么办 对方把保权的房子卖给我怎么办 内存卡在手机上显示损坏怎么办 内存卡显示但是下载不了东西怎么办 相机sd卡没办法读卡怎么办 尼康相机新sd卡显示满了怎么办 修冰箱没几个月又坏了怎么办 星露谷物语活动中心成了仓库怎么办 你家如果油锅起火时应该怎么办 当你遇到电器起火时应该怎么办 酷派大神f2充不进去电怎么办 借车子别人开违章一年了怎么办 快递刚发货我点错了收货怎么办 快递收货地址错了已经发货了怎么办 老公在外地工地上夜班吃不好怎么办 工人在工地干活被吊车撞了怎么办 小米手机记不得保密柜密码怎么办 华为手机文件保密柜密码忘了怎么办 超市寄存柜密码纸丢了怎么办 佳明软件安装不到手机上怎么办 最好的朋友的抢了自己的活怎么办 支付宝被实名验证上限了怎么办 装修预留插座被挡了一半怎么办 衣服放在柜子里面有味道怎么办 河北发货一个柜到天津港口怎么办 家用潜水泵更换电容后压力小怎么办 无塔供水井水水位低了怎么办 应急部成立林场的专业灭火队怎么办 路由器我恢复出厂设置断网了怎么办 多囊卵巢综合症不来月经怎么办 天刀普通血战苍梧城没有太白怎么办 天涯明月刀把沈妄言打入水中怎么办