先拋出來一個(gè)問題其垄?
redis 本身有客戶端扔役,為什么要對(duì)redis客戶端進(jìn)行二次封裝权纤?
大概在11年時(shí)侯社证,第一次接觸redis,那時(shí)侯研究過redis的各種數(shù)據(jù)結(jié)構(gòu)逼龟,直接拿redis的客戶端jedis直接用。公司安排人要對(duì)jedis進(jìn)行封裝追葡,當(dāng)時(shí)就很不理解腺律,為什么非要封裝一次才可以?
寫框架之后
后來自己寫框架宜肉,意識(shí)到一些東西是需要封裝的匀钧,比如連接的打開和釋放,比如一些危險(xiǎn)的方法谬返,比如keys * 比如flushdb 等
后來形成了這樣的代碼結(jié)構(gòu)
T execute(Executor executor, KEY key)throws CacheConnectionException {
ShardedJedis jedis =null;
try {
Long startTime = System.currentTimeMillis();
jedis =this.pool.getResource();.//連接的獲取
T result = executor.execute(jedis);
this.pool.returnResource(jedis);//連接的釋放
Long endTime = System.currentTimeMillis();
if(this.cacheMonitor!=null) {
this.cacheMonitor.monitor(startTime, endTime, key);
}
return result;
}catch (JedisConnectionException e) {
this.pool.returnBrokenResource(jedis);//出錯(cuò)時(shí)連接釋放
logger.error(this.getInfo() + SYMBOL.COLON + e.getMessage());
throw new CacheConnectionException(e.getMessage());
}
}
業(yè)務(wù)層會(huì)出現(xiàn)這樣的代碼
@Override
public Long addToSet(final KEY key,final Object value)throws CacheConnectionException {
//拋出connection exception異常提示業(yè)務(wù)方捕獲處理
return redisPool.execute(new Executor() {
@Override
public Long execute(ShardedJedis jedis) {
return jedis.sadd(key.key(),value.toString());
}
},key);
}
對(duì)連接的打開和釋放進(jìn)行了封裝榴捡,避免業(yè)務(wù)端忘關(guān)鏈接而導(dǎo)致連接超時(shí)。拋出聲明式異常朱浴,提示業(yè)務(wù)端處理鏈接斷開的場(chǎng)景。
從業(yè)務(wù)使用上來講基本不會(huì)有什么問題达椰。
但這種結(jié)構(gòu)存在幾個(gè)問題
業(yè)務(wù)端存在jedis代碼翰蠢,如果想換jedis客戶端,成本很大
如果業(yè)務(wù)端想換另一種no sql 成本一樣很大啰劲。
監(jiān)控梁沧,當(dāng)業(yè)務(wù)足夠復(fù)雜時(shí),對(duì)key的監(jiān)控蝇裤,問題排查和熱點(diǎn)隔離成為痛點(diǎn)廷支,如果讓業(yè)務(wù)端對(duì)每一個(gè) redis的請(qǐng)求點(diǎn)都加代碼監(jiān)控的話频鉴,這個(gè)成本依然很大。
一般公司業(yè)務(wù)都是從單體應(yīng)用到分布式應(yīng)用,如果某臺(tái)機(jī)器報(bào)超時(shí)恋拍,對(duì)業(yè)務(wù)的key的監(jiān)控就比較困難,比如
KEY為USER:1 USER:2 ...USER.N 進(jìn)行監(jiān)控垛孔,如何識(shí)別這是一組KEY?
REDIS分布式架構(gòu)演進(jìn)
所以對(duì)KEY的規(guī)范和統(tǒng)一監(jiān)控就成為痛點(diǎn)
意淫部分
- KEY的規(guī)范
模塊.業(yè)務(wù)類型:key id
redis KEY的定義
REDIS 相關(guān)類圖
REDIS 操作接口定義
package com.sparrow.cache;
import com.sparrow.constant.cache.KEY;
import com.sparrow.exception.CacheConnectionException;
import com.sparrow.support.Entity;
import java.util.List;
import java.util.Map;
/**
* @author harry
* @date 2018/1/18
*/
public interface CacheClient {
Map hashGetAll(KEYkey)throws CacheConnectionException;
Map hashGetAll(KEYkey,Class keyClazz, Class dataClazz)throws CacheConnectionException;
Long getHashSize(KEYkey)throws CacheConnectionException;
Long getSetSize(KEYkey)throws CacheConnectionException;
Long removeFromOrderSet(KEYkey, Long from, Long to)throws CacheConnectionException;
Double getScore(KEYkey, Object value)throws CacheConnectionException;
Long getIndexOfOrderSet(KEYkey, Object value)throws CacheConnectionException;
Map getAllWithScore(KEYkey, Class clazz)throws CacheConnectionException;
Long getListSize(KEYkey)throws CacheConnectionException;
String hashGet(KEYkey, String hashKey)throws CacheConnectionException;
T hashGet(KEYkey, String hashKey, Class clazz)throws CacheConnectionException;
Long hashSet(KEYkey, String hashKey, Object value)throws CacheConnectionException;
//order set
Long getOrderSetSize(KEYkey)throws CacheConnectionException;
Long addToSet(KEYkey, Object value)throws CacheConnectionException;
Long addToSet(KEYkey, String... value)throws CacheConnectionException;
Integer addToSet(KEYkey, Iterable values)throws CacheConnectionException;
Long addToList(KEYkey, Object value)throws CacheConnectionException;
Long removeFromList(KEYkey, Object value)throws CacheConnectionException;
Long removeFromSet(KEYkey, Object value)throws CacheConnectionException;
Long addToOrderSet(KEYkey, Object value, Long score)throws CacheConnectionException;
Long removeFromOrderSet(KEYkey, Object value)throws CacheConnectionException;
Boolean existInSet(KEYkey, Object value)throws CacheConnectionException;
Long addToList(KEYkey, String... value)throws CacheConnectionException;
Integer addToList(KEYkey, Iterable values)throws CacheConnectionException;
Long expire(KEYkey, Integer expire)throws CacheConnectionException;
Long delete(KEYkey)throws CacheConnectionException;
Long expireAt(KEYkey, Long expire)throws CacheConnectionException;
String setExpire(KEYkey, Integer seconds, Object value)throws CacheConnectionException;
String set(KEYkey, Entity value)throws CacheConnectionException;
String set(KEYkey, Object value)throws CacheConnectionException;
String get(KEYkey)throws CacheConnectionException;
T get(KEYkey, Class clazz)throws CacheConnectionException;
List getAllOfList(KEYkey)throws CacheConnectionException;
List getAllOfList(KEYkey, Class clazz)throws CacheConnectionException;
Long setIfNotExist(KEYkey, Object value)throws CacheConnectionException;
Long append(KEYkey, Object value)throws CacheConnectionException;
Long decrease(KEYkey)throws CacheConnectionException;
Long decrease(KEYkey, Long count)throws CacheConnectionException;
Long increase(KEYkey, Long count)throws CacheConnectionException;
Long increase(KEYkey)throws CacheConnectionException;
boolean bit(KEYkey, Integer offset)throws CacheConnectionException;
}
業(yè)務(wù)方必須統(tǒng)一按KEY類型讀寫redis,類型保護(hù)施敢,保證KEY的規(guī)范一致周荐。
KEY的定義
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.sparrow.constant.cache;
import com.sparrow.constant.magic.SYMBOL;
import com.sparrow.core.Pair;
import com.sparrow.support.ModuleSupport;
import com.sparrow.utility.StringUtility;
import java.util.Arrays;
/**
* Created by harry on 2018/1/8.
*/
public class KEY {
private Stringbusiness;
private ObjectbusinessId;
private Stringmodule;
private KEY(){}
private KEY(Builder builder) {
this.business = builder.business.getKey();
this.module=builder.business.getModule();
if (builder.businessId !=null) {
this.businessId = StringUtility.join(Arrays.asList(builder.businessId), SYMBOL.DOT);
}
}
public static KEY parse(String key){
if(StringUtility.isNullOrEmpty(key)){
return null;
}
KEY k=new KEY();
Pair businessWithId=Pair.split(key,SYMBOL.COLON);
k.businessId=businessWithId.getSecond();
String[] businessArray=businessWithId.getFirst().split("\\.");
k.module=businessArray[0];
k.business=businessWithId.getFirst();
return k;
}
public String key() {
if (StringUtility.isNullOrEmpty(this.businessId)) {
return this.business;
}
return this.business + SYMBOL.COLON +this.businessId;
}
public String getBusiness() {
return business;
}
public String getModule() {
return module;
}
public static class Business {
private Stringmodule;
private Stringkey;
public Business(ModuleSupport module, String... business) {
this.module = module.name();
this.key =this.module;
if (business !=null && business.length >0) {
this.key += SYMBOL.DOT + StringUtility.join(Arrays.asList(business), SYMBOL.DOT);
}
}
public String getKey() {
return key;
}
public String getModule() {
return module;
}
}
public static class Builder {
private Businessbusiness;
private Object[]businessId;
public Builder business(Business business) {
this.business = business;
return this;
}
public Builder businessId(Object... businessId) {
this.businessId = businessId;
return this;
}
public KEY build() {
return new KEY(this);
}
}
}
業(yè)務(wù)方可通過實(shí)現(xiàn)CacheMonitor 接口,對(duì)KEY進(jìn)行統(tǒng)一監(jiān)控
/**
* Created by harry on 2018/1/25.
*/
public class SparrowCacheMonitorimplements CacheMonitor{
@Override
public void monitor(Long startTime, Long endTime, KEY key) {
可以對(duì)module 或business維護(hù)對(duì)KEY進(jìn)行監(jiān)控
System.out.println("module-"+key.getModule()+" business.type-"+key.getBusiness()+" key-"+key.key()+" start.time-"+startTime+" end.time-"+endTime);
}
}
DEMO實(shí)例
/**
* @author by harry
*/
public class RedisTest {
public static void main(String[] args)throws CacheConnectionException {
Container container =new SparrowContainerImpl();
//定義模塊僵娃,一個(gè)業(yè)務(wù)會(huì)存在多個(gè)模塊
ModuleSupport OD=new ModuleSupport() {
@Override
public String code() {
return "01";
}
@Override
public String name() {
return "OD";
}
};
//相同模塊下會(huì)存在多個(gè)業(yè)務(wù)
KEY.Business od=new KEY.Business(OD,"POOL");
container.init();
CacheClient client = container.getBean("cacheClient");
//相同業(yè)務(wù)下存在多個(gè)KEY
KEY key =new KEY.Builder().business(od).businessId("BJS","CHI","HU").build();
client.set(key,"test");
KEY k2=KEY.parse("OD.POOL:BJS.CHI.HU");
System.out.println("key:"+k2.key()+",module:"+k2.getModule()+" business:"+k2.getBusiness());
}
}
運(yùn)行結(jié)果
容器初始化...
module-OD business.type-OD.POOL key-OD.POOL:BJS.CHI.HU start.time-1516877958682 end.time-1516877958714
key:OD.POOL:BJS.CHI.HU,module:OD business:OD.POOL
源碼下載