Last active
June 16, 2023 09:54
-
-
Save chenws1012/a4ed6cd6a8144d54fb17f4c4f0718307 to your computer and use it in GitHub Desktop.
#HystrixRequestContext
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext; | |
import com.netflix.hystrix.strategy.concurrency.HystrixRequestVariableDefault; | |
import java.util.Map; | |
/** | |
* @author [email protected] on 2023/5/16 | |
* | |
* In order to pass request context to a FeignClient thread. | |
*/ | |
public class ServiceContextHolder { | |
private static final HystrixRequestVariableDefault<Map<String, String>> context = new HystrixRequestVariableDefault<>(); | |
static Map<String, String> getServiceContext() { | |
initServiceContext(); | |
return context.get(); | |
} | |
static void setServiceContext(Map<String, String> contexts) { | |
initServiceContext(); | |
context.set(contexts); | |
} | |
static void destroy() { | |
if (HystrixRequestContext.isCurrentThreadInitialized()) { | |
HystrixRequestContext.getContextForCurrentThread().shutdown(); | |
} | |
} | |
private static void initServiceContext() { | |
if (!HystrixRequestContext.isCurrentThreadInitialized()) { | |
HystrixRequestContext.initializeContext(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment