Commit 2c95314b by Nepxion

增加Spring Cloud Gateway兼容Hystrix的线程隔离

parent b4679b05
......@@ -20,6 +20,8 @@ import com.nepxion.discovery.plugin.strategy.adapter.DiscoveryEnabledAdapter;
import com.nepxion.discovery.plugin.strategy.constant.StrategyConstant;
import com.nepxion.discovery.plugin.strategy.gateway.adapter.DefaultDiscoveryEnabledAdapter;
import com.nepxion.discovery.plugin.strategy.gateway.filter.GatewayStrategyFilter;
import com.nepxion.discovery.plugin.strategy.gateway.wrapper.DefaultCallableWrapper;
import com.nepxion.discovery.plugin.strategy.wrapper.CallableWrapper;
@Configuration
@AutoConfigureBefore(RibbonClientConfiguration.class)
......@@ -35,4 +37,10 @@ public class GatewayStrategyAutoConfiguration {
public DiscoveryEnabledAdapter discoveryEnabledAdapter() {
return new DefaultDiscoveryEnabledAdapter();
}
@Bean
@ConditionalOnProperty(value = StrategyConstant.SPRING_APPLICATION_STRATEGY_HYSTRIX_THREADLOCAL_SUPPORTED, matchIfMissing = false)
public CallableWrapper callableWrapper() {
return new DefaultCallableWrapper();
}
}
\ No newline at end of file
package com.nepxion.discovery.plugin.strategy.gateway.wrapper;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @author Hao Huang
* @version 1.0
*/
import java.util.concurrent.Callable;
import org.springframework.web.server.ServerWebExchange;
import com.nepxion.discovery.plugin.strategy.gateway.context.GatewayStrategyContext;
import com.nepxion.discovery.plugin.strategy.wrapper.CallableWrapper;
public class DefaultCallableWrapper implements CallableWrapper {
@Override
public <T> Callable<T> wrapCallable(Callable<T> delegate) {
ServerWebExchange exchange = GatewayStrategyContext.getCurrentContext().getExchange();
return new Callable<T>() {
@Override
public T call() throws Exception {
try {
GatewayStrategyContext.getCurrentContext().setExchange(exchange);
return delegate.call();
} finally {
GatewayStrategyContext.clearCurrentContext();
}
}
};
}
}
\ No newline at end of file
......@@ -82,6 +82,12 @@
<artifactId>discovery-plugin-strategy-starter-gateway</artifactId>
</dependency>
<!-- 当Spring Coud Gateway用Hystrix做线程隔离的时候,才需要导入下面的包 -->
<!-- <dependency>
<groupId>com.nepxion</groupId>
<artifactId>discovery-plugin-strategy-starter-hystrix</artifactId>
</dependency> -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
......
package com.nepxion.discovery.plugin.example.gateway.impl;
/**
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Copyright: Copyright (c) 2017-2050</p>
* <p>Company: Nepxion</p>
* @author Haojun Ren
* @version 1.0
*/
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyFallback {
@RequestMapping("/fallback")
public String fallback() {
return "Spring Cloud Gateway Fallback";
}
}
\ No newline at end of file
......@@ -29,3 +29,10 @@ spring.cloud.gateway.routes[0].predicates[0]=Path=/discovery-springcloud-example
# Gateway如果用Consul做服务注册发现中心,必须配置StripPrefix=1,其他配置中心配不配无所谓,但建议都配
spring.cloud.gateway.routes[0].filters[0]=StripPrefix=1
spring.cloud.gateway.routes[0].uri=lb://discovery-springcloud-example-a
# Hystrix配置
spring.cloud.gateway.routes[0].filters[0].name=Hystrix
spring.cloud.gateway.routes[0].filters[0].args.name=default
spring.cloud.gateway.routes[0].filters[0].args.fallbackUri=forward:/fallback
hystrix.command.default.execution.isolation.strategy=thread
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=3000
\ No newline at end of file
......@@ -75,3 +75,5 @@ spring.boot.admin.client.url=http://localhost:5555
# spring.application.strategy.zone.avoidance.rule.enabled=true
# 路由策略过滤器的执行顺序(Order)。缺失则默认为9000
# spring.application.strategy.gateway.route.filter.order=9000
# 开启Spring Cloud Gateway网关上实现Hystrix线程隔离模式做服务隔离时,必须把spring.application.strategy.hystrix.threadlocal.supported设置为true,同时要引入discovery-plugin-strategy-starter-hystrix包,否则线程切换时会发生ThreadLocal上下文对象丢失
# spring.application.strategy.hystrix.threadlocal.supported=true
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment