解決 併發請求丟失header請求頭

2021-10-09 01:33:47 字數 2011 閱讀 4457

前段時間在工作的過程中發現併發呼叫的情況下,部分請求會丟失請求頭的部分資訊,故在此做下記錄

注意requestattributes這個字段,是解決問題的關鍵所在

@slf4j

public class taskrunnable implements runnable

public taskrunnable(string name, runnable runnable, requestattributes requestattributes)

@override

public void run()

if (requestattributes != null)

runnable.run();

} finally

mdcutils.clear();}}

}

@slf4j

public class taskcallableimplements callable

public taskcallable(string name, callablecallable, requestattributes requestattributes)

@override

public t call() throws exception

if (requestattributes != null)

return callable.call();

} finally

mdcutils.clear();}}

}

重點:requestattributes requestattributes = requestcontextholder.getrequestattributes();

public class taskthreadpoolexecutor extends threadpoolexecutor 

public taskthreadpoolexecutor(int corepoolsize, int maximumpoolsize, long keepalivetime, timeunit unit,

blockingqueueworkqueue, threadfactory threadfactory)

public taskthreadpoolexecutor(int corepoolsize, int maximumpoolsize, long keepalivetime, timeunit unit,

blockingqueueworkqueue, rejectedexecutionhandler handler)

public taskthreadpoolexecutor(int corepoolsize, int maximumpoolsize, long keepalivetime, timeunit unit,

blockingqueueworkqueue, threadfactory threadfactory,

rejectedexecutionhandler handler)

@override

public void execute(runnable command) else

}}

private static final executorservice executorservice = new taskthreadpoolexecutor(20, 200,

200l, timeunit.seconds,

new linkedblockingqueue(5000), new threadfactory()

}, new threadpoolexecutor.abortpolicy());

completablefuturefuture1 = completablefuture.supplyasync(() -> get***service(), executorservice);

Redis和請求佇列解決高併發

準備工作,查詢商品資訊,將剩餘數量同步到redis中 jedis jedis jedispool.getresource buygood good buygoodservice.getbyid good id jedis.set residue good id,good.getresidue jed...

解決ajax跨域請求資料cookie丟失問題

前端 以jquery為例 需要加入 複製 如下 xhrfields crossdomain true,ajax nb crossdomain true,success function failurecallback function lj v,datatype datatype 程式設計客棧 伺服...

利用redis快取解決高併發下後端重複請求措施

最近在進行壓力測試的時候發現在高併發下,有些介面很可能因為重複請求導致對資料庫操作出來的資料不是你想要的那個樣子。比如,使用者簽到,你只想讓使用者一天簽到一次,為了防止簽到多次,你對於每次強求,都去查詢資料庫今天是不是已經簽到了,如果簽了,就不讓繼續簽到,如果沒簽到,插入簽到資料,更新積分資料什麼的...