闃块噷宸村反寰湇鍔℃灦鏋勭殑鍥涘ぇ閲戝垰鍒╁櫒

Posted 寮€鍙戣€呮妧鏈墠绾?/a>

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了闃块噷宸村反寰湇鍔℃灦鏋勭殑鍥涘ぇ閲戝垰鍒╁櫒相关的知识,希望对你有一定的参考价值。

鐐瑰嚮鈥?/span>寮€鍙戣€呮妧鏈墠绾?/span>鈥濓紝閫夋嫨鈥滄槦鏍囸煍濃€?/span>

璁╀竴閮ㄥ垎寮€鍙戣€呯湅鍒版湭鏉?/span>


鏉ヨ嚜锛氶樋閲屽反宸翠腑闂翠欢 Photo @Christopher Campbell 

鏂?nbsp;| 瀛斿嚒鍕?/span>



姒傝堪




浜掕仈缃戝簲鐢ㄥ彂灞曞埌浠婂ぉ锛屼粠鍗曚綋搴旂敤鏋舵瀯鍒?SOA 浠ュ強浠婂ぉ鐨勫井鏈嶅姟锛岄殢鐫€寰湇鍔″寲鐨勪笉鏂崌绾ц繘鍖栵紝鏈嶅姟鍜屾湇鍔′箣闂寸殑绋冲畾鎬у彉寰楄秺鏉ヨ秺閲嶈锛屽垎甯冨紡绯荤粺涔嬫墍浠ュ鏉傦紝涓昏鍘熷洜鏄垎甯冨紡绯荤粺闇€瑕佽€冭檻鍒扮綉缁滅殑寤舵椂鍜屼笉鍙潬锛屽井鏈嶅姟寰堥噸瑕佺殑涓€涓壒璐ㄥ氨鏄渶瑕佷繚璇佹湇鍔″箓绛夛紝淇濊瘉骞傜瓑鎬у緢閲嶈鐨勫墠鎻愰渶瑕佸垎甯冨紡閿佹帶鍒跺苟鍙戯紝鍚屾椂缂撳瓨銆侀檷绾у拰闄愭祦鏄繚鎶ゅ井鏈嶅姟绯荤粺杩愯绋冲畾鎬х殑涓夊ぇ鍒╁櫒銆?/span>

闅忕潃涓氬姟涓嶆柇鐨勫彂灞曪紝鎸変笟鍔″煙鐨勫垝鍒嗗瓙绯荤粺瓒婃潵瓒婂锛屾瘡涓笟鍔$郴缁熼兘闇€瑕佺紦瀛樸€侀檺娴併€佸垎甯冨紡閿併€佸箓绛夊伐鍏风粍浠讹紝 distributed-tools 缁勪欢(鏆傛湭寮€婧?姝e紡鍖呭惈浜嗕笂杩板垎甯冨紡绯荤粺鎵€闇€瑕佺殑鍩虹鍔熻兘缁勪欢銆?/span>

distributed-tools 缁勪欢鍩轰簬 tair銆乺edis 鍒嗗埆鎻愪緵浜?2 涓?springboot starter 锛屼娇鐢ㄨ捣鏉ラ潪甯哥畝鍗曘€?/span>

浠ヤ娇鐢ㄧ紦瀛樹娇鐢?redis 涓轰緥锛?application.properties 娣诲姞濡備笅閰嶇疆锛?/span>


redis.extend.hostName=127.0.0.1
redis.extend.port=6379
redis.extend.password=pwdcode
redis.extend.timeout=10000

redis.idempotent.enabled=true


鎺ヤ笅鏉ョ殑绡囧箙锛岄噸鐐逛細浠嬬粛涓€涓嬬紦瀛樸€侀檺娴併€佸垎甯冨紡閿併€佸箓绛夌殑浣跨敤鏂瑰紡銆?/span>



缂撳瓨




缂撳瓨鐨勪娇鐢ㄥ彲浠ヨ鏃犲涓嶅湪锛屼粠搴旂敤璇锋眰鐨勮闂矾寰勬潵鐪嬶紝鐢ㄦ埛 user -> 娴忚鍣ㄧ紦瀛?-> 鍙嶅悜浠g悊缂撳瓨-> WEB鏈嶅姟鍣ㄧ紦瀛?-> 搴旂敤绋嬪簭缂撳瓨 -> 鏁版嵁搴撶紦瀛樼瓑锛屽嚑涔庢瘡鏉¢摼璺兘鍏呮枼鐫€缂撳瓨鐨勪娇鐢紝缂撳瓨鏈€鐩寸櫧鐨勮В閲婂氨鏄€滅敤绌洪棿鎹㈡椂闂粹€濈殑绠楁硶銆傜紦瀛樺氨鏄妸涓€浜涙暟鎹殏鏃跺瓨鏀句簬鏌愪簺鍦版柟锛屽彲鑳芥槸鍐呭瓨锛屼篃鏈夊彲鑳界‖鐩樸€傛€讳箣锛岀洰鐨勫氨鏄负浜嗛伩鍏嶆煇浜涜€楁椂鐨勬搷浣溿€傛垜浠父瑙佺殑鑰楁椂鐨勬搷浣滐紝姣斿鏁版嵁搴撶殑鏌ヨ銆佷竴浜涙暟鎹殑璁$畻缁撴灉锛屾垨鑰呮槸涓轰簡鍑忚交鏈嶅姟鍣ㄧ殑鍘嬪姏銆傚叾瀹炲噺杞诲帇鍔涗篃鏄洜鏌ヨ鎴栬绠楋紝铏界劧鐭€楁椂锛屼絾鎿嶄綔寰堥绻侊紝绱姞璧锋潵涔熷緢闀匡紝閫犳垚涓ラ噸鎺掗槦绛夋儏鍐碉紝鏈嶅姟鍣ㄦ姉涓嶄綇銆?/span>

distributed-tools 缁勪欢鎻愪緵浜嗕竴涓?CacheEngine 鎺ュ彛锛屽熀浜?Tair銆丷edis 鍒嗗埆鏈変笉鍚岀殑瀹炵幇锛屽叿浣?CacheEngine 瀹氫箟濡備笅锛?/span>


    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);


get 鏂规硶閽堝 key 杩涜鏌ヨ锛?put 瀛樺偍缂撳瓨鏁版嵁锛?invalid 鍒犻櫎缂撳瓨鏁版嵁銆?/span>



闄愭祦




鍦ㄥ垎甯冨紡绯荤粺涓紝灏ゅ叾闈㈠涓€浜涚鏉€銆佺灛鏃堕珮骞跺彂鍦烘櫙锛岄兘闇€瑕佽繘琛屼竴浜涢檺娴佹帾鏂斤紝淇濊瘉绯荤粺鐨勯珮鍙敤銆傞€氬父鏉ヨ闄愭祦鐨勭洰鐨勬槸閫氳繃瀵瑰苟鍙戣闂?璇锋眰杩涜闄愰€燂紝鎴栬€呬竴涓椂闂寸獥鍙e唴鐨勭殑璇锋眰杩涜闄愰€熸潵淇濇姢绯荤粺锛屼竴鏃﹁揪鍒伴檺鍒堕€熺巼鍒欏彲浠?鎷掔粷鏈嶅姟锛堝畾鍚戝埌閿欒椤垫垨鍛婄煡璧勬簮娌℃湁浜嗭級銆佹帓闃?鎴?绛夊緟锛堟瘮濡傜鏉€銆佽瘎璁恒€佷笅鍗曪級銆侀檷绾э紙杩斿洖鎵樺簳鏁版嵁鎴栭粯璁ゆ暟鎹紝濡傚晢鍝佽鎯呴〉搴撳瓨榛樿鏈夎揣锛夈€?/span>

甯歌鐨勪竴浜涢檺娴佺畻娉曞寘鎷浐瀹氱獥鍙c€佹粦鍔ㄧ獥鍙c€佹紡妗躲€佷护鐗屾《锛宒istributed-tools 缁勪欢鐩墠鍩轰簬璁℃暟鍣ㄥ彧瀹炵幇浜嗗浐瀹氱獥鍙g畻娉曪紝鍏蜂綋浣跨敤鏂瑰紡濡備笅锛?/span>


 /**
* 鎸囧畾杩囨湡鏃堕棿鑷璁℃暟鍣紝榛樿姣忔+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);


鍩轰簬 CacheEngine 鐨?rateLimit 鏂规硶鍙互瀹炵幇闄愭祦锛?expireTime 鍙兘璁惧畾鍥哄畾绐楀彛鏃堕棿锛岄潪婊戝姩绐楀彛鏃堕棿銆?/span>

鍙﹀ distributed-tools 缁勪欢鎻愪緵浜嗘ā鏉?RateLimitTemplate 鍙互绠€鍖栭檺娴佺殑鏄撶敤鎬э紝鍙互鐩存帴璋冪敤 RateLimitTemplate 鐨?execute 鏂规硶澶勭悊闄愭祦闂銆?/span>


 /**
* @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();
}


鍙﹀ distributed-tools 缁勪欢杩樻彁渚涗簡娉ㄨВ @RateLimit 鐨勪娇鐢ㄦ柟寮忥紝鍏蜂綋娉ㄨВ RateLimit 瀹氫箟濡備笅锛?/span>


@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;
}


浠讳綍鏂规硶娣诲姞涓婅堪娉ㄨВ鍏峰浜嗕竴瀹氱殑闄愭祦鑳藉姏(鍏蜂綋鏂规硶闇€瑕佸湪 spring aop 鎸囧畾鎷︽埅鑼冨洿鍐?锛屽涓婁唬鐮佽〃绀轰互鍙傛暟 key 浣滀负闄愭祦 key 锛屾瘡 2 鍒嗛挓璇锋眰娆℃暟涓嶈秴杩?5 娆★紝瓒呰繃闄愬埗鍚庨樆濉?3 鍒嗛挓銆?/span>



鍒嗗竷寮忛攣




鍦?Java 鍗曚竴杩涚▼涓€氳繃 synchronized 鍏抽敭瀛楀拰 ReentrantLock 鍙噸鍏ラ攣鍙互瀹炵幇鍦ㄥ绾跨▼鐜涓帶鍒跺璧勬簮鐨勫苟鍙戣闂紝閫氬父鏈湴鐨勫姞閿佸線寰€涓嶈兘婊¤冻鎴戜滑鐨勯渶瑕侊紝鎴戜滑鏇村鐨勯潰瀵瑰満鏅槸鍒嗗竷寮忕郴缁熻法杩涚▼鐨勯攣锛岀畝绉颁负鍒嗗竷寮忛攣銆傚垎甯冨紡閿佸疄鐜版墜娈甸€氬父鏄皢閿佹爣璁板瓨鍦ㄥ唴瀛樹腑锛屽彧鏄鍐呭瓨涓嶆槸鏌愪釜杩涚▼鍒嗛厤鐨勫唴瀛樿€屾槸鍏叡鍐呭瓨濡?Redis銆乀air 锛岃嚦浜庡埄鐢ㄦ暟鎹簱銆佹枃浠剁瓑鍋氶攣涓庡崟鏈虹殑瀹炵幇鏄竴鏍风殑锛屽彧瑕佷繚璇佹爣璁拌兘浜掓枼灏辫銆傚垎甯冨紡閿佺浉瀵瑰崟鏈鸿繘绋嬬殑閿佷箣鎵€浠ュ鏉傦紝涓昏鍘熷洜鏄垎甯冨紡绯荤粺闇€瑕佽€冭檻鍒扮綉缁滅殑寤舵椂鍜屼笉鍙潬銆?/span>

 distributed-tools 缁勪欢鎻愪緵鐨勫垎甯冨紡閿佽鍏峰濡備笅鐗规€э細


  • 浜掓枼鎬?鍚屾湰鍦伴攣涓€鏍峰叿鏈変簰鏂ユ€э紝浣嗘槸鍒嗗竷寮忛攣闇€瑕佷繚璇佸湪涓嶅悓鑺傜偣杩涚▼鐨勪笉鍚岀嚎绋嬬殑浜掓枼銆?/span>

  • 鍙噸鍏ユ€?鍚屼竴涓妭鐐逛笂鐨勫悓涓€涓嚎绋嬪鏋滆幏鍙栦簡閿佷箣鍚庨偅涔堜篃鍙互鍐嶆鑾峰彇杩欎釜閿併€?/span>

  • 閿佽秴鏃?鍜屾湰鍦伴攣涓€鏍锋敮鎸侀攣瓒呮椂锛岄槻姝㈡閿侊紝閫氳繃寮傛蹇冭烦 demon 绾跨▼鍒锋柊杩囨湡鏃堕棿锛岄槻姝㈢壒娈婂満鏅?濡?FGC 姝婚攣瓒呮椂)涓嬫閿併€?/span>

  • 楂樻€ц兘銆侀珮鍙敤:鍔犻攣鍜岃В閿侀渶瑕侀珮鎬ц兘锛屽悓鏃朵篃闇€瑕佷繚璇侀珮鍙敤闃叉鍒嗗竷寮忛攣澶辨晥锛屽彲浠ュ鍔犻檷绾с€?/span>

  • 鏀寔闃诲鍜岄潪闃诲:鍚?ReentrantLock 涓€鏍锋敮鎸?lock 鍜?trylock 浠ュ強 tryLock ( long timeOut )銆?/span>

  • 鍏钩閿佸拰闈炲叕骞抽攣(涓嶆敮鎸?:鍏钩閿佹槸鎸夌収璇锋眰鍔犻攣鐨勯『搴忚幏寰楅攣锛岄潪鍏钩閿佸氨鐩稿弽鏄棤搴忕殑锛岀洰鍓?distributed-tools 缁勪欢鎻愪緵鐨勫垎甯冨紡閿佷笉鏀寔璇ョ壒鎬с€?/span>



distributed-tools 缁勪欢鎻愪緵鐨勫垎甯冨紡閿侊紝浣跨敤璧锋潵闈炲父绠€鍗曪紝鎻愪緵浜嗕竴涓垎甯冨紡閿佹ā鏉匡細DistributedLockTemplate 锛屽彲浠ョ洿鎺ヨ皟鐢ㄦā鏉挎彁渚涚殑闈欐€佹柟娉?濡備笅)锛?/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();
}
}


骞傜瓑




 鍦ㄥ垎甯冨紡绯荤粺璁捐涓箓绛夋€ц璁′腑鍗佸垎閲嶈鐨勶紝灏ゅ叾鍦ㄥ鏉傜殑寰湇鍔′腑涓€濂楃郴缁熶腑鍖呭惈浜嗗涓瓙绯荤粺鏈嶅姟锛岃€屼竴涓瓙绯荤粺鏈嶅姟寰€寰€浼氬幓璋冪敤鍙︿竴涓湇鍔★紝鑰屾湇鍔¤皟鐢ㄦ湇鍔℃棤闈炲氨鏄娇鐢?RPC 閫氫俊鎴栬€?restful 锛屽垎甯冨紡绯荤粺涓殑缃戠粶寤舵椂鎴栦腑鏂槸閬垮厤涓嶄簡鐨勶紝閫氬父浼氬鑷存湇鍔$殑璋冪敤灞傝Е鍙戦噸璇曘€傚叿鏈夎繖涓€鎬ц川鐨勬帴鍙e湪璁捐鏃舵€绘槸绉夋寔杩欐牱鐨勪竴绉嶇悊蹇碉細璋冪敤鎺ュ彛鍙戠敓寮傚父骞朵笖閲嶅灏濊瘯鏃讹紝鎬绘槸浼氶€犳垚绯荤粺鎵€鏃犳硶鎵垮彈鐨勬崯澶憋紝鎵€浠ュ繀椤婚樆姝㈣繖绉嶇幇璞$殑鍙戠敓銆?/span>

骞傜瓑閫氬父浼氭湁涓や釜缁村害锛?/span>
1. 绌洪棿缁村害涓婄殑骞傜瓑锛屽嵆骞傜瓑瀵硅薄鐨勮寖鍥达紝鏄釜浜鸿繕鏄満鏋勶紝鏄煇涓€娆′氦鏄撹繕鏄煇绉嶇被鍨嬬殑浜ゆ槗銆?/span>
2. 鏃堕棿缁村害涓婄殑骞傜瓑锛屽嵆骞傜瓑鐨勪繚璇佹椂闂达紝鏄嚑涓皬鏃躲€佸嚑澶╄繕鏄案涔呮€х殑銆?/span>

鍦ㄥ疄闄呯郴缁熶腑鏈夊緢澶氭搷浣滐紝涓嶇鎿嶄綔澶氬皯娆★紝閮藉簲璇ヤ骇鐢熶竴鏍风殑鏁堟灉鎴栬繑鍥炵浉鍚岀殑缁撴灉銆備互涓嬭繖浜涘簲鐢ㄥ満鏅篃鏄€氬父姣旇緝甯歌鐨勫簲鐢ㄥ満鏅細
1. 鍓嶇閲嶅鎻愪氦璇锋眰锛屼笖璇锋眰鏁版嵁鐩稿悓鏃讹紝鍚庡彴闇€瑕佽繑鍥炲搴旇繖涓姹傜殑鐩稿悓缁撴灉銆?/span>
2. 鍙戣捣涓€娆℃敮浠樿姹傦紝鏀粯涓績搴旇鍙墸鐢ㄦ埛璐︽埛涓€娆¢挶锛屽綋閬囧埌缃戠粶涓柇鎴栫郴缁熷紓甯告椂锛屼篃搴旇鍙墸涓€娆¢挶銆?/span>
3. 鍙戦€佹秷鎭紝鍚屾牱鍐呭鐨勭煭淇″彂缁欑敤鎴峰彧鍙戜竴娆°€?/span>
4. 鍒涘缓涓氬姟璁㈠崟锛屼竴娆′笟鍔¤姹傚彧鑳藉垱寤轰竴涓紝閲嶈瘯璇锋眰鍒涘缓澶氫釜灏变細鍑哄ぇ闂銆?/span>
5. 鍩轰簬 msgId 鐨勬秷鎭箓绛夊鐞嗐€?/span>

鍦ㄦ寮忎娇鐢?distributed-tools 缁勪欢鎻愪緵鐨勫箓绛変箣鍓嶏紝鎴戜滑鍏堢湅涓?distributed-tools 骞傜瓑缁勪欢鐨勮璁°€?/span>


闃块噷宸村反寰湇鍔℃灦鏋勭殑鍥涘ぇ閲戝垰鍒╁櫒


骞傜瓑 key 鎻愬彇鑳藉姏锛氳幏鍙栧敮涓€骞傜瓑 key




骞傜瓑 key 鐨勬彁鍙栨敮鎸?2 涓敞瑙o細IdempotentTxId銆両dempotentTxIdGetter锛屼换鎰忔柟娉曟坊鍔犱互涓?2 娉ㄨВ锛屽嵆鍙彁鍙栧埌鐩稿叧骞傜瓑 key 锛屽墠鎻愭潯浠舵槸闇€瑕佸皢 Idempotent 娉ㄨВ娣诲姞鐩稿叧闇€瑕佸箓绛夌殑鏂规硶涓娿€?/span>

濡傛灉鍗曠函浣跨敤骞傜瓑妯℃澘杩涜涓氬姟澶勭悊锛岄渶瑕佽嚜宸辫缃浉鍏冲箓绛塳ey锛屼笖瑕佷繚璇佸叾鍞竴鎬с€?/span>


鍒嗗竷寮忛攣鏈嶅姟鑳藉姏锛氭彁渚涘叏灞€鍔犻攣銆佽В閿佺殑鑳藉姏


distributed-tools 骞傜瓑缁勪欢闇€瑕佷娇鐢ㄨ嚜韬彁渚涚殑鍒嗗竷寮忛攣鍔熻兘锛屼繚璇佸叾骞跺彂鍞竴鎬э紝 distributed-tools 鎻愪緵鐨勫垎甯冨紡閿佽兘澶熸彁渚涘叾鍙潬銆佺ǔ瀹氱殑鍔犻攣銆佽В閿佽兘鍔涖€?/span>


楂樻€ц兘鐨勫啓鍏ャ€佹煡璇㈣兘鍔涳細閽堝骞傜瓑缁撴灉鏌ヨ涓庡瓨鍌?/span>


distributed-tools 骞傜瓑缁勪欢鎻愪緵浜嗗熀浜?tair 銆?redis 鐨勫瓨鍌ㄥ疄鐜帮紝鍚屾椂鏀寔鑷畾涔変竴绾с€佷簩绾у瓨鍌ㄩ€氳繃 spring 渚濊禆娉ㄥ叆鍒?IdempotentService 锛屽缓璁?distributed-tools 骞傜瓑瀛樺偍缁撴灉涓€绾у瓨鍌?tair mdb 锛屼簩绾у瓨鍌╨db鎴栬€?tablestore 锛屼竴绾у瓨鍌ㄤ繚璇佸叾楂樻€ц兘锛屼簩绾у瓨鍌ㄤ繚璇佸叾鍙潬鎬с€?/span>



浜岀骇瀛樺偍骞惰鏌ヨ浼氳繑鍥炴煡璇㈡渶蹇殑骞傜瓑缁撴灉銆?/span>

浜岀骇瀛樺偍骞惰寮傛鍐欏叆锛岃繘涓€姝ユ彁楂樻€ц兘銆?/span>

楂樺彲鐢ㄧ殑骞傜瓑鍐欏叆銆佹煡璇㈣兘鍔涳細骞傜瓑瀛樺偍鍑虹幇寮傚父锛屼笉褰卞搷涓氬姟姝e父娴佺▼锛屽鍔犲閿?/span>

distributed-tools 骞傜瓑缁勪欢鏀寔浜岀骇瀛樺偍锛屼负浜嗕繚璇佸叾楂樺彲鐢紝姣曠珶浜岀骇瀛樺偍鍑虹幇鏁呴殰鐨勬鐜囧お浣庯紝涓嶄細瀵艰嚧涓氬姟涓婁笉鍙敤锛屽鏋滀簩绾у瓨鍌ㄥ悓鏃跺嚭鐜版晠闅滐紝涓氬姟涓婂仛浜嗕竴瀹氱殑瀹归敊锛岄拡瀵逛笉纭畾鎬х殑寮傚父閲囧彇閲嶈瘯绛栫暐锛屼細鎵ц鍏蜂綋骞傜瓑鏂规硶銆?/span>

涓€绾у瓨鍌ㄤ笌浜岀骇瀛樺偍鐨勫啓鍏ヤ笌鏌ヨ澶勭悊杩涜闅旂锛屼换浣曚竴绾у瓨鍌ㄧ殑寮傚父涓嶄細褰卞搷鏁翠綋涓氬姟鎵ц銆?/span>

鍦ㄤ簡瑙d簡 distributed-tools 缁勪欢骞傜瓑涔嬪悗锛屾帴涓嬫潵鎴戜滑鏉ョ湅涓嬪浣曞幓浣跨敤骞傜瓑缁勪欢锛岄鍏堜簡瑙d笅 common-api 鎻愪緵鐨勫箓绛夋敞瑙?鍏蜂綋骞傜瓑娉ㄨВ浣跨敤鏂瑰紡濡備笅锛?/span>


闃块噷宸村反寰湇鍔℃灦鏋勭殑鍥涘ぇ閲戝垰鍒╁櫒


骞傜瓑鎷︽埅鍣ㄨ幏鍙栧箓绛?ID 鐨勪紭鍏堢骇锛?/span>


  1. 棣栧厛鍒ゆ柇 Idempotent 鐨?spelKey 鐨勫睘鎬ф槸鍚︿负绌猴紝濡傛灉涓嶄负绌轰細鏍规嵁 spelKey 瀹氫箟鐨?spring 琛ㄨ揪寮忕敓鎴愬箓绛?ID 銆?/span>
  2. 鍏舵鍒ゆ柇鍙傛暟鏄惁鍖呭惈 IdempotentTxId 娉ㄨВ锛屽鏋滄湁 IdempotentTxId 锛屼細鐩存帴鑾峰彇鍙傛暟鍊肩敓鎴愬箓绛?ID 銆?/span>
  3. 鍐嶆閫氳繃鍙嶅皠鑾峰彇鍙傛暟瀵硅薄灞炴€ф槸鍚﹀寘鍚?IdempotentTxId 娉ㄨВ锛屽鏋滃璞″睘鎬у寘鍚?IdempotentTxId 娉ㄨВ浼氳幏鍙栬鍙傛暟瀵硅薄灞炴€х敓鎴愬箓绛?ID 銆?/span>
  4. 鏈€鍚庝互涓婁笁绉嶆儏鍐典粛鏈幏鍙栧埌骞傜瓑 ID 锛屼細杩涗竴姝ラ€氳繃鍙嶅皠鑾峰彇鍙傛暟瀵硅薄鐨?Method 鏄惁瀹氫箟 IdempotentTxIdGetter 娉ㄨВ锛屽鏋滃寘鍚娉ㄨВ鍒欓€氳繃鍙嶅皠鐢熸垚骞傜瓑 ID 銆?/span>




浠g爜浣跨敤绀轰緥锛?/span>


    @Idempotent(spelKey = "#request.requestId", firstLevelExpireDate = 7锛宻econdLevelExpireDate = 30)
publicvoidexecute(BizFlowRequest request){
..................
}


濡備笂杩颁唬鐮佽〃绀轰粠 request 鑾峰彇 requestId 浣滀负骞傜瓑 key 锛屼竴绾у瓨鍌ㄦ湁鏁堟湡 7 澶╋紝浜岀骇瀛樺偍鏈夋晥鏈?30 澶┿€?/span>

distributed-tools 闄や簡鍙互浣跨敤骞傜瓑娉ㄨВ澶栵紝骞傜瓑缁勪欢杩樻彁渚涗簡涓€涓€氱敤骞傜瓑妯℃澘 IdempotentTemplate 锛屼娇鐢ㄥ箓绛夋ā鏉跨殑鍓嶆彁蹇呴』璁剧疆 tair.idempotent.enabled=true鎴栬€卹edis.idempotent.enabled=true 锛岄粯璁や负 false 锛屽悓鏃堕渶瑕佹寚瀹氬箓绛夌粨鏋滀竴绾у瓨鍌紝骞傜瓑缁撴灉瀛樺偍涓哄彲閫夐」閰嶇疆銆?/span>


鍏蜂綋浣跨敤骞傜瓑妯℃澘 IdempotentTemplate 鐨勬柟娉曞涓嬶細


/**
* 骞傜瓑妯℃澘澶勭悊鍣?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)
{

........
}


request锛?/span>
骞傜瓑鍙傛暟 IdempotentRequest 缁勮锛屽彲浠ヨ缃箓绛夊弬鏁板拰骞傜瓑鍞竴 ID 銆?/span>

executeSupplier锛?/span>
鍏蜂綋骞傜瓑鐨勬柟娉曢€昏緫锛屾瘮濡傞拡瀵规敮浠樸€佷笅鍗曟帴鍙o紝鍙互閫氳繃 JDK8 鍑芥暟寮忔帴鍙?Supplier Callback 杩涜澶勭悊銆?/span>

resultBiConsumer锛?/span>
骞傜瓑杩斿洖缁撴灉鐨勫鐞嗭紝璇ュ弬鏁板彲浠ヤ负绌猴紝濡傛灉涓虹┖閲囧彇榛樿鐨勫鐞嗭紝鏍规嵁骞傜瓑缁撴灉锛屽鏋滄垚鍔熴€佷笉鍙噸璇曠殑寮傚父閿欒鐮侊紝鐩存帴杩斿洖缁撴灉锛屽鏋滃け璐ュ彲閲嶈瘯寮傚父閿欒鐮侊紝浼氳繘琛岄噸璇曞鐞嗐€?/span>


濡傛灉璇ュ弬鏁板€间笉涓虹┖锛屽彲浠ラ拡瀵硅繑鍥炲箓绛夌粨鏋滆繘琛岀壒娈婇€昏緫澶勭悊璁剧疆 ResultStatus(ResultStatus 鍖呭惈涓夌鐘舵€佸寘鎷垚鍔熴€佸け璐ュ彲閲嶈瘯銆佸け璐ヤ笉鍙噸璇?銆?/span>


浣滆€呬俊鎭細

瀛斿嚒鍕囷紝鑺卞悕浜戠媱锛岄樋閲屼簯-寮€鏀惧钩鍙伴珮绾ф妧鏈锛屽楂樺苟鍙戙€侀珮鎬ц兘銆侀珮鍙敤銆佸彲浼哥缉鐨勫垎甯冨紡绯荤粺鏋舵瀯璁捐鏈変赴瀵岀粡楠岋紝Cloud Native鍧氬畾鎷ユ姢鑰咃紝鍧氬畧寮€鍙戜竴绾挎墦纾ㄥ尃鑹虹殑鏋舵瀯甯堛€?/span>


绂忓埄鏃堕棿锛?/span>

鍦ㄨ繖閲岋紝鎴戜负澶у鍑嗗浜嗕竴浠?020骞存渶鏂版渶鍏ㄧ殑銆奐ava闈㈣瘯棰樺強绛旀V3.0銆嬶紝杩欏鐢靛瓙涔︽兜鐩栦簡璇稿鍚庣鎶€鏈爤鐨勯潰璇曢鍜岀瓟妗堬紝鐩镐俊鍙互甯姪澶у鍦ㄦ渶鐭殑鏃堕棿鍐呭涔燡ava鍚庣鐨勫ぇ澶氭暟闈㈣瘯棰橈紝浠庤€屾嬁鍒拌嚜宸卞績浠殑offer銆?/p>

鎴簡寮犲浘锛屽ぇ瀹跺彲浠ヤ粩缁嗘煡鐪嬪乏杈圭殑鑿滃崟鏍忥紝瑕嗙洊鐨勭煡璇嗛潰鐪熺殑寰堝箍锛岃€屼笖璐ㄩ噺閮藉緢涓嶉敊銆?

闃块噷宸村反寰湇鍔℃灦鏋勭殑鍥涘ぇ閲戝垰鍒╁櫒
璧勬枡鑾峰彇鏂规硶
  1. 鎵弿涓嬫柟浜岀淮鐮?/span>

  2. 鍦ㄤ笅闈㈢殑浜岀淮鍚庡彴鍥炲鍏抽敭璇嶏細Java鏍稿績鏁寸悊


闃块噷宸村反寰湇鍔℃灦鏋勭殑鍥涘ぇ閲戝垰鍒╁櫒




END


鍚庡彴鍥炲鈥滅數瀛愪功鈥?/span> 鈥滆祫鏂欌€?/span> 棰嗗彇涓€浠藉共璐э紝鏁扮櫨鎶€鏈數瀛愪功绛変綘
寮€鍙戣€呮妧鏈墠绾?锛屾眹闆嗘妧鏈墠绾垮揩璁拰鍏虫敞琛屼笟瓒嬪娍锛屽ぇ鍘傚共璐э紝鏄紑鍙戣€呯粡鍘嗗拰鎴愰暱鐨勪紭绉€鎸囧崡銆?/span>
鍘嗗彶鎺ㄨ崘


  





闃块噷宸村反寰湇鍔℃灦鏋勭殑鍥涘ぇ閲戝垰鍒╁櫒
闃块噷宸村反寰湇鍔℃灦鏋勭殑鍥涘ぇ閲戝垰鍒╁櫒
濂芥枃鐐逛釜鍦ㄧ湅鍚э紒

以上是关于闃块噷宸村反寰湇鍔℃灦鏋勭殑鍥涘ぇ閲戝垰鍒╁櫒的主要内容,如果未能解决你的问题,请参考以下文章

闃块噷寰湇鍔℃媶鍒?鏈嶅姟娌荤悊鍘熷垯

web缃戠珯闃块噷浜戞湇鍔″櫒椤圭洰閮ㄧ讲

闃块噷浜戞湇鍔″櫒ECS Ubuntu18.04 鍒濇浣跨敤閰嶇疆鏁欑▼(鍥惧舰鐣岄潰瀹夎)

浜戞爾骞茶揣鍥為 | 琛屼笟椤剁骇NoSQL鎴愬憳鍧愰晣锛孨oSQL鏁版嵁搴撲笓鍦洪噸鐐硅В鏋愶紒

java8鍥涘ぇ鍑芥暟寮忔帴鍙?/h1>