Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
discovery
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
谢捷峰
discovery
Commits
2c95314b
Commit
2c95314b
authored
May 30, 2019
by
Nepxion
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加Spring Cloud Gateway兼容Hystrix的线程隔离
parent
b4679b05
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
89 additions
and
4 deletions
+89
-4
discovery-plugin-strategy-starter-gateway/src/main/java/com/nepxion/discovery/plugin/strategy/gateway/configuration/GatewayStrategyAutoConfiguration.java
+9
-0
discovery-plugin-strategy-starter-gateway/src/main/java/com/nepxion/discovery/plugin/strategy/gateway/wrapper/DefaultCallableWrapper.java
+39
-0
discovery-springcloud-example-gateway/pom.xml
+6
-0
discovery-springcloud-example-gateway/src/main/java/com/nepxion/discovery/plugin/example/gateway/impl/MyFallback.java
+22
-0
discovery-springcloud-example-gateway/src/main/resources/application.properties
+9
-2
discovery-springcloud-example-gateway/src/main/resources/bootstrap.properties
+4
-2
No files found.
discovery-plugin-strategy-starter-gateway/src/main/java/com/nepxion/discovery/plugin/strategy/gateway/configuration/GatewayStrategyAutoConfiguration.java
View file @
2c95314b
...
@@ -20,6 +20,8 @@ import com.nepxion.discovery.plugin.strategy.adapter.DiscoveryEnabledAdapter;
...
@@ -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.constant.StrategyConstant
;
import
com.nepxion.discovery.plugin.strategy.gateway.adapter.DefaultDiscoveryEnabledAdapter
;
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.filter.GatewayStrategyFilter
;
import
com.nepxion.discovery.plugin.strategy.gateway.wrapper.DefaultCallableWrapper
;
import
com.nepxion.discovery.plugin.strategy.wrapper.CallableWrapper
;
@Configuration
@Configuration
@AutoConfigureBefore
(
RibbonClientConfiguration
.
class
)
@AutoConfigureBefore
(
RibbonClientConfiguration
.
class
)
...
@@ -35,4 +37,10 @@ public class GatewayStrategyAutoConfiguration {
...
@@ -35,4 +37,10 @@ public class GatewayStrategyAutoConfiguration {
public
DiscoveryEnabledAdapter
discoveryEnabledAdapter
()
{
public
DiscoveryEnabledAdapter
discoveryEnabledAdapter
()
{
return
new
DefaultDiscoveryEnabledAdapter
();
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
discovery-plugin-strategy-starter-gateway/src/main/java/com/nepxion/discovery/plugin/strategy/gateway/wrapper/DefaultCallableWrapper.java
0 → 100644
View file @
2c95314b
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
discovery-springcloud-example-gateway/pom.xml
View file @
2c95314b
...
@@ -82,6 +82,12 @@
...
@@ -82,6 +82,12 @@
<artifactId>
discovery-plugin-strategy-starter-gateway
</artifactId>
<artifactId>
discovery-plugin-strategy-starter-gateway
</artifactId>
</dependency>
</dependency>
<!-- 当Spring Coud Gateway用Hystrix做线程隔离的时候,才需要导入下面的包 -->
<!-- <dependency>
<groupId>com.nepxion</groupId>
<artifactId>discovery-plugin-strategy-starter-hystrix</artifactId>
</dependency> -->
<dependency>
<dependency>
<groupId>
de.codecentric
</groupId>
<groupId>
de.codecentric
</groupId>
<artifactId>
spring-boot-admin-starter-client
</artifactId>
<artifactId>
spring-boot-admin-starter-client
</artifactId>
...
...
discovery-springcloud-example-gateway/src/main/java/com/nepxion/discovery/plugin/example/gateway/impl/MyFallback.java
0 → 100644
View file @
2c95314b
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
discovery-springcloud-example-gateway/src/main/resources/application.properties
View file @
2c95314b
...
@@ -28,4 +28,11 @@ spring.cloud.gateway.routes[0].predicates[0]=Path=/discovery-springcloud-example
...
@@ -28,4 +28,11 @@ spring.cloud.gateway.routes[0].predicates[0]=Path=/discovery-springcloud-example
# spring.cloud.gateway.routes[0].filters[0]=SetPath=/discovery-springcloud-example-a/**
# spring.cloud.gateway.routes[0].filters[0]=SetPath=/discovery-springcloud-example-a/**
# Gateway如果用Consul做服务注册发现中心,必须配置StripPrefix=1,其他配置中心配不配无所谓,但建议都配
# Gateway如果用Consul做服务注册发现中心,必须配置StripPrefix=1,其他配置中心配不配无所谓,但建议都配
spring.cloud.gateway.routes[0].filters[0]=
StripPrefix
=
1
spring.cloud.gateway.routes[0].filters[0]=
StripPrefix
=
1
spring.cloud.gateway.routes[0]
.uri
=
lb://discovery-springcloud-example-a
spring.cloud.gateway.routes[0]
.uri
=
lb://discovery-springcloud-example-a
\ No newline at end of file
# 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
discovery-springcloud-example-gateway/src/main/resources/bootstrap.properties
View file @
2c95314b
...
@@ -74,4 +74,6 @@ spring.boot.admin.client.url=http://localhost:5555
...
@@ -74,4 +74,6 @@ spring.boot.admin.client.url=http://localhost:5555
# 开启和关闭Ribbon默认的ZoneAvoidanceRule负载均衡策略。一旦关闭,则使用RoundRobin简单轮询负载均衡策略。缺失则默认为true
# 开启和关闭Ribbon默认的ZoneAvoidanceRule负载均衡策略。一旦关闭,则使用RoundRobin简单轮询负载均衡策略。缺失则默认为true
# spring.application.strategy.zone.avoidance.rule.enabled=true
# spring.application.strategy.zone.avoidance.rule.enabled=true
# 路由策略过滤器的执行顺序(Order)。缺失则默认为9000
# 路由策略过滤器的执行顺序(Order)。缺失则默认为9000
#
spring.application.strategy.gateway.route.filter.order
=
9000
# spring.application.strategy.gateway.route.filter.order=9000
\ No newline at end of file
# 开启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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment