SpringMvc集成使用redisCluster

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wzl19870309/article/details/74035255

最近在项目中引入了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 = 10
redis.minIdle = 2
redis.maxIdle = 10
redis.maxWait = 600000
#cluster地址,ip和端口之间使用冒号,然后使用逗号分隔
redis.service.location = ip1:port1,ip2:port2,ip3:posrt3
#如果redis开启了密码验证,需要配置密码
redis.service.authPassword = ****
redis.service.timeout = 300000
redis.service.socketTimeout = 60000
redis.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);
        }
    }

}

使用方式:

…………………………
//注入
@Autowired
private JedisCluster jedisCluster;

……
//直接使用jedisCluster中的方法
jedisCluster.get(key);
jedisCluster.set(key,value);
……

猜你喜欢

转载自blog.csdn.net/wzl19870309/article/details/74035255
今日推荐