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
b3999a2f
Commit
b3999a2f
authored
Feb 01, 2019
by
Nepxion
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
增加IP地址和端口去路由的功能
parent
56176321
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
79 additions
and
1 deletions
+79
-1
discovery-common/src/main/java/com/nepxion/discovery/common/constant/DiscoveryConstant.java
+1
-0
discovery-plugin-strategy-starter-gateway/src/main/java/com/nepxion/discovery/plugin/strategy/gateway/adapter/DefaultDiscoveryEnabledAdapter.java
+15
-0
discovery-plugin-strategy-starter-service/src/main/java/com/nepxion/discovery/plugin/strategy/service/adapter/DefaultDiscoveryEnabledAdapter.java
+15
-0
discovery-plugin-strategy-starter-zuul/src/main/java/com/nepxion/discovery/plugin/strategy/zuul/adapter/DefaultDiscoveryEnabledAdapter.java
+15
-0
discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/adapter/AbstractDiscoveryEnabledAdapter.java
+32
-0
discovery-springcloud-example-service/src/main/resources/bootstrap.properties
+1
-1
No files found.
discovery-common/src/main/java/com/nepxion/discovery/common/constant/DiscoveryConstant.java
View file @
b3999a2f
...
...
@@ -25,6 +25,7 @@ public class DiscoveryConstant {
public
static
final
String
SPRING_APPLICATION_NAME
=
"spring.application.name"
;
public
static
final
String
GROUP
=
"group"
;
public
static
final
String
REGION
=
"region"
;
public
static
final
String
ADDRESS
=
"address"
;
public
static
final
String
SERVICE_ID
=
"serviceId"
;
public
static
final
String
HOST
=
"host"
;
public
static
final
String
PORT
=
"port"
;
...
...
discovery-plugin-strategy-starter-gateway/src/main/java/com/nepxion/discovery/plugin/strategy/gateway/adapter/DefaultDiscoveryEnabledAdapter.java
View file @
b3999a2f
...
...
@@ -52,4 +52,18 @@ public class DefaultDiscoveryEnabledAdapter extends AbstractDiscoveryEnabledAdap
return
exchange
.
getRequest
().
getHeaders
().
getFirst
(
DiscoveryConstant
.
REGION
);
}
@Override
protected
String
getAddressValue
(
Server
server
)
{
ServerWebExchange
exchange
=
gatewayStrategyContextHolder
.
getExchange
();
if
(
exchange
==
null
)
{
String
serviceId
=
server
.
getMetaInfo
().
getAppName
().
toLowerCase
();
LOG
.
warn
(
"The ServerWebExchange object is null, ignore to do region filter for service={}..."
,
serviceId
);
return
null
;
}
return
exchange
.
getRequest
().
getHeaders
().
getFirst
(
DiscoveryConstant
.
ADDRESS
);
}
}
\ No newline at end of file
discovery-plugin-strategy-starter-service/src/main/java/com/nepxion/discovery/plugin/strategy/service/adapter/DefaultDiscoveryEnabledAdapter.java
View file @
b3999a2f
...
...
@@ -52,4 +52,18 @@ public class DefaultDiscoveryEnabledAdapter extends AbstractDiscoveryEnabledAdap
return
attributes
.
getRequest
().
getHeader
(
DiscoveryConstant
.
REGION
);
}
@Override
protected
String
getAddressValue
(
Server
server
)
{
ServletRequestAttributes
attributes
=
serviceStrategyContextHolder
.
getRestAttributes
();
if
(
attributes
==
null
)
{
String
serviceId
=
server
.
getMetaInfo
().
getAppName
().
toLowerCase
();
LOG
.
warn
(
"The ServletRequestAttributes object is null, ignore to do region filter for service={}..."
,
serviceId
);
return
null
;
}
return
attributes
.
getRequest
().
getHeader
(
DiscoveryConstant
.
ADDRESS
);
}
}
\ No newline at end of file
discovery-plugin-strategy-starter-zuul/src/main/java/com/nepxion/discovery/plugin/strategy/zuul/adapter/DefaultDiscoveryEnabledAdapter.java
View file @
b3999a2f
...
...
@@ -53,4 +53,18 @@ public class DefaultDiscoveryEnabledAdapter extends AbstractDiscoveryEnabledAdap
return
request
.
getHeader
(
DiscoveryConstant
.
REGION
);
}
@Override
protected
String
getAddressValue
(
Server
server
)
{
HttpServletRequest
request
=
zuulStrategyContextHolder
.
getRequest
();
if
(
request
==
null
)
{
String
serviceId
=
server
.
getMetaInfo
().
getAppName
().
toLowerCase
();
LOG
.
warn
(
"The HttpServletRequest object is null, ignore to do region filter for service={}..."
,
serviceId
);
return
null
;
}
return
request
.
getHeader
(
DiscoveryConstant
.
ADDRESS
);
}
}
\ No newline at end of file
discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/adapter/AbstractDiscoveryEnabledAdapter.java
View file @
b3999a2f
...
...
@@ -9,6 +9,7 @@ package com.nepxion.discovery.plugin.strategy.adapter;
* @version 1.0
*/
import
java.util.List
;
import
java.util.Map
;
import
org.apache.commons.lang3.StringUtils
;
...
...
@@ -16,6 +17,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import
com.nepxion.discovery.common.constant.DiscoveryConstant
;
import
com.nepxion.discovery.common.util.JsonUtil
;
import
com.nepxion.discovery.common.util.StringUtil
;
import
com.netflix.loadbalancer.Server
;
public
abstract
class
AbstractDiscoveryEnabledAdapter
implements
DiscoveryEnabledAdapter
{
...
...
@@ -34,6 +36,11 @@ public abstract class AbstractDiscoveryEnabledAdapter implements DiscoveryEnable
return
false
;
}
enabled
=
applyAddress
(
server
,
metadata
);
if
(!
enabled
)
{
return
false
;
}
return
applyStrategy
(
server
,
metadata
);
}
...
...
@@ -81,6 +88,28 @@ public abstract class AbstractDiscoveryEnabledAdapter implements DiscoveryEnable
return
false
;
}
@SuppressWarnings
(
"unchecked"
)
private
boolean
applyAddress
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
String
addressValue
=
getAddressValue
(
server
);
if
(
StringUtils
.
isEmpty
(
addressValue
))
{
return
true
;
}
String
serviceId
=
server
.
getMetaInfo
().
getAppName
().
toLowerCase
();
Map
<
String
,
String
>
addressMap
=
JsonUtil
.
fromJson
(
addressValue
,
Map
.
class
);
String
addresses
=
addressMap
.
get
(
serviceId
);
if
(
addresses
==
null
)
{
return
true
;
}
List
<
String
>
addressList
=
StringUtil
.
splitToList
(
addresses
,
DiscoveryConstant
.
SEPARATE
);
if
(
addressList
.
contains
(
server
.
getHostPort
())
||
addressList
.
contains
(
server
.
getHost
()))
{
return
true
;
}
return
false
;
}
private
boolean
applyStrategy
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
if
(
discoveryEnabledStrategy
==
null
)
{
return
true
;
...
...
@@ -92,4 +121,6 @@ public abstract class AbstractDiscoveryEnabledAdapter implements DiscoveryEnable
protected
abstract
String
getVersionValue
(
Server
server
);
protected
abstract
String
getRegionValue
(
Server
server
);
protected
abstract
String
getAddressValue
(
Server
server
);
}
\ No newline at end of file
discovery-springcloud-example-service/src/main/resources/bootstrap.properties
View file @
b3999a2f
...
...
@@ -93,7 +93,7 @@ spring.application.strategy.scan.packages=com.nepxion.discovery.plugin.example.s
# 启动和关闭用户自定义和编程灰度路由策略的时候,对REST方式的调用拦截。缺失则默认为false
spring.application.strategy.rest.intercept.enabled
=
true
# 用户自定义和编程灰度路由策略的时候,对REST方式调用拦截的时候(支持Feign或者RestTemplate调用),需要把来自外部的指定Header参数传递到服务里,如果多个用“;”分隔,不允许出现空格。该项配置只对服务有效,对网关无效
spring.application.strategy.request.headers
=
version;region;token
spring.application.strategy.request.headers
=
version;region;
address;
token
# 启动和关闭用户自定义和编程灰度路由策略的时候日志打印,注意每调用一次都会打印一次,会对性能有所影响,建议压测环境和生产环境关闭。缺失则默认为false
spring.application.strategy.intercept.log.print
=
true
# 开启服务端实现Hystrix线程隔离模式做服务隔离时,必须把spring.application.strategy.hystrix.threadlocal.supported设置为true,同时要引入discovery-plugin-strategy-starter-hystrix包,否则线程切换时会发生ThreadLocal上下文对象丢失
...
...
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