赞
踩
- @GetMapping(value = "/getServerInfo/{serviceName}")
- public String getServer1Info(@PathVariable(value = "serviceName") String serviceName) {
- LOGGER.info("当前线程ID:" + Thread.currentThread().getId() + "当前线程Name" + Thread.currentThread().getName());
- RequestContextHolder.currentRequestAttributes().setAttribute("context", "main-thread-context", SCOPE_REQUEST);
- return consumeService.getServerInfo(serviceName);
- }
- @Override
- @HystrixCommand(fallbackMethod = "getServerInfoFallback",
- commandProperties = {@HystrixProperty(name = "execution.isolation.strategy", value = "THREAD")},
- commandKey = "cust2GetServerInfo",
- threadPoolKey = "cust2ThreadPool",
- groupKey = "cust2")
- public String getServerInfo(String serviceName) {
- LOGGER.info(RibbonFilterContextHolder.getCurrentContext().get("TAG"));
- LOGGER.info(RequestContextHolder.currentRequestAttributes().getAttribute("context", SCOPE_REQUEST).toString());
- //如果是service1则需要添加http认证头,service1暂时添加了认证机制;反之service2不需要认证直接发出请求即可
- if ("service1".equals(serviceName)) {
- HttpEntity<String> requestEntity = new HttpEntity<String>(getHeaders());
- ResponseEntity<String> responseEntity = restTemplate.exchange("http://" + serviceName + "/getServerInfo?userName=shuaishuai", HttpMethod.GET, requestEntity, String.class);
- return responseEntity.getBody();
- } else
- return restTemplate.getForObject("http://" + serviceName + "/getServerInfo?userName=shuaishuai", String.class);
- }
-
- public String getServerInfoFallback(String serviceName, Throwable e) {
- if (e != null) {
- LOGGER.error(e.getMessage());
- }
- return "Maybe the server named " + serviceName + " is not normal running";
- }

而在RequestContextHolder中变量定义如下
类别
|
抽象实现
|
Event Notifier
| |
Metrics Publisher
| |
Properties Strategy
| |
Concurrency Strategy
| |
Command Execution Hook
|
- public class RequestContextHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy {
-
- @Override
- public <T> Callable<T> wrapCallable(Callable<T> callable) {
- return new RequestAttributeAwareCallable<>(callable, RequestContextHolder.getRequestAttributes());
- }
-
- static class RequestAttributeAwareCallable<T> implements Callable<T> {
-
- private final Callable<T> delegate;
- private final RequestAttributes requestAttributes;
-
- public RequestAttributeAwareCallable(Callable<T> callable, RequestAttributes requestAttributes) {
- this.delegate = callable;
- this.requestAttributes = requestAttributes;
- }
-
- @Override
- public T call() throws Exception {
- try {
- RequestContextHolder.setRequestAttributes(requestAttributes);
- return delegate.call();
- } finally {
- RequestContextHolder.resetRequestAttributes();
- }
- }
- }
- }

@PostConstruct
public void init() {
HystrixPlugins.getInstance().registerConcurrencyStrategy(new RequestContextHystrixConcurrencyStrategy());
}
- public interface HystrixCallableWrapper {
-
- /**
- * 包装Callable实例
- *
- * @param callable 待包装实例
- * @param <T> 返回类型
- * @return 包装后的实例
- */
- <T> Callable<T> wrap(Callable<T> callable);
-
- }
- public class RequestContextHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy {
- private final Collection<HystrixCallableWrapper> wrappers;
-
- public RequestContextHystrixConcurrencyStrategy(Collection<HystrixCallableWrapper> wrappers) {
- this.wrappers = wrappers;
- }
-
- @Override
- public <T> Callable<T> wrapCallable(Callable<T> callable) {
- return new CallableWrapperChain(callable, wrappers.iterator()).wrapCallable();
- }
-
- private static class CallableWrapperChain<T> {
-
- private final Callable<T> callable;
-
- private final Iterator<HystrixCallableWrapper> wrappers;
-
- CallableWrapperChain(Callable<T> callable, Iterator<HystrixCallableWrapper> wrappers) {
- this.callable = callable;
- this.wrappers = wrappers;
- }
-
- Callable<T> wrapCallable() {
- Callable<T> delegate = callable;
- while (wrappers.hasNext()) {
- delegate = wrappers.next().wrap(delegate);
- }
- return delegate;
- }
- }
- }

- public final class RequestAttributeAwareCallableWrapper implements HystrixCallableWrapper {
- @Override
- public <T> Callable<T> wrap(Callable<T> callable) {
- return new RequestAttributeAwareCallable(callable, RequestContextHolder.getRequestAttributes());
- }
-
- static class RequestAttributeAwareCallable<T> implements Callable<T> {
-
- private final Callable<T> delegate;
- private final RequestAttributes requestAttributes;
-
- RequestAttributeAwareCallable(Callable<T> callable, RequestAttributes requestAttributes) {
- this.delegate = callable;
- this.requestAttributes = requestAttributes;
- }
-
- @Override
- public T call() throws Exception {
- try {
- RequestContextHolder.setRequestAttributes(requestAttributes);
- return delegate.call();
- } finally {
- RequestContextHolder.resetRequestAttributes();
- }
- }
- }
- }

- public class MdcAwareCallableWrapper implements HystrixCallableWrapper {
- @Override
- public <T> Callable<T> wrap(Callable<T> callable) {
- return new MdcAwareCallable<>(callable, MDC.getCopyOfContextMap());
- }
-
- private class MdcAwareCallable<T> implements Callable<T> {
-
- private final Callable<T> delegate;
-
- private final Map<String, String> contextMap;
-
- public MdcAwareCallable(Callable<T> callable, Map<String, String> contextMap) {
- this.delegate = callable;
- this.contextMap = contextMap != null ? contextMap : new HashMap();
- }
-
- @Override
- public T call() throws Exception {
- try {
- MDC.setContextMap(contextMap);
- return delegate.call();
- } finally {
- MDC.clear();
- }
- }
- }
- }

- @Bean
- public HystrixCallableWrapper requestAttributeAwareCallableWrapper() {
- return new RequestAttributeAwareCallableWrapper();
- }
-
- @Bean
- public HystrixCallableWrapper mdcAwareCallableWrapper(){
- return new MdcAwareCallableWrapper();
- }
-
- @Autowired(required = false)
- private List<HystrixCallableWrapper> wrappers = new ArrayList<>();
-
- @PostConstruct
- public void init() {
- HystrixPlugins.getInstance().registerConcurrencyStrategy(new RequestContextHystrixConcurrencyStrategy(wrappers));
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。