先拋出來一個問題?
redis 本身有客戶端,爲什麼要對redis客戶端進行二次封裝?
大概在11年時侯,第一次接觸redis,那時侯研究過redis的各種數據結構,直接拿redis的客戶端jedis直接用。公司安排人要對jedis進行封裝,當時就很不理解,爲什麼非要封裝一次纔可以?
寫框架之後
後來自己寫框架,意識到一些東西是需要封裝的,比如連接的打開和釋放,比如一些危險的方法,比如keys * 比如flushdb 等
後來形成了這樣的代碼結構
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);//出錯時連接釋放
logger.error(this.getInfo() + SYMBOL.COLON + e.getMessage());
throw new CacheConnectionException(e.getMessage());
}
}
業務層會出現這樣的代碼
@Override
public Long addToSet(final KEY key,final Object value)throws CacheConnectionException {
//拋出connection exception異常提示業務方捕獲處理
return redisPool.execute(new Executor() {
@Override
public Long execute(ShardedJedis jedis) {
return jedis.sadd(key.key(),value.toString());
}
},key);
}
對連接的打開和釋放進行了封裝,避免業務端忘關鏈接而導致連接超時。拋出聲明式異常,提示業務端處理鏈接斷開的場景。
從業務使用上來講基本不會有什麼問題。
但這種結構存在幾個問題
業務端存在jedis代碼,如果想換jedis客戶端,成本很大
如果業務端想換另一種no sql 成本一樣很大。
監控,當業務足夠複雜時,對key的監控,問題排查和熱點隔離成爲痛點,如果讓業務端對每一個 redis的請求點都加代碼監控的話,這個成本依然很大。
一般公司業務都是從單體應用到分佈式應用,如果某臺機器報超時,對業務的key的監控就比較困難,比如
KEY爲USER:1 USER:2 ...USER.N 進行監控,如何識別這是一組KEY?
REDIS分佈式架構演進
所以對KEY的規範和統一監控就成爲痛點
意淫部分
- KEY的規範
模塊.業務類型:key id
redis KEY的定義
REDIS 相關類圖
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;
}
業務方必須統一按KEY類型讀寫redis,類型保護,保證KEY的規範一致。
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);
}
}
}
業務方可通過實現CacheMonitor 接口,對KEY進行統一監控
/**
* Created by harry on 2018/1/25.
*/
public class SparrowCacheMonitorimplements CacheMonitor{
@Override
public void monitor(Long startTime, Long endTime, KEY key) {
可以對module 或business維護對KEY進行監控
System.out.println("module-"+key.getModule()+" business.type-"+key.getBusiness()+" key-"+key.key()+" start.time-"+startTime+" end.time-"+endTime);
}
}
DEMO實例
/**
* @author by harry
*/
public class RedisTest {
public static void main(String[] args)throws CacheConnectionException {
Container container =new SparrowContainerImpl();
//定義模塊,一個業務會存在多個模塊
ModuleSupport OD=new ModuleSupport() {
@Override
public String code() {
return "01";
}
@Override
public String name() {
return "OD";
}
};
//相同模塊下會存在多個業務
KEY.Business od=new KEY.Business(OD,"POOL");
container.init();
CacheClient client = container.getBean("cacheClient");
//相同業務下存在多個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());
}
}
運行結果
容器初始化...
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
源碼下載