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
804ddb6c
Commit
804ddb6c
authored
Nov 10, 2018
by
Nepxion
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
服务端使用Hystrix做线程模式的服务隔离时,实现服务灰度路由的功能
parent
56373d73
Show whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
170 additions
and
41 deletions
+170
-41
discovery-plugin-strategy-starter-service/src/main/java/com/nepxion/discovery/plugin/strategy/service/adapter/DefaultDiscoveryEnabledAdapter.java
+2
-2
discovery-plugin-strategy-starter-service/src/main/java/com/nepxion/discovery/plugin/strategy/service/aop/FeignStrategyInterceptor.java
+1
-1
discovery-plugin-strategy-starter-service/src/main/java/com/nepxion/discovery/plugin/strategy/service/aop/RestTemplateStrategyInterceptor.java
+1
-1
discovery-plugin-strategy-starter-service/src/main/java/com/nepxion/discovery/plugin/strategy/service/aop/RpcStrategyAutoScanProxy.java
+3
-3
discovery-plugin-strategy-starter-service/src/main/java/com/nepxion/discovery/plugin/strategy/service/aop/RpcStrategyInterceptor.java
+8
-7
discovery-plugin-strategy-starter-service/src/main/java/com/nepxion/discovery/plugin/strategy/service/configuration/ServiceStrategyAutoConfiguration.java
+15
-6
discovery-plugin-strategy-starter-service/src/main/java/com/nepxion/discovery/plugin/strategy/service/context/RestStrategyContext.java
+59
-0
discovery-plugin-strategy-starter-service/src/main/java/com/nepxion/discovery/plugin/strategy/service/context/RpcStrategyContext.java
+8
-8
discovery-plugin-strategy-starter-service/src/main/java/com/nepxion/discovery/plugin/strategy/service/context/ServiceStrategyContextHolder.java
+8
-8
discovery-plugin-strategy-starter-service/src/main/java/com/nepxion/discovery/plugin/strategy/service/wrapper/DefaultCallableWrapper.java
+40
-0
discovery-springcloud-example-service/pom.xml
+6
-0
discovery-springcloud-example-service/src/main/java/com/nepxion/discovery/plugin/example/service/DiscoveryApplicationA1.java
+1
-0
discovery-springcloud-example-service/src/main/java/com/nepxion/discovery/plugin/example/service/feign/AFeignImpl.java
+10
-0
discovery-springcloud-example-service/src/main/java/com/nepxion/discovery/plugin/example/service/impl/MyDiscoveryEnabledStrategy.java
+2
-2
discovery-springcloud-example-service/src/main/resources/bootstrap.properties
+3
-0
discovery-springcloud-example-zuul/pom.xml
+3
-3
No files found.
discovery-plugin-strategy-starter-service/src/main/java/com/nepxion/discovery/plugin/strategy/service/adapter/DefaultDiscoveryEnabledAdapter.java
View file @
804ddb6c
...
@@ -27,7 +27,7 @@ public class DefaultDiscoveryEnabledAdapter extends AbstractDiscoveryEnabledAdap
...
@@ -27,7 +27,7 @@ public class DefaultDiscoveryEnabledAdapter extends AbstractDiscoveryEnabledAdap
@Override
@Override
protected
String
getVersionValue
(
Server
server
)
{
protected
String
getVersionValue
(
Server
server
)
{
ServletRequestAttributes
attributes
=
serviceStrategyContextHolder
.
getRe
que
stAttributes
();
ServletRequestAttributes
attributes
=
serviceStrategyContextHolder
.
getRestAttributes
();
if
(
attributes
==
null
)
{
if
(
attributes
==
null
)
{
String
serviceId
=
server
.
getMetaInfo
().
getAppName
().
toLowerCase
();
String
serviceId
=
server
.
getMetaInfo
().
getAppName
().
toLowerCase
();
...
@@ -41,7 +41,7 @@ public class DefaultDiscoveryEnabledAdapter extends AbstractDiscoveryEnabledAdap
...
@@ -41,7 +41,7 @@ public class DefaultDiscoveryEnabledAdapter extends AbstractDiscoveryEnabledAdap
@Override
@Override
protected
String
getRegionValue
(
Server
server
)
{
protected
String
getRegionValue
(
Server
server
)
{
ServletRequestAttributes
attributes
=
serviceStrategyContextHolder
.
getRe
que
stAttributes
();
ServletRequestAttributes
attributes
=
serviceStrategyContextHolder
.
getRestAttributes
();
if
(
attributes
==
null
)
{
if
(
attributes
==
null
)
{
String
serviceId
=
server
.
getMetaInfo
().
getAppName
().
toLowerCase
();
String
serviceId
=
server
.
getMetaInfo
().
getAppName
().
toLowerCase
();
...
...
discovery-plugin-strategy-starter-service/src/main/java/com/nepxion/discovery/plugin/strategy/service/aop/FeignStrategyInterceptor.java
View file @
804ddb6c
...
@@ -41,7 +41,7 @@ public class FeignStrategyInterceptor implements RequestInterceptor {
...
@@ -41,7 +41,7 @@ public class FeignStrategyInterceptor implements RequestInterceptor {
@Override
@Override
public
void
apply
(
RequestTemplate
requestTemplate
)
{
public
void
apply
(
RequestTemplate
requestTemplate
)
{
ServletRequestAttributes
attributes
=
serviceStrategyContextHolder
.
getRe
que
stAttributes
();
ServletRequestAttributes
attributes
=
serviceStrategyContextHolder
.
getRestAttributes
();
if
(
attributes
==
null
)
{
if
(
attributes
==
null
)
{
return
;
return
;
}
}
...
...
discovery-plugin-strategy-starter-service/src/main/java/com/nepxion/discovery/plugin/strategy/service/aop/RestTemplateStrategyInterceptor.java
View file @
804ddb6c
...
@@ -44,7 +44,7 @@ public class RestTemplateStrategyInterceptor implements ClientHttpRequestInterce
...
@@ -44,7 +44,7 @@ public class RestTemplateStrategyInterceptor implements ClientHttpRequestInterce
@Override
@Override
public
ClientHttpResponse
intercept
(
HttpRequest
request
,
byte
[]
body
,
ClientHttpRequestExecution
execution
)
throws
IOException
{
public
ClientHttpResponse
intercept
(
HttpRequest
request
,
byte
[]
body
,
ClientHttpRequestExecution
execution
)
throws
IOException
{
ServletRequestAttributes
attributes
=
serviceStrategyContextHolder
.
getRe
que
stAttributes
();
ServletRequestAttributes
attributes
=
serviceStrategyContextHolder
.
getRestAttributes
();
if
(
attributes
==
null
)
{
if
(
attributes
==
null
)
{
return
execution
.
execute
(
request
,
body
);
return
execution
.
execute
(
request
,
body
);
}
}
...
...
discovery-plugin-strategy-starter-service/src/main/java/com/nepxion/discovery/plugin/strategy/service/aop/
Service
StrategyAutoScanProxy.java
→
discovery-plugin-strategy-starter-service/src/main/java/com/nepxion/discovery/plugin/strategy/service/aop/
Rpc
StrategyAutoScanProxy.java
View file @
804ddb6c
...
@@ -17,7 +17,7 @@ import com.nepxion.matrix.proxy.aop.DefaultAutoScanProxy;
...
@@ -17,7 +17,7 @@ import com.nepxion.matrix.proxy.aop.DefaultAutoScanProxy;
import
com.nepxion.matrix.proxy.mode.ProxyMode
;
import
com.nepxion.matrix.proxy.mode.ProxyMode
;
import
com.nepxion.matrix.proxy.mode.ScanMode
;
import
com.nepxion.matrix.proxy.mode.ScanMode
;
public
class
Service
StrategyAutoScanProxy
extends
DefaultAutoScanProxy
{
public
class
Rpc
StrategyAutoScanProxy
extends
DefaultAutoScanProxy
{
private
static
final
long
serialVersionUID
=
8436914718400274011L
;
private
static
final
long
serialVersionUID
=
8436914718400274011L
;
private
String
[]
commonInterceptorNames
;
private
String
[]
commonInterceptorNames
;
...
@@ -25,14 +25,14 @@ public class ServiceStrategyAutoScanProxy extends DefaultAutoScanProxy {
...
@@ -25,14 +25,14 @@ public class ServiceStrategyAutoScanProxy extends DefaultAutoScanProxy {
@SuppressWarnings
(
"rawtypes"
)
@SuppressWarnings
(
"rawtypes"
)
private
Class
[]
methodAnnotations
;
private
Class
[]
methodAnnotations
;
public
Service
StrategyAutoScanProxy
(
String
scanPackages
)
{
public
Rpc
StrategyAutoScanProxy
(
String
scanPackages
)
{
super
(
scanPackages
,
ProxyMode
.
BY_CLASS_ANNOTATION_ONLY
,
ScanMode
.
FOR_CLASS_ANNOTATION_ONLY
);
super
(
scanPackages
,
ProxyMode
.
BY_CLASS_ANNOTATION_ONLY
,
ScanMode
.
FOR_CLASS_ANNOTATION_ONLY
);
}
}
@Override
@Override
protected
String
[]
getCommonInterceptorNames
()
{
protected
String
[]
getCommonInterceptorNames
()
{
if
(
commonInterceptorNames
==
null
)
{
if
(
commonInterceptorNames
==
null
)
{
commonInterceptorNames
=
new
String
[]
{
"
service
StrategyInterceptor"
};
commonInterceptorNames
=
new
String
[]
{
"
rpc
StrategyInterceptor"
};
}
}
return
commonInterceptorNames
;
return
commonInterceptorNames
;
...
...
discovery-plugin-strategy-starter-service/src/main/java/com/nepxion/discovery/plugin/strategy/service/aop/
Service
StrategyInterceptor.java
→
discovery-plugin-strategy-starter-service/src/main/java/com/nepxion/discovery/plugin/strategy/service/aop/
Rpc
StrategyInterceptor.java
View file @
804ddb6c
...
@@ -18,11 +18,11 @@ import org.slf4j.Logger;
...
@@ -18,11 +18,11 @@ import org.slf4j.Logger;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
com.nepxion.discovery.plugin.strategy.service.constant.ServiceStrategyConstant
;
import
com.nepxion.discovery.plugin.strategy.service.constant.ServiceStrategyConstant
;
import
com.nepxion.discovery.plugin.strategy.service.context.
Service
StrategyContext
;
import
com.nepxion.discovery.plugin.strategy.service.context.
Rpc
StrategyContext
;
import
com.nepxion.matrix.proxy.aop.AbstractInterceptor
;
import
com.nepxion.matrix.proxy.aop.AbstractInterceptor
;
public
class
Service
StrategyInterceptor
extends
AbstractInterceptor
{
public
class
Rpc
StrategyInterceptor
extends
AbstractInterceptor
{
private
static
final
Logger
LOG
=
LoggerFactory
.
getLogger
(
Service
StrategyInterceptor
.
class
);
private
static
final
Logger
LOG
=
LoggerFactory
.
getLogger
(
Rpc
StrategyInterceptor
.
class
);
@Override
@Override
public
Object
invoke
(
MethodInvocation
invocation
)
throws
Throwable
{
public
Object
invoke
(
MethodInvocation
invocation
)
throws
Throwable
{
...
@@ -46,19 +46,19 @@ public class ServiceStrategyInterceptor extends AbstractInterceptor {
...
@@ -46,19 +46,19 @@ public class ServiceStrategyInterceptor extends AbstractInterceptor {
}
}
}
}
ServiceStrategyContext
context
=
Service
StrategyContext
.
getCurrentContext
();
RpcStrategyContext
context
=
Rpc
StrategyContext
.
getCurrentContext
();
context
.
add
(
ServiceStrategyConstant
.
CLASS
,
proxiedClass
);
context
.
add
(
ServiceStrategyConstant
.
CLASS
,
proxiedClass
);
context
.
add
(
ServiceStrategyConstant
.
METHOD
,
methodName
);
context
.
add
(
ServiceStrategyConstant
.
METHOD
,
methodName
);
context
.
add
(
ServiceStrategyConstant
.
PARAMETER_MAP
,
parameterMap
);
context
.
add
(
ServiceStrategyConstant
.
PARAMETER_MAP
,
parameterMap
);
LOG
.
debug
(
"
Service
strategy context is set with {}"
,
context
);
LOG
.
debug
(
"
Rpc
strategy context is set with {}"
,
context
);
try
{
try
{
return
invocation
.
proceed
();
return
invocation
.
proceed
();
}
finally
{
}
finally
{
Service
StrategyContext
.
clearCurrentContext
();
Rpc
StrategyContext
.
clearCurrentContext
();
LOG
.
debug
(
"
Service
strategy context is cleared"
);
LOG
.
debug
(
"
Rpc
strategy context is cleared"
);
}
}
}
}
}
}
\ No newline at end of file
discovery-plugin-strategy-starter-service/src/main/java/com/nepxion/discovery/plugin/strategy/service/configuration/ServiceStrategyAutoConfiguration.java
View file @
804ddb6c
...
@@ -25,10 +25,12 @@ import com.nepxion.discovery.plugin.strategy.constant.StrategyConstant;
...
@@ -25,10 +25,12 @@ import com.nepxion.discovery.plugin.strategy.constant.StrategyConstant;
import
com.nepxion.discovery.plugin.strategy.service.adapter.DefaultDiscoveryEnabledAdapter
;
import
com.nepxion.discovery.plugin.strategy.service.adapter.DefaultDiscoveryEnabledAdapter
;
import
com.nepxion.discovery.plugin.strategy.service.aop.FeignStrategyInterceptor
;
import
com.nepxion.discovery.plugin.strategy.service.aop.FeignStrategyInterceptor
;
import
com.nepxion.discovery.plugin.strategy.service.aop.RestTemplateStrategyInterceptor
;
import
com.nepxion.discovery.plugin.strategy.service.aop.RestTemplateStrategyInterceptor
;
import
com.nepxion.discovery.plugin.strategy.service.aop.
Service
StrategyAutoScanProxy
;
import
com.nepxion.discovery.plugin.strategy.service.aop.
Rpc
StrategyAutoScanProxy
;
import
com.nepxion.discovery.plugin.strategy.service.aop.
Service
StrategyInterceptor
;
import
com.nepxion.discovery.plugin.strategy.service.aop.
Rpc
StrategyInterceptor
;
import
com.nepxion.discovery.plugin.strategy.service.constant.ServiceStrategyConstant
;
import
com.nepxion.discovery.plugin.strategy.service.constant.ServiceStrategyConstant
;
import
com.nepxion.discovery.plugin.strategy.service.context.ServiceStrategyContextHolder
;
import
com.nepxion.discovery.plugin.strategy.service.context.ServiceStrategyContextHolder
;
import
com.nepxion.discovery.plugin.strategy.service.wrapper.DefaultCallableWrapper
;
import
com.nepxion.discovery.plugin.strategy.wrapper.CallableWrapper
;
@Configuration
@Configuration
@AutoConfigureBefore
(
RibbonClientConfiguration
.
class
)
@AutoConfigureBefore
(
RibbonClientConfiguration
.
class
)
...
@@ -39,7 +41,7 @@ public class ServiceStrategyAutoConfiguration {
...
@@ -39,7 +41,7 @@ public class ServiceStrategyAutoConfiguration {
@Bean
@Bean
@ConditionalOnProperty
(
value
=
ServiceStrategyConstant
.
SPRING_APPLICATION_STRATEGY_SCAN_PACKAGES
,
matchIfMissing
=
false
)
@ConditionalOnProperty
(
value
=
ServiceStrategyConstant
.
SPRING_APPLICATION_STRATEGY_SCAN_PACKAGES
,
matchIfMissing
=
false
)
public
ServiceStrategyAutoScanProxy
service
StrategyAutoScanProxy
()
{
public
RpcStrategyAutoScanProxy
rpc
StrategyAutoScanProxy
()
{
String
scanPackages
=
environment
.
getProperty
(
ServiceStrategyConstant
.
SPRING_APPLICATION_STRATEGY_SCAN_PACKAGES
);
String
scanPackages
=
environment
.
getProperty
(
ServiceStrategyConstant
.
SPRING_APPLICATION_STRATEGY_SCAN_PACKAGES
);
if
(
StringUtils
.
isEmpty
(
scanPackages
))
{
if
(
StringUtils
.
isEmpty
(
scanPackages
))
{
throw
new
DiscoveryException
(
ServiceStrategyConstant
.
SPRING_APPLICATION_STRATEGY_SCAN_PACKAGES
+
"'s value can't be empty, remove it if useless"
);
throw
new
DiscoveryException
(
ServiceStrategyConstant
.
SPRING_APPLICATION_STRATEGY_SCAN_PACKAGES
+
"'s value can't be empty, remove it if useless"
);
...
@@ -49,12 +51,12 @@ public class ServiceStrategyAutoConfiguration {
...
@@ -49,12 +51,12 @@ public class ServiceStrategyAutoConfiguration {
throw
new
DiscoveryException
(
"It can't scan packages for '"
+
ServiceStrategyConstant
.
EXCLUSION_SCAN_PACKAGES
+
"', please check '"
+
ServiceStrategyConstant
.
SPRING_APPLICATION_STRATEGY_SCAN_PACKAGES
+
"'"
);
throw
new
DiscoveryException
(
"It can't scan packages for '"
+
ServiceStrategyConstant
.
EXCLUSION_SCAN_PACKAGES
+
"', please check '"
+
ServiceStrategyConstant
.
SPRING_APPLICATION_STRATEGY_SCAN_PACKAGES
+
"'"
);
}
}
return
new
Service
StrategyAutoScanProxy
(
scanPackages
);
return
new
Rpc
StrategyAutoScanProxy
(
scanPackages
);
}
}
@Bean
@Bean
@ConditionalOnProperty
(
value
=
ServiceStrategyConstant
.
SPRING_APPLICATION_STRATEGY_SCAN_PACKAGES
,
matchIfMissing
=
false
)
@ConditionalOnProperty
(
value
=
ServiceStrategyConstant
.
SPRING_APPLICATION_STRATEGY_SCAN_PACKAGES
,
matchIfMissing
=
false
)
public
ServiceStrategyInterceptor
service
StrategyInterceptor
()
{
public
RpcStrategyInterceptor
rpc
StrategyInterceptor
()
{
String
scanPackages
=
environment
.
getProperty
(
ServiceStrategyConstant
.
SPRING_APPLICATION_STRATEGY_SCAN_PACKAGES
);
String
scanPackages
=
environment
.
getProperty
(
ServiceStrategyConstant
.
SPRING_APPLICATION_STRATEGY_SCAN_PACKAGES
);
if
(
StringUtils
.
isEmpty
(
scanPackages
))
{
if
(
StringUtils
.
isEmpty
(
scanPackages
))
{
throw
new
DiscoveryException
(
ServiceStrategyConstant
.
SPRING_APPLICATION_STRATEGY_SCAN_PACKAGES
+
" can't be empty, remove it if useless"
);
throw
new
DiscoveryException
(
ServiceStrategyConstant
.
SPRING_APPLICATION_STRATEGY_SCAN_PACKAGES
+
" can't be empty, remove it if useless"
);
...
@@ -64,7 +66,7 @@ public class ServiceStrategyAutoConfiguration {
...
@@ -64,7 +66,7 @@ public class ServiceStrategyAutoConfiguration {
throw
new
DiscoveryException
(
"It can't scan packages for '"
+
ServiceStrategyConstant
.
EXCLUSION_SCAN_PACKAGES
+
"', please check '"
+
ServiceStrategyConstant
.
SPRING_APPLICATION_STRATEGY_SCAN_PACKAGES
+
"'"
);
throw
new
DiscoveryException
(
"It can't scan packages for '"
+
ServiceStrategyConstant
.
EXCLUSION_SCAN_PACKAGES
+
"', please check '"
+
ServiceStrategyConstant
.
SPRING_APPLICATION_STRATEGY_SCAN_PACKAGES
+
"'"
);
}
}
return
new
Service
StrategyInterceptor
();
return
new
Rpc
StrategyInterceptor
();
}
}
@Bean
@Bean
...
@@ -99,4 +101,10 @@ public class ServiceStrategyAutoConfiguration {
...
@@ -99,4 +101,10 @@ public class ServiceStrategyAutoConfiguration {
public
ServiceStrategyContextHolder
serviceStrategyContextHolder
()
{
public
ServiceStrategyContextHolder
serviceStrategyContextHolder
()
{
return
new
ServiceStrategyContextHolder
();
return
new
ServiceStrategyContextHolder
();
}
}
@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-service/src/main/java/com/nepxion/discovery/plugin/strategy/service/context/RestStrategyContext.java
0 → 100644
View file @
804ddb6c
package
com
.
nepxion
.
discovery
.
plugin
.
strategy
.
service
.
context
;
/**
* <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.apache.commons.lang3.builder.EqualsBuilder
;
import
org.apache.commons.lang3.builder.HashCodeBuilder
;
import
org.apache.commons.lang3.builder.ToStringBuilder
;
import
org.apache.commons.lang3.builder.ToStringStyle
;
import
org.springframework.web.context.request.ServletRequestAttributes
;
public
class
RestStrategyContext
{
private
static
final
ThreadLocal
<
RestStrategyContext
>
THREAD_LOCAL
=
new
InheritableThreadLocal
<
RestStrategyContext
>()
{
@Override
protected
RestStrategyContext
initialValue
()
{
return
new
RestStrategyContext
();
}
};
public
static
RestStrategyContext
getCurrentContext
()
{
return
THREAD_LOCAL
.
get
();
}
public
static
void
clearCurrentContext
()
{
THREAD_LOCAL
.
remove
();
}
private
ServletRequestAttributes
requestAttributes
;
public
ServletRequestAttributes
getRequestAttributes
()
{
return
requestAttributes
;
}
public
void
setRequestAttributes
(
ServletRequestAttributes
requestAttributes
)
{
this
.
requestAttributes
=
requestAttributes
;
}
@Override
public
int
hashCode
()
{
return
HashCodeBuilder
.
reflectionHashCode
(
this
);
}
@Override
public
boolean
equals
(
Object
object
)
{
return
EqualsBuilder
.
reflectionEquals
(
this
,
object
);
}
@Override
public
String
toString
()
{
return
ToStringBuilder
.
reflectionToString
(
this
,
ToStringStyle
.
MULTI_LINE_STYLE
);
}
}
\ No newline at end of file
discovery-plugin-strategy-starter-service/src/main/java/com/nepxion/discovery/plugin/strategy/service/context/
Service
StrategyContext.java
→
discovery-plugin-strategy-starter-service/src/main/java/com/nepxion/discovery/plugin/strategy/service/context/
Rpc
StrategyContext.java
View file @
804ddb6c
...
@@ -18,15 +18,15 @@ import org.apache.commons.lang3.builder.HashCodeBuilder;
...
@@ -18,15 +18,15 @@ import org.apache.commons.lang3.builder.HashCodeBuilder;
import
org.apache.commons.lang3.builder.ToStringBuilder
;
import
org.apache.commons.lang3.builder.ToStringBuilder
;
import
org.apache.commons.lang3.builder.ToStringStyle
;
import
org.apache.commons.lang3.builder.ToStringStyle
;
public
class
Service
StrategyContext
{
public
class
Rpc
StrategyContext
{
private
static
final
ThreadLocal
<
ServiceStrategyContext
>
THREAD_LOCAL
=
new
InheritableThreadLocal
<
Service
StrategyContext
>()
{
private
static
final
ThreadLocal
<
RpcStrategyContext
>
THREAD_LOCAL
=
new
InheritableThreadLocal
<
Rpc
StrategyContext
>()
{
@Override
@Override
protected
Service
StrategyContext
initialValue
()
{
protected
Rpc
StrategyContext
initialValue
()
{
return
new
Service
StrategyContext
();
return
new
Rpc
StrategyContext
();
}
}
};
};
public
static
Service
StrategyContext
getCurrentContext
()
{
public
static
Rpc
StrategyContext
getCurrentContext
()
{
return
THREAD_LOCAL
.
get
();
return
THREAD_LOCAL
.
get
();
}
}
...
@@ -36,7 +36,7 @@ public class ServiceStrategyContext {
...
@@ -36,7 +36,7 @@ public class ServiceStrategyContext {
private
final
Map
<
String
,
Object
>
attributes
=
new
LinkedHashMap
<
String
,
Object
>();
private
final
Map
<
String
,
Object
>
attributes
=
new
LinkedHashMap
<
String
,
Object
>();
public
Service
StrategyContext
add
(
String
key
,
Object
value
)
{
public
Rpc
StrategyContext
add
(
String
key
,
Object
value
)
{
attributes
.
put
(
key
,
value
);
attributes
.
put
(
key
,
value
);
return
this
;
return
this
;
...
@@ -46,13 +46,13 @@ public class ServiceStrategyContext {
...
@@ -46,13 +46,13 @@ public class ServiceStrategyContext {
return
attributes
.
get
(
key
);
return
attributes
.
get
(
key
);
}
}
public
Service
StrategyContext
remove
(
String
key
)
{
public
Rpc
StrategyContext
remove
(
String
key
)
{
attributes
.
remove
(
key
);
attributes
.
remove
(
key
);
return
this
;
return
this
;
}
}
public
Service
StrategyContext
clear
()
{
public
Rpc
StrategyContext
clear
()
{
attributes
.
clear
();
attributes
.
clear
();
return
this
;
return
this
;
...
...
discovery-plugin-strategy-starter-service/src/main/java/com/nepxion/discovery/plugin/strategy/service/context/ServiceStrategyContextHolder.java
View file @
804ddb6c
package
com
.
nepxion
.
discovery
.
plugin
.
strategy
.
service
.
context
;
package
com
.
nepxion
.
discovery
.
plugin
.
strategy
.
service
.
context
;
import
java.util.Map
;
/**
/**
* <p>Title: Nepxion Discovery</p>
* <p>Title: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
* <p>Description: Nepxion Discovery</p>
...
@@ -11,6 +9,8 @@ import java.util.Map;
...
@@ -11,6 +9,8 @@ import java.util.Map;
* @version 1.0
* @version 1.0
*/
*/
import
java.util.Map
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.core.env.ConfigurableEnvironment
;
import
org.springframework.core.env.ConfigurableEnvironment
;
import
org.springframework.web.context.request.RequestContextHolder
;
import
org.springframework.web.context.request.RequestContextHolder
;
...
@@ -22,17 +22,16 @@ public class ServiceStrategyContextHolder {
...
@@ -22,17 +22,16 @@ public class ServiceStrategyContextHolder {
@Autowired
@Autowired
private
ConfigurableEnvironment
environment
;
private
ConfigurableEnvironment
environment
;
public
ServletRequestAttributes
getRe
que
stAttributes
()
{
public
ServletRequestAttributes
getRestAttributes
()
{
Boolean
hystrixThreadlocalSupported
=
environment
.
getProperty
(
StrategyConstant
.
SPRING_APPLICATION_STRATEGY_HYSTRIX_THREADLOCAL_SUPPORTED
,
Boolean
.
class
,
Boolean
.
FALSE
);
Boolean
hystrixThreadlocalSupported
=
environment
.
getProperty
(
StrategyConstant
.
SPRING_APPLICATION_STRATEGY_HYSTRIX_THREADLOCAL_SUPPORTED
,
Boolean
.
class
,
Boolean
.
FALSE
);
if
(
hystrixThreadlocalSupported
)
{
if
(
hystrixThreadlocalSupported
)
{
// 服务端使用Hystrix做线程模式的服务隔离时,实现服务灰度路由的功能,预留待实现
return
RestStrategyContext
.
getCurrentContext
().
getRequestAttributes
();
return
(
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
();
}
else
{
}
else
{
return
(
ServletRequestAttributes
)
RequestContextHolder
.
ge
tRequestAttributes
();
return
(
ServletRequestAttributes
)
RequestContextHolder
.
curren
tRequestAttributes
();
}
}
}
}
public
Map
<
String
,
Object
>
get
Method
Attributes
()
{
public
Map
<
String
,
Object
>
get
Rpc
Attributes
()
{
return
Service
StrategyContext
.
getCurrentContext
().
getAttributes
();
return
Rpc
StrategyContext
.
getCurrentContext
().
getAttributes
();
}
}
}
}
\ No newline at end of file
discovery-plugin-strategy-starter-service/src/main/java/com/nepxion/discovery/plugin/strategy/service/wrapper/DefaultCallableWrapper.java
0 → 100644
View file @
804ddb6c
package
com
.
nepxion
.
discovery
.
plugin
.
strategy
.
service
.
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.context.request.RequestContextHolder
;
import
org.springframework.web.context.request.ServletRequestAttributes
;
import
com.nepxion.discovery.plugin.strategy.service.context.RestStrategyContext
;
import
com.nepxion.discovery.plugin.strategy.wrapper.CallableWrapper
;
public
class
DefaultCallableWrapper
implements
CallableWrapper
{
@Override
public
<
T
>
Callable
<
T
>
wrapCallable
(
Callable
<
T
>
delegate
)
{
ServletRequestAttributes
requestAttributes
=
(
ServletRequestAttributes
)
RequestContextHolder
.
currentRequestAttributes
();
return
new
Callable
<
T
>()
{
@Override
public
T
call
()
throws
Exception
{
try
{
RestStrategyContext
.
getCurrentContext
().
setRequestAttributes
(
requestAttributes
);
return
delegate
.
call
();
}
finally
{
RestStrategyContext
.
clearCurrentContext
();
}
}
};
}
}
\ No newline at end of file
discovery-springcloud-example-service/pom.xml
View file @
804ddb6c
...
@@ -82,6 +82,12 @@
...
@@ -82,6 +82,12 @@
<artifactId>
discovery-plugin-strategy-starter-service
</artifactId>
<artifactId>
discovery-plugin-strategy-starter-service
</artifactId>
</dependency>
</dependency>
<!-- 当服务用Hystrix做线程隔离的时候,才需要导入下面的包 -->
<!-- <dependency>
<groupId>com.nepxion</groupId>
<artifactId>discovery-plugin-strategy-starter-hystrix</artifactId>
</dependency> -->
<dependency>
<dependency>
<groupId>
org.springframework.cloud
</groupId>
<groupId>
org.springframework.cloud
</groupId>
<artifactId>
spring-cloud-starter-alibaba-sentinel
</artifactId>
<artifactId>
spring-cloud-starter-alibaba-sentinel
</artifactId>
...
...
discovery-springcloud-example-service/src/main/java/com/nepxion/discovery/plugin/example/service/DiscoveryApplicationA1.java
View file @
804ddb6c
...
@@ -32,6 +32,7 @@ import com.nepxion.discovery.plugin.strategy.service.aop.RestTemplateStrategyInt
...
@@ -32,6 +32,7 @@ import com.nepxion.discovery.plugin.strategy.service.aop.RestTemplateStrategyInt
@SpringBootApplication
@SpringBootApplication
@EnableDiscoveryClient
@EnableDiscoveryClient
@EnableFeignClients
@EnableFeignClients
// @EnableCircuitBreaker
public
class
DiscoveryApplicationA1
{
public
class
DiscoveryApplicationA1
{
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
System
.
setProperty
(
"spring.profiles.active"
,
"a1"
);
System
.
setProperty
(
"spring.profiles.active"
,
"a1"
);
...
...
discovery-springcloud-example-service/src/main/java/com/nepxion/discovery/plugin/example/service/feign/AFeignImpl.java
View file @
804ddb6c
...
@@ -27,6 +27,7 @@ public class AFeignImpl extends AbstractFeignImpl implements AFeign {
...
@@ -27,6 +27,7 @@ public class AFeignImpl extends AbstractFeignImpl implements AFeign {
private
BFeign
bFeign
;
private
BFeign
bFeign
;
@Override
@Override
// @HystrixCommand(fallbackMethod = "fallback", commandProperties = { @HystrixProperty(name = "execution.isolation.strategy", value = "THREAD") })
public
String
invoke
(
@RequestBody
String
value
)
{
public
String
invoke
(
@RequestBody
String
value
)
{
value
=
doInvoke
(
value
);
value
=
doInvoke
(
value
);
value
=
bFeign
.
invoke
(
value
);
value
=
bFeign
.
invoke
(
value
);
...
@@ -35,4 +36,12 @@ public class AFeignImpl extends AbstractFeignImpl implements AFeign {
...
@@ -35,4 +36,12 @@ public class AFeignImpl extends AbstractFeignImpl implements AFeign {
return
value
;
return
value
;
}
}
/*public String fallback(String value, Throwable e) {
if (e != null) {
LOG.error("Fallback error", e);
}
return "Fallback by Hystrix";
}*/
}
}
\ No newline at end of file
discovery-springcloud-example-service/src/main/java/com/nepxion/discovery/plugin/example/service/impl/MyDiscoveryEnabledStrategy.java
View file @
804ddb6c
...
@@ -44,7 +44,7 @@ public class MyDiscoveryEnabledStrategy implements DiscoveryEnabledStrategy {
...
@@ -44,7 +44,7 @@ public class MyDiscoveryEnabledStrategy implements DiscoveryEnabledStrategy {
// 根据Rest调用传来的Header参数(例如Token),选取执行调用请求的服务实例
// 根据Rest调用传来的Header参数(例如Token),选取执行调用请求的服务实例
private
boolean
applyFromHeader
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
private
boolean
applyFromHeader
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
ServletRequestAttributes
attributes
=
serviceStrategyContextHolder
.
getRe
que
stAttributes
();
ServletRequestAttributes
attributes
=
serviceStrategyContextHolder
.
getRestAttributes
();
if
(
attributes
==
null
)
{
if
(
attributes
==
null
)
{
return
true
;
return
true
;
}
}
...
@@ -70,7 +70,7 @@ public class MyDiscoveryEnabledStrategy implements DiscoveryEnabledStrategy {
...
@@ -70,7 +70,7 @@ public class MyDiscoveryEnabledStrategy implements DiscoveryEnabledStrategy {
// 根据RPC调用传来的方法参数(例如接口名、方法名、参数名或参数值等),选取执行调用请求的服务实例
// 根据RPC调用传来的方法参数(例如接口名、方法名、参数名或参数值等),选取执行调用请求的服务实例
@SuppressWarnings
(
"unchecked"
)
@SuppressWarnings
(
"unchecked"
)
private
boolean
applyFromMethod
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
private
boolean
applyFromMethod
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
Map
<
String
,
Object
>
attributes
=
serviceStrategyContextHolder
.
get
Method
Attributes
();
Map
<
String
,
Object
>
attributes
=
serviceStrategyContextHolder
.
get
Rpc
Attributes
();
String
serviceId
=
server
.
getMetaInfo
().
getAppName
().
toLowerCase
();
String
serviceId
=
server
.
getMetaInfo
().
getAppName
().
toLowerCase
();
String
version
=
metadata
.
get
(
DiscoveryConstant
.
VERSION
);
String
version
=
metadata
.
get
(
DiscoveryConstant
.
VERSION
);
...
...
discovery-springcloud-example-service/src/main/resources/bootstrap.properties
View file @
804ddb6c
...
@@ -89,3 +89,5 @@ spring.boot.admin.url=http://localhost:5555
...
@@ -89,3 +89,5 @@ spring.boot.admin.url=http://localhost:5555
spring.application.strategy.scan.packages
=
com.nepxion.discovery.plugin.example.service.feign
spring.application.strategy.scan.packages
=
com.nepxion.discovery.plugin.example.service.feign
# 用户自定义和编程灰度路由策略的时候,对REST调用拦截的时候(支持Feign或者RestTemplate调用),需要把来自外部的指定Header参数传递到服务里,如果多个用“;”分隔,不允许出现空格。该项配置只对服务有效,对网关无效。缺失则默认关闭该功能
# 用户自定义和编程灰度路由策略的时候,对REST调用拦截的时候(支持Feign或者RestTemplate调用),需要把来自外部的指定Header参数传递到服务里,如果多个用“;”分隔,不允许出现空格。该项配置只对服务有效,对网关无效。缺失则默认关闭该功能
spring.application.strategy.request.headers
=
version;region;token
spring.application.strategy.request.headers
=
version;region;token
# 开启服务端实现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
discovery-springcloud-example-zuul/pom.xml
View file @
804ddb6c
...
@@ -82,11 +82,11 @@
...
@@ -82,11 +82,11 @@
<artifactId>
discovery-plugin-strategy-starter-zuul
</artifactId>
<artifactId>
discovery-plugin-strategy-starter-zuul
</artifactId>
</dependency>
</dependency>
<!--
Used for zuul hystrix thread isolation
-->
<!--
当Zuul用Hystrix做线程隔离的时候,才需要导入下面的包
-->
<dependency>
<
!-- <
dependency>
<groupId>com.nepxion</groupId>
<groupId>com.nepxion</groupId>
<artifactId>discovery-plugin-strategy-starter-hystrix</artifactId>
<artifactId>discovery-plugin-strategy-starter-hystrix</artifactId>
</dependency>
</dependency>
-->
<dependency>
<dependency>
<groupId>
de.codecentric
</groupId>
<groupId>
de.codecentric
</groupId>
...
...
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