From 5e183b974a293e3450078eb4b82c3370f0b6d516 Mon Sep 17 00:00:00 2001 From: Nepxion <1394997@qq.com> Date: Fri, 22 Jun 2018 00:38:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitattributes | 2 + .gitignore | 32 +++ COPYRIGHT | 5 + LICENSE | 201 ++++++++++++++++ discovery-plugin-starter/pom.xml | 23 ++ .../main/resources/META-INF/spring.factories | 5 + discovery-plugin/pom.xml | 48 ++++ .../plugin/config/DiscoveryPluginConfig.java | 63 +++++ .../config/DiscoveryPluginConfigLoader.java | 93 ++++++++ .../config/DiscoveryPluginConfigParser.java | 139 +++++++++++ .../constant/DiscoveryPluginConstant.java | 32 +++ ...iscoveryApplicationContextInitializer.java | 45 ++++ .../decorator/DiscoveryClientDecorator.java | 60 +++++ .../EurekaServiceRegistryDecorator.java | 69 ++++++ .../plugin/entity/ConsumerEntity.java | 61 +++++ .../plugin/entity/DiscoveryEntity.java | 55 +++++ .../discovery/plugin/entity/FilterEntity.java | 70 ++++++ .../discovery/plugin/entity/FilterType.java | 46 ++++ .../plugin/entity/VersionEntity.java | 52 ++++ .../exception/DiscoveryPluginException.java | 30 +++ .../plugin/loader/AbstractFileLoader.java | 36 +++ .../discovery/plugin/loader/FileLoader.java | 19 ++ .../plugin/strategy/FilterStrategy.java | 44 ++++ .../plugin/strategy/VersionStrategy.java | 72 ++++++ .../plugin/util/BeanRegisterUtil.java | 35 +++ .../discovery/plugin/xml/Dom4JConstant.java | 15 ++ .../discovery/plugin/xml/Dom4JParser.java | 65 +++++ .../discovery/plugin/xml/Dom4JReader.java | 123 ++++++++++ .../discovery/plugin/xml/Dom4JWriter.java | 51 ++++ discovery-springcloud-example-a/pom.xml | 68 ++++++ .../example/DiscoveryPluginApplication.java | 22 ++ .../example/DiscoveryPluginController.java | 30 +++ .../example/DiscoveryPluginSimulator.java | 85 +++++++ .../src/main/resources/application.properties | 12 + .../src/main/resources/discovery1.xml | 24 ++ .../src/main/resources/discovery2.xml | 24 ++ .../src/main/resources/discovery3.xml | 24 ++ .../src/main/resources/logback.xml | 49 ++++ discovery-springcloud-example-b1/pom.xml | 62 +++++ .../example/DiscoveryPluginApplicationB1.java | 22 ++ .../src/main/resources/application.properties | 5 + .../src/main/resources/logback.xml | 49 ++++ discovery-springcloud-example-b2/pom.xml | 62 +++++ .../example/DiscoveryPluginApplicationB2.java | 22 ++ .../src/main/resources/application.properties | 5 + .../src/main/resources/logback.xml | 49 ++++ pom.xml | 223 ++++++++++++++++++ version.bat | 20 ++ 48 files changed, 2448 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 COPYRIGHT create mode 100644 LICENSE create mode 100644 discovery-plugin-starter/pom.xml create mode 100644 discovery-plugin-starter/src/main/resources/META-INF/spring.factories create mode 100644 discovery-plugin/pom.xml create mode 100644 discovery-plugin/src/main/java/com/nepxion/discovery/plugin/config/DiscoveryPluginConfig.java create mode 100644 discovery-plugin/src/main/java/com/nepxion/discovery/plugin/config/DiscoveryPluginConfigLoader.java create mode 100644 discovery-plugin/src/main/java/com/nepxion/discovery/plugin/config/DiscoveryPluginConfigParser.java create mode 100644 discovery-plugin/src/main/java/com/nepxion/discovery/plugin/constant/DiscoveryPluginConstant.java create mode 100644 discovery-plugin/src/main/java/com/nepxion/discovery/plugin/context/DiscoveryApplicationContextInitializer.java create mode 100644 discovery-plugin/src/main/java/com/nepxion/discovery/plugin/decorator/DiscoveryClientDecorator.java create mode 100644 discovery-plugin/src/main/java/com/nepxion/discovery/plugin/decorator/EurekaServiceRegistryDecorator.java create mode 100644 discovery-plugin/src/main/java/com/nepxion/discovery/plugin/entity/ConsumerEntity.java create mode 100644 discovery-plugin/src/main/java/com/nepxion/discovery/plugin/entity/DiscoveryEntity.java create mode 100644 discovery-plugin/src/main/java/com/nepxion/discovery/plugin/entity/FilterEntity.java create mode 100644 discovery-plugin/src/main/java/com/nepxion/discovery/plugin/entity/FilterType.java create mode 100644 discovery-plugin/src/main/java/com/nepxion/discovery/plugin/entity/VersionEntity.java create mode 100644 discovery-plugin/src/main/java/com/nepxion/discovery/plugin/exception/DiscoveryPluginException.java create mode 100644 discovery-plugin/src/main/java/com/nepxion/discovery/plugin/loader/AbstractFileLoader.java create mode 100644 discovery-plugin/src/main/java/com/nepxion/discovery/plugin/loader/FileLoader.java create mode 100644 discovery-plugin/src/main/java/com/nepxion/discovery/plugin/strategy/FilterStrategy.java create mode 100644 discovery-plugin/src/main/java/com/nepxion/discovery/plugin/strategy/VersionStrategy.java create mode 100644 discovery-plugin/src/main/java/com/nepxion/discovery/plugin/util/BeanRegisterUtil.java create mode 100644 discovery-plugin/src/main/java/com/nepxion/discovery/plugin/xml/Dom4JConstant.java create mode 100644 discovery-plugin/src/main/java/com/nepxion/discovery/plugin/xml/Dom4JParser.java create mode 100644 discovery-plugin/src/main/java/com/nepxion/discovery/plugin/xml/Dom4JReader.java create mode 100644 discovery-plugin/src/main/java/com/nepxion/discovery/plugin/xml/Dom4JWriter.java create mode 100644 discovery-springcloud-example-a/pom.xml create mode 100644 discovery-springcloud-example-a/src/main/java/com/nepxion/discovery/plugin/example/DiscoveryPluginApplication.java create mode 100644 discovery-springcloud-example-a/src/main/java/com/nepxion/discovery/plugin/example/DiscoveryPluginController.java create mode 100644 discovery-springcloud-example-a/src/main/java/com/nepxion/discovery/plugin/example/DiscoveryPluginSimulator.java create mode 100644 discovery-springcloud-example-a/src/main/resources/application.properties create mode 100644 discovery-springcloud-example-a/src/main/resources/discovery1.xml create mode 100644 discovery-springcloud-example-a/src/main/resources/discovery2.xml create mode 100644 discovery-springcloud-example-a/src/main/resources/discovery3.xml create mode 100644 discovery-springcloud-example-a/src/main/resources/logback.xml create mode 100644 discovery-springcloud-example-b1/pom.xml create mode 100644 discovery-springcloud-example-b1/src/main/java/com/nepxion/discovery/plugin/example/DiscoveryPluginApplicationB1.java create mode 100644 discovery-springcloud-example-b1/src/main/resources/application.properties create mode 100644 discovery-springcloud-example-b1/src/main/resources/logback.xml create mode 100644 discovery-springcloud-example-b2/pom.xml create mode 100644 discovery-springcloud-example-b2/src/main/java/com/nepxion/discovery/plugin/example/DiscoveryPluginApplicationB2.java create mode 100644 discovery-springcloud-example-b2/src/main/resources/application.properties create mode 100644 discovery-springcloud-example-b2/src/main/resources/logback.xml create mode 100644 pom.xml create mode 100644 version.bat diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000000..47ffff598f --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Declare files that will always have UNIX line endings on checkout. +*.sh text eol=lf \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..763236eddc --- /dev/null +++ b/.gitignore @@ -0,0 +1,32 @@ +# Compiled class file +*.class +.classpath +.springBeans +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +*.class +*.classpath +*.project +*.springBeans +log/ +test-output/ + +# Package Files # +*.jar +*.war +*.ear +*.zip +*.tar.gz +*.rar +*.swp +*.log +*.ctxt +# nodejs local modules +.tags* +.idea/ +*.iml +.gradle/ +.settings/ +target/ +hs_err_pid* \ No newline at end of file diff --git a/COPYRIGHT b/COPYRIGHT new file mode 100644 index 0000000000..d6b6daa5f9 --- /dev/null +++ b/COPYRIGHT @@ -0,0 +1,5 @@ +Nepxion Discovery +Copyright 2017-2050 The Nepxion Studio + +This product includes software developed at +The Nepxion Studio (http://www.nepxion.com). \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000..a916d09499 --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2010-2050 The Nepxion Studio + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/discovery-plugin-starter/pom.xml b/discovery-plugin-starter/pom.xml new file mode 100644 index 0000000000..bc03deddb9 --- /dev/null +++ b/discovery-plugin-starter/pom.xml @@ -0,0 +1,23 @@ + + + discovery-plugin-starter + Nepxion Discovery Plugin Starter + jar + 4.0.0 + Nepxion Discovery is + http://www.nepxion.com + + + com.nepxion + discovery + 1.0.0 + + + + + ${project.groupId} + discovery-plugin + + + \ No newline at end of file diff --git a/discovery-plugin-starter/src/main/resources/META-INF/spring.factories b/discovery-plugin-starter/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000000..d0e6cbc87f --- /dev/null +++ b/discovery-plugin-starter/src/main/resources/META-INF/spring.factories @@ -0,0 +1,5 @@ +org.springframework.context.ApplicationContextInitializer=\ +com.nepxion.discovery.plugin.context.DiscoveryApplicationContextInitializer + +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +com.nepxion.discovery.plugin.config.DiscoveryPluginConfig \ No newline at end of file diff --git a/discovery-plugin/pom.xml b/discovery-plugin/pom.xml new file mode 100644 index 0000000000..fbec5772d6 --- /dev/null +++ b/discovery-plugin/pom.xml @@ -0,0 +1,48 @@ + + + discovery-plugin + Nepxion Discovery Plugin + jar + 4.0.0 + Nepxion Discovery is + http://www.nepxion.com + + + com.nepxion + discovery + 1.0.0 + + + + + ${project.groupId} + eventbus-aop + + + + org.apache.commons + commons-lang3 + + + + org.apache.commons + commons-collections4 + + + + commons-io + commons-io + + + + dom4j + dom4j + + + + org.springframework.cloud + spring-cloud-starter-eureka + + + \ No newline at end of file diff --git a/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/config/DiscoveryPluginConfig.java b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/config/DiscoveryPluginConfig.java new file mode 100644 index 0000000000..6d2b520a4d --- /dev/null +++ b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/config/DiscoveryPluginConfig.java @@ -0,0 +1,63 @@ +package com.nepxion.discovery.plugin.config; + +/** + *

Title: Nepxion Discovery

+ *

Description: Nepxion Discovery

+ *

Copyright: Copyright (c) 2017-2050

+ *

Company: Nepxion

+ * @author Haojun Ren + * @version 1.0 + */ + +import java.util.concurrent.locks.ReentrantLock; + +import javax.annotation.PostConstruct; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.nepxion.discovery.plugin.entity.DiscoveryEntity; +import com.nepxion.discovery.plugin.strategy.FilterStrategy; +import com.nepxion.discovery.plugin.strategy.VersionStrategy; + +@Configuration +public class DiscoveryPluginConfig { + @Autowired + private DiscoveryPluginConfigLoader discoveryPluginConfigLoader; + + @Bean + public DiscoveryPluginConfigParser discoveryPluginConfigParser() { + return new DiscoveryPluginConfigParser(); + } + + @Bean + public DiscoveryPluginConfigLoader discoveryPluginConfigLoader() { + return new DiscoveryPluginConfigLoader(); + } + + @Bean + public DiscoveryEntity discoveryEntity() { + return new DiscoveryEntity(); + } + + @Bean + public ReentrantLock reentrantLock() { + return new ReentrantLock(); + } + + @Bean + public FilterStrategy filterStrategy() { + return new FilterStrategy(); + } + + @Bean + public VersionStrategy versionStrategy() { + return new VersionStrategy(); + } + + @PostConstruct + public void initialize() { + discoveryPluginConfigLoader.initialize(); + } +} \ No newline at end of file diff --git a/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/config/DiscoveryPluginConfigLoader.java b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/config/DiscoveryPluginConfigLoader.java new file mode 100644 index 0000000000..704b520115 --- /dev/null +++ b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/config/DiscoveryPluginConfigLoader.java @@ -0,0 +1,93 @@ +package com.nepxion.discovery.plugin.config; + +/** + *

Title: Nepxion Discovery

+ *

Description: Nepxion Discovery

+ *

Copyright: Copyright (c) 2017-2050

+ *

Company: Nepxion

+ * @author Haojun Ren + * @version 1.0 + */ + +import java.io.IOException; +import java.io.InputStream; + +import org.apache.commons.io.IOUtils; +import org.dom4j.DocumentException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; + +import com.google.common.eventbus.Subscribe; +import com.nepxion.discovery.plugin.constant.DiscoveryPluginConstant; +import com.nepxion.discovery.plugin.exception.DiscoveryPluginException; +import com.nepxion.discovery.plugin.loader.FileLoader; +import com.nepxion.eventbus.annotation.EventBus; +import com.nepxion.eventbus.core.Event; + +@EventBus +public class DiscoveryPluginConfigLoader { + private static final Logger LOG = LoggerFactory.getLogger(DiscoveryPluginConfigLoader.class); + + @Value("${" + DiscoveryPluginConstant.SPRING_APPLICATION_DISCOVERY_REMOTE_CONFIG_ENABLED + ":false}") + private Boolean remoteConfigEnabled; + + @Autowired + private FileLoader fileLoader; + + @Autowired + private DiscoveryPluginConfigParser discoveryPluginConfigParser; + + public void initialize() throws DiscoveryPluginException { + LOG.info("********** {} config starts to initialize **********", remoteConfigEnabled ? "Remote" : "Local"); + + InputStream inputStream = null; + try { + if (remoteConfigEnabled) { + inputStream = fileLoader.getRemoteInputStream(); + } else { + inputStream = fileLoader.getLocalInputStream(); + } + parse(inputStream); + } catch (IOException e) { + throw new DiscoveryPluginException(e); + } catch (DocumentException e) { + throw new DiscoveryPluginException(e); + } finally { + if (inputStream != null) { + IOUtils.closeQuietly(inputStream); + } + } + } + + @Subscribe + public void refresh(Event event) { + if (!remoteConfigEnabled) { + return; + } + + LOG.info("********** Remote config change has been retrieved **********"); + + InputStream inputStream = (InputStream) event.getSource(); + try { + parse(inputStream); + } catch (IOException e) { + throw new DiscoveryPluginException(e); + } catch (DocumentException e) { + throw new DiscoveryPluginException(e); + } finally { + if (inputStream != null) { + IOUtils.closeQuietly(inputStream); + } + } + } + + private void parse(InputStream inputStream) throws DocumentException, IOException { + if (inputStream == null) { + throw new DiscoveryPluginException("Failed to load " + (remoteConfigEnabled ? "remote" : "local") + " config, no input stream returns"); + } + + discoveryPluginConfigParser.parse(inputStream); + } +} \ No newline at end of file diff --git a/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/config/DiscoveryPluginConfigParser.java b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/config/DiscoveryPluginConfigParser.java new file mode 100644 index 0000000000..45932343b0 --- /dev/null +++ b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/config/DiscoveryPluginConfigParser.java @@ -0,0 +1,139 @@ +package com.nepxion.discovery.plugin.config; + +/** + *

Title: Nepxion Discovery

+ *

Description: Nepxion Discovery

+ *

Copyright: Copyright (c) 2017-2050

+ *

Company: Nepxion

+ * @author Haojun Ren + * @version 1.0 + */ + +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.concurrent.locks.ReentrantLock; + +import org.apache.commons.lang3.StringUtils; +import org.dom4j.Element; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; + +import com.nepxion.discovery.plugin.constant.DiscoveryPluginConstant; +import com.nepxion.discovery.plugin.entity.ConsumerEntity; +import com.nepxion.discovery.plugin.entity.DiscoveryEntity; +import com.nepxion.discovery.plugin.entity.FilterEntity; +import com.nepxion.discovery.plugin.entity.FilterType; +import com.nepxion.discovery.plugin.entity.VersionEntity; +import com.nepxion.discovery.plugin.exception.DiscoveryPluginException; +import com.nepxion.discovery.plugin.xml.Dom4JParser; + +public class DiscoveryPluginConfigParser extends Dom4JParser { + private static final Logger LOG = LoggerFactory.getLogger(DiscoveryPluginConfigParser.class); + + @Autowired + private DiscoveryEntity discoveryEntity; + + @Autowired + private ReentrantLock reentrantLock; + + @SuppressWarnings("rawtypes") + @Override + protected void parseRoot(Element element) { + LOG.info("Start to parse discovery.xml..."); + + int filterElementCount = element.elements(DiscoveryPluginConstant.FILTER_ELEMENT_NAME).size(); + if (filterElementCount > 1) { + throw new DiscoveryPluginException("The count of element [" + DiscoveryPluginConstant.FILTER_ELEMENT_NAME + "] can't be more than 1"); + } + + int versionElementCount = element.elements(DiscoveryPluginConstant.VERSION_ELEMENT_NAME).size(); + if (versionElementCount > 1) { + throw new DiscoveryPluginException("The count of element [" + DiscoveryPluginConstant.VERSION_ELEMENT_NAME + "] can't be more than 1"); + } + + FilterEntity filterEntity = new FilterEntity(); + VersionEntity versionEntity = new VersionEntity(); + for (Iterator elementIterator = element.elementIterator(); elementIterator.hasNext();) { + Object childElementObject = elementIterator.next(); + if (childElementObject instanceof Element) { + Element childElement = (Element) childElementObject; + + if (StringUtils.equals(childElement.getName(), DiscoveryPluginConstant.FILTER_ELEMENT_NAME)) { + parseFilter(childElement, filterEntity); + } else if (StringUtils.equals(childElement.getName(), DiscoveryPluginConstant.VERSION_ELEMENT_NAME)) { + parseVersion(childElement, versionEntity); + } + } + } + + try { + reentrantLock.lock(); + + discoveryEntity.setFilterEntity(filterEntity); + discoveryEntity.setVersionEntity(versionEntity); + } finally { + reentrantLock.unlock(); + } + + LOG.info("Discovery entity is {}", discoveryEntity); + } + + @SuppressWarnings("rawtypes") + private void parseFilter(Element element, FilterEntity filterEntity) { + String filterType = element.attribute(DiscoveryPluginConstant.FILTER_TYPE_ATTRIBUTE_NAME).getData().toString().trim(); + String globalFilterValue = element.attribute(DiscoveryPluginConstant.FILTER_VALUE_ATTRIBUTE_NAME).getData().toString().trim(); + filterEntity.setFilterType(FilterType.fromString(filterType)); + filterEntity.setFilterValue(globalFilterValue); + + Map filterMap = filterEntity.getFilterMap(); + + for (Iterator elementIterator = element.elementIterator(); elementIterator.hasNext();) { + Object childElementObject = elementIterator.next(); + if (childElementObject instanceof Element) { + Element childElement = (Element) childElementObject; + + String serviceName = childElement.attribute(DiscoveryPluginConstant.SERVICE_NAME_ATTRIBUTE_NAME).getData().toString().trim(); + String filterValue = childElement.attribute(DiscoveryPluginConstant.FILTER_VALUE_ATTRIBUTE_NAME).getData().toString().trim(); + + filterMap.put(serviceName, filterValue); + } + } + } + + @SuppressWarnings("rawtypes") + private void parseVersion(Element element, VersionEntity versionEntity) { + List consumerEntityList = versionEntity.getConsumerEntityList(); + for (Iterator elementIterator = element.elementIterator(); elementIterator.hasNext();) { + Object childElementObject = elementIterator.next(); + if (childElementObject instanceof Element) { + Element childElement = (Element) childElementObject; + + ConsumerEntity consumerEntity = new ConsumerEntity(); + String serviceName = childElement.attribute(DiscoveryPluginConstant.SERVICE_NAME_ATTRIBUTE_NAME).getData().toString().trim(); + consumerEntity.setServiceName(serviceName); + + parseConsumer(childElement, consumerEntity); + + consumerEntityList.add(consumerEntity); + } + } + } + + @SuppressWarnings("rawtypes") + private void parseConsumer(Element element, ConsumerEntity consumerEntity) { + Map providerMap = consumerEntity.getProviderMap(); + for (Iterator elementIterator = element.elementIterator(); elementIterator.hasNext();) { + Object childElementObject = elementIterator.next(); + if (childElementObject instanceof Element) { + Element childElement = (Element) childElementObject; + + String serviceName = childElement.attribute(DiscoveryPluginConstant.SERVICE_NAME_ATTRIBUTE_NAME).getData().toString().trim(); + String versionValue = childElement.attribute(DiscoveryPluginConstant.VERSION_VALUE_NAME_ATTRIBUTE_NAME).getData().toString().trim(); + + providerMap.put(serviceName, versionValue); + } + } + } +} \ No newline at end of file diff --git a/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/constant/DiscoveryPluginConstant.java b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/constant/DiscoveryPluginConstant.java new file mode 100644 index 0000000000..365a5f86e6 --- /dev/null +++ b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/constant/DiscoveryPluginConstant.java @@ -0,0 +1,32 @@ +package com.nepxion.discovery.plugin.constant; + +/** + *

Title: Nepxion Discovery

+ *

Description: Nepxion Discovery

+ *

Copyright: Copyright (c) 2017-2050

+ *

Company: Nepxion

+ * @author Haojun Ren + * @version 1.0 + */ + +public class DiscoveryPluginConstant { + public static final String SPRING_APPLICATION_DISCOVERY_FILTER_ENABLED = "spring.application.discovery.filter.enabled"; + public static final String SPRING_APPLICATION_DISCOVERY_VERSION_ENABLED = "spring.application.discovery.version.enabled"; + public static final String SPRING_APPLICATION_DISCOVERY_REMOTE_CONFIG_ENABLED = "spring.application.discovery.remote.config.enabled"; + + public static final String SPRING_APPLICATION_NAME = "spring.application.name"; + public static final String EUREKA_METADATA_VERSION = "version"; + + public static final String DISCOVERY_ELEMENT_NAME = "discovery"; + public static final String FILTER_ELEMENT_NAME = "filter"; + public static final String SERVICE_ELEMENT_NAME = "service"; + public static final String FILTER_TYPE_ATTRIBUTE_NAME = "filter-type"; + public static final String FILTER_VALUE_ATTRIBUTE_NAME = "filter-value"; + public static final String SERVICE_NAME_ATTRIBUTE_NAME = "service-name"; + public static final String VERSION_ELEMENT_NAME = "version"; + public static final String CONSUMER_ELEMENT_NAME = "consumer"; + public static final String PROVIDER_ELEMENT_NAME = "provider"; + public static final String VERSION_VALUE_NAME_ATTRIBUTE_NAME = "version-value"; + + public static final String SEPARATE = ";"; +} \ No newline at end of file diff --git a/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/context/DiscoveryApplicationContextInitializer.java b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/context/DiscoveryApplicationContextInitializer.java new file mode 100644 index 0000000000..abc39af762 --- /dev/null +++ b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/context/DiscoveryApplicationContextInitializer.java @@ -0,0 +1,45 @@ +package com.nepxion.discovery.plugin.context; + +/** + *

Title: Nepxion Discovery

+ *

Description: Nepxion Discovery

+ *

Copyright: Copyright (c) 2017-2050

+ *

Company: Nepxion

+ * @author Haojun Ren + * @version 1.0 + */ + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean; +import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaServiceRegistry; +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.ConfigurableApplicationContext; + +import com.nepxion.discovery.plugin.decorator.DiscoveryClientDecorator; +import com.nepxion.discovery.plugin.decorator.EurekaServiceRegistryDecorator; + +public class DiscoveryApplicationContextInitializer implements ApplicationContextInitializer { + @Override + public void initialize(ConfigurableApplicationContext applicationContext) { + applicationContext.getBeanFactory().addBeanPostProcessor(new InstantiationAwareBeanPostProcessorAdapter() { + @Override + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + if (bean instanceof DiscoveryClient) { + DiscoveryClient discoveryClient = (DiscoveryClient) bean; + return new DiscoveryClientDecorator(discoveryClient, applicationContext); + } else if (bean instanceof EurekaServiceRegistry) { + EurekaServiceRegistry eurekaServiceRegistry = (EurekaServiceRegistry) bean; + return new EurekaServiceRegistryDecorator(eurekaServiceRegistry, applicationContext); + } else if (bean instanceof EurekaInstanceConfigBean) { + EurekaInstanceConfigBean instanceConfig = (EurekaInstanceConfigBean) bean; + instanceConfig.setPreferIpAddress(true); + return bean; + } else { + return bean; + } + } + }); + } +} \ No newline at end of file diff --git a/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/decorator/DiscoveryClientDecorator.java b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/decorator/DiscoveryClientDecorator.java new file mode 100644 index 0000000000..e442594162 --- /dev/null +++ b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/decorator/DiscoveryClientDecorator.java @@ -0,0 +1,60 @@ +package com.nepxion.discovery.plugin.decorator; + +/** + *

Title: Nepxion Discovery

+ *

Description: Nepxion Discovery

+ *

Copyright: Copyright (c) 2017-2050

+ *

Company: Nepxion

+ * @author Haojun Ren + * @version 1.0 + */ + +import java.util.List; + +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.env.ConfigurableEnvironment; + +import com.nepxion.discovery.plugin.constant.DiscoveryPluginConstant; +import com.nepxion.discovery.plugin.strategy.VersionStrategy; + +public class DiscoveryClientDecorator implements DiscoveryClient { + private DiscoveryClient discoveryClient; + private ConfigurableApplicationContext applicationContext; + private ConfigurableEnvironment environment; + + public DiscoveryClientDecorator(DiscoveryClient discoveryClient, ConfigurableApplicationContext applicationContext) { + this.discoveryClient = discoveryClient; + this.applicationContext = applicationContext; + this.environment = applicationContext.getEnvironment(); + } + + @Override + public String description() { + return discoveryClient.description(); + } + + @Deprecated + @Override + public ServiceInstance getLocalServiceInstance() { + return discoveryClient.getLocalServiceInstance(); + } + + @Override + public List getInstances(String serviceId) { + String applicationName = environment.getProperty(DiscoveryPluginConstant.SPRING_APPLICATION_NAME); + + List instances = discoveryClient.getInstances(serviceId); + + VersionStrategy versionStrategy = applicationContext.getBean(VersionStrategy.class); + versionStrategy.apply(applicationName, serviceId, instances); + + return instances; + } + + @Override + public List getServices() { + return discoveryClient.getServices(); + } +} \ No newline at end of file diff --git a/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/decorator/EurekaServiceRegistryDecorator.java b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/decorator/EurekaServiceRegistryDecorator.java new file mode 100644 index 0000000000..a0f8d84098 --- /dev/null +++ b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/decorator/EurekaServiceRegistryDecorator.java @@ -0,0 +1,69 @@ +package com.nepxion.discovery.plugin.decorator; + +/** + *

Title: Nepxion Discovery

+ *

Description: Nepxion Discovery

+ *

Copyright: Copyright (c) 2017-2050

+ *

Company: Nepxion

+ * @author Haojun Ren + * @version 1.0 + */ + +import org.springframework.cloud.client.serviceregistry.ServiceRegistry; +import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaRegistration; +import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaServiceRegistry; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.env.ConfigurableEnvironment; + +import com.nepxion.discovery.plugin.constant.DiscoveryPluginConstant; +import com.nepxion.discovery.plugin.strategy.FilterStrategy; + +public class EurekaServiceRegistryDecorator extends EurekaServiceRegistry { + private ServiceRegistry serviceRegistry; + private ConfigurableApplicationContext applicationContext; + private ConfigurableEnvironment environment; + + public EurekaServiceRegistryDecorator(ServiceRegistry serviceRegistry, ConfigurableApplicationContext applicationContext) { + this.serviceRegistry = serviceRegistry; + this.applicationContext = applicationContext; + this.environment = applicationContext.getEnvironment(); + } + + @Override + public void register(EurekaRegistration registration) { + boolean discoveryFilterEnabled = Boolean.valueOf(environment.getProperty(DiscoveryPluginConstant.SPRING_APPLICATION_DISCOVERY_FILTER_ENABLED)); + if (discoveryFilterEnabled) { + discoveryFilterEnabled(registration); + } + + serviceRegistry.register(registration); + } + + private void discoveryFilterEnabled(EurekaRegistration registration) { + String serviceId = registration.getServiceId(); + String ipAddress = registration.getInstanceConfig().getIpAddress(); + + FilterStrategy filterStrategy = applicationContext.getBean(FilterStrategy.class); + filterStrategy.apply(serviceId, ipAddress); + } + + @Override + public void deregister(EurekaRegistration registration) { + serviceRegistry.deregister(registration); + } + + @Override + public void close() { + serviceRegistry.close(); + } + + @Override + public void setStatus(EurekaRegistration registration, String status) { + serviceRegistry.setStatus(registration, status); + } + + @Override + public Object getStatus(EurekaRegistration registration) { + return serviceRegistry.getStatus(registration); + } +} \ No newline at end of file diff --git a/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/entity/ConsumerEntity.java b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/entity/ConsumerEntity.java new file mode 100644 index 0000000000..9d94a22b82 --- /dev/null +++ b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/entity/ConsumerEntity.java @@ -0,0 +1,61 @@ +package com.nepxion.discovery.plugin.entity; + +/** + *

Title: Nepxion Discovery

+ *

Description: Nepxion Discovery

+ *

Copyright: Copyright (c) 2017-2050

+ *

Company: Nepxion

+ * @author Haojun Ren + * @version 1.0 + */ + +import java.io.Serializable; +import java.util.LinkedHashMap; +import java.util.Map; + +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; + +public class ConsumerEntity implements Serializable { + private static final long serialVersionUID = -2976565258172148792L; + + private String serviceName; + private Map providerMap = new LinkedHashMap(); + + public ConsumerEntity() { + + } + + public String getServiceName() { + return serviceName; + } + + public void setServiceName(String serviceName) { + this.serviceName = serviceName; + } + + public Map getProviderMap() { + return providerMap; + } + + public void setProviderMap(Map providerMap) { + this.providerMap = providerMap; + } + + @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 diff --git a/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/entity/DiscoveryEntity.java b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/entity/DiscoveryEntity.java new file mode 100644 index 0000000000..6faf954577 --- /dev/null +++ b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/entity/DiscoveryEntity.java @@ -0,0 +1,55 @@ +package com.nepxion.discovery.plugin.entity; + +/** + *

Title: Nepxion Discovery

+ *

Description: Nepxion Discovery

+ *

Copyright: Copyright (c) 2017-2050

+ *

Company: Nepxion

+ * @author Haojun Ren + * @version 1.0 + */ + +import java.io.Serializable; + +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; + +public class DiscoveryEntity implements Serializable { + private static final long serialVersionUID = 7079024435084528751L; + + private FilterEntity filterEntity; + private VersionEntity versionEntity; + + public FilterEntity getFilterEntity() { + return filterEntity; + } + + public void setFilterEntity(FilterEntity filterEntity) { + this.filterEntity = filterEntity; + } + + public VersionEntity getVersionEntity() { + return versionEntity; + } + + public void setVersionEntity(VersionEntity versionEntity) { + this.versionEntity = versionEntity; + } + + @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 diff --git a/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/entity/FilterEntity.java b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/entity/FilterEntity.java new file mode 100644 index 0000000000..b819c6d862 --- /dev/null +++ b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/entity/FilterEntity.java @@ -0,0 +1,70 @@ +package com.nepxion.discovery.plugin.entity; + +/** + *

Title: Nepxion Discovery

+ *

Description: Nepxion Discovery

+ *

Copyright: Copyright (c) 2017-2050

+ *

Company: Nepxion

+ * @author Haojun Ren + * @version 1.0 + */ + +import java.io.Serializable; +import java.util.LinkedHashMap; +import java.util.Map; + +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; + +public class FilterEntity implements Serializable { + private static final long serialVersionUID = -2097322826969006191L; + + private FilterType filterType; + private String filterValue; + private Map filterMap = new LinkedHashMap(); + + public FilterEntity() { + + } + + public FilterType getFilterType() { + return filterType; + } + + public void setFilterType(FilterType filterType) { + this.filterType = filterType; + } + + public String getFilterValue() { + return filterValue; + } + + public void setFilterValue(String filterValue) { + this.filterValue = filterValue; + } + + public Map getFilterMap() { + return filterMap; + } + + public void setFilterMap(Map filterMap) { + this.filterMap = filterMap; + } + + @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 diff --git a/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/entity/FilterType.java b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/entity/FilterType.java new file mode 100644 index 0000000000..97587c2ac1 --- /dev/null +++ b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/entity/FilterType.java @@ -0,0 +1,46 @@ +package com.nepxion.discovery.plugin.entity; + +/** + *

Title: Nepxion Discovery

+ *

Description: Nepxion Discovery

+ *

Copyright: Copyright (c) 2017-2050

+ *

Company: Nepxion

+ * @author Haojun Ren + * @version 1.0 + */ + +public enum FilterType { + BLACKLIST("BLACKLIST", "黑名单"), + WHITELIST("WHITELIST", "白名单"); + + private String value; + private String description; + + private FilterType(String value, String description) { + this.value = value; + this.description = description; + } + + public String getValue() { + return value; + } + + public String getDescription() { + return description; + } + + public static FilterType fromString(String value) { + for (FilterType type : FilterType.values()) { + if (type.getValue().equalsIgnoreCase(value)) { + return type; + } + } + + throw new IllegalArgumentException("No matched type with value=" + value); + } + + @Override + public String toString() { + return value; + } +} \ No newline at end of file diff --git a/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/entity/VersionEntity.java b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/entity/VersionEntity.java new file mode 100644 index 0000000000..07bde344dd --- /dev/null +++ b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/entity/VersionEntity.java @@ -0,0 +1,52 @@ +package com.nepxion.discovery.plugin.entity; + +/** + *

Title: Nepxion Discovery

+ *

Description: Nepxion Discovery

+ *

Copyright: Copyright (c) 2017-2050

+ *

Company: Nepxion

+ * @author Haojun Ren + * @version 1.0 + */ + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; + +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; + +public class VersionEntity implements Serializable { + private static final long serialVersionUID = 6281838121286637807L; + + private List consumerEntityList = new ArrayList(); + + public VersionEntity() { + + } + + public List getConsumerEntityList() { + return consumerEntityList; + } + + public void setConsumerEntityList(List consumerEntityList) { + this.consumerEntityList = consumerEntityList; + } + + @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 diff --git a/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/exception/DiscoveryPluginException.java b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/exception/DiscoveryPluginException.java new file mode 100644 index 0000000000..f801b845e4 --- /dev/null +++ b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/exception/DiscoveryPluginException.java @@ -0,0 +1,30 @@ +package com.nepxion.discovery.plugin.exception; + +/** + *

Title: Nepxion Discovery

+ *

Description: Nepxion Discovery

+ *

Copyright: Copyright (c) 2017-2050

+ *

Company: Nepxion

+ * @author Haojun Ren + * @version 1.0 + */ + +public class DiscoveryPluginException extends RuntimeException { + private static final long serialVersionUID = 7975167663357170655L; + + public DiscoveryPluginException() { + super(); + } + + public DiscoveryPluginException(String message) { + super(message); + } + + public DiscoveryPluginException(String message, Throwable cause) { + super(message, cause); + } + + public DiscoveryPluginException(Throwable cause) { + super(cause); + } +} \ No newline at end of file diff --git a/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/loader/AbstractFileLoader.java b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/loader/AbstractFileLoader.java new file mode 100644 index 0000000000..0c53d17849 --- /dev/null +++ b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/loader/AbstractFileLoader.java @@ -0,0 +1,36 @@ +package com.nepxion.discovery.plugin.loader; + +/** + *

Title: Nepxion Discovery

+ *

Description: Nepxion Discovery

+ *

Copyright: Copyright (c) 2017-2050

+ *

Company: Nepxion

+ * @author Haojun Ren + * @version 1.0 + */ + +import java.io.IOException; +import java.io.InputStream; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; + +public abstract class AbstractFileLoader implements FileLoader { + @Autowired + private ApplicationContext applicationContext; + + @Override + public InputStream getLocalInputStream() throws IOException { + String localContextPath = getLocalContextPath(); + if (StringUtils.isEmpty(localContextPath)) { + return null; + } + + String localFilePath = applicationContext.getEnvironment().resolvePlaceholders(localContextPath); + + return applicationContext.getResource(localFilePath).getInputStream(); + } + + protected abstract String getLocalContextPath(); +} \ No newline at end of file diff --git a/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/loader/FileLoader.java b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/loader/FileLoader.java new file mode 100644 index 0000000000..43d9319cb2 --- /dev/null +++ b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/loader/FileLoader.java @@ -0,0 +1,19 @@ +package com.nepxion.discovery.plugin.loader; + +/** + *

Title: Nepxion Discovery

+ *

Description: Nepxion Discovery

+ *

Copyright: Copyright (c) 2017-2050

+ *

Company: Nepxion

+ * @author Haojun Ren + * @version 1.0 + */ + +import java.io.IOException; +import java.io.InputStream; + +public interface FileLoader { + InputStream getLocalInputStream() throws IOException; + + InputStream getRemoteInputStream() throws IOException; +} \ No newline at end of file diff --git a/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/strategy/FilterStrategy.java b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/strategy/FilterStrategy.java new file mode 100644 index 0000000000..9f152801dd --- /dev/null +++ b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/strategy/FilterStrategy.java @@ -0,0 +1,44 @@ +package com.nepxion.discovery.plugin.strategy; + +/** + *

Title: Nepxion Discovery

+ *

Description: Nepxion Discovery

+ *

Copyright: Copyright (c) 2017-2050

+ *

Company: Nepxion

+ * @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.plugin.constant.DiscoveryPluginConstant; +import com.nepxion.discovery.plugin.entity.DiscoveryEntity; +import com.nepxion.discovery.plugin.entity.FilterEntity; +import com.nepxion.discovery.plugin.exception.DiscoveryPluginException; + +public class FilterStrategy { + @Autowired + private DiscoveryEntity discoveryEntity; + + public void apply(String serviceId, String ipAddress) { + FilterEntity filterEntity = discoveryEntity.getFilterEntity(); + String globalFilterValue = filterEntity.getFilterValue(); + validate(globalFilterValue, ipAddress); + + Map filterMap = filterEntity.getFilterMap(); + String filterValue = filterMap.get(serviceId); + validate(filterValue, ipAddress); + } + + private void validate(String filterValue, String ipAddress) { + String[] filterArray = StringUtils.split(filterValue, DiscoveryPluginConstant.SEPARATE); + for (String filter : filterArray) { + if (ipAddress.startsWith(filter)) { + throw new DiscoveryPluginException(ipAddress + " isn't allowed to register to Eureka server"); + } + } + } +} \ No newline at end of file diff --git a/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/strategy/VersionStrategy.java b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/strategy/VersionStrategy.java new file mode 100644 index 0000000000..d2093e2177 --- /dev/null +++ b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/strategy/VersionStrategy.java @@ -0,0 +1,72 @@ +package com.nepxion.discovery.plugin.strategy; + +/** + *

Title: Nepxion Discovery

+ *

Description: Nepxion Discovery

+ *

Copyright: Copyright (c) 2017-2050

+ *

Company: Nepxion

+ * @author Haojun Ren + * @version 1.0 + */ + +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.concurrent.locks.ReentrantLock; + +import org.apache.commons.lang.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.client.ServiceInstance; + +import com.nepxion.discovery.plugin.constant.DiscoveryPluginConstant; +import com.nepxion.discovery.plugin.entity.ConsumerEntity; +import com.nepxion.discovery.plugin.entity.DiscoveryEntity; +import com.nepxion.discovery.plugin.entity.VersionEntity; + +public class VersionStrategy { + @Autowired + private DiscoveryEntity discoveryEntity; + + @Autowired + private ReentrantLock reentrantLock; + + public void apply(String consumerServiceId, String providerServiceId, List instances) { + try { + reentrantLock.lock(); + + VersionEntity versionEntity = discoveryEntity.getVersionEntity(); + ConsumerEntity consumerEntity = getConsumerEntity(consumerServiceId, versionEntity); + if (consumerEntity != null) { + Map providerMap = consumerEntity.getProviderMap(); + String version = providerMap.get(providerServiceId); + if (StringUtils.isNotEmpty(version)) { + String[] versionArray = StringUtils.split(version, DiscoveryPluginConstant.SEPARATE); + List versionList = Arrays.asList(versionArray); + Iterator iterator = instances.iterator(); + while (iterator.hasNext()) { + ServiceInstance serviceInstance = iterator.next(); + String metaDataVersion = serviceInstance.getMetadata().get(DiscoveryPluginConstant.EUREKA_METADATA_VERSION); + if (!versionList.contains(metaDataVersion)) { + iterator.remove(); + } + } + } + } + } finally { + reentrantLock.unlock(); + } + } + + private ConsumerEntity getConsumerEntity(String consumerServiceId, VersionEntity versionEntity) { + List consumerEntityList = versionEntity.getConsumerEntityList(); + for (ConsumerEntity consumerEntity : consumerEntityList) { + String serviceName = consumerEntity.getServiceName(); + if (StringUtils.equals(consumerServiceId, serviceName)) { + return consumerEntity; + } + } + + return null; + } +} \ No newline at end of file diff --git a/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/util/BeanRegisterUtil.java b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/util/BeanRegisterUtil.java new file mode 100644 index 0000000000..ba68cc1c62 --- /dev/null +++ b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/util/BeanRegisterUtil.java @@ -0,0 +1,35 @@ +package com.nepxion.discovery.plugin.util; + +/** + *

Title: Nepxion Discovery

+ *

Description: Nepxion Discovery

+ *

Copyright: Copyright (c) 2017-2050

+ *

Company: Nepxion

+ * @author Haojun Ren + * @version 1.0 + */ + +import org.springframework.beans.factory.support.AbstractBeanDefinition; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.context.ConfigurableApplicationContext; + +public class BeanRegisterUtil { + public static BeanDefinitionRegistry getRegistry(ConfigurableApplicationContext applicationContext) { + return (BeanDefinitionRegistry) applicationContext.getBeanFactory(); + } + + public static void registerBean(BeanDefinitionRegistry beanDefinitionRegistry, String beanName, String beanClassName) { + BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(beanClassName); + AbstractBeanDefinition beanDefinition = beanDefinitionBuilder.getBeanDefinition(); + beanDefinition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE); + beanDefinitionRegistry.registerBeanDefinition(beanName, beanDefinition); + } + + public static void registerBean(BeanDefinitionRegistry beanDefinitionRegistry, String beanName, Class beanClass) { + BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(beanClass); + AbstractBeanDefinition beanDefinition = beanDefinitionBuilder.getBeanDefinition(); + beanDefinition.setAutowireMode(AbstractBeanDefinition.AUTOWIRE_BY_TYPE); + beanDefinitionRegistry.registerBeanDefinition(beanName, beanDefinition); + } +} \ No newline at end of file diff --git a/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/xml/Dom4JConstant.java b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/xml/Dom4JConstant.java new file mode 100644 index 0000000000..6c1526d2c7 --- /dev/null +++ b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/xml/Dom4JConstant.java @@ -0,0 +1,15 @@ +package com.nepxion.discovery.plugin.xml; + +/** + *

Title: Nepxion Discovery

+ *

Description: Nepxion Discovery

+ *

Copyright: Copyright (c) 2017-2050

+ *

Company: Nepxion

+ * @author Haojun Ren + * @version 1.0 + */ + +public class Dom4JConstant { + public static final String ENCODING_UTF_8 = "UTF-8"; + public static final String ENCODING_ISO_8859_1 = "ISO-8859-1"; +} \ No newline at end of file diff --git a/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/xml/Dom4JParser.java b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/xml/Dom4JParser.java new file mode 100644 index 0000000000..de6652937b --- /dev/null +++ b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/xml/Dom4JParser.java @@ -0,0 +1,65 @@ +package com.nepxion.discovery.plugin.xml; + +/** + *

Title: Nepxion Discovery

+ *

Description: Nepxion Discovery

+ *

Copyright: Copyright (c) 2017-2050

+ *

Company: Nepxion

+ * @author Haojun Ren + * @version 1.0 + */ + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.UnsupportedEncodingException; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; + +public abstract class Dom4JParser { + public void parse(String text) throws DocumentException { + Document document = Dom4JReader.getDocument(text); + + parse(document); + } + + public void parseFormat(String text) throws DocumentException, UnsupportedEncodingException { + Document document = Dom4JReader.getFormatDocument(text); + + parse(document); + } + + public void parse(File file) throws DocumentException, IOException, UnsupportedEncodingException { + Document document = Dom4JReader.getDocument(file); + + parse(document); + } + + public void parseFormat(File file) throws DocumentException, IOException, UnsupportedEncodingException { + Document document = Dom4JReader.getFormatDocument(file); + + parse(document); + } + + public void parse(InputStream inputStream) throws DocumentException, IOException { + Document document = Dom4JReader.getDocument(inputStream); + + parse(document); + } + + public void parseFormat(InputStream inputStream) throws DocumentException, IOException, UnsupportedEncodingException { + Document document = Dom4JReader.getFormatDocument(inputStream); + + parse(document); + } + + public void parse(Document document) { + Element rootElement = document.getRootElement(); + + parseRoot(rootElement); + } + + protected abstract void parseRoot(Element element); +} \ No newline at end of file diff --git a/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/xml/Dom4JReader.java b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/xml/Dom4JReader.java new file mode 100644 index 0000000000..df254eca4e --- /dev/null +++ b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/xml/Dom4JReader.java @@ -0,0 +1,123 @@ +package com.nepxion.discovery.plugin.xml; + +/** + *

Title: Nepxion Discovery

+ *

Description: Nepxion Discovery

+ *

Copyright: Copyright (c) 2017-2050

+ *

Company: Nepxion

+ * @author Haojun Ren + * @version 1.0 + */ + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.io.UnsupportedEncodingException; +import java.net.URL; + +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.DocumentHelper; +import org.dom4j.io.SAXReader; +import org.xml.sax.InputSource; + +public class Dom4JReader { + public static Document getDocument(String text) throws DocumentException { + return DocumentHelper.parseText(text); + } + + public static Document getFormatDocument(String text) throws DocumentException, UnsupportedEncodingException { + return getFormatDocument(text, Dom4JConstant.ENCODING_UTF_8); + } + + public static Document getFormatDocument(String text, String charset) throws DocumentException, UnsupportedEncodingException { + String formatText = new String(text.getBytes(Dom4JConstant.ENCODING_ISO_8859_1), Dom4JConstant.ENCODING_UTF_8); + + return getDocument(formatText); + } + + public static Document getDocument(File file) throws DocumentException, IOException { + InputStream inputStream = new FileInputStream(file); + + return getDocument(inputStream); + } + + public static Document getFormatDocument(File file) throws DocumentException, IOException, UnsupportedEncodingException { + return getFormatDocument(file, Dom4JConstant.ENCODING_UTF_8); + } + + public static Document getFormatDocument(File file, String charset) throws DocumentException, IOException, UnsupportedEncodingException { + InputStream inputStream = new FileInputStream(file); + + return getFormatDocument(inputStream, charset); + } + + public static Document getDocument(InputSource inputSource) throws DocumentException { + SAXReader saxReader = new SAXReader(); + + return saxReader.read(inputSource); + } + + public static Document getFormatDocument(InputSource inputSource) throws DocumentException { + return getFormatDocument(inputSource, Dom4JConstant.ENCODING_UTF_8); + } + + public static Document getFormatDocument(InputSource inputSource, String charset) throws DocumentException { + inputSource.setEncoding(charset); + + return getDocument(inputSource); + } + + public static Document getDocument(InputStream inputStream) throws DocumentException, IOException { + SAXReader saxReader = new SAXReader(); + + Document document = null; + try { + document = saxReader.read(inputStream); + } catch (DocumentException e) { + throw e; + } finally { + if (inputStream != null) { + inputStream.close(); + } + } + + return document; + } + + public static Document getFormatDocument(InputStream inputStream) throws DocumentException, IOException, UnsupportedEncodingException { + return getFormatDocument(inputStream, Dom4JConstant.ENCODING_UTF_8); + } + + public static Document getFormatDocument(InputStream inputStream, String charset) throws DocumentException, IOException, UnsupportedEncodingException { + Reader inputStreamReader = new InputStreamReader(inputStream, charset); + + return getDocument(inputStreamReader); + } + + public static Document getDocument(Reader reader) throws DocumentException, IOException { + SAXReader saxReader = new SAXReader(); + + Document document = null; + try { + document = saxReader.read(reader); + } catch (DocumentException e) { + throw e; + } finally { + if (reader != null) { + reader.close(); + } + } + + return document; + } + + public static Document getDocument(URL url) throws DocumentException { + SAXReader saxReader = new SAXReader(); + + return saxReader.read(url); + } +} \ No newline at end of file diff --git a/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/xml/Dom4JWriter.java b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/xml/Dom4JWriter.java new file mode 100644 index 0000000000..c200271441 --- /dev/null +++ b/discovery-plugin/src/main/java/com/nepxion/discovery/plugin/xml/Dom4JWriter.java @@ -0,0 +1,51 @@ +package com.nepxion.discovery.plugin.xml; + +/** + *

Title: Nepxion Discovery

+ *

Description: Nepxion Discovery

+ *

Copyright: Copyright (c) 2017-2050

+ *

Company: Nepxion

+ * @author Haojun Ren + * @version 1.0 + */ + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.UnsupportedEncodingException; + +import org.dom4j.Document; +import org.dom4j.DocumentHelper; +import org.dom4j.io.OutputFormat; +import org.dom4j.io.XMLWriter; + +public class Dom4JWriter { + public static Document createDocument() { + return DocumentHelper.createDocument(); + } + + public static String getText(Document document) throws IOException, UnsupportedEncodingException { + return getText(document, Dom4JConstant.ENCODING_UTF_8); + } + + public static String getText(Document document, String charset) throws IOException, UnsupportedEncodingException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + OutputFormat outputFormat = new OutputFormat(" ", true, charset); + + try { + XMLWriter writer = new XMLWriter(baos, outputFormat); + writer.write(document); + + baos.flush(); + } catch (UnsupportedEncodingException e) { + throw e; + } catch (IOException e) { + throw e; + } finally { + if (baos != null) { + baos.close(); + } + } + + return baos.toString(charset); + } +} \ No newline at end of file diff --git a/discovery-springcloud-example-a/pom.xml b/discovery-springcloud-example-a/pom.xml new file mode 100644 index 0000000000..bce62b1e16 --- /dev/null +++ b/discovery-springcloud-example-a/pom.xml @@ -0,0 +1,68 @@ + + + com.nepxion + discovery-springcloud-example-a + Nepxion Discovery Spring Cloud Example A + jar + 4.0.0 + 1.0.0 + Nepxion Discovery is + http://www.nepxion.com + + + org.springframework.boot + spring-boot-starter-parent + 1.5.6.RELEASE + + + + Dalston.SR4 + + 1.0.0 + 1.8 + UTF-8 + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring.cloud.version} + pom + import + + + + + + + com.nepxion + discovery-plugin-starter + ${discovery.plugin.version} + + + + org.springframework.cloud + spring-cloud-starter-eureka + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + -parameters + + ${project.build.sourceEncoding} + ${java.version} + ${java.version} + + + + + \ No newline at end of file diff --git a/discovery-springcloud-example-a/src/main/java/com/nepxion/discovery/plugin/example/DiscoveryPluginApplication.java b/discovery-springcloud-example-a/src/main/java/com/nepxion/discovery/plugin/example/DiscoveryPluginApplication.java new file mode 100644 index 0000000000..e8accafbdf --- /dev/null +++ b/discovery-springcloud-example-a/src/main/java/com/nepxion/discovery/plugin/example/DiscoveryPluginApplication.java @@ -0,0 +1,22 @@ +package com.nepxion.discovery.plugin.example; + +/** + *

Title: Nepxion Discovery

+ *

Description: Nepxion Discovery

+ *

Copyright: Copyright (c) 2017-2050

+ *

Company: Nepxion

+ * @author Haojun Ren + * @version 1.0 + */ + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + +@SpringBootApplication +@EnableDiscoveryClient +public class DiscoveryPluginApplication { + public static void main(String[] args) { + new SpringApplicationBuilder(DiscoveryPluginApplication.class).web(true).run(args); + } +} \ No newline at end of file diff --git a/discovery-springcloud-example-a/src/main/java/com/nepxion/discovery/plugin/example/DiscoveryPluginController.java b/discovery-springcloud-example-a/src/main/java/com/nepxion/discovery/plugin/example/DiscoveryPluginController.java new file mode 100644 index 0000000000..4af96a2844 --- /dev/null +++ b/discovery-springcloud-example-a/src/main/java/com/nepxion/discovery/plugin/example/DiscoveryPluginController.java @@ -0,0 +1,30 @@ +package com.nepxion.discovery.plugin.example; + +/** + *

Title: Nepxion Discovery

+ *

Description: Nepxion Discovery

+ *

Copyright: Copyright (c) 2017-2050

+ *

Company: Nepxion

+ * @author Haojun Ren + * @version 1.0 + */ + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class DiscoveryPluginController { + @Autowired + private DiscoveryClient discoveryClient; + + @RequestMapping(value = "/instances", method = RequestMethod.GET) + public List instances() { + return discoveryClient.getInstances("discovery-springcloud-example-b"); + } +} \ No newline at end of file diff --git a/discovery-springcloud-example-a/src/main/java/com/nepxion/discovery/plugin/example/DiscoveryPluginSimulator.java b/discovery-springcloud-example-a/src/main/java/com/nepxion/discovery/plugin/example/DiscoveryPluginSimulator.java new file mode 100644 index 0000000000..dd4d7478c4 --- /dev/null +++ b/discovery-springcloud-example-a/src/main/java/com/nepxion/discovery/plugin/example/DiscoveryPluginSimulator.java @@ -0,0 +1,85 @@ +package com.nepxion.discovery.plugin.example; + +/** + *

Title: Nepxion Discovery

+ *

Description: Nepxion Discovery

+ *

Copyright: Copyright (c) 2017-2050

+ *

Company: Nepxion

+ * @author Haojun Ren + * @version 1.0 + */ + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.Timer; +import java.util.TimerTask; +import java.util.concurrent.ThreadLocalRandom; + +import javax.annotation.PostConstruct; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Component; + +import com.nepxion.discovery.plugin.loader.AbstractFileLoader; +import com.nepxion.discovery.plugin.loader.FileLoader; +import com.nepxion.eventbus.core.Event; +import com.nepxion.eventbus.core.EventControllerFactory; + +@Component +public class DiscoveryPluginSimulator { + // 模拟从本地配置和远程配置获取discovery.xml + @Bean + public FileLoader fileLoader() { + return new AbstractFileLoader() { + @Override + public InputStream getRemoteInputStream() throws IOException { + // 本地文件模拟代替远程文件 + return createInputStream("src/main/resources/discovery1.xml"); + } + + @Override + protected String getLocalContextPath() { + // 配置文件放在resources目录下 + return "classpath:discovery1.xml"; + + // 配置文件放在工程根目录下 + // return "file:discovery.xml"; + } + }; + } + + // 模拟每隔15秒通过事件总线接收远程配置中心推送过来的配置更新 + @Autowired + private EventControllerFactory eventControllerFactory; + + @PostConstruct + public void initialize() { + ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current(); + + Timer timer = new Timer(); + timer.scheduleAtFixedRate(new TimerTask() { + public void run() { + // 本地文件模拟代替远程文件,随机读取 + int index = threadLocalRandom.nextInt(3) + 1; + InputStream inputStream = createInputStream("src/main/resources/discovery" + index + ".xml"); + eventControllerFactory.getAsyncController().post(new Event(inputStream)); + } + }, 0L, 15000L); + } + + private InputStream createInputStream(String fileName) { + File file = new File(fileName); + InputStream inputStream = null; + try { + inputStream = new FileInputStream(file); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + + return inputStream; + } +} \ No newline at end of file diff --git a/discovery-springcloud-example-a/src/main/resources/application.properties b/discovery-springcloud-example-a/src/main/resources/application.properties new file mode 100644 index 0000000000..b008f0a5c0 --- /dev/null +++ b/discovery-springcloud-example-a/src/main/resources/application.properties @@ -0,0 +1,12 @@ +# Spring cloud config +spring.application.name=discovery-springcloud-example-a +server.port=4321 +eureka.client.serviceUrl.defaultZone=http://10.0.75.1:9528/eureka/ +eureka.instance.metadataMap.version=1.0 + +# Blacklist or Whitelist filter +spring.application.discovery.filter.enabled=false +# Multi-versions control +spring.application.discovery.version.enabled=true +# Get remote config +spring.application.discovery.remote.config.enabled=true \ No newline at end of file diff --git a/discovery-springcloud-example-a/src/main/resources/discovery1.xml b/discovery-springcloud-example-a/src/main/resources/discovery1.xml new file mode 100644 index 0000000000..8e32b3db85 --- /dev/null +++ b/discovery-springcloud-example-a/src/main/resources/discovery1.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/discovery-springcloud-example-a/src/main/resources/discovery2.xml b/discovery-springcloud-example-a/src/main/resources/discovery2.xml new file mode 100644 index 0000000000..572511c7fb --- /dev/null +++ b/discovery-springcloud-example-a/src/main/resources/discovery2.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/discovery-springcloud-example-a/src/main/resources/discovery3.xml b/discovery-springcloud-example-a/src/main/resources/discovery3.xml new file mode 100644 index 0000000000..f99ba576ee --- /dev/null +++ b/discovery-springcloud-example-a/src/main/resources/discovery3.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/discovery-springcloud-example-a/src/main/resources/logback.xml b/discovery-springcloud-example-a/src/main/resources/logback.xml new file mode 100644 index 0000000000..6ce0c9c182 --- /dev/null +++ b/discovery-springcloud-example-a/src/main/resources/logback.xml @@ -0,0 +1,49 @@ + + + + + + + discovery %date %level [%thread] %logger{10} [%file:%line] - %msg%n + UTF-8 + + + log/discovery-%d{yyyy-MM-dd}.%i.log + 50MB + + + INFO + + + true + + + + 0 + 512 + + + + + + + + discovery %date %level [%thread] %logger{10} [%file:%line] - %msg%n + UTF-8 + + + + INFO + + + + + + + + + + + + + diff --git a/discovery-springcloud-example-b1/pom.xml b/discovery-springcloud-example-b1/pom.xml new file mode 100644 index 0000000000..2df786c214 --- /dev/null +++ b/discovery-springcloud-example-b1/pom.xml @@ -0,0 +1,62 @@ + + + com.nepxion + discovery-springcloud-example-b1 + Nepxion Discovery Spring Cloud Example B1 + jar + 4.0.0 + 1.0.0 + Nepxion Discovery is + http://www.nepxion.com + + + org.springframework.boot + spring-boot-starter-parent + 1.5.6.RELEASE + + + + Dalston.SR4 + + 1.0.0 + 1.8 + UTF-8 + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring.cloud.version} + pom + import + + + + + + + org.springframework.cloud + spring-cloud-starter-eureka + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + -parameters + + ${project.build.sourceEncoding} + ${java.version} + ${java.version} + + + + + \ No newline at end of file diff --git a/discovery-springcloud-example-b1/src/main/java/com/nepxion/discovery/plugin/example/DiscoveryPluginApplicationB1.java b/discovery-springcloud-example-b1/src/main/java/com/nepxion/discovery/plugin/example/DiscoveryPluginApplicationB1.java new file mode 100644 index 0000000000..9d820bfcfb --- /dev/null +++ b/discovery-springcloud-example-b1/src/main/java/com/nepxion/discovery/plugin/example/DiscoveryPluginApplicationB1.java @@ -0,0 +1,22 @@ +package com.nepxion.discovery.plugin.example; + +/** + *

Title: Nepxion Discovery

+ *

Description: Nepxion Discovery

+ *

Copyright: Copyright (c) 2017-2050

+ *

Company: Nepxion

+ * @author Haojun Ren + * @version 1.0 + */ + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + +@SpringBootApplication +@EnableDiscoveryClient +public class DiscoveryPluginApplicationB1 { + public static void main(String[] args) { + new SpringApplicationBuilder(DiscoveryPluginApplicationB1.class).web(true).run(args); + } +} \ No newline at end of file diff --git a/discovery-springcloud-example-b1/src/main/resources/application.properties b/discovery-springcloud-example-b1/src/main/resources/application.properties new file mode 100644 index 0000000000..9701d39176 --- /dev/null +++ b/discovery-springcloud-example-b1/src/main/resources/application.properties @@ -0,0 +1,5 @@ +# Spring cloud config +spring.application.name=discovery-springcloud-example-b +server.port=4322 +eureka.client.serviceUrl.defaultZone=http://10.0.75.1:9528/eureka/ +eureka.instance.metadataMap.version=1.0 \ No newline at end of file diff --git a/discovery-springcloud-example-b1/src/main/resources/logback.xml b/discovery-springcloud-example-b1/src/main/resources/logback.xml new file mode 100644 index 0000000000..6ce0c9c182 --- /dev/null +++ b/discovery-springcloud-example-b1/src/main/resources/logback.xml @@ -0,0 +1,49 @@ + + + + + + + discovery %date %level [%thread] %logger{10} [%file:%line] - %msg%n + UTF-8 + + + log/discovery-%d{yyyy-MM-dd}.%i.log + 50MB + + + INFO + + + true + + + + 0 + 512 + + + + + + + + discovery %date %level [%thread] %logger{10} [%file:%line] - %msg%n + UTF-8 + + + + INFO + + + + + + + + + + + + + diff --git a/discovery-springcloud-example-b2/pom.xml b/discovery-springcloud-example-b2/pom.xml new file mode 100644 index 0000000000..6fd6cb2f3c --- /dev/null +++ b/discovery-springcloud-example-b2/pom.xml @@ -0,0 +1,62 @@ + + + com.nepxion + discovery-springcloud-example-b2 + Nepxion Discovery Spring Cloud Example B2 + jar + 4.0.0 + 1.0.0 + Nepxion Discovery is + http://www.nepxion.com + + + org.springframework.boot + spring-boot-starter-parent + 1.5.6.RELEASE + + + + Dalston.SR4 + + 1.0.0 + 1.8 + UTF-8 + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring.cloud.version} + pom + import + + + + + + + org.springframework.cloud + spring-cloud-starter-eureka + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + -parameters + + ${project.build.sourceEncoding} + ${java.version} + ${java.version} + + + + + \ No newline at end of file diff --git a/discovery-springcloud-example-b2/src/main/java/com/nepxion/discovery/plugin/example/DiscoveryPluginApplicationB2.java b/discovery-springcloud-example-b2/src/main/java/com/nepxion/discovery/plugin/example/DiscoveryPluginApplicationB2.java new file mode 100644 index 0000000000..8629c143e6 --- /dev/null +++ b/discovery-springcloud-example-b2/src/main/java/com/nepxion/discovery/plugin/example/DiscoveryPluginApplicationB2.java @@ -0,0 +1,22 @@ +package com.nepxion.discovery.plugin.example; + +/** + *

Title: Nepxion Discovery

+ *

Description: Nepxion Discovery

+ *

Copyright: Copyright (c) 2017-2050

+ *

Company: Nepxion

+ * @author Haojun Ren + * @version 1.0 + */ + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + +@SpringBootApplication +@EnableDiscoveryClient +public class DiscoveryPluginApplicationB2 { + public static void main(String[] args) { + new SpringApplicationBuilder(DiscoveryPluginApplicationB2.class).web(true).run(args); + } +} \ No newline at end of file diff --git a/discovery-springcloud-example-b2/src/main/resources/application.properties b/discovery-springcloud-example-b2/src/main/resources/application.properties new file mode 100644 index 0000000000..6387632e11 --- /dev/null +++ b/discovery-springcloud-example-b2/src/main/resources/application.properties @@ -0,0 +1,5 @@ +# Spring cloud config +spring.application.name=discovery-springcloud-example-b +server.port=4323 +eureka.client.serviceUrl.defaultZone=http://10.0.75.1:9528/eureka/ +eureka.instance.metadataMap.version=1.1 \ No newline at end of file diff --git a/discovery-springcloud-example-b2/src/main/resources/logback.xml b/discovery-springcloud-example-b2/src/main/resources/logback.xml new file mode 100644 index 0000000000..6ce0c9c182 --- /dev/null +++ b/discovery-springcloud-example-b2/src/main/resources/logback.xml @@ -0,0 +1,49 @@ + + + + + + + discovery %date %level [%thread] %logger{10} [%file:%line] - %msg%n + UTF-8 + + + log/discovery-%d{yyyy-MM-dd}.%i.log + 50MB + + + INFO + + + true + + + + 0 + 512 + + + + + + + + discovery %date %level [%thread] %logger{10} [%file:%line] - %msg%n + UTF-8 + + + + INFO + + + + + + + + + + + + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000000..c901185b53 --- /dev/null +++ b/pom.xml @@ -0,0 +1,223 @@ + + + com.nepxion + discovery + Nepxion Discovery + pom + 4.0.0 + 1.0.0 + Nepxion Discovery is + http://www.nepxion.com + + + discovery-plugin + discovery-plugin-starter + discovery-springcloud-example-a + discovery-springcloud-example-b1 + discovery-springcloud-example-b2 + + + + 1.0.6 + 3.6 + 4.1 + 2.5 + 1.6.1 + Dalston.SR4 + 3.3.7 + 2.9.1 + 1.8 + UTF-8 + + + + + + ${project.groupId} + discovery-plugin + ${project.version} + + + + ${project.groupId} + discovery-plugin-starter + ${project.version} + + + + ${project.groupId} + eventbus-aop + ${eventbus.version} + + + + org.apache.commons + commons-lang3 + ${commons.lang3.version} + + + + org.apache.commons + commons-collections4 + ${commons.collections4.version} + + + + commons-io + commons-io + ${commons.io.version} + + + + dom4j + dom4j + ${dom4j.version} + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring.cloud.version} + pom + import + + + + + + + com.lmax + disruptor + ${disruptor.version} + provided + + + + org.apache.logging.log4j + log4j-1.2-api + ${log4j.version} + provided + + + + org.apache.logging.log4j + log4j-slf4j-impl + ${log4j.version} + provided + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + ${project.build.sourceEncoding} + ${java.version} + ${java.version} + + + + + + + + + release + + + + org.sonatype.plugins + nexus-staging-maven-plugin + 1.6.3 + true + + oss + https://oss.sonatype.org/ + true + + + + org.apache.maven.plugins + maven-gpg-plugin + 1.6 + + + verify + + sign + + + + + + org.apache.maven.plugins + maven-source-plugin + 3.0.1 + + + package + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 2.9.1 + + + package + + jar + + + + + + + + + + oss + https://oss.sonatype.org/content/repositories/snapshots/ + + + oss + https://oss.sonatype.org/service/local/staging/deploy/maven2/ + + + + + + + + The Apache Software License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + + + + + https://github.com/Nepxion/Discovery + scm:git:https://github.com/Nepxion/Discovery.git + scm:git:https://github.com/Nepxion/Discovery.git + v${project.version} + + + + Nepxion + http://www.nepxion.com + + + + + Haojun Ren + 1394997@qq.com + http://www.nepxion.com + + + \ No newline at end of file diff --git a/version.bat b/version.bat new file mode 100644 index 0000000000..bc49173534 --- /dev/null +++ b/version.bat @@ -0,0 +1,20 @@ +@echo on +@echo ============================================================= +@echo $ $ +@echo $ Nepxion Discovery $ +@echo $ $ +@echo $ $ +@echo $ $ +@echo $ Nepxion Studio All Right Reserved $ +@echo $ Copyright (C) 2017-2050 $ +@echo $ $ +@echo ============================================================= +@echo. +@echo off + +@title Nepxion Discovery +@color 0a + +call mvn versions:set -DnewVersion=1.0.0 + +pause \ No newline at end of file