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
2cf18ac2
Commit
2cf18ac2
authored
Jul 21, 2018
by
Nepxion
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
拆分获取本地配置和远程配置
parent
57157137
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
89 additions
and
47 deletions
+89
-47
discovery-plugin-config-center/src/main/java/com/nepxion/discovery/plugin/configcenter/ConfigAdapter.java
+2
-2
discovery-plugin-config-center/src/main/java/com/nepxion/discovery/plugin/configcenter/ConfigInitializer.java
+22
-19
discovery-plugin-config-center/src/main/java/com/nepxion/discovery/plugin/configcenter/configuration/ConfigAutoConfiguration.java
+16
-0
discovery-plugin-config-center/src/main/java/com/nepxion/discovery/plugin/configcenter/loader/ConfigLoader.java
+2
-3
discovery-plugin-config-center/src/main/java/com/nepxion/discovery/plugin/configcenter/loader/LocalConfigLoader.java
+8
-7
discovery-plugin-config-center/src/main/java/com/nepxion/discovery/plugin/configcenter/loader/RemoteConfigLoader.java
+15
-0
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/constant/PluginConstant.java
+1
-0
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/context/PluginContextAware.java
+9
-0
discovery-springcloud-example/src/main/java/com/nepxion/discovery/plugin/example/DiscoveryApplicationA1.java
+2
-3
discovery-springcloud-example/src/main/java/com/nepxion/discovery/plugin/example/adapter/MyConfigAdapter.java
+7
-13
discovery-springcloud-example/src/main/resources/bootstrap.properties
+5
-0
No files found.
discovery-plugin-config-center/src/main/java/com/nepxion/discovery/plugin/configcenter/ConfigAdapter.java
View file @
2cf18ac2
...
@@ -11,11 +11,11 @@ package com.nepxion.discovery.plugin.configcenter;
...
@@ -11,11 +11,11 @@ package com.nepxion.discovery.plugin.configcenter;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
com.nepxion.discovery.plugin.configcenter.loader.
Abstract
ConfigLoader
;
import
com.nepxion.discovery.plugin.configcenter.loader.
Remote
ConfigLoader
;
import
com.nepxion.discovery.plugin.framework.event.PluginEventWapper
;
import
com.nepxion.discovery.plugin.framework.event.PluginEventWapper
;
import
com.nepxion.discovery.plugin.framework.event.RuleUpdatedEvent
;
import
com.nepxion.discovery.plugin.framework.event.RuleUpdatedEvent
;
public
abstract
class
ConfigAdapter
extends
Abstract
ConfigLoader
{
public
abstract
class
ConfigAdapter
extends
Remote
ConfigLoader
{
@Autowired
@Autowired
private
PluginEventWapper
pluginEventWapper
;
private
PluginEventWapper
pluginEventWapper
;
...
...
discovery-plugin-config-center/src/main/java/com/nepxion/discovery/plugin/configcenter/ConfigInitializer.java
View file @
2cf18ac2
...
@@ -17,7 +17,8 @@ import org.slf4j.Logger;
...
@@ -17,7 +17,8 @@ import org.slf4j.Logger;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
com.nepxion.discovery.plugin.configcenter.loader.ConfigLoader
;
import
com.nepxion.discovery.plugin.configcenter.loader.LocalConfigLoader
;
import
com.nepxion.discovery.plugin.configcenter.loader.RemoteConfigLoader
;
import
com.nepxion.discovery.plugin.framework.adapter.PluginAdapter
;
import
com.nepxion.discovery.plugin.framework.adapter.PluginAdapter
;
import
com.nepxion.discovery.plugin.framework.context.PluginContextAware
;
import
com.nepxion.discovery.plugin.framework.context.PluginContextAware
;
import
com.nepxion.discovery.plugin.framework.entity.RuleEntity
;
import
com.nepxion.discovery.plugin.framework.entity.RuleEntity
;
...
@@ -31,8 +32,11 @@ public class ConfigInitializer {
...
@@ -31,8 +32,11 @@ public class ConfigInitializer {
@Autowired
@Autowired
private
PluginAdapter
pluginAdapter
;
private
PluginAdapter
pluginAdapter
;
@Autowired
private
LocalConfigLoader
localConfigLoader
;
@Autowired
(
required
=
false
)
@Autowired
(
required
=
false
)
private
ConfigLoader
c
onfigLoader
;
private
RemoteConfigLoader
remoteC
onfigLoader
;
@Autowired
@Autowired
private
ConfigParser
configParser
;
private
ConfigParser
configParser
;
...
@@ -48,15 +52,9 @@ public class ConfigInitializer {
...
@@ -48,15 +52,9 @@ public class ConfigInitializer {
return
;
return
;
}
}
if
(
configLoader
==
null
)
{
LOG
.
info
(
"********** ConfigLoader isn't provided, ignore to initialize **********"
);
return
;
}
LOG
.
info
(
"********** Rule starts to initialize **********"
);
LOG
.
info
(
"********** Rule starts to initialize **********"
);
InputStream
inputStream
=
getInputStream
(
configLoader
);
InputStream
inputStream
=
getInputStream
();
try
{
try
{
RuleEntity
ruleEntity
=
configParser
.
parse
(
inputStream
);
RuleEntity
ruleEntity
=
configParser
.
parse
(
inputStream
);
pluginAdapter
.
setLocalRule
(
ruleEntity
);
pluginAdapter
.
setLocalRule
(
ruleEntity
);
...
@@ -65,22 +63,27 @@ public class ConfigInitializer {
...
@@ -65,22 +63,27 @@ public class ConfigInitializer {
}
}
}
}
public
InputStream
getInputStream
(
ConfigLoader
configLoader
)
{
public
InputStream
getInputStream
()
{
InputStream
inputStream
=
null
;
InputStream
inputStream
=
null
;
try
{
inputStream
=
configLoader
.
getRemoteInputStream
();
}
catch
(
Exception
e
)
{
LOG
.
warn
(
"Get remote input stream failed"
,
e
);
}
if
(
inputStream
!=
null
)
{
if
(
remoteConfigLoader
!=
null
)
{
LOG
.
info
(
"********** Remote input stream is retrieved **********"
);
try
{
inputStream
=
remoteConfigLoader
.
getInputStream
();
}
catch
(
Exception
e
)
{
LOG
.
warn
(
"Get remote input stream failed"
,
e
);
}
return
inputStream
;
if
(
inputStream
!=
null
)
{
LOG
.
info
(
"********** Remote input stream is retrieved **********"
);
return
inputStream
;
}
}
else
{
LOG
.
info
(
"********** Remote config loader isn't provided, use local config loader **********"
);
}
}
try
{
try
{
inputStream
=
configLoader
.
getLocal
InputStream
();
inputStream
=
localConfigLoader
.
get
InputStream
();
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
LOG
.
warn
(
"Get local input stream failed"
,
e
);
LOG
.
warn
(
"Get local input stream failed"
,
e
);
}
}
...
...
discovery-plugin-config-center/src/main/java/com/nepxion/discovery/plugin/configcenter/configuration/ConfigAutoConfiguration.java
View file @
2cf18ac2
...
@@ -9,14 +9,30 @@ package com.nepxion.discovery.plugin.configcenter.configuration;
...
@@ -9,14 +9,30 @@ package com.nepxion.discovery.plugin.configcenter.configuration;
* @version 1.0
* @version 1.0
*/
*/
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.ConfigParser
;
import
com.nepxion.discovery.plugin.configcenter.ConfigParser
;
import
com.nepxion.discovery.plugin.configcenter.loader.LocalConfigLoader
;
import
com.nepxion.discovery.plugin.framework.context.PluginContextAware
;
@Configuration
@Configuration
public
class
ConfigAutoConfiguration
{
public
class
ConfigAutoConfiguration
{
@Autowired
private
PluginContextAware
pluginContextAware
;
@Bean
public
LocalConfigLoader
localConfigLoader
()
{
return
new
LocalConfigLoader
()
{
@Override
protected
String
getPath
()
{
return
pluginContextAware
.
getConfigPath
();
}
};
}
@Bean
@Bean
public
ConfigInitializer
configInitializer
()
{
public
ConfigInitializer
configInitializer
()
{
return
new
ConfigInitializer
();
return
new
ConfigInitializer
();
...
...
discovery-plugin-config-center/src/main/java/com/nepxion/discovery/plugin/configcenter/loader/ConfigLoader.java
View file @
2cf18ac2
...
@@ -12,7 +12,5 @@ package com.nepxion.discovery.plugin.configcenter.loader;
...
@@ -12,7 +12,5 @@ package com.nepxion.discovery.plugin.configcenter.loader;
import
java.io.InputStream
;
import
java.io.InputStream
;
public
interface
ConfigLoader
{
public
interface
ConfigLoader
{
InputStream
getLocalInputStream
()
throws
Exception
;
InputStream
getInputStream
()
throws
Exception
;
InputStream
getRemoteInputStream
()
throws
Exception
;
}
}
\ No newline at end of file
discovery-plugin-config-center/src/main/java/com/nepxion/discovery/plugin/configcenter/loader/
Abstract
ConfigLoader.java
→
discovery-plugin-config-center/src/main/java/com/nepxion/discovery/plugin/configcenter/loader/
Local
ConfigLoader.java
View file @
2cf18ac2
...
@@ -15,21 +15,21 @@ import org.apache.commons.lang3.StringUtils;
...
@@ -15,21 +15,21 @@ import org.apache.commons.lang3.StringUtils;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.context.ApplicationContext
;
import
org.springframework.context.ApplicationContext
;
public
abstract
class
Abstract
ConfigLoader
implements
ConfigLoader
{
public
abstract
class
Local
ConfigLoader
implements
ConfigLoader
{
@Autowired
@Autowired
private
ApplicationContext
applicationContext
;
private
ApplicationContext
applicationContext
;
@Override
@Override
public
InputStream
get
Local
InputStream
()
throws
Exception
{
public
InputStream
getInputStream
()
throws
Exception
{
String
localContextPath
=
getLocalContex
tPath
();
String
path
=
ge
tPath
();
if
(
StringUtils
.
isEmpty
(
localContextP
ath
))
{
if
(
StringUtils
.
isEmpty
(
p
ath
))
{
return
null
;
return
null
;
}
}
String
localFilePath
=
applicationContext
.
getEnvironment
().
resolvePlaceholders
(
localContextP
ath
);
String
filePath
=
applicationContext
.
getEnvironment
().
resolvePlaceholders
(
p
ath
);
return
applicationContext
.
getResource
(
localF
ilePath
).
getInputStream
();
return
applicationContext
.
getResource
(
f
ilePath
).
getInputStream
();
}
}
protected
abstract
String
get
LocalContext
Path
();
protected
abstract
String
getPath
();
}
}
\ No newline at end of file
discovery-plugin-config-center/src/main/java/com/nepxion/discovery/plugin/configcenter/loader/RemoteConfigLoader.java
0 → 100644
View file @
2cf18ac2
package
com
.
nepxion
.
discovery
.
plugin
.
configcenter
.
loader
;
/**
* <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
*/
public
abstract
class
RemoteConfigLoader
implements
ConfigLoader
{
}
\ No newline at end of file
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/constant/PluginConstant.java
View file @
2cf18ac2
...
@@ -14,6 +14,7 @@ public class PluginConstant {
...
@@ -14,6 +14,7 @@ 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_PATH
=
"spring.application.config.path"
;
public
static
final
String
SPRING_APPLICATION_NAME
=
"spring.application.name"
;
public
static
final
String
SPRING_APPLICATION_NAME
=
"spring.application.name"
;
public
static
final
String
SERVICE_ID
=
"serviceId"
;
public
static
final
String
SERVICE_ID
=
"serviceId"
;
...
...
discovery-plugin-framework/src/main/java/com/nepxion/discovery/plugin/framework/context/PluginContextAware.java
View file @
2cf18ac2
...
@@ -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
getConfigPath
()
{
return
getConfigPath
(
environment
);
}
public
static
Boolean
isRegisterControlEnabled
(
Environment
environment
)
{
public
static
Boolean
isRegisterControlEnabled
(
Environment
environment
)
{
return
environment
.
getProperty
(
PluginConstant
.
SPRING_APPLICATION_REGISTER_CONTROL_ENABLED
,
Boolean
.
class
,
Boolean
.
TRUE
);
return
environment
.
getProperty
(
PluginConstant
.
SPRING_APPLICATION_REGISTER_CONTROL_ENABLED
,
Boolean
.
class
,
Boolean
.
TRUE
);
}
}
...
@@ -107,4 +111,8 @@ public class PluginContextAware implements ApplicationContextAware {
...
@@ -107,4 +111,8 @@ public class PluginContextAware implements ApplicationContextAware {
public
static
Boolean
isConfigRestControlEnabled
(
Environment
environment
)
{
public
static
Boolean
isConfigRestControlEnabled
(
Environment
environment
)
{
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
getConfigPath
(
Environment
environment
)
{
return
environment
.
getProperty
(
PluginConstant
.
SPRING_APPLICATION_CONFIG_PATH
);
}
}
}
\ No newline at end of file
discovery-springcloud-example/src/main/java/com/nepxion/discovery/plugin/example/DiscoveryApplicationA1.java
View file @
2cf18ac2
...
@@ -15,7 +15,6 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
...
@@ -15,7 +15,6 @@ import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import
org.springframework.cloud.openfeign.EnableFeignClients
;
import
org.springframework.cloud.openfeign.EnableFeignClients
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
import
com.nepxion.discovery.plugin.example.adapter.MyConfigAdapter
;
import
com.nepxion.discovery.plugin.example.extension.MyDiscoveryListener
;
import
com.nepxion.discovery.plugin.example.extension.MyDiscoveryListener
;
import
com.nepxion.discovery.plugin.example.extension.MyRegisterListener
;
import
com.nepxion.discovery.plugin.example.extension.MyRegisterListener
;
import
com.nepxion.discovery.plugin.example.extension.MySubscriber
;
import
com.nepxion.discovery.plugin.example.extension.MySubscriber
;
...
@@ -30,10 +29,10 @@ public class DiscoveryApplicationA1 {
...
@@ -30,10 +29,10 @@ public class DiscoveryApplicationA1 {
new
SpringApplicationBuilder
(
DiscoveryApplicationA1
.
class
).
run
(
args
);
new
SpringApplicationBuilder
(
DiscoveryApplicationA1
.
class
).
run
(
args
);
}
}
@Bean
/*
@Bean
public MyConfigAdapter myConfigAdapter() {
public MyConfigAdapter myConfigAdapter() {
return new MyConfigAdapter();
return new MyConfigAdapter();
}
}
*/
@Bean
@Bean
public
MyRegisterListener
myRegisterListener
()
{
public
MyRegisterListener
myRegisterListener
()
{
...
...
discovery-springcloud-example/src/main/java/com/nepxion/discovery/plugin/example/adapter/MyConfigAdapter.java
View file @
2cf18ac2
...
@@ -12,33 +12,26 @@ package com.nepxion.discovery.plugin.example.adapter;
...
@@ -12,33 +12,26 @@ package com.nepxion.discovery.plugin.example.adapter;
import
java.io.File
;
import
java.io.File
;
import
java.io.InputStream
;
import
java.io.InputStream
;
import
javax.annotation.PostConstruct
;
import
org.apache.commons.io.FileUtils
;
import
org.apache.commons.io.FileUtils
;
import
com.nepxion.discovery.plugin.configcenter.ConfigAdapter
;
import
com.nepxion.discovery.plugin.configcenter.ConfigAdapter
;
import
com.nepxion.discovery.plugin.framework.event.RuleUpdatedEvent
;
// 模拟主动从本地或远程配置中心获取规则
// 模拟主动从本地或远程配置中心获取规则
// 模拟订阅远程配置中心的规则更新
// 模拟订阅远程配置中心的规则更新
public
class
MyConfigAdapter
extends
ConfigAdapter
{
public
class
MyConfigAdapter
extends
ConfigAdapter
{
// 从本地获取规则
@Override
protected
String
getLocalContextPath
()
{
// 规则文件放在resources目录下
return
"classpath:rule.xml"
;
// 规则文件放在工程根目录下
// return "file:rule.xml";
}
// 从远程配置中心获取规则
// 从远程配置中心获取规则
@Override
@Override
public
InputStream
get
Remote
InputStream
()
throws
Exception
{
public
InputStream
getInputStream
()
throws
Exception
{
return
FileUtils
.
openInputStream
(
new
File
(
"src/main/resources/rule.xml"
));
return
FileUtils
.
openInputStream
(
new
File
(
"src/main/resources/rule.xml"
));
}
}
// 订阅远程配置中心的规则更新(推送策略自己决定,可以所有服务都只对应一个规则信息,也可以根据服务名获取对应的规则信息)
// 订阅远程配置中心的规则更新(推送策略自己决定,可以所有服务都只对应一个规则信息,也可以根据服务名获取对应的规则信息)
/*
@PostConstruct
@PostConstruct
public
void
update
()
throws
Exception
{
public
void
update
()
throws
Exception
{
InputStream
inputStream
=
FileUtils
.
openInputStream
(
new
File
(
"src/main/resources/rule.xml"
));
InputStream
inputStream
=
FileUtils
.
openInputStream
(
new
File
(
"src/main/resources/rule.xml"
));
fireRuleUpdated
(
new
RuleUpdatedEvent
(
inputStream
),
true
);
fireRuleUpdated
(
new
RuleUpdatedEvent
(
inputStream
),
true
);
}
*/
}
}
}
\ No newline at end of file
discovery-springcloud-example/src/main/resources/bootstrap.properties
View file @
2cf18ac2
...
@@ -29,6 +29,11 @@ spring.application.register.control.enabled=true
...
@@ -29,6 +29,11 @@ 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,规则文件放在工程根目录下,便于把规则文件放置在外部,方便修改
spring.application.config.path
=
classpath:rule.xml
# Swagger config
# Swagger config
swagger.service.base.package
=
com.nepxion.discovery.plugin.admincenter.endpoint
swagger.service.base.package
=
com.nepxion.discovery.plugin.admincenter.endpoint
...
...
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