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
435654a9
Commit
435654a9
authored
Jul 26, 2018
by
Nepxion
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
扩展支持多格式的规则,包括XML和JSON
parent
49341f86
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
362 additions
and
4 deletions
+362
-4
discovery-plugin-config-center/src/main/java/com/nepxion/discovery/plugin/configcenter/configuration/ConfigAutoConfiguration.java
+12
-1
discovery-plugin-config-center/src/main/java/com/nepxion/discovery/plugin/configcenter/parser/json/JsonConfigParser.java
+42
-0
discovery-plugin-config-center/src/main/java/com/nepxion/discovery/plugin/configcenter/parser/json/jackson/JacksonSerializer.java
+69
-0
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/constant/PluginConstant.java
+5
-1
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/context/PluginContextAware.java
+8
-0
discovery-springcloud-example-gateway/src/main/resources/bootstrap.properties
+5
-1
discovery-springcloud-example-gateway/src/main/resources/rule.json
+108
-0
discovery-springcloud-example/src/main/resources/bootstrap.properties
+5
-1
discovery-springcloud-example/src/main/resources/rule.json
+108
-0
No files found.
discovery-plugin-config-center/src/main/java/com/nepxion/discovery/plugin/configcenter/configuration/ConfigAutoConfiguration.java
View file @
435654a9
...
@@ -9,15 +9,19 @@ package com.nepxion.discovery.plugin.configcenter.configuration;
...
@@ -9,15 +9,19 @@ package com.nepxion.discovery.plugin.configcenter.configuration;
* @version 1.0
* @version 1.0
*/
*/
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Configuration
;
import
com.nepxion.discovery.plugin.configcenter.ConfigInitializer
;
import
com.nepxion.discovery.plugin.configcenter.ConfigInitializer
;
import
com.nepxion.discovery.plugin.configcenter.loader.LocalConfigLoader
;
import
com.nepxion.discovery.plugin.configcenter.loader.LocalConfigLoader
;
import
com.nepxion.discovery.plugin.configcenter.parser.json.JsonConfigParser
;
import
com.nepxion.discovery.plugin.configcenter.parser.xml.XmlConfigParser
;
import
com.nepxion.discovery.plugin.configcenter.parser.xml.XmlConfigParser
;
import
com.nepxion.discovery.plugin.framework.config.PluginConfigParser
;
import
com.nepxion.discovery.plugin.framework.config.PluginConfigParser
;
import
com.nepxion.discovery.plugin.framework.constant.PluginConstant
;
import
com.nepxion.discovery.plugin.framework.context.PluginContextAware
;
import
com.nepxion.discovery.plugin.framework.context.PluginContextAware
;
import
com.nepxion.discovery.plugin.framework.exception.PluginException
;
@Configuration
@Configuration
public
class
ConfigAutoConfiguration
{
public
class
ConfigAutoConfiguration
{
...
@@ -26,7 +30,14 @@ public class ConfigAutoConfiguration {
...
@@ -26,7 +30,14 @@ public class ConfigAutoConfiguration {
@Bean
@Bean
public
PluginConfigParser
pluginConfigParser
()
{
public
PluginConfigParser
pluginConfigParser
()
{
return
new
XmlConfigParser
();
String
configFormat
=
pluginContextAware
.
getConfigFormat
();
if
(
StringUtils
.
equals
(
configFormat
,
PluginConstant
.
XML_FORMAT
))
{
return
new
XmlConfigParser
();
}
else
if
(
StringUtils
.
equals
(
configFormat
,
PluginConstant
.
JSON_FORMAT
))
{
return
new
JsonConfigParser
();
}
throw
new
PluginException
(
"Invalid config format for '"
+
configFormat
+
"'"
);
}
}
@Bean
@Bean
...
...
discovery-plugin-config-center/src/main/java/com/nepxion/discovery/plugin/configcenter/parser/json/JsonConfigParser.java
0 → 100644
View file @
435654a9
package
com
.
nepxion
.
discovery
.
plugin
.
configcenter
.
parser
.
json
;
/**
* <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.StringUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
com.nepxion.discovery.plugin.configcenter.parser.json.jackson.JacksonSerializer
;
import
com.nepxion.discovery.plugin.framework.config.PluginConfigParser
;
import
com.nepxion.discovery.plugin.framework.entity.RuleEntity
;
import
com.nepxion.discovery.plugin.framework.exception.PluginException
;
public
class
JsonConfigParser
implements
PluginConfigParser
{
private
static
final
Logger
LOG
=
LoggerFactory
.
getLogger
(
JsonConfigParser
.
class
);
@Override
public
RuleEntity
parse
(
String
config
)
{
if
(
StringUtils
.
isEmpty
(
config
))
{
throw
new
PluginException
(
"Config is null or empty"
);
}
try
{
RuleEntity
ruleEntity
=
JacksonSerializer
.
fromJson
(
config
,
RuleEntity
.
class
);
ruleEntity
.
setContent
(
config
);
LOG
.
info
(
"Rule entity=\n{}"
,
ruleEntity
);
return
ruleEntity
;
}
catch
(
Exception
e
)
{
throw
new
PluginException
(
e
.
getMessage
(),
e
);
}
}
}
\ No newline at end of file
discovery-plugin-config-center/src/main/java/com/nepxion/discovery/plugin/configcenter/parser/json/jackson/JacksonSerializer.java
0 → 100644
View file @
435654a9
package
com
.
nepxion
.
discovery
.
plugin
.
configcenter
.
parser
.
json
.
jackson
;
/**
* <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.text.SimpleDateFormat
;
import
org.apache.commons.lang3.StringUtils
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.core.type.TypeReference
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.nepxion.discovery.plugin.framework.constant.PluginConstant
;
public
class
JacksonSerializer
{
private
static
ObjectMapper
objectMapper
;
static
{
objectMapper
=
new
ObjectMapper
();
objectMapper
.
setDateFormat
(
new
SimpleDateFormat
(
PluginConstant
.
DATE_FORMAT
));
}
public
static
<
T
>
String
toJson
(
T
object
)
{
if
(
object
==
null
)
{
throw
new
IllegalArgumentException
(
"Object is null"
);
}
try
{
return
objectMapper
.
writeValueAsString
(
object
);
}
catch
(
JsonProcessingException
e
)
{
throw
new
IllegalArgumentException
(
e
.
getMessage
(),
e
);
}
}
public
static
<
T
>
T
fromJson
(
String
json
,
Class
<
T
>
clazz
)
{
if
(
StringUtils
.
isEmpty
(
json
))
{
throw
new
IllegalArgumentException
(
"Json is null or empty"
);
}
try
{
return
objectMapper
.
readValue
(
json
,
clazz
);
}
catch
(
Exception
e
)
{
throw
new
IllegalArgumentException
(
e
.
getMessage
(),
e
);
}
}
public
static
<
T
>
T
fromJson
(
String
json
,
TypeReference
<
T
>
typeReference
)
{
if
(
StringUtils
.
isEmpty
(
json
))
{
throw
new
IllegalArgumentException
(
"Json is null or empty"
);
}
try
{
return
objectMapper
.
readValue
(
json
,
typeReference
);
}
catch
(
Exception
e
)
{
throw
new
IllegalArgumentException
(
e
.
getMessage
(),
e
);
}
}
public
static
ObjectMapper
getObjectMapper
()
{
return
objectMapper
;
}
}
\ No newline at end of file
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/constant/PluginConstant.java
View file @
435654a9
...
@@ -14,9 +14,10 @@ public class PluginConstant {
...
@@ -14,9 +14,10 @@ public class PluginConstant {
public
static
final
String
SPRING_APPLICATION_REGISTER_CONTROL_ENABLED
=
"spring.application.register.control.enabled"
;
public
static
final
String
SPRING_APPLICATION_REGISTER_CONTROL_ENABLED
=
"spring.application.register.control.enabled"
;
public
static
final
String
SPRING_APPLICATION_DISCOVERY_CONTROL_ENABLED
=
"spring.application.discovery.control.enabled"
;
public
static
final
String
SPRING_APPLICATION_DISCOVERY_CONTROL_ENABLED
=
"spring.application.discovery.control.enabled"
;
public
static
final
String
SPRING_APPLICATION_CONFIG_REST_CONTROL_ENABLED
=
"spring.application.config.rest.control.enabled"
;
public
static
final
String
SPRING_APPLICATION_CONFIG_REST_CONTROL_ENABLED
=
"spring.application.config.rest.control.enabled"
;
public
static
final
String
SPRING_APPLICATION_CONFIG_FORMAT
=
"spring.application.config.format"
;
public
static
final
String
SPRING_APPLICATION_CONFIG_PATH
=
"spring.application.config.path"
;
public
static
final
String
SPRING_APPLICATION_CONFIG_PATH
=
"spring.application.config.path"
;
public
static
final
String
SPRING_APPLICATION_GROUP_KEY
=
"spring.application.group.key"
;
public
static
final
String
SPRING_APPLICATION_GROUP_KEY
=
"spring.application.group.key"
;
public
static
final
String
SPRING_APPLICATION_NAME
=
"spring.application.name"
;
public
static
final
String
SPRING_APPLICATION_NAME
=
"spring.application.name"
;
public
static
final
String
GROUP
=
"group"
;
public
static
final
String
GROUP
=
"group"
;
public
static
final
String
SERVICE_ID
=
"serviceId"
;
public
static
final
String
SERVICE_ID
=
"serviceId"
;
...
@@ -29,6 +30,9 @@ public class PluginConstant {
...
@@ -29,6 +30,9 @@ public class PluginConstant {
public
static
final
String
DYNAMIC_RULE
=
"dynamicRule"
;
public
static
final
String
DYNAMIC_RULE
=
"dynamicRule"
;
public
static
final
String
REACH_MAX_LIMITED_COUNT
=
"reach max limited count"
;
public
static
final
String
REACH_MAX_LIMITED_COUNT
=
"reach max limited count"
;
public
static
final
String
XML_FORMAT
=
"xml"
;
public
static
final
String
JSON_FORMAT
=
"json"
;
public
static
final
String
DATE_FORMAT
=
"yyyy-MM-dd HH:mm:ss.SSS"
;
public
static
final
String
DATE_FORMAT
=
"yyyy-MM-dd HH:mm:ss.SSS"
;
public
static
final
String
ENCODING_UTF_8
=
"UTF-8"
;
public
static
final
String
ENCODING_UTF_8
=
"UTF-8"
;
public
static
final
String
SEPARATE
=
";"
;
public
static
final
String
SEPARATE
=
";"
;
...
...
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/context/PluginContextAware.java
View file @
435654a9
...
@@ -96,6 +96,10 @@ public class PluginContextAware implements ApplicationContextAware {
...
@@ -96,6 +96,10 @@ public class PluginContextAware implements ApplicationContextAware {
return
isConfigRestControlEnabled
(
environment
);
return
isConfigRestControlEnabled
(
environment
);
}
}
public
String
getConfigFormat
()
{
return
getConfigFormat
(
environment
);
}
public
String
getConfigPath
()
{
public
String
getConfigPath
()
{
return
getConfigPath
(
environment
);
return
getConfigPath
(
environment
);
}
}
...
@@ -116,6 +120,10 @@ public class PluginContextAware implements ApplicationContextAware {
...
@@ -116,6 +120,10 @@ public class PluginContextAware implements ApplicationContextAware {
return
environment
.
getProperty
(
PluginConstant
.
SPRING_APPLICATION_CONFIG_REST_CONTROL_ENABLED
,
Boolean
.
class
,
Boolean
.
TRUE
);
return
environment
.
getProperty
(
PluginConstant
.
SPRING_APPLICATION_CONFIG_REST_CONTROL_ENABLED
,
Boolean
.
class
,
Boolean
.
TRUE
);
}
}
public
static
String
getConfigFormat
(
Environment
environment
)
{
return
environment
.
getProperty
(
PluginConstant
.
SPRING_APPLICATION_CONFIG_FORMAT
,
PluginConstant
.
XML_FORMAT
);
}
public
static
String
getConfigPath
(
Environment
environment
)
{
public
static
String
getConfigPath
(
Environment
environment
)
{
return
environment
.
getProperty
(
PluginConstant
.
SPRING_APPLICATION_CONFIG_PATH
);
return
environment
.
getProperty
(
PluginConstant
.
SPRING_APPLICATION_CONFIG_PATH
);
}
}
...
...
discovery-springcloud-example-gateway/src/main/resources/bootstrap.properties
View file @
435654a9
...
@@ -33,8 +33,12 @@ spring.application.register.control.enabled=true
...
@@ -33,8 +33,12 @@ spring.application.register.control.enabled=true
spring.application.discovery.control.enabled
=
true
spring.application.discovery.control.enabled
=
true
# 开启和关闭通过Rest方式对规则配置的控制和推送。一旦关闭,只能通过远程配置中心来控制和推送。缺失则默认为true
# 开启和关闭通过Rest方式对规则配置的控制和推送。一旦关闭,只能通过远程配置中心来控制和推送。缺失则默认为true
spring.application.config.rest.control.enabled
=
true
spring.application.config.rest.control.enabled
=
true
# 本地规则文件的路径,支持两种方式:classpath:rule.xml - 规则文件放在resources目录下,便于打包进jar;file:rule.xml - 规则文件放在工程根目录下,放置在外部便于修改。缺失则默认为不装载本地规则
# 规则文件的格式,支持xml和json。缺失则默认为xml
spring.application.config.format
=
xml
# spring.application.config.format=json
# 本地规则文件的路径,支持两种方式:classpath:rule.xml(rule.json) - 规则文件放在resources目录下,便于打包进jar;file:rule.xml(rule.json) - 规则文件放在工程根目录下,放置在外部便于修改。缺失则默认为不装载本地规则
spring.application.config.path
=
classpath:rule.xml
spring.application.config.path
=
classpath:rule.xml
# spring.application.config.path=classpath:rule.json
# 为微服务归类的Key,一般通过group字段来归类,例如eureka.instance.metadataMap.group=xxx-group或者eureka.instance.metadataMap.application=xxx-application。缺失则默认为group
# 为微服务归类的Key,一般通过group字段来归类,例如eureka.instance.metadataMap.group=xxx-group或者eureka.instance.metadataMap.application=xxx-application。缺失则默认为group
# spring.application.group.key=group
# spring.application.group.key=group
# spring.application.group.key=application
# spring.application.group.key=application
...
...
discovery-springcloud-example-gateway/src/main/resources/rule.json
0 → 100644
View file @
435654a9
{
"registerEntity"
:
{
"hostFilterEntity"
:
{
"filterType"
:
"BLACKLIST"
,
"filterValueList"
:
[
"10.10"
,
"11.11"
],
"filterMap"
:
{
"discovery-springcloud-example-a"
:
[
"172.16"
]
}
},
"countFilterEntity"
:
{
"filterValue"
:
10000
,
"filterMap"
:
{
"discovery-springcloud-example-a"
:
5000
}
}
},
"discoveryEntity"
:
{
"hostFilterEntity"
:
{
"filterType"
:
"BLACKLIST"
,
"filterValueList"
:
[
"10.10"
,
"11.11"
],
"filterMap"
:
{
"discovery-springcloud-example-b"
:
[
"172.16"
]
}
},
"versionFilterEntity"
:
{
"serviceEntityMap"
:
{
"discovery-springcloud-example-zuul"
:
[
{
"consumerServiceName"
:
"discovery-springcloud-example-zuul"
,
"providerServiceName"
:
"discovery-springcloud-example-a"
,
"consumerVersionValueList"
:
[
"1.0"
],
"providerVersionValueList"
:
[
"1.0"
]
},
{
"consumerServiceName"
:
"discovery-springcloud-example-zuul"
,
"providerServiceName"
:
"discovery-springcloud-example-a"
,
"consumerVersionValueList"
:
[
"1.1"
],
"providerVersionValueList"
:
[
"1.1"
]
}
],
"discovery-springcloud-example-a"
:
[
{
"consumerServiceName"
:
"discovery-springcloud-example-a"
,
"providerServiceName"
:
"discovery-springcloud-example-b"
,
"consumerVersionValueList"
:
[
"1.0"
],
"providerVersionValueList"
:
[
"1.0"
]
},
{
"consumerServiceName"
:
"discovery-springcloud-example-a"
,
"providerServiceName"
:
"discovery-springcloud-example-b"
,
"consumerVersionValueList"
:
[
"1.1"
],
"providerVersionValueList"
:
[
"1.1"
]
}
],
"discovery-springcloud-example-b"
:
[
{
"consumerServiceName"
:
"discovery-springcloud-example-b"
,
"providerServiceName"
:
"discovery-springcloud-example-c"
,
"consumerVersionValueList"
:
[
"1.0"
],
"providerVersionValueList"
:
[
"1.0"
,
"1.1"
]
},
{
"consumerServiceName"
:
"discovery-springcloud-example-b"
,
"providerServiceName"
:
"discovery-springcloud-example-c"
,
"consumerVersionValueList"
:
[
"1.1"
],
"providerVersionValueList"
:
[
"1.2"
]
}
]
}
}
}
}
\ No newline at end of file
discovery-springcloud-example/src/main/resources/bootstrap.properties
View file @
435654a9
...
@@ -33,8 +33,12 @@ spring.application.register.control.enabled=true
...
@@ -33,8 +33,12 @@ spring.application.register.control.enabled=true
spring.application.discovery.control.enabled
=
true
spring.application.discovery.control.enabled
=
true
# 开启和关闭通过Rest方式对规则配置的控制和推送。一旦关闭,只能通过远程配置中心来控制和推送。缺失则默认为true
# 开启和关闭通过Rest方式对规则配置的控制和推送。一旦关闭,只能通过远程配置中心来控制和推送。缺失则默认为true
spring.application.config.rest.control.enabled
=
true
spring.application.config.rest.control.enabled
=
true
# 本地规则文件的路径,支持两种方式:classpath:rule.xml - 规则文件放在resources目录下,便于打包进jar;file:rule.xml - 规则文件放在工程根目录下,放置在外部便于修改。缺失则默认为不装载本地规则
# 规则文件的格式,支持xml和json。缺失则默认为xml
spring.application.config.format
=
xml
# spring.application.config.format=json
# 本地规则文件的路径,支持两种方式:classpath:rule.xml(rule.json) - 规则文件放在resources目录下,便于打包进jar;file:rule.xml(rule.json) - 规则文件放在工程根目录下,放置在外部便于修改。缺失则默认为不装载本地规则
spring.application.config.path
=
classpath:rule.xml
spring.application.config.path
=
classpath:rule.xml
# spring.application.config.path=classpath:rule.json
# 为微服务归类的Key,一般通过group字段来归类,例如eureka.instance.metadataMap.group=xxx-group或者eureka.instance.metadataMap.application=xxx-application。缺失则默认为group
# 为微服务归类的Key,一般通过group字段来归类,例如eureka.instance.metadataMap.group=xxx-group或者eureka.instance.metadataMap.application=xxx-application。缺失则默认为group
# spring.application.group.key=group
# spring.application.group.key=group
# spring.application.group.key=application
# spring.application.group.key=application
...
...
discovery-springcloud-example/src/main/resources/rule.json
0 → 100644
View file @
435654a9
{
"registerEntity"
:
{
"hostFilterEntity"
:
{
"filterType"
:
"BLACKLIST"
,
"filterValueList"
:
[
"10.10"
,
"11.11"
],
"filterMap"
:
{
"discovery-springcloud-example-a"
:
[
"172.16"
]
}
},
"countFilterEntity"
:
{
"filterValue"
:
10000
,
"filterMap"
:
{
"discovery-springcloud-example-a"
:
5000
}
}
},
"discoveryEntity"
:
{
"hostFilterEntity"
:
{
"filterType"
:
"BLACKLIST"
,
"filterValueList"
:
[
"10.10"
,
"11.11"
],
"filterMap"
:
{
"discovery-springcloud-example-b"
:
[
"172.16"
]
}
},
"versionFilterEntity"
:
{
"serviceEntityMap"
:
{
"discovery-springcloud-example-zuul"
:
[
{
"consumerServiceName"
:
"discovery-springcloud-example-zuul"
,
"providerServiceName"
:
"discovery-springcloud-example-a"
,
"consumerVersionValueList"
:
[
"1.0"
],
"providerVersionValueList"
:
[
"1.0"
]
},
{
"consumerServiceName"
:
"discovery-springcloud-example-zuul"
,
"providerServiceName"
:
"discovery-springcloud-example-a"
,
"consumerVersionValueList"
:
[
"1.1"
],
"providerVersionValueList"
:
[
"1.1"
]
}
],
"discovery-springcloud-example-a"
:
[
{
"consumerServiceName"
:
"discovery-springcloud-example-a"
,
"providerServiceName"
:
"discovery-springcloud-example-b"
,
"consumerVersionValueList"
:
[
"1.0"
],
"providerVersionValueList"
:
[
"1.0"
]
},
{
"consumerServiceName"
:
"discovery-springcloud-example-a"
,
"providerServiceName"
:
"discovery-springcloud-example-b"
,
"consumerVersionValueList"
:
[
"1.1"
],
"providerVersionValueList"
:
[
"1.1"
]
}
],
"discovery-springcloud-example-b"
:
[
{
"consumerServiceName"
:
"discovery-springcloud-example-b"
,
"providerServiceName"
:
"discovery-springcloud-example-c"
,
"consumerVersionValueList"
:
[
"1.0"
],
"providerVersionValueList"
:
[
"1.0"
,
"1.1"
]
},
{
"consumerServiceName"
:
"discovery-springcloud-example-b"
,
"providerServiceName"
:
"discovery-springcloud-example-c"
,
"consumerVersionValueList"
:
[
"1.1"
],
"providerVersionValueList"
:
[
"1.2"
]
}
]
}
}
}
}
\ 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