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
37c41831
Commit
37c41831
authored
Aug 21, 2018
by
Nepxion
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
重构策略模块
parent
7c6eaf43
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
105 additions
and
145 deletions
+105
-145
discovery-plugin-strategy-extension-gateway/src/main/java/com/nepxion/discovery/plugin/strategy/extension/gateway/adapter/VersionDiscoveryEnabledAdapter.java
+11
-48
discovery-plugin-strategy-extension-service/src/main/java/com/nepxion/discovery/plugin/strategy/extension/service/adapter/VersionDiscoveryEnabledAdapter.java
+12
-49
discovery-plugin-strategy-extension-zuul/src/main/java/com/nepxion/discovery/plugin/strategy/extension/zuul/adapter/VersionDiscoveryEnabledAdapter.java
+11
-48
discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/adapter/AbstractVersionDiscoveryEnabledAdapter.java
+71
-0
No files found.
discovery-plugin-strategy-extension-gateway/src/main/java/com/nepxion/discovery/plugin/strategy/extension/gateway/adapter/VersionDiscoveryEnabledAdapter.java
View file @
37c41831
...
@@ -9,64 +9,26 @@ package com.nepxion.discovery.plugin.strategy.extension.gateway.adapter;
...
@@ -9,64 +9,26 @@ package com.nepxion.discovery.plugin.strategy.extension.gateway.adapter;
* @version 1.0
* @version 1.0
*/
*/
import
java.util.Map
;
import
org.springframework.http.server.reactive.ServerHttpRequest
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
com.nepxion.discovery.common.constant.DiscoveryConstant
;
import
com.nepxion.discovery.common.constant.DiscoveryConstant
;
import
com.nepxion.discovery.common.util.JsonUtil
;
import
com.nepxion.discovery.plugin.strategy.adapter.AbstractVersionDiscoveryEnabledAdapter
;
import
com.nepxion.discovery.plugin.strategy.adapter.DiscoveryEnabledAdapter
;
import
com.nepxion.discovery.plugin.strategy.adapter.DiscoveryEnabledStrategy
;
import
com.nepxion.discovery.plugin.strategy.extension.gateway.context.GatewayStrategyContext
;
import
com.nepxion.discovery.plugin.strategy.extension.gateway.context.GatewayStrategyContext
;
import
com.netflix.loadbalancer.Server
;
public
class
VersionDiscoveryEnabledAdapter
implements
DiscoveryEnabledAdapter
{
@Autowired
(
required
=
false
)
private
DiscoveryEnabledStrategy
discoveryEnabledStrategy
;
public
class
VersionDiscoveryEnabledAdapter
extends
AbstractVersionDiscoveryEnabledAdapter
{
@Override
@Override
p
ublic
boolean
apply
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
p
rotected
String
getVersionJson
(
)
{
boolean
enabled
=
applyVersion
(
server
,
metadata
);
ServerHttpRequest
request
=
getRequest
(
);
if
(
!
enabled
)
{
if
(
request
==
null
)
{
return
false
;
return
null
;
}
}
return
applyStrategy
(
server
,
metadata
);
return
request
.
getHeaders
().
getFirst
(
DiscoveryConstant
.
VERSION
);
}
}
@SuppressWarnings
(
"unchecked"
)
public
ServerHttpRequest
getRequest
()
{
private
boolean
applyVersion
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
GatewayStrategyContext
context
=
GatewayStrategyContext
.
getCurrentContext
();
GatewayStrategyContext
context
=
GatewayStrategyContext
.
getCurrentContext
();
String
versionJson
=
context
.
getExchange
().
getRequest
().
getHeaders
().
getFirst
(
DiscoveryConstant
.
VERSION
);
if
(
StringUtils
.
isEmpty
(
versionJson
))
{
return
true
;
}
String
serviceId
=
server
.
getMetaInfo
().
getAppName
().
toLowerCase
();
String
version
=
metadata
.
get
(
DiscoveryConstant
.
VERSION
);
if
(
StringUtils
.
isEmpty
(
version
))
{
return
true
;
}
Map
<
String
,
String
>
versionMap
=
JsonUtil
.
fromJson
(
versionJson
,
Map
.
class
);
String
versions
=
versionMap
.
get
(
serviceId
);
if
(
versions
==
null
)
{
return
true
;
}
if
(
versions
.
contains
(
version
))
{
return
true
;
}
return
false
;
}
private
boolean
applyStrategy
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
if
(
discoveryEnabledStrategy
==
null
)
{
return
true
;
}
return
discoveryEnabledStrategy
.
apply
(
server
,
metadata
);
return
context
.
getExchange
().
getRequest
(
);
}
}
}
}
\ No newline at end of file
discovery-plugin-strategy-extension-service/src/main/java/com/nepxion/discovery/plugin/strategy/extension/service/adapter/VersionDiscoveryEnabledAdapter.java
View file @
37c41831
...
@@ -9,69 +9,31 @@ package com.nepxion.discovery.plugin.strategy.extension.service.adapter;
...
@@ -9,69 +9,31 @@ package com.nepxion.discovery.plugin.strategy.extension.service.adapter;
* @version 1.0
* @version 1.0
*/
*/
import
java
.util.Map
;
import
java
x.servlet.http.HttpServletRequest
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.context.request.RequestContextHolder
;
import
org.springframework.web.context.request.RequestContextHolder
;
import
org.springframework.web.context.request.ServletRequestAttributes
;
import
org.springframework.web.context.request.ServletRequestAttributes
;
import
com.nepxion.discovery.common.constant.DiscoveryConstant
;
import
com.nepxion.discovery.common.constant.DiscoveryConstant
;
import
com.nepxion.discovery.common.util.JsonUtil
;
import
com.nepxion.discovery.plugin.strategy.adapter.AbstractVersionDiscoveryEnabledAdapter
;
import
com.nepxion.discovery.plugin.strategy.adapter.DiscoveryEnabledAdapter
;
import
com.nepxion.discovery.plugin.strategy.adapter.DiscoveryEnabledStrategy
;
import
com.netflix.loadbalancer.Server
;
public
class
VersionDiscoveryEnabledAdapter
implements
DiscoveryEnabledAdapter
{
@Autowired
(
required
=
false
)
private
DiscoveryEnabledStrategy
discoveryEnabledStrategy
;
public
class
VersionDiscoveryEnabledAdapter
extends
AbstractVersionDiscoveryEnabledAdapter
{
@Override
@Override
p
ublic
boolean
apply
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
p
rotected
String
getVersionJson
(
)
{
boolean
enabled
=
applyVersion
(
server
,
metadata
);
HttpServletRequest
request
=
getRequest
(
);
if
(
!
enabled
)
{
if
(
request
==
null
)
{
return
false
;
return
null
;
}
}
return
applyStrategy
(
server
,
metadata
);
return
request
.
getHeader
(
DiscoveryConstant
.
VERSION
);
}
}
@SuppressWarnings
(
"unchecked"
)
public
HttpServletRequest
getRequest
()
{
private
boolean
applyVersion
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
ServletRequestAttributes
attributes
=
(
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
();
ServletRequestAttributes
attributes
=
(
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
();
if
(
attributes
==
null
)
{
if
(
attributes
==
null
)
{
return
true
;
return
null
;
}
String
versionJson
=
attributes
.
getRequest
().
getHeader
(
DiscoveryConstant
.
VERSION
);
if
(
StringUtils
.
isEmpty
(
versionJson
))
{
return
true
;
}
String
serviceId
=
server
.
getMetaInfo
().
getAppName
().
toLowerCase
();
String
version
=
metadata
.
get
(
DiscoveryConstant
.
VERSION
);
if
(
StringUtils
.
isEmpty
(
version
))
{
return
true
;
}
Map
<
String
,
String
>
versionMap
=
JsonUtil
.
fromJson
(
versionJson
,
Map
.
class
);
String
versions
=
versionMap
.
get
(
serviceId
);
if
(
versions
==
null
)
{
return
true
;
}
if
(
versions
.
contains
(
version
))
{
return
true
;
}
return
false
;
}
private
boolean
applyStrategy
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
if
(
discoveryEnabledStrategy
==
null
)
{
return
true
;
}
}
return
discoveryEnabledStrategy
.
apply
(
server
,
metadata
);
return
attributes
.
getRequest
(
);
}
}
}
}
\ No newline at end of file
discovery-plugin-strategy-extension-zuul/src/main/java/com/nepxion/discovery/plugin/strategy/extension/zuul/adapter/VersionDiscoveryEnabledAdapter.java
View file @
37c41831
...
@@ -9,64 +9,26 @@ package com.nepxion.discovery.plugin.strategy.extension.zuul.adapter;
...
@@ -9,64 +9,26 @@ package com.nepxion.discovery.plugin.strategy.extension.zuul.adapter;
* @version 1.0
* @version 1.0
*/
*/
import
java.util.Map
;
import
javax.servlet.http.HttpServletRequest
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
com.nepxion.discovery.common.constant.DiscoveryConstant
;
import
com.nepxion.discovery.common.constant.DiscoveryConstant
;
import
com.nepxion.discovery.common.util.JsonUtil
;
import
com.nepxion.discovery.plugin.strategy.adapter.AbstractVersionDiscoveryEnabledAdapter
;
import
com.nepxion.discovery.plugin.strategy.adapter.DiscoveryEnabledAdapter
;
import
com.nepxion.discovery.plugin.strategy.adapter.DiscoveryEnabledStrategy
;
import
com.netflix.loadbalancer.Server
;
import
com.netflix.zuul.context.RequestContext
;
import
com.netflix.zuul.context.RequestContext
;
public
class
VersionDiscoveryEnabledAdapter
implements
DiscoveryEnabledAdapter
{
public
class
VersionDiscoveryEnabledAdapter
extends
AbstractVersionDiscoveryEnabledAdapter
{
@Autowired
(
required
=
false
)
private
DiscoveryEnabledStrategy
discoveryEnabledStrategy
;
@Override
@Override
p
ublic
boolean
apply
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
p
rotected
String
getVersionJson
(
)
{
boolean
enabled
=
applyVersion
(
server
,
metadata
);
HttpServletRequest
request
=
getRequest
(
);
if
(
!
enabled
)
{
if
(
request
==
null
)
{
return
false
;
return
null
;
}
}
return
applyStrategy
(
server
,
metadata
);
return
request
.
getHeader
(
DiscoveryConstant
.
VERSION
);
}
}
@SuppressWarnings
(
"unchecked"
)
public
HttpServletRequest
getRequest
()
{
private
boolean
applyVersion
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
RequestContext
context
=
RequestContext
.
getCurrentContext
();
RequestContext
context
=
RequestContext
.
getCurrentContext
();
String
versionJson
=
context
.
getRequest
().
getHeader
(
DiscoveryConstant
.
VERSION
);
if
(
StringUtils
.
isEmpty
(
versionJson
))
{
return
true
;
}
String
serviceId
=
server
.
getMetaInfo
().
getAppName
().
toLowerCase
();
String
version
=
metadata
.
get
(
DiscoveryConstant
.
VERSION
);
if
(
StringUtils
.
isEmpty
(
version
))
{
return
true
;
}
Map
<
String
,
String
>
versionMap
=
JsonUtil
.
fromJson
(
versionJson
,
Map
.
class
);
String
versions
=
versionMap
.
get
(
serviceId
);
if
(
versions
==
null
)
{
return
true
;
}
if
(
versions
.
contains
(
version
))
{
return
true
;
}
return
false
;
}
private
boolean
applyStrategy
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
if
(
discoveryEnabledStrategy
==
null
)
{
return
true
;
}
return
discoveryEnabledStrategy
.
apply
(
server
,
metadata
);
return
context
.
getRequest
(
);
}
}
}
}
\ No newline at end of file
discovery-plugin-strategy/src/main/java/com/nepxion/discovery/plugin/strategy/adapter/AbstractVersionDiscoveryEnabledAdapter.java
0 → 100644
View file @
37c41831
package
com
.
nepxion
.
discovery
.
plugin
.
strategy
.
adapter
;
/**
* <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
java.util.Map
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
com.nepxion.discovery.common.constant.DiscoveryConstant
;
import
com.nepxion.discovery.common.util.JsonUtil
;
import
com.netflix.loadbalancer.Server
;
public
abstract
class
AbstractVersionDiscoveryEnabledAdapter
implements
DiscoveryEnabledAdapter
{
@Autowired
(
required
=
false
)
private
DiscoveryEnabledStrategy
discoveryEnabledStrategy
;
@Override
public
boolean
apply
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
boolean
enabled
=
applyVersion
(
server
,
metadata
);
if
(!
enabled
)
{
return
false
;
}
return
applyStrategy
(
server
,
metadata
);
}
@SuppressWarnings
(
"unchecked"
)
private
boolean
applyVersion
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
String
versionJson
=
getVersionJson
();
if
(
StringUtils
.
isEmpty
(
versionJson
))
{
return
true
;
}
String
serviceId
=
server
.
getMetaInfo
().
getAppName
().
toLowerCase
();
String
version
=
metadata
.
get
(
DiscoveryConstant
.
VERSION
);
if
(
StringUtils
.
isEmpty
(
version
))
{
return
true
;
}
Map
<
String
,
String
>
versionMap
=
JsonUtil
.
fromJson
(
versionJson
,
Map
.
class
);
String
versions
=
versionMap
.
get
(
serviceId
);
if
(
versions
==
null
)
{
return
true
;
}
if
(
versions
.
contains
(
version
))
{
return
true
;
}
return
false
;
}
private
boolean
applyStrategy
(
Server
server
,
Map
<
String
,
String
>
metadata
)
{
if
(
discoveryEnabledStrategy
==
null
)
{
return
true
;
}
return
discoveryEnabledStrategy
.
apply
(
server
,
metadata
);
}
protected
abstract
String
getVersionJson
();
}
\ 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