1. 程式人生 > >redis-cluster通用java介面

redis-cluster通用java介面

1.redis-cluster

2 redis-cluster介面

public interface IRedis<K,HK,HV> {
    void setHashObject(K key, Map<HK, HV> fieldValues, int expireSecond);

    void setHashObject(K key, Map<HK, HV> fieldValues);

    Map<HK, HV> getHashObject(K key, List<HK> fields);

    boolean expire(K key, int
expireSeconds); void setObject(K key, HV value, int expireSecond); void setObject(K key, HV value); HV getObject(K key); }

實現類:

@Component
public class RedisImpl<K, HK, HV> implements IRedis<K, HK, HV> {
    private static final int DEFAULT_EXPIRED_TIME=7*24*3600;
    @Resource
    private
RedisTemplate<K, HV> redisTemplate; public void setHashObject(K key, Map<HK, HV> fieldValues, int expireSecond) { HashOperations<K, HK, HV> valueOperate = redisTemplate.opsForHash(); if (!CollectionUtils.isEmpty(fieldValues)) { valueOperate.putAll(key, fieldValues); } expire(key, expireSecond); } @Override public
void setHashObject(K key, Map<HK, HV> fieldValues) { setHashObject(key,fieldValues, DEFAULT_EXPIRED_TIME); } public Map<HK, HV> getHashObject(K key, List<HK> fields) { HashOperations<K, HK, HV> valueOperate = redisTemplate.opsForHash(); Map<HK, HV> map = new HashMap<>(); if (!CollectionUtils.isEmpty(fields)) { for (HK field : fields) { HV obj = valueOperate.get(key, field); map.put(field, obj); } } return map; } public boolean expire(K key, int expireSeconds) { return redisTemplate.expire(key, expireSeconds, TimeUnit.SECONDS); } public void setObject(K key, HV value, int expireSecond) { ValueOperations<K, HV> valueOperate = redisTemplate.opsForValue(); valueOperate.set(key, value); redisTemplate.expire(key, expireSecond, TimeUnit.SECONDS); } @Override public void setObject(K key, HV value) { setObject(key,value,DEFAULT_EXPIRED_TIME); } public HV getObject(K key) { ValueOperations<K, HV> valueOperate = redisTemplate.opsForValue(); return valueOperate.get(key); } }

spring-bean配置檔案

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    #開啟@Component和@Service註解
    <context:component-scan base-package="com.cweeyii.**"/>
    #指定配置檔案的位置
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:redis.properties</value>
            </list>
        </property>
    </bean>
    #redis-cluster的具體配置
    #客戶端連結池配置
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}"/>
        <property name="maxTotal" value="${redis.maxTotal}"/>
        <property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>
        <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
        <property name="testOnReturn" value="${redis.testOnReturn}"/>
    </bean>
    #redis-cluster叢集地址
    <bean id="redisCluterConfig" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
        <constructor-arg index="0">
            <set>
                <value>${redis.host_and_port1}</value>
                <value>${redis.host_and_port2}</value>
                <value>${redis.host_and_port3}</value>
                <value>${redis.host_and_port4}</value>
                <value>${redis.host_and_port5}</value>
                <value>${redis.host_and_port6}</value>
            </set>
        </constructor-arg>
    </bean>
    #redis-client與redis-cluster的連結配置
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <constructor-arg index="0" ref="redisCluterConfig"/>
        <constructor-arg index="1" ref="poolConfig"/>
    </bean>
    #spring-data-redis包裝,指定key,value, hkey,hvaue序列化和反序列類
    <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
    <bean id="valueRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"/>
        <property name="keySerializer" ref="stringRedisSerializer"/>
        <property name="hashKeySerializer" ref="stringRedisSerializer"/>

        <property name="valueSerializer" ref="valueRedisSerializer"/>
        <property name="hashValueSerializer" ref="valueRedisSerializer"/>
    </bean>

redis相關配置

redis.host=192.168.31.88
redis.port=6379
redis.maxIdle=200
redis.maxTotal=1000
redis.maxWaitMillis=2000
redis.testOnBorrow=true
redis.testOnReturn=true

redis.host1=192.168.31.88
redis.port1=6379
redis.host2=192.168.31.88
redis.port2=6380
redis.host3=192.168.31.234
redis.port3=6379
redis.host4=192.168.31.234
redis.port4=6380
redis.host5=192.168.31.186
redis.port5=6379
redis.host6=192.168.31.186
redis.port6=6380

redis.host_and_port1=192.168.31.88:6379
redis.host_and_port2=192.168.31.88:6380
redis.host_and_port3=192.168.31.234:6379
redis.host_and_port4=192.168.31.234:6380
redis.host_and_port5=192.168.31.186:6379
redis.host_and_port6=192.168.31.186:6380

spring測試程式

@Ignore
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:applicationContext-redis.xml"})
public class BaseTest {
    private static final Logger LOGGER = LoggerFactory.getLogger(BaseTest.class);

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        LOGGER.info("run setUpBeforeClass");
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        LOGGER.info("run tearDownAfterClass");
    }

    @Test
    public void doNothing() {
        LOGGER.info("run doNothing");
    }
}
public class RedisClientTest extends BaseTest {
    @Resource
    private IRedis iRedis;

    @Test
    public void testSetObject() {
        for (int i = 0; i < 1000; i++) {
            String key = "firstKey" + i;
            String value = "firstValue" + i;
            iRedis.setObject(key, value, DateUtils.getRandomExpireDays(21, 7) * 24 * 3600);
        }

        for (int i = 0; i < 10; i++) {
            String key = "firstKey" + i;
            String value = (String) iRedis.getObject(key);
            System.out.println(value);
        }
    }

    @Test
    public void testHash() {
        Map<String, Integer> multiValueMap = new HashMap<>();
        for (int i = 0; i < 1000; i++) {
            String hkey = "firstKey" + i;
            multiValueMap.put(hkey, i);
        }
        String key = "key";
        iRedis.setHashObject(key, multiValueMap);
        List<String> hKeyList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            hKeyList.add("firstKey" + i);
        }
        Map<String, Integer> valueMap = iRedis.getHashObject(key, hKeyList);
        System.out.print(valueMap);
    }
}

輸出結果
setObject

firstValue0
firstValue1
firstValue2
firstValue3
firstValue4
firstValue5
firstValue6
firstValue7
firstValue8
firstValue9

setHashObject

{firstKey8=8, firstKey7=7, firstKey9=9, firstKey0=0, firstKey2=2, firstKey1=1, firstKey4=4, firstKey3=3, firstKey6=6, firstKey5=5}

相關推薦

redis-cluster通用java介面

1.redis-cluster 2 redis-cluster介面 public interface IRedis<K,HK,HV> { void setHashObject(K key, Map<HK, HV>

虛擬機搭建redis單機版及redis-cluster,使用redis desktop manager和java(eclipse)連接redis過程遇到問題匯總

init clu centos 一律 有用 tex 保護模式 bin service 如果你看到這裏,我默認你已經安裝好了redis,並且已經成功的在虛擬機的Linux系統中ping通。 介紹一下我的環境:VMware虛擬機安裝centos 6.5版的Linux系統,red

redis的哨兵和clusterjava程式碼中的配置

1.哨兵: applicationContext.xml: <!-- redis sentinel 配置 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig

java連線redis-cluster叢集方式

轉載自:http://blog.csdn.net/u013322876/article/details/50595833 redis相關網站: 作業系統:centos 6.3 redis版本:3.0.6 java客戶端版本: jedis 2.7.2 redi

關於 Redis Cluster 模式下獲取 Java Client 的問題

Redis連線池對於單機模式來說,官網有提供個JedisPool工具類,用起來也比較方便 但是對於Redis叢集模式下,官網沒有提供連線池的工具類,於是就需要我們自己來實現類似的功能了,我這裡採用的是單例模式的方法來解決高併發的場景。 單機模式

redis cluster模式key的模糊刪除-java操作

不管是redis單節點還是redis cluster模式都是不支援模糊刪除的。當然你也可以自己去寫shell指令碼去刪除。這裡我自己想的一種方式,採用多執行緒操作叢集下redis單節點,countdownlatch統計彙總刪除。 上程式碼: 執行緒池: public cl

java介面連線redis

在搭建完redis之後,那麼我們是要呼叫這個java介面來連線這個redis 那麼是如何的進行連線呢?? 直接的使用redis的建構函式就是可以連線的了, public class JedisT

redis-cluster的安裝管理

redis-cluster redis redis集群部署 redis-cluster的安裝管理 聲明:本文只允許用於個人學習交流使用,如有錯誤之處請多多指正。文檔版本:Version 1.0修改記錄:2015-10-30環境介紹系統環境:RedHat Enterprise Linux Serve

redis 之 使用java操作redis

main print 數據庫 cli 防火墻 images enc png red 1. 在java操作redis需要使用jedis插件,並且linux要開啟相關的防火墻。 重啟防火墻服務 : 2. 新建maven項目: 3.添加項目依賴: <dependenc

在 Windows 上測試 Redis Cluster的集群填坑筆記

san hat aix gdb ima erl omx ngs isa %E8%AE%A1%E7%AE%97%E6%9C%BA%E7%A8%8B%E5%BA%8F%E7%9A%84%E6%80%9D%E7%BB%B4%E9%80%BB%E8%BE%91%2010%20-%2

第十三章 redis-cluster原理

執行命令 shm 擴容 一段時間 本地 集群 端口號 保存 ron 一、基本定義 虛擬槽slot分區算法,優點是擴容縮容簡單:直接把slot及每個slot上的數據進行縮放即可 redis定義了0-16383(總共為16384個slot,即214個slot) slot會均勻

redis cluster 實踐總結

監聽 截至目前 實踐 啟動 但是 -- 是什麽 size 發現 最近項目接觸到了redis cluster,現在趁著使用做一下總結,記錄一下遇到過的問題,簡單的概述一下常用到的命令和功能。 本篇文章主要是以運維的角度去講述如何去更好的規劃redis cluster和跳坑

JFinal redis cluster集群插件

ext 註意 param nal system static private rim spa JFinal redis cluster集群插件 JFinal 框架到了2.1版本號,可是依舊僅僅支持redis的主從集群,沒有看到Cluster集群的插件。筆者

redis cluster

增加 對數 沒有 端口號 cover clas 刪除 org actor redis cluster 集群命令 註 :這些命令是集群所獨有的。執行下述命令要先登錄(集群已經創建 )集群配置文件:需註意 cluster-migration-barrier 1 clu

predis連接redis sentinel和redis cluster

predis的使用 predis連redis cluster predis連redis sentinel 開發之前都是用phpredis連接redis服務的,後來隨著sentinel和redis cluster的成熟,redis主從都結合sentinel做了高可用,部分數據和並發大的業務使

多節點 安裝redis cluster安裝部署-4.0.1

redis cluster 4.0 安裝、配置 環境節點數量IP:172.17.7.11 CPU :12 核 MEM:96G 啟動服務數量:6 使用端口:7001~12IP:172.17.7.25 CPU :12 核 MEM:96G 啟動服務數量:6 使用端口:70

Windows 配置Reids集群 Redis Cluster

下載 com all 不支持 支持 由於 ble ech 功能 1. 下載安裝Redis Redis官方不支持Windows,但是Microsoft Open Tech group在 GitHub上開發了一個Win64的版本,下載地址為: 下載Redis 啟動服

Redis Cluster 添加/刪除 完整折騰步驟

slot redis clust Redis還是挺好玩的,今天測試了集群的添加、刪除節點、重分配slot等。更深入的理解redis的遊戲規則。步驟繁多,但是詳細。環境解釋:我是在一臺Centos 6.9上測試的,各個redis節點以端口號區分。文中針對各個redis,我只是以端口號代表。~~~~M

Redis Cluster 4.0 on CentOS 6.9 搭建

combine zip config 失效 對應關系 誰的 direct iss 新節點 集群簡介 Redis 集群是一個可以在多個 Redis 節點之間進行數據共享的設施(installation)。 Redis 集群不支持那些需要同時處理多個鍵的 Redis 命令, 因

Ubuntu 16.04下Redis Cluster集群搭建(官方原始方案)

選擇 正數 mil 請求 點數據 包含 最終 util 交互 前提:先安裝好Redis,參考:http://www.cnblogs.com/EasonJim/p/7599941.html 說明:Redis Cluster集群模式可以做到動態增加節點和下線節點,使用起來非常