Created
August 19, 2016 11:55
-
-
Save Dreampie/d85b8ddd7e978df8802b98680df0baa0 to your computer and use it in GitHub Desktop.
This file contains 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
public class HystrixContextFilter extends OncePerRequestFilter { | |
public HystrixContextFilter() { | |
} | |
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException { | |
HystrixRequestContext context = HystrixRequestContext.initializeContext(); | |
//HystrixPlugins.getInstance().registerConcurrencyStrategy(new SubjectService.SessionConcurrencyStrategy()); | |
try { | |
chain.doFilter(request, response); | |
} finally { | |
context.shutdown(); | |
} | |
} | |
} |
This file contains 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
@Configuration | |
@ConditionalOnClass(HystrixCommand.class) | |
@ConditionalOnProperty(value = "hystrix.plugin.HystrixConcurrencyStrategy.implementation", matchIfMissing = true) | |
public class HystrixConcurrencyAutoConfiguration { | |
@Autowired(required = false) | |
private HystrixConcurrencyStrategy existingConcurrencyStrategy; | |
@PostConstruct | |
public void init() { | |
// Keeps references of existing Hystrix plugins. | |
HystrixCommandExecutionHook commandExecutionHook = HystrixPlugins.getInstance().getCommandExecutionHook(); | |
HystrixEventNotifier eventNotifier = HystrixPlugins.getInstance().getEventNotifier(); | |
HystrixMetricsPublisher metricsPublisher = HystrixPlugins.getInstance().getMetricsPublisher(); | |
HystrixPropertiesStrategy propertiesStrategy = HystrixPlugins.getInstance().getPropertiesStrategy(); | |
HystrixPlugins.reset(); | |
HystrixPlugins.getInstance().registerConcurrencyStrategy(new tv.acfun.cloud.common.hystrix.HystrixConcurrencyStrategy(existingConcurrencyStrategy)); | |
HystrixPlugins.getInstance().registerCommandExecutionHook(commandExecutionHook); | |
HystrixPlugins.getInstance().registerEventNotifier(eventNotifier); | |
HystrixPlugins.getInstance().registerMetricsPublisher(metricsPublisher); | |
HystrixPlugins.getInstance().registerPropertiesStrategy(propertiesStrategy); | |
} | |
} |
This file contains 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
public class HystrixConcurrencyStrategy extends com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy { | |
private com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy existingConcurrencyStrategy; | |
public HystrixConcurrencyStrategy(com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy existingConcurrencyStrategy) { | |
this.existingConcurrencyStrategy = existingConcurrencyStrategy; | |
} | |
public BlockingQueue<Runnable> getBlockingQueue(int maxQueueSize) { | |
return existingConcurrencyStrategy != null | |
? existingConcurrencyStrategy.getBlockingQueue(maxQueueSize) | |
: super.getBlockingQueue(maxQueueSize); | |
} | |
public <T> HystrixRequestVariable<T> getRequestVariable(HystrixRequestVariableLifecycle<T> rv) { | |
return existingConcurrencyStrategy != null | |
? existingConcurrencyStrategy.getRequestVariable(rv) | |
: super.getRequestVariable(rv); | |
} | |
public ThreadPoolExecutor getThreadPool(HystrixThreadPoolKey threadPoolKey, HystrixProperty<Integer> corePoolSize, | |
HystrixProperty<Integer> maximumPoolSize, HystrixProperty<Integer> keepAliveTime, | |
TimeUnit unit, BlockingQueue<Runnable> workQueue) { | |
return existingConcurrencyStrategy != null | |
? existingConcurrencyStrategy.getThreadPool(threadPoolKey, corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue) | |
: super.getThreadPool(threadPoolKey, corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue); | |
} | |
public <T> Callable<T> wrapCallable(Callable<T> callable) { | |
return existingConcurrencyStrategy != null | |
? existingConcurrencyStrategy.wrapCallable(new HystrixCallable<T>(callable)) | |
: super.wrapCallable(new HystrixCallable<T>(callable)); | |
} | |
} |
This file contains 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
public class HystrixCallable<T> implements Callable<T> { | |
private final Callable<T> callable; | |
private final Session session; | |
public HystrixCallable(Callable<T> callable) { | |
this.callable = callable; | |
this.session = SubjectService.sessionTL.get(); | |
} | |
public T call() throws Exception { | |
Session original = SubjectService.sessionTL.get(); | |
SubjectService.sessionTL.set(session); | |
try { | |
return callable.call(); | |
} finally { | |
SubjectService.sessionTL.set(original); | |
} | |
} | |
} |
This file contains 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
@HystrixCommand(groupKey = UserConstants.HYSTRIX_API_GROUPKEY) | |
public HttpResponse<User> signInV10(HttpServletRequest request, HttpServletResponse response, User user) { | |
//update threadlocal'user | |
return new HttpResponse<>(result, HttpStatus.OK); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment