A Redis cluster in a Java Web project or a click-to-version configuration

Source: Internet
Author: User
Tags java web redis cluster

Stand-alone configuration
 <bean id = "jedisPoolConfig" class = "redis.clients.jedis.JedisPoolConfig">
        <!-Maximum number of connections->
        <property name = "maxTotal" value = "100" />
        <!-Maximum number of idle connections->
        <property name = "maxIdle" value = "300" />
        <!-The maximum number of connections released each time->
        <property name = "numTestsPerEvictionRun" value = "1024" />
        <!-Scanning interval to release the connection (ms)->
        <property name = "timeBetweenEvictionRunsMillis" value = "30000" />
        <!-Minimum connection idle time->
        <property name = "minEvictableIdleTimeMillis" value = "1800000" />
        <!-How long after the connection is idle and release, when the idle time> this value and idle connection> maximum idle connection number is released directly->
        <property name = "softMinEvictableIdleTimeMillis" value = "10000" />
        <!-The maximum number of milliseconds to wait for connection, less than zero: blocking indefinite time, default -1->
        <property name = "maxWaitMillis" value = "1500" />
        <!-Check validity when getting connection, default false->
        <property name = "testOnBorrow" value = "true" />
        <!-Check validity when idle, default false->
        <property name = "testWhileIdle" value = "true" />
        <!-Whether to block when the connection is exhausted, false to report an exception, true block until timeout, the default true->
        <property name = "blockWhenExhausted" value = "false" />
    </ bean>
     <bean id = "redisClient" class = "redis.clients.jedis.JedisPool">
        <constructor-arg name = "poolConfig" ref = "jedisPoolConfig"> </ constructor-arg>
        <constructor-arg name = "host" value = "$ {redis.host}"> </ constructor-arg>
        <constructor-arg name = "port" value = "$ {redis.port}"> </ constructor-arg>

        <!-<constructor-arg name = "timeout" value = "$ {redis.timeOut}"> </ constructor-arg>
        <constructor-arg name = "password" value = "$ {redis.password}"> </ constructor-arg>->
    </ bean>
    <bean id = "jedisClient" class = "com.yagoo.wificontrolsys.redis.impl.JedisClientSingle" />
Cluster version configuration, where constructor-arg name = "host" value = "192.168.199.203" can be replaced by specifying ip

<bean id = "redisClient" class = "redis.clients.jedis.JedisCluster">
        <constructor-arg name = "nodes">
            <set>
                <bean class = "redis.clients.jedis.HostAndPort">
                    <constructor-arg name = "host" value = "192.168.199.203"> </ constructor-arg>
                    <constructor-arg name = "port" value = "7001"> </ constructor-arg>
                </ bean>
                <bean class = "redis.clients.jedis.HostAndPort">
                    <constructor-arg name = "host" value = "192.168.199.203"> </ constructor-arg>
                    <constructor-arg name = "port" value = "7002"> </ constructor-arg>
                </ bean>
                <bean class = "redis.clients.jedis.HostAndPort">
                    <constructor-arg name = "host" value = "192.168.199.203"> </ constructor-arg>
                    <constructor-arg name = "port" value = "7003"> </ constructor-arg>
                </ bean>
                <bean class = "redis.clients.jedis.HostAndPort">
                    <constructor-arg name = "host" value = "192.168.199.203"> </ constructor-arg>
                    <constructor-arg name = "port" value = "7004"> </ constructor-arg>
                </ bean>
                <bean class = "redis.clients.jedis.HostAndPort">
                    <constructor-arg name = "host" value = "192.168.199.203"> </ constructor-arg>
                    <constructor-arg name = "port" value = "7005"> </ constructor-arg>
                </ bean>
                <bean class = "redis.clients.jedis.HostAndPort">
                    <constructor-arg name = "host" value = "192.168.199.203"> </ constructor-arg>
                    <constructor-arg name = "port" value = "7006"> </ constructor-arg>
                </ bean>
            </ set>
        </ constructor-arg>
        <constructor-arg name = "poolConfig" ref = "jedisPoolConfig"> </ constructor-arg>
    </ bean>
    <bean id = "jedisClient" class = "com.wenwuyi.study.rest.dao.impl.JedisClientCluster" />
Two client code
Client interface

/ **
 * Copyright (c) 2017. yagoosafe.com All right reserved. This software is the
 * confidential and proprietary information of yagoosafe.com ("Confidential Information").
 * You shall not disclose such Confidential Information and shall use it only in accordance
 * with the terms of the license agreement you entered into with yagoosafe.com.
 * /
package com.yagoo.wificontrolsys.redis;

/ **
 * Class name: JedisClient.java
 * Description: jedis client
 * Time: 4:59:30 PM, March 8, 2018
 * @author yangchangjiang
 * @version 1.0
 * /
public interface JedisClient {
    / **
     *
     * Get information based on key
     * @param key
     * @return String
     * /
    String get (String key);
    / **
     *
     * Setting information
     * @param key
     * @param value
     * @return String
     * /
    String set (String key, String value);
    / **
     *
     * Set information with expiration time
     * @param key
     * @param value
     * @param expire
     * @return String
     * /
    String set (String key, String value, int expire);
    / **
     *
     * hset with multiple key values
     * @param hkey
     * @param key
     * @return String
     * /
    String hget (String hkey, String key);
    / **
     *
     * hset with multiple key values and value values
     * @param hkey
     * @param key
     * @param value
     * @return long
     * /
    long hset (String hkey, String key, String value);
    / **
     *
     * Incr key value +1
     * @param key
     *@return long
     * /
    long incr (String key);
    / **
     *
     * Set expiration time
     * @param key
     * @param second
     * @return long
     * /
    long expire (String key, int second);
    / **
     *
     * View expiration time
     * @param key
     * @return long
     * /
    long ttl (String key);
    / **
     *
     * Delete the corresponding key value
     * @param key
     * @return long
     * /
    long del (String key);
    / **
     *
     * Delete hkey and key
     * @param hkey
     * @param key
     * @return long
     * /
    long hdel (String hkey, String key);
}
Stand-alone version

/ *
 * Copyright 2017 wenwuyi.cn All right reserved. This software is the
 * confidential and proprietary information of yagoosafe.com ("Confidential
 * Information "). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with wenwuyi.cn.
 * /
package com.yagoo.wificontrolsys.redis.impl;

import org.springframework.beans.factory.annotation.Autowired;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import com.yagoo.wificontrolsys.redis.JedisClient;

/ **
 * Class name JedisClientSingle.java class
 * The role of the class: Redis stand-alone version
 * @author YCJ Author E-mail: [email protected]
 * @date Created at: 12:51:39 PM, December 24, 2017
 * @version 1.0
 * /
public class JedisClientSingle implements JedisClient {

    @Autowired
    private JedisPool jedisPool;

    @Override
    public String get (String key) {
        Jedis jedis = jedisPool.getResource ();
        String string = jedis.get (key);
        jedis.close ();
        return string;
    }

    @Override
    public String set (String key, String value) {
        Jedis jedis = jedisPool.getResource ();
        String string = jedis.set (key, value);
        jedis.close ();
        return string;
    }

    @Override
    public String hget (String hkey, String key) {
        Jedis jedis = jedisPool.getResource ();
        String string = jedis.hget (hkey, key);
        jedis.close ();
        return string;
    }

    @Override
    public long hset (String hkey, String key, String value) {
        Jedis jedis = jedisPool.getResource ();
        Long result = jedis.hset (hkey, key, value);
        jedis.close ();
        return result;
    }

    @Override
    public long incr (String key) {
        Jedis jedis = jedisPool.getResource ();
        Long result = jedis.incr (key);
        jedis.close ();
        return result;
    }

    @Override
    public long expire (String key, int second) {
        Jedis jedis = jedisPool.getResource ();
        Long result = jedis.expire (key, second);
        jedis.close ();
        return result;
    }

    @Override
    public long ttl (String key) {
        Jedis jedis = jedisPool.getResource ();
        Long result = jedis.ttl (key);
        jedis.close ();
        return result;
    }

    @Override
    public long del (String key) {
        Jedis jedis = jedisPool.getResource ();
        Long result = jedis.del (key);
        jedis.close ();
        return result;
    }

    @Override
    public long hdel (String hkey, String key) {
        Jedis jedis = jedisPool.getResource ();
        Long result = jedis.hdel (hkey, key);
        jedis.close ();
        return result;
    }

    @Override
    public String set (String key, String value, int expire) {
        Jedis jedis = jedisPool.getResource ();
        String string = jedis.set (key, value);
        jedis.expire (key, expire);
        jedis.close ();
        return string;
    }

}
Cluster version

/ *
 * Copyright 2017 wenwuyi.cn All right reserved. This software is the
 * confidential and proprietary information of yagoosafe.com ("Confidential
 * Information "). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with wenwuyi.cn.
 * /
package com.yagoo.wificontrolsys.redis.impl;

import org.springframework.beans.factory.annotation.Autowired;

import redis.clients.jedis.JedisCluster;

import com.yagoo.wificontrolsys.redis.JedisClient;

/ **
 * Class name JedisClientCluster.java class
 * The role of the class: redis cluster version
 * @author YCJ Author E-mail: [email protected]
 * @date Created on: December 24, 2017 at 12:54:15 PM
 * @version 1.0
 * /
public class JedisClientCluster implements JedisClient {

    @Autowired
    private JedisCluster jedisCluster;

    @Override
    public String get (String key) {
        return jedisCluster.get (key);
    }

    @Override
    public String set (String key, String value) {
        return jedisCluster.set (key, value);
    }

    @Override
    public String hget (String hkey, String key) {
        return jedisCluster.hget (hkey, key);
    }

    @Override
    public long hset (String hkey, String key, String value) {
        return jedisCluster.hset (hkey, key, value);
    }

    @Override
    public long incr (String key) {
        return jedisCluster.incr (key);
    }

    @Override
    public long expire (String key, int second) {
        return jedisCluster.expire (key, second);
    }

    @Override
    public long ttl (String key) {
return jedisCluster.ttl (key);
     }

     @Override
     public long del (String key) {
         return jedisCluster.del (key);
     }

     @Override
     public long hdel (String hkey, String key) {
         return jedisCluster.hdel (hkey, key);
     }

     @Override
     public String set (String key, String value, int expire) {
         String string = jedisCluster.set (key, value);
         jedisCluster.expire (key, expire);
         return string;
     }

}
Detailed explanation of redis cluster or click version configuration in java web project

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.