闃块噷宸村反寰湇鍔℃灦鏋勭殑鍥涘ぇ閲戝垰鍒╁櫒
Posted 寮€鍙戣€呮妧鏈墠绾?/a>
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了闃块噷宸村反寰湇鍔℃灦鏋勭殑鍥涘ぇ閲戝垰鍒╁櫒相关的知识,希望对你有一定的参考价值。
鐐瑰嚮鈥?/span>寮€鍙戣€呮妧鏈墠绾?/span>鈥濓紝閫夋嫨鈥滄槦鏍囸煍濃€?/span>
璁╀竴閮ㄥ垎寮€鍙戣€呯湅鍒版湭鏉?/span>
鏉ヨ嚜锛氶樋閲屽反宸翠腑闂翠欢 Photo @Christopher Campbell
鏂?nbsp;| 瀛斿嚒鍕?/span>
姒傝堪
redis.extend.hostName=127.0.0.1
redis.extend.port=6379
redis.extend.password=pwdcode
redis.extend.timeout=10000
redis.idempotent.enabled=true
缂撳瓨
public String get(String key);
/**
* 鑾峰彇鎸囧畾鐨刱ey瀵瑰簲鐨勫璞?寮傚父涔熶細杩斿洖null
*
* @param key
* @param clazz
* @return
*/
public <T> T get(String key, Class<T> clz);
/**
* 瀛樺偍缂撳瓨鏁版嵁,蹇界暐杩囨湡鏃堕棿
*
* @param key
* @param value
* @return
*/
public <T extends Serializable> booleanput(String key, T value);
/**
* 瀛樺偍缂撳瓨鏁版嵁
*
* @param key
* @param value
* @param expiredTime
* @param unit
* @return
*/
public <T extends Serializable> booleanput(String key, T value, int expiredTime, TimeUnit unit);
/**
* 鍩轰簬key鍒犻櫎缂撳瓨鏁版嵁
*
* @param key
* @return
*/
publicbooleaninvalid(String key);
闄愭祦
/**
* 鎸囧畾杩囨湡鏃堕棿鑷璁℃暟鍣紝榛樿姣忔+1锛岄潪婊戝姩绐楀彛
*
* @param key 璁℃暟鍣ㄨ嚜澧瀔ey
* @param expireTime 杩囨湡鏃堕棿
* @param unit 鏃堕棿鍗曚綅
* @return
*/
publiclongincrCount(String key, int expireTime, TimeUnit unit);
/**
* 鎸囧畾杩囨湡鏃堕棿鑷璁℃暟鍣?鍗曚綅鏃堕棿鍐呰秴杩囨渶澶у€紃ateThreshold杩斿洖true锛屽惁鍒欒繑鍥瀎alse
*
* @param key 闄愭祦key
* @param rateThreshold 闄愭祦闃堝€?br> * @param expireTime 鍥哄畾绐楀彛鏃堕棿
* @param unit 鏃堕棿鍗曚綅
* @return
*/
publicbooleanrateLimit(final String key, finalint rateThreshold, int expireTime, TimeUnit unit);
/**
* @param limitKey 闄愭祦KEY
* @param resultSupplier 鍥炶皟鏂规硶
* @param rateThreshold 闄愭祦闃堝€?br> * @param limitTime 闄愬埗鏃堕棿娈?br> * @param blockDuration 闃诲鏃堕棿娈?br> * @param unit 鏃堕棿鍗曚綅
* @param errCodeEnum 鎸囧畾闄愭祦閿欒鐮?br> * @return
*/
public <T> T execute(String limitKey, Supplier<T> resultSupplier, long rateThreshold, long limitTime,
long blockDuration, TimeUnit unit, ErrCodeEnum errCodeEnum){
boolean blocked = tryAcquire(limitKey, rateThreshold, limitTime, blockDuration, unit);
if (errCodeEnum != null) {
AssertUtils.assertTrue(blocked, errCodeEnum);
} else {
AssertUtils.assertTrue(blocked, ExceptionEnumType.ACQUIRE_LOCK_FAIL);
}
return resultSupplier.get();
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface RateLimit {
/**
* 闄愭祦KEY
*/
String limitKey();
/**
* 鍏佽璁块棶鐨勬鏁帮紝榛樿鍊糓AX_VALUE
*/
longlimitCount()default Long.MAX_VALUE;
/**
* 鏃堕棿娈?br> */
longtimeRange();
/**
* 闃诲鏃堕棿娈?br> */
longblockDuration();
/**
* 鏃堕棿鍗曚綅锛岄粯璁や负绉?br> */
TimeUnit timeUnit()default TimeUnit.SECONDS;
}
@RateLimit(limitKey = "#key", limitCount = 5, timeRange = 2, blockDuration = 3, timeUnit = TimeUnit.MINUTES)
public String testLimit2(String key){
..........
return key;
}
鍒嗗竷寮忛攣
浜掓枼鎬?鍚屾湰鍦伴攣涓€鏍峰叿鏈変簰鏂ユ€э紝浣嗘槸鍒嗗竷寮忛攣闇€瑕佷繚璇佸湪涓嶅悓鑺傜偣杩涚▼鐨勪笉鍚岀嚎绋嬬殑浜掓枼銆?/span>
鍙噸鍏ユ€?鍚屼竴涓妭鐐逛笂鐨勫悓涓€涓嚎绋嬪鏋滆幏鍙栦簡閿佷箣鍚庨偅涔堜篃鍙互鍐嶆鑾峰彇杩欎釜閿併€?/span>
閿佽秴鏃?鍜屾湰鍦伴攣涓€鏍锋敮鎸侀攣瓒呮椂锛岄槻姝㈡閿侊紝閫氳繃寮傛蹇冭烦 demon 绾跨▼鍒锋柊杩囨湡鏃堕棿锛岄槻姝㈢壒娈婂満鏅?濡?FGC 姝婚攣瓒呮椂)涓嬫閿併€?/span>
楂樻€ц兘銆侀珮鍙敤:鍔犻攣鍜岃В閿侀渶瑕侀珮鎬ц兘锛屽悓鏃朵篃闇€瑕佷繚璇侀珮鍙敤闃叉鍒嗗竷寮忛攣澶辨晥锛屽彲浠ュ鍔犻檷绾с€?/span>
鏀寔闃诲鍜岄潪闃诲:鍚?ReentrantLock 涓€鏍锋敮鎸?lock 鍜?trylock 浠ュ強 tryLock ( long timeOut )銆?/span>
鍏钩閿佸拰闈炲叕骞抽攣(涓嶆敮鎸?:鍏钩閿佹槸鎸夌収璇锋眰鍔犻攣鐨勯『搴忚幏寰楅攣锛岄潪鍏钩閿佸氨鐩稿弽鏄棤搴忕殑锛岀洰鍓?distributed-tools 缁勪欢鎻愪緵鐨勫垎甯冨紡閿佷笉鏀寔璇ョ壒鎬с€?/span>
/**
* 鍒嗗竷寮忛攣澶勭悊妯℃澘鎵ц鍣?br> *
* @param lockKey 鍒嗗竷寮忛攣key
* @param resultSupplier 鍒嗗竷寮忛攣澶勭悊鍥炶皟
* @param waitTime 閿佺瓑寰呮椂闂?br> * @param unit 鏃堕棿鍗曚綅
* @param errCodeEnum 鎸囧畾鐗规畩閿欒鐮佽繑鍥?br> * @return
*/
public static <T> T execute(String lockKey, Supplier<T> resultSupplier, long waitTime, TimeUnit unit,
ErrCodeEnum errCodeEnum){
AssertUtils.assertTrue(StringUtils.isNotBlank(lockKey), ExceptionEnumType.PARAMETER_ILLEGALL);
boolean locked = false;
Lock lock = DistributedReentrantLock.newLock(lockKey);
try {
locked = waitTime > 0 ? lock.tryLock(waitTime, unit) : lock.tryLock();
} catch (InterruptedException e) {
throw new RuntimeException(String.format("lock error,lockResource:%s", lockKey), e);
}
if (errCodeEnum != null) {
AssertUtils.assertTrue(locked, errCodeEnum);
} else {
AssertUtils.assertTrue(locked, ExceptionEnumType.ACQUIRE_LOCK_FAIL);
}
try {
return resultSupplier.get();
} finally {
lock.unlock();
}
}
骞傜瓑
1. 绌洪棿缁村害涓婄殑骞傜瓑锛屽嵆骞傜瓑瀵硅薄鐨勮寖鍥达紝鏄釜浜鸿繕鏄満鏋勶紝鏄煇涓€娆′氦鏄撹繕鏄煇绉嶇被鍨嬬殑浜ゆ槗銆?/span>
2. 鏃堕棿缁村害涓婄殑骞傜瓑锛屽嵆骞傜瓑鐨勪繚璇佹椂闂达紝鏄嚑涓皬鏃躲€佸嚑澶╄繕鏄案涔呮€х殑銆?/span>
1. 鍓嶇閲嶅鎻愪氦璇锋眰锛屼笖璇锋眰鏁版嵁鐩稿悓鏃讹紝鍚庡彴闇€瑕佽繑鍥炲搴旇繖涓姹傜殑鐩稿悓缁撴灉銆?/span>
2. 鍙戣捣涓€娆℃敮浠樿姹傦紝鏀粯涓績搴旇鍙墸鐢ㄦ埛璐︽埛涓€娆¢挶锛屽綋閬囧埌缃戠粶涓柇鎴栫郴缁熷紓甯告椂锛屼篃搴旇鍙墸涓€娆¢挶銆?/span>
3. 鍙戦€佹秷鎭紝鍚屾牱鍐呭鐨勭煭淇″彂缁欑敤鎴峰彧鍙戜竴娆°€?/span>
4. 鍒涘缓涓氬姟璁㈠崟锛屼竴娆′笟鍔¤姹傚彧鑳藉垱寤轰竴涓紝閲嶈瘯璇锋眰鍒涘缓澶氫釜灏变細鍑哄ぇ闂銆?/span>
5. 鍩轰簬 msgId 鐨勬秷鎭箓绛夊鐞嗐€?/span>
骞傜瓑 key 鎻愬彇鑳藉姏锛氳幏鍙栧敮涓€骞傜瓑 key
鍒嗗竷寮忛攣鏈嶅姟鑳藉姏锛氭彁渚涘叏灞€鍔犻攣銆佽В閿佺殑鑳藉姏
distributed-tools 骞傜瓑缁勪欢闇€瑕佷娇鐢ㄨ嚜韬彁渚涚殑鍒嗗竷寮忛攣鍔熻兘锛屼繚璇佸叾骞跺彂鍞竴鎬э紝 distributed-tools 鎻愪緵鐨勫垎甯冨紡閿佽兘澶熸彁渚涘叾鍙潬銆佺ǔ瀹氱殑鍔犻攣銆佽В閿佽兘鍔涖€?/span>
楂樻€ц兘鐨勫啓鍏ャ€佹煡璇㈣兘鍔涳細閽堝骞傜瓑缁撴灉鏌ヨ涓庡瓨鍌?/span>
distributed-tools 骞傜瓑缁勪欢鎻愪緵浜嗗熀浜?tair 銆?redis 鐨勫瓨鍌ㄥ疄鐜帮紝鍚屾椂鏀寔鑷畾涔変竴绾с€佷簩绾у瓨鍌ㄩ€氳繃 spring 渚濊禆娉ㄥ叆鍒?IdempotentService 锛屽缓璁?distributed-tools 骞傜瓑瀛樺偍缁撴灉涓€绾у瓨鍌?tair mdb 锛屼簩绾у瓨鍌╨db鎴栬€?tablestore 锛屼竴绾у瓨鍌ㄤ繚璇佸叾楂樻€ц兘锛屼簩绾у瓨鍌ㄤ繚璇佸叾鍙潬鎬с€?/span>
浜岀骇瀛樺偍骞惰寮傛鍐欏叆锛岃繘涓€姝ユ彁楂樻€ц兘銆?/span>
distributed-tools 骞傜瓑缁勪欢鏀寔浜岀骇瀛樺偍锛屼负浜嗕繚璇佸叾楂樺彲鐢紝姣曠珶浜岀骇瀛樺偍鍑虹幇鏁呴殰鐨勬鐜囧お浣庯紝涓嶄細瀵艰嚧涓氬姟涓婁笉鍙敤锛屽鏋滀簩绾у瓨鍌ㄥ悓鏃跺嚭鐜版晠闅滐紝涓氬姟涓婂仛浜嗕竴瀹氱殑瀹归敊锛岄拡瀵逛笉纭畾鎬х殑寮傚父閲囧彇閲嶈瘯绛栫暐锛屼細鎵ц鍏蜂綋骞傜瓑鏂规硶銆?/span>
-
棣栧厛鍒ゆ柇 Idempotent 鐨?spelKey 鐨勫睘鎬ф槸鍚︿负绌猴紝濡傛灉涓嶄负绌轰細鏍规嵁 spelKey 瀹氫箟鐨?spring 琛ㄨ揪寮忕敓鎴愬箓绛?ID 銆?/span> -
鍏舵鍒ゆ柇鍙傛暟鏄惁鍖呭惈 IdempotentTxId 娉ㄨВ锛屽鏋滄湁 IdempotentTxId 锛屼細鐩存帴鑾峰彇鍙傛暟鍊肩敓鎴愬箓绛?ID 銆?/span> -
鍐嶆閫氳繃鍙嶅皠鑾峰彇鍙傛暟瀵硅薄灞炴€ф槸鍚﹀寘鍚?IdempotentTxId 娉ㄨВ锛屽鏋滃璞″睘鎬у寘鍚?IdempotentTxId 娉ㄨВ浼氳幏鍙栬鍙傛暟瀵硅薄灞炴€х敓鎴愬箓绛?ID 銆?/span> -
鏈€鍚庝互涓婁笁绉嶆儏鍐典粛鏈幏鍙栧埌骞傜瓑 ID 锛屼細杩涗竴姝ラ€氳繃鍙嶅皠鑾峰彇鍙傛暟瀵硅薄鐨?Method 鏄惁瀹氫箟 IdempotentTxIdGetter 娉ㄨВ锛屽鏋滃寘鍚娉ㄨВ鍒欓€氳繃鍙嶅皠鐢熸垚骞傜瓑 ID 銆?/span>
@Idempotent(spelKey = "#request.requestId", firstLevelExpireDate = 7锛宻econdLevelExpireDate = 30)
publicvoidexecute(BizFlowRequest request){
..................
}
/**
* 骞傜瓑妯℃澘澶勭悊鍣?br> *
* @param request 骞傜瓑Request淇℃伅
* @param executeSupplier 骞傜瓑澶勭悊鍥炶皟function
* @param resultPreprocessConsumer 骞傜瓑缁撴灉鍥炶皟function 鍙互瀵圭粨鏋滃仛浜涢澶勭悊
* @param ifResultNeedIdempotence 闄や簡鏍规嵁寮傚父杩橀渶瑕佹牴鎹粨鏋滃垽瀹氭槸鍚﹂渶瑕佸箓绛夋€х殑鍦烘櫙鍙互鎻愪緵姝ゅ弬鏁?br> * @return
*/
public R execute(IdempotentRequest<P> request, Supplier<R> executeSupplier,
Consumer<IdempotentResult<P, R>> resultPreprocessConsumer, Predicate<R> ifResultNeedIdempotence){
........
}
骞傜瓑鍙傛暟 IdempotentRequest 缁勮锛屽彲浠ヨ缃箓绛夊弬鏁板拰骞傜瓑鍞竴 ID 銆?/span>
鍏蜂綋骞傜瓑鐨勬柟娉曢€昏緫锛屾瘮濡傞拡瀵规敮浠樸€佷笅鍗曟帴鍙o紝鍙互閫氳繃 JDK8 鍑芥暟寮忔帴鍙?Supplier Callback 杩涜澶勭悊銆?/span>
骞傜瓑杩斿洖缁撴灉鐨勫鐞嗭紝璇ュ弬鏁板彲浠ヤ负绌猴紝濡傛灉涓虹┖閲囧彇榛樿鐨勫鐞嗭紝鏍规嵁骞傜瓑缁撴灉锛屽鏋滄垚鍔熴€佷笉鍙噸璇曠殑寮傚父閿欒鐮侊紝鐩存帴杩斿洖缁撴灉锛屽鏋滃け璐ュ彲閲嶈瘯寮傚父閿欒鐮侊紝浼氳繘琛岄噸璇曞鐞嗐€?/span>
浣滆€呬俊鎭細
瀛斿嚒鍕囷紝鑺卞悕浜戠媱锛岄樋閲屼簯-寮€鏀惧钩鍙伴珮绾ф妧鏈锛屽楂樺苟鍙戙€侀珮鎬ц兘銆侀珮鍙敤銆佸彲浼哥缉鐨勫垎甯冨紡绯荤粺鏋舵瀯璁捐鏈変赴瀵岀粡楠岋紝Cloud Native鍧氬畾鎷ユ姢鑰咃紝鍧氬畧寮€鍙戜竴绾挎墦纾ㄥ尃鑹虹殑鏋舵瀯甯堛€?/span>
绂忓埄鏃堕棿锛?/span>
鍦ㄨ繖閲岋紝鎴戜负澶у鍑嗗浜嗕竴浠?020骞存渶鏂版渶鍏ㄧ殑銆奐ava闈㈣瘯棰樺強绛旀V3.0銆嬶紝杩欏鐢靛瓙涔︽兜鐩栦簡璇稿鍚庣鎶€鏈爤鐨勯潰璇曢鍜岀瓟妗堬紝鐩镐俊鍙互甯姪澶у鍦ㄦ渶鐭殑鏃堕棿鍐呭涔燡ava鍚庣鐨勫ぇ澶氭暟闈㈣瘯棰橈紝浠庤€屾嬁鍒拌嚜宸卞績浠殑offer銆?/p>
鎵弿涓嬫柟浜岀淮鐮?/span>
鍦ㄤ笅闈㈢殑浜岀淮鍚庡彴鍥炲鍏抽敭璇嶏細Java鏍稿績鏁寸悊
鍚庡彴鍥炲鈥滅數瀛愪功鈥?/span> 鈥滆祫鏂欌€?/span> 棰嗗彇涓€浠藉共璐э紝鏁扮櫨鎶€鏈數瀛愪功绛変綘 寮€鍙戣€呮妧鏈墠绾?锛屾眹闆嗘妧鏈墠绾垮揩璁拰鍏虫敞琛屼笟瓒嬪娍锛屽ぇ鍘傚共璐э紝鏄紑鍙戣€呯粡鍘嗗拰鎴愰暱鐨勪紭绉€鎸囧崡銆?/span>
以上是关于闃块噷宸村反寰湇鍔℃灦鏋勭殑鍥涘ぇ閲戝垰鍒╁櫒的主要内容,如果未能解决你的问题,请参考以下文章
闃块噷浜戞湇鍔″櫒ECS Ubuntu18.04 鍒濇浣跨敤閰嶇疆鏁欑▼(鍥惧舰鐣岄潰瀹夎)