初始提交
This commit is contained in:
commit
d161e148ac
|
@ -0,0 +1,58 @@
|
|||
stages: # 定义了一条pipeline
|
||||
- 代码检测 # 定义了stage
|
||||
- 测试
|
||||
- 编译
|
||||
|
||||
#- 发布JAR包
|
||||
|
||||
PMD扫描: # 定义了一个job
|
||||
stage: 代码检测 # 绑定到stage
|
||||
plugin: PMD # 指定使用的插件
|
||||
checkRule: # 校验结果
|
||||
- pmd1 = 0 && pmd2 = 0
|
||||
|
||||
STC扫描:
|
||||
stage: 代码检测
|
||||
plugin: STC
|
||||
checkRule:
|
||||
- stc = 0
|
||||
|
||||
单元测试:
|
||||
stage: 测试
|
||||
plugin: LINKQ-UT # java单元测试插件
|
||||
checkRule:
|
||||
- passRate = 100
|
||||
tools:
|
||||
jdk: '1.8' #jdk版本枚举:1.6、1.7、1.8
|
||||
maven: 3.2.5
|
||||
parameters:
|
||||
encoding: UTF-8 # 编码设置
|
||||
COVERAGE_EXCLUSIONS: '**/bootstrap/**,**/integration/**,**/client/pb/**,**/MetaApplication,**/registry/jraft/**,**/DataApplication,**/SessionApplication' # 覆盖率排除
|
||||
pluginConfig:
|
||||
markdown: admin/result.md # 向页面输出一些自定义内容
|
||||
aciTags: 8G
|
||||
|
||||
集成测试:
|
||||
stage: 测试
|
||||
plugin: LINKQ-IT # java集成测试插件
|
||||
checkRule:
|
||||
- passRate = 100
|
||||
tools:
|
||||
jdk: '1.8'
|
||||
parameters:
|
||||
encoding: UTF-8 # 编码设置
|
||||
pluginConfig:
|
||||
privateRepo: false, # 是否是私有仓库
|
||||
parallelCount: 1 # 并发执行个数
|
||||
enableOracle: false # 是否依赖Oracle
|
||||
|
||||
编译:
|
||||
stage: 编译
|
||||
plugin: ANT-BUILD # 使用插件ANT-BUILD
|
||||
pluginConfig:
|
||||
image: reg.docker.alibaba-inc.com/antb/sofa-build:0.0.2
|
||||
script:
|
||||
- export PATH=$PATH:/opt/taobao/java/bin:/opt/taobao/maven/bin
|
||||
- java -version
|
||||
- mvn -version
|
||||
- mvn clean package -Dmaven.test.skip=true -Palipay-dev
|
|
@ -0,0 +1,27 @@
|
|||
---
|
||||
name: Ask Question
|
||||
about: Ask a question about usage or feature
|
||||
|
||||
---
|
||||
|
||||
### Your question
|
||||
|
||||
describe your question clearly
|
||||
|
||||
### Your scenes
|
||||
|
||||
describe your use scenes (why need this feature)
|
||||
|
||||
### Your advice
|
||||
|
||||
describe the advice or solution you'd like
|
||||
|
||||
### Environment
|
||||
|
||||
- SOFARegistry version:
|
||||
- JVM version (e.g. `java -version`):
|
||||
- OS version (e.g. `uname -a`):
|
||||
- Maven version:
|
||||
- IDE version:
|
||||
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
---
|
||||
name: Bug Report
|
||||
about: Create a report to help us improve
|
||||
|
||||
---
|
||||
|
||||
### Describe the bug
|
||||
|
||||
A clear and concise description of what the bug is.
|
||||
|
||||
### Expected behavior
|
||||
|
||||
### Actual behavior
|
||||
|
||||
### Steps to reproduce
|
||||
|
||||
### Minimal yet complete reproducer code (or GitHub URL to code)
|
||||
|
||||
### Environment
|
||||
|
||||
- SOFARegistry version:
|
||||
- JVM version (e.g. `java -version`):
|
||||
- OS version (e.g. `uname -a`):
|
||||
- Maven version:
|
||||
- IDE version:
|
|
@ -0,0 +1,21 @@
|
|||
---
|
||||
name: Feature request
|
||||
about: Suggest an idea for this project
|
||||
title: ''
|
||||
labels: ''
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
## In what area(s)?
|
||||
|
||||
<!-- Remove the '> ' to select -->
|
||||
|
||||
> /area runtime
|
||||
> /area operator
|
||||
> /area placement
|
||||
> /area docs
|
||||
> /area test-and-release
|
||||
|
||||
## Describe the feature
|
||||
<!-- Please also discuss possible business value -->
|
|
@ -0,0 +1,14 @@
|
|||
### Motivation:
|
||||
|
||||
Explain the context, and why you're making that change.
|
||||
To make others understand what is the problem you're trying to solve.
|
||||
|
||||
### Modification:
|
||||
|
||||
Describe the idea and modifications you've done.
|
||||
|
||||
### Result:
|
||||
|
||||
Fixes #<GitHub issue number>.
|
||||
|
||||
If there is no issue then describe the changes introduced by this PR.
|
|
@ -0,0 +1,32 @@
|
|||
# This workflow will build a Java project with Maven
|
||||
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
|
||||
|
||||
name: integration test
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master ]
|
||||
pull_request:
|
||||
branches: [ master ]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Set up JDK 1.8
|
||||
uses: actions/setup-java@v1
|
||||
with:
|
||||
java-version: 1.8
|
||||
- name: Integration Test
|
||||
run: mvn compile -B
|
||||
&& mvn clean test -DisSkipUnitTest=true "-Dtest.logging.level=ERROR"
|
||||
- name: Publish Test Report
|
||||
if: ${{ always() }}
|
||||
uses: ScaCap/action-surefire-report@v1
|
||||
with:
|
||||
fail_on_test_failures: true
|
||||
skip_publishing: true
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
name: pmd
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master ]
|
||||
pull_request:
|
||||
branches: [ master ]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Set up JDK 1.8
|
||||
uses: actions/setup-java@v1
|
||||
with:
|
||||
java-version: 1.8
|
||||
- name: PMD
|
||||
run: mvn install -Dmaven.test.skip=true
|
||||
&& mvn pmd:check
|
|
@ -0,0 +1,36 @@
|
|||
# This workflow will build a Java project with Maven
|
||||
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven
|
||||
|
||||
name: unit test
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master ]
|
||||
pull_request:
|
||||
branches: [ master ]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Set up JDK 1.8
|
||||
uses: actions/setup-java@v1
|
||||
with:
|
||||
java-version: 1.8
|
||||
- name: Unit Testt
|
||||
run: mvn compile -B
|
||||
&& mvn clean test -DisSkipIntegrationTest=true "-Dtest.logging.level=ERROR" --fail-at-end --batch-mode
|
||||
- name: Publish Test Report
|
||||
if: ${{ always() }}
|
||||
uses: ScaCap/action-surefire-report@v1
|
||||
with:
|
||||
fail_on_test_failures: true
|
||||
skip_publishing: true
|
||||
- name: Codecov
|
||||
uses: codecov/codecov-action@v1
|
||||
with:
|
||||
token: ${{secrets.CODECOV_TOKEN}}
|
||||
fail_ci_if_error: false
|
|
@ -0,0 +1,20 @@
|
|||
# Maven (examples)
|
||||
target
|
||||
dependency-reduced-pom.xml
|
||||
|
||||
# IntelliJ IDEA
|
||||
.idea
|
||||
*.iml
|
||||
|
||||
# Eclipse
|
||||
.classpath
|
||||
.project
|
||||
.settings
|
||||
|
||||
# OS X
|
||||
.DS_Store
|
||||
|
||||
# log
|
||||
logs/
|
||||
*.log
|
||||
LOG_HOME_IS_UNDEFINED
|
|
@ -0,0 +1,17 @@
|
|||
language: java
|
||||
sudo: false
|
||||
|
||||
dist: trusty
|
||||
|
||||
jdk:
|
||||
- oraclejdk8
|
||||
|
||||
install:
|
||||
- mvn clean install -DskipTests -B -V
|
||||
- mvn test
|
||||
|
||||
script:
|
||||
- sh ./tools/check_format.sh
|
||||
|
||||
after_success:
|
||||
- bash <(curl -s https://codecov.io/bash)
|
|
@ -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 [yyyy] [name of copyright owner]
|
||||
|
||||
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.
|
|
@ -0,0 +1,10 @@
|
|||
VERSION := $(shell cat VERSION)
|
||||
|
||||
proto:
|
||||
protoc --proto_path=./server/common/model/src/main/resources/proto --java_out=server/common/model/src/main/java server/common/model/src/main/resources/proto/*.proto # protoc-3.5.1
|
||||
|
||||
image_build:
|
||||
docker build . -f docker/Dockerfile -t sofaregistry/sofaregistry:$(VERSION)
|
||||
|
||||
image_push:
|
||||
docker push sofaregistry/sofaregistry:$(VERSION)
|
|
@ -0,0 +1,58 @@
|
|||
# SOFARegistry
|
||||
|
||||
[](https://github.com/sofastack/sofa-registry/actions/workflows/unit-test.yml)
|
||||
[](https://github.com/sofastack/sofa-registry/actions/workflows/integration-test.yml)
|
||||

|
||||
[](https://codecov.io/gh/sofastack/sofa-registry)
|
||||

|
||||
|
||||
SOFARegistry 是蚂蚁金服开源的一个生产级、高时效、高可用的服务注册中心。SOFARegistry 最早源自于淘宝的 ConfigServer,十年来,随着蚂蚁金服的业务发展,注册中心架构已经演进至第六代。目前 SOFARegistry 不仅全面服务于蚂蚁金服的自有业务,还随着蚂蚁金融科技服务众多合作伙伴,同时也兼容开源生态。SOFARegistry 采用 AP 架构,支持秒级时效性推送,同时采用分层架构支持无限水平扩展。
|
||||
|
||||
## 功能特性
|
||||
|
||||
- 支持服务发布与服务订阅
|
||||
- 支持服务变更时的主动推送
|
||||
- 丰富的 REST 接口
|
||||
- 采用分层架构及数据分片,支持海量连接及海量数据
|
||||
- 支持多副本备份,保证数据高可用
|
||||
- 基于 [SOFABolt](https://github.com/alipay/sofa-bolt) 通信框架,服务上下线秒级通知
|
||||
- AP 架构,保证网络分区下的可用性
|
||||
|
||||
|
||||
## 需要
|
||||
|
||||
编译需要 JDK 8 及以上、Maven 3.2.5 及以上。
|
||||
|
||||
运行需要 JDK 6 及以上,服务端运行需要 JDK 8及以上。
|
||||
|
||||
**推荐使用JDK 8,JDK 16尚未被测试,可能会有兼容性问题**
|
||||
|
||||
## 文档
|
||||
|
||||
- [快速开始](https://www.sofastack.tech/sofa-registry/docs/Server-QuickStart)
|
||||
- [开发手册](https://www.sofastack.tech/sofa-registry/docs/JAVA-SDK)
|
||||
- [运维手册](https://www.sofastack.tech/sofa-registry/docs/Deployment)
|
||||
- [发布历史](https://www.sofastack.tech/sofa-registry/docs/ReleaseNotes)
|
||||
- [发展路线](https://www.sofastack.tech/sofa-registry/docs/RoadMap)
|
||||
- 源码解析
|
||||
- [发布订阅推送](https://www.sofastack.tech/projects/sofa-registry/code-analyze/code-analyze-publish-subscription-push/)
|
||||
- [registry meta 选主](https://www.sofastack.tech/projects/sofa-registry/code-analyze/code-analyze-registry-meta/)
|
||||
- [SlotTable](https://www.sofastack.tech/projects/sofa-registry/code-analyze/code-analyze-slottable/)
|
||||
- [数据倒排索引](https://www.sofastack.tech/projects/sofa-registry/code-analyze/code-analyze-data-inverted-index/)
|
||||
- [数据表监听](https://www.sofastack.tech/projects/sofa-registry/code-analyze/code-analyza-data-table-listening/)
|
||||
- [无损运维](https://www.sofastack.tech/projects/sofa-registry/code-analyze/code-analyze-non-destructive-o-and-m/)
|
||||
- [推送延迟 trace](https://www.sofastack.tech/projects/sofa-registry/code-analyze/code-analyze-push-delay-trace/)
|
||||
- [推送开关](https://www.sofastack.tech/projects/sofa-registry/code-analyze/code-analyze-push-switch/)
|
||||
- [通讯数据压缩](https://www.sofastack.tech/projects/sofa-registry/code-analyze/code-analyze-communication-data-compression/)
|
||||
## 贡献
|
||||
|
||||
[如何参与 SOFARegistry 代码贡献](https://www.sofastack.tech/sofa-registry/docs/Contributing)
|
||||
|
||||
|
||||
## 致谢
|
||||
|
||||
SOFARegistry 最早源于阿里内部的 ConfigServer,感谢毕玄创造了 ConfigServer,使 SOFARegistry 的发展有了良好的基础。同时,部分代码参考了 Netflix 的 [Eureka](https://github.com/Netflix/eureka),感谢 Netflix 开源了如此优秀框架。
|
||||
|
||||
## 开源许可
|
||||
|
||||
SOFARegistry 基于 [Apache License 2.0](https://github.com/sofastack/sofa-registry/blob/master/LICENSE) 协议
|
|
@ -0,0 +1,9 @@
|
|||
# Security Policy
|
||||
|
||||
## Reporting a Vulnerability
|
||||
|
||||
If you have apprehensions regarding SOFAStack's security or you discover vulnerability or potential threat, don’t hesitate to get in touch with us by dropping a mail at sofastack@antgroup.com.
|
||||
|
||||
In the mail, specify the description of the issue or potential threat. You are also urged to recommend the way to reproduce and replicate the issue. The SOFAStack community will get back to you after assessing and analysing the findings.
|
||||
|
||||
PLEASE PAY ATTENTION to report the security issue on the security email before disclosing it on public domain.
|
|
@ -0,0 +1,30 @@
|
|||
# build version
|
||||
version: '{build}'
|
||||
|
||||
# environment settings
|
||||
environment:
|
||||
matrix:
|
||||
- JAVA_HOME: C:\Program Files\Java\jdk1.8.0
|
||||
|
||||
platform: x64
|
||||
|
||||
# install required tools (java, maven)
|
||||
install:
|
||||
- cmd: echo %JAVA_HOME%
|
||||
- cmd: echo %M2_HOME%
|
||||
|
||||
# Do not build on tags
|
||||
skip_tags: true
|
||||
|
||||
# build and install artifacts
|
||||
build_script:
|
||||
- mvn clean install -DskipTests
|
||||
|
||||
# verify artifacts
|
||||
test_script:
|
||||
- mvn test
|
||||
|
||||
# preserve dependencies between builds
|
||||
cache:
|
||||
- C:\maven\
|
||||
- C:\Users\appveyor\.m2
|
|
@ -0,0 +1,280 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.alipay.sofa</groupId>
|
||||
<artifactId>registry-client-all</artifactId>
|
||||
<version>6.1.9</version>
|
||||
|
||||
<name>${project.groupId}:${project.artifactId}</name>
|
||||
<url>http://github.com/alipay/sofa-registry</url>
|
||||
<description>A high-performance, high-extensibility, production-level Java Registry framework.</description>
|
||||
|
||||
<licenses>
|
||||
<license>
|
||||
<name>The Apache License, Version 2.0</name>
|
||||
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
|
||||
</license>
|
||||
</licenses>
|
||||
|
||||
<organization>
|
||||
<name>The Ant Financial</name>
|
||||
<url>http://www.antfin.com/</url>
|
||||
</organization>
|
||||
|
||||
<developers>
|
||||
<developer>
|
||||
<name>Hui Wang</name>
|
||||
<email>shangyu.wh@alibaba-inc.com</email>
|
||||
<organization>Ant Financial</organization>
|
||||
</developer>
|
||||
<developer>
|
||||
<name>KeZhu Wu</name>
|
||||
<email>kezhu.wukz@antfin.com</email>
|
||||
<organization>Ant Financial</organization>
|
||||
</developer>
|
||||
<developer>
|
||||
<name>Jie Cao</name>
|
||||
<email>caojie.cj@antfin.com</email>
|
||||
<organization>Ant Financial</organization>
|
||||
</developer>
|
||||
</developers>
|
||||
|
||||
<scm>
|
||||
<connection>scm:git:git://github.com/alipay/sofa-registry.git</connection>
|
||||
<developerConnection>scm:git:ssh://github.com/alipay/sofa-registry.git</developerConnection>
|
||||
<url>http://github.com/alipay/sofa-registry/tree/master</url>
|
||||
</scm>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>1.6</maven.compiler.source>
|
||||
<maven.compiler.target>1.6</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<bolt.version>1.6.2</bolt.version>
|
||||
<hessian.version>3.3.8</hessian.version>
|
||||
<lookout.version>1.5.2</lookout.version>
|
||||
<sofa.common.tools.version>1.0.12</sofa.common.tools.version>
|
||||
<netty.version>4.1.42.Final</netty.version>
|
||||
<slf4j.version>1.7.21</slf4j.version>
|
||||
<main.user.dir>../../</main.user.dir>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.alipay.sofa</groupId>
|
||||
<artifactId>registry-core</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alipay.sofa</groupId>
|
||||
<artifactId>registry-client-api</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alipay.sofa</groupId>
|
||||
<artifactId>registry-client-log</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alipay.sofa</groupId>
|
||||
<artifactId>registry-client-impl</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alipay.sofa</groupId>
|
||||
<artifactId>bolt</artifactId>
|
||||
<version>${bolt.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alipay.sofa</groupId>
|
||||
<artifactId>hessian</artifactId>
|
||||
<version>${hessian.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alipay.sofa.common</groupId>
|
||||
<artifactId>sofa-common-tools</artifactId>
|
||||
<version>${sofa.common.tools.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alipay.sofa.lookout</groupId>
|
||||
<artifactId>lookout-api</artifactId>
|
||||
<version>${lookout.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.netty</groupId>
|
||||
<artifactId>netty-all</artifactId>
|
||||
<version>${netty.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
<version>${slf4j.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source}</source>
|
||||
<target>${maven.compiler.target}</target>
|
||||
<encoding>${project.build.sourceEncoding}</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>3.0.2</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<addMavenDescriptor>false</addMavenDescriptor>
|
||||
<index>true</index>
|
||||
<manifest>
|
||||
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
|
||||
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
|
||||
</manifest>
|
||||
<manifestEntries>
|
||||
<Implementation-Build>${maven.build.timestamp}</Implementation-Build>
|
||||
</manifestEntries>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>2.4.3</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<createSourcesJar>true</createSourcesJar>
|
||||
<promoteTransitiveDependencies>false</promoteTransitiveDependencies>
|
||||
<keepDependenciesWithProvidedScope>false</keepDependenciesWithProvidedScope>
|
||||
<createDependencyReducedPom>true</createDependencyReducedPom>
|
||||
<artifactSet>
|
||||
<includes>
|
||||
<include>com.alipay.sofa:registry-core</include>
|
||||
<include>com.alipay.sofa:registry-client-api</include>
|
||||
<include>com.alipay.sofa:registry-client-log</include>
|
||||
<include>com.alipay.sofa:registry-client-impl</include>
|
||||
</includes>
|
||||
</artifactSet>
|
||||
<transformers>
|
||||
<!-- META-INF/services -->
|
||||
<transformer
|
||||
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
|
||||
<!-- spring相关 -->
|
||||
<transformer
|
||||
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
|
||||
<resource>META-INF/spring.handlers</resource>
|
||||
</transformer>
|
||||
<transformer
|
||||
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
|
||||
<resource>META-INF/spring.schemas</resource>
|
||||
</transformer>
|
||||
<!-- 不包含pom信息 -->
|
||||
<transformer
|
||||
implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
|
||||
<resource>pom.xml</resource>
|
||||
</transformer>
|
||||
<transformer
|
||||
implementation="org.apache.maven.plugins.shade.resource.DontIncludeResourceTransformer">
|
||||
<resource>pom.properties</resource>
|
||||
</transformer>
|
||||
</transformers>
|
||||
<filters>
|
||||
<filter>
|
||||
<artifact>*:*</artifact>
|
||||
<excludes>
|
||||
<exclude>META-INF/maven/**</exclude>
|
||||
</excludes>
|
||||
</filter>
|
||||
</filters>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>release</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>2.10.4</version>
|
||||
<configuration>
|
||||
<encoding>${project.build.sourceEncoding}</encoding>
|
||||
<detectOfflineLinks>true</detectOfflineLinks>
|
||||
<breakiterator>true</breakiterator>
|
||||
<author>false</author>
|
||||
<keywords>true</keywords>
|
||||
<quiet>true</quiet>
|
||||
<includeDependencySources>true</includeDependencySources>
|
||||
<dependencySourceIncludes>
|
||||
<dependencySourceInclude>com.alipay.sofa:registry-core</dependencySourceInclude>
|
||||
<dependencySourceInclude>com.alipay.sofa:registry-client-api</dependencySourceInclude>
|
||||
<dependencySourceInclude>com.alipay.sofa:registry-client-log</dependencySourceInclude>
|
||||
<dependencySourceInclude>com.alipay.sofa:registry-client-impl</dependencySourceInclude>
|
||||
</dependencySourceIncludes>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-javadocs</id>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.sonatype.plugins</groupId>
|
||||
<artifactId>nexus-staging-maven-plugin</artifactId>
|
||||
<version>1.6.7</version>
|
||||
<extensions>true</extensions>
|
||||
<configuration>
|
||||
<serverId>ossrh</serverId>
|
||||
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
|
||||
<autoReleaseAfterClose>false</autoReleaseAfterClose>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-gpg-plugin</artifactId>
|
||||
<version>1.5</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>sign-artifacts</id>
|
||||
<phase>verify</phase>
|
||||
<goals>
|
||||
<goal>sign</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<distributionManagement>
|
||||
<repository>
|
||||
<id>ossrh</id>
|
||||
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
|
||||
</repository>
|
||||
<snapshotRepository>
|
||||
<id>ossrh</id>
|
||||
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
|
||||
</snapshotRepository>
|
||||
</distributionManagement>
|
||||
</profile>
|
||||
</profiles>
|
||||
</project>
|
|
@ -0,0 +1,66 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>com.alipay.sofa</groupId>
|
||||
<artifactId>registry-client-parent</artifactId>
|
||||
<version>6.1.9</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>registry-client-api</artifactId>
|
||||
|
||||
<properties>
|
||||
<main.user.dir>../../</main.user.dir>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.alipay.sofa</groupId>
|
||||
<artifactId>registry-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source}</source>
|
||||
<target>${maven.compiler.target}</target>
|
||||
<encoding>${project.build.sourceEncoding}</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-install-plugin</artifactId>
|
||||
<configuration>
|
||||
<skip>${module.install.skip}</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<configuration>
|
||||
<skip>${module.deploy.skip}</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.sonatype.plugins</groupId>
|
||||
<artifactId>nexus-staging-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<skipNexusStagingDeployMojo>${nexus.staging.deploy.mojo.skip}</skipNexusStagingDeployMojo>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.api;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.model.ConfigData;
|
||||
|
||||
/**
|
||||
* The interface Persistence data observer.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : ConfigDataObserver.java, v 0.1 2018-04-17 17:02 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public interface ConfigDataObserver {
|
||||
|
||||
/**
|
||||
* Handle data.
|
||||
*
|
||||
* @param dataId the data id
|
||||
* @param configData the persistence data
|
||||
*/
|
||||
void handleData(String dataId, ConfigData configData);
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.api;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.model.ConfigData;
|
||||
|
||||
/**
|
||||
* The interface Configurator.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : Configurator.java, v 0.1 2018-04-17 17:26 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public interface Configurator extends Register {
|
||||
|
||||
/**
|
||||
* Gets data observer.
|
||||
*
|
||||
* @return the data observer
|
||||
*/
|
||||
ConfigDataObserver getDataObserver();
|
||||
|
||||
/**
|
||||
* Sets config data observer.
|
||||
*
|
||||
* @param configDataObserver the config data observer
|
||||
*/
|
||||
void setDataObserver(ConfigDataObserver configDataObserver);
|
||||
|
||||
/**
|
||||
* Peek data config data.
|
||||
*
|
||||
* @return the config data
|
||||
*/
|
||||
ConfigData peekData();
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.api;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.model.Event;
|
||||
|
||||
/**
|
||||
* The interface Event bus.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : EventBus.java, v 0.1 2018-07-12 21:11 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public interface EventBus {
|
||||
|
||||
/**
|
||||
* Is enable boolean.
|
||||
*
|
||||
* @return the boolean
|
||||
*/
|
||||
boolean isEnable();
|
||||
|
||||
/**
|
||||
* Is enable boolean.
|
||||
*
|
||||
* @param eventClass the event class
|
||||
* @return the boolean
|
||||
*/
|
||||
boolean isEnable(Class<? extends Event> eventClass);
|
||||
|
||||
/**
|
||||
* Register.
|
||||
*
|
||||
* @param eventClass the event class
|
||||
* @param eventSubscriber the event subscriber
|
||||
*/
|
||||
void register(Class<? extends Event> eventClass, EventSubscriber eventSubscriber);
|
||||
|
||||
/**
|
||||
* Un register.
|
||||
*
|
||||
* @param eventClass the event class
|
||||
* @param eventSubscriber the event subscriber
|
||||
*/
|
||||
void unRegister(Class<? extends Event> eventClass, EventSubscriber eventSubscriber);
|
||||
|
||||
/**
|
||||
* Post event.
|
||||
*
|
||||
* @param event the event
|
||||
*/
|
||||
void post(final Event event);
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.api;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.model.Event;
|
||||
|
||||
/**
|
||||
* The interface Event subscriber.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : EventSubscriber.java, v 0.1 2018-07-12 21:12 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public interface EventSubscriber {
|
||||
|
||||
/**
|
||||
* Is sync boolean.
|
||||
*
|
||||
* @return the boolean
|
||||
*/
|
||||
boolean isSync();
|
||||
|
||||
/**
|
||||
* On event.
|
||||
*
|
||||
* @param event the event
|
||||
*/
|
||||
void onEvent(Event event);
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.api;
|
||||
|
||||
/**
|
||||
* The interface Publisher.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : Publisher.java, v 0.1 2017-11-22 16:09 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public interface Publisher extends Register {
|
||||
|
||||
/**
|
||||
* Publish.
|
||||
*
|
||||
* @param data the data
|
||||
*/
|
||||
void republish(String... data);
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.api;
|
||||
|
||||
/**
|
||||
* The interface Register.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : Register.java, v 0.1 2017-11-22 16:00 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public interface Register {
|
||||
|
||||
/** Reset. */
|
||||
void reset();
|
||||
|
||||
/**
|
||||
* Is registered boolean.
|
||||
*
|
||||
* @return the boolean
|
||||
*/
|
||||
boolean isRegistered();
|
||||
|
||||
/** Unregister. */
|
||||
void unregister();
|
||||
|
||||
/**
|
||||
* Gets data id.
|
||||
*
|
||||
* @return String data id
|
||||
*/
|
||||
String getDataId();
|
||||
|
||||
/**
|
||||
* Gets group.
|
||||
*
|
||||
* @return the group
|
||||
*/
|
||||
String getGroup();
|
||||
|
||||
/**
|
||||
* Gets regist id.
|
||||
*
|
||||
* @return the regist id
|
||||
*/
|
||||
String getRegistId();
|
||||
|
||||
/**
|
||||
* Is enabled boolean.
|
||||
*
|
||||
* @return boolean boolean
|
||||
*/
|
||||
boolean isEnabled();
|
||||
|
||||
/**
|
||||
* Gets timestamp.
|
||||
*
|
||||
* @return the timestamp
|
||||
*/
|
||||
long getTimestamp();
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.api;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.model.RegistryType;
|
||||
import com.alipay.sofa.registry.client.api.registration.ConfiguratorRegistration;
|
||||
import com.alipay.sofa.registry.client.api.registration.PublisherRegistration;
|
||||
import com.alipay.sofa.registry.client.api.registration.SubscriberRegistration;
|
||||
|
||||
/**
|
||||
* The interface Registry client.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : RegistryClient.java, v 0.1 2017-11-20 18:16 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public interface RegistryClient {
|
||||
|
||||
/**
|
||||
* Register publisher.
|
||||
*
|
||||
* @param registration the registration
|
||||
* @param data the data
|
||||
* @return the publisher
|
||||
*/
|
||||
Publisher register(PublisherRegistration registration, String... data);
|
||||
|
||||
/**
|
||||
* Register multi subscriber multi.
|
||||
*
|
||||
* @param registration the registration
|
||||
* @return the subscriber multi
|
||||
*/
|
||||
Subscriber register(SubscriberRegistration registration);
|
||||
|
||||
/**
|
||||
* Register configurator.
|
||||
*
|
||||
* @param registration the registration
|
||||
* @return the configurator
|
||||
*/
|
||||
Configurator register(ConfiguratorRegistration registration);
|
||||
|
||||
/**
|
||||
* Unregister all publishers or subscribers belong to dataId.
|
||||
*
|
||||
* @param dataId the data id
|
||||
* @param group registration group, use default group if null
|
||||
* @param registryType the registry type, publisher or subscriber
|
||||
* @return unregister total registers
|
||||
*/
|
||||
int unregister(String dataId, String group, RegistryType registryType);
|
||||
}
|
|
@ -0,0 +1,175 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.api;
|
||||
|
||||
/**
|
||||
* The interface Registry client config.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : RegistryClientConfig.java, v 0.1 2017-11-23 19:52 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public interface RegistryClientConfig {
|
||||
|
||||
/**
|
||||
* Gets env.
|
||||
*
|
||||
* @return the env
|
||||
*/
|
||||
String getEnv();
|
||||
|
||||
/**
|
||||
* Gets instance id.
|
||||
*
|
||||
* @return the instance id
|
||||
*/
|
||||
String getInstanceId();
|
||||
|
||||
String getIp();
|
||||
|
||||
/**
|
||||
* Gets cell.
|
||||
*
|
||||
* @return the cell
|
||||
*/
|
||||
String getZone();
|
||||
|
||||
/**
|
||||
* Gets registry endpoint.
|
||||
*
|
||||
* @return the registry endpoint
|
||||
*/
|
||||
String getRegistryEndpoint();
|
||||
|
||||
/**
|
||||
* Gets registry endpoint port.
|
||||
*
|
||||
* @return the registry endpoint port
|
||||
*/
|
||||
int getRegistryEndpointPort();
|
||||
|
||||
/**
|
||||
* Gets data center.
|
||||
*
|
||||
* @return the data center
|
||||
*/
|
||||
String getDataCenter();
|
||||
|
||||
/**
|
||||
* Gets app name.
|
||||
*
|
||||
* @return the app name
|
||||
*/
|
||||
String getAppName();
|
||||
|
||||
/**
|
||||
* Gets connect timeout.
|
||||
*
|
||||
* @return the connect timeout
|
||||
*/
|
||||
int getConnectTimeout();
|
||||
|
||||
/**
|
||||
* Gets socket timeout.
|
||||
*
|
||||
* @return the socket timeout
|
||||
*/
|
||||
int getSocketTimeout();
|
||||
|
||||
/**
|
||||
* Gets invoke timeout.
|
||||
*
|
||||
* @return the invoke timeout
|
||||
*/
|
||||
int getInvokeTimeout();
|
||||
|
||||
/**
|
||||
* Gets recheck interval.
|
||||
*
|
||||
* @return the recheck interval
|
||||
*/
|
||||
int getRecheckInterval();
|
||||
|
||||
/**
|
||||
* Gets observer thread core size.
|
||||
*
|
||||
* @return the observer thread core size
|
||||
*/
|
||||
int getObserverThreadCoreSize();
|
||||
|
||||
/**
|
||||
* Gets observer thread max size.
|
||||
*
|
||||
* @return the observer thread max size
|
||||
*/
|
||||
int getObserverThreadMaxSize();
|
||||
|
||||
/**
|
||||
* Gets observer thread queue length.
|
||||
*
|
||||
* @return the observer thread queue length
|
||||
*/
|
||||
int getObserverThreadQueueLength();
|
||||
|
||||
/**
|
||||
* Gets observer callback timeout.
|
||||
*
|
||||
* @return the observer callback timeout
|
||||
*/
|
||||
int getObserverCallbackTimeout();
|
||||
|
||||
/**
|
||||
* Gets sync config retry interval.
|
||||
*
|
||||
* @return the sync config retry interval
|
||||
*/
|
||||
int getSyncConfigRetryInterval();
|
||||
|
||||
/**
|
||||
* Gets access key.
|
||||
*
|
||||
* @return the access key
|
||||
*/
|
||||
String getAccessKey();
|
||||
|
||||
/**
|
||||
* Gets secret key.
|
||||
*
|
||||
* @return the secret key
|
||||
*/
|
||||
String getSecretKey();
|
||||
|
||||
/**
|
||||
* Gets algorithm.
|
||||
*
|
||||
* @return the algorithm
|
||||
*/
|
||||
String getAlgorithm();
|
||||
|
||||
/**
|
||||
* Gets auth cache interval.
|
||||
*
|
||||
* @return the auth cache interval
|
||||
*/
|
||||
long getAuthCacheInterval();
|
||||
|
||||
/**
|
||||
* Is event bus enable boolean.
|
||||
*
|
||||
* @return the boolean
|
||||
*/
|
||||
boolean isEventBusEnable();
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.api;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.model.UserData;
|
||||
import com.alipay.sofa.registry.core.model.ScopeEnum;
|
||||
|
||||
/**
|
||||
* The interface Subscriber multi.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : Subscriber.java, v 0.1 2017-11-23 14:35 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public interface Subscriber extends Register {
|
||||
|
||||
/**
|
||||
* Gets data observer.
|
||||
*
|
||||
* @return the data observer
|
||||
*/
|
||||
SubscriberDataObserver getDataObserver();
|
||||
|
||||
/**
|
||||
* Sets data observer.
|
||||
*
|
||||
* @param observer the observer
|
||||
*/
|
||||
void setDataObserver(SubscriberDataObserver observer);
|
||||
|
||||
/**
|
||||
* Peek data user data multi.
|
||||
*
|
||||
* @return the user data multi
|
||||
*/
|
||||
UserData peekData();
|
||||
|
||||
/**
|
||||
* Gets scope enum.
|
||||
*
|
||||
* @return the scope enum
|
||||
*/
|
||||
ScopeEnum getScopeEnum();
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.api;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.model.UserData;
|
||||
|
||||
/**
|
||||
* The interface Subscriber data observer multi.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : SubscriberDataObserver.java, v 0.1 2017-11-23 15:16 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public interface SubscriberDataObserver {
|
||||
|
||||
/**
|
||||
* Handle data.
|
||||
*
|
||||
* @param dataId the data id
|
||||
* @param data the data
|
||||
*/
|
||||
void handleData(String dataId, UserData data);
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.api.exception;
|
||||
|
||||
/**
|
||||
* The type Duplicate exception.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : DuplicateException.java, v 0.1 2017-11-30 19:46 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class DuplicateException extends IllegalArgumentException {
|
||||
|
||||
/** UID */
|
||||
private static final long serialVersionUID = 167969795120169890L;
|
||||
|
||||
/** Instantiates a new Duplicate exception. */
|
||||
public DuplicateException() {}
|
||||
|
||||
/**
|
||||
* Instantiates a new Duplicate exception.
|
||||
*
|
||||
* @param s the s
|
||||
*/
|
||||
public DuplicateException(String s) {
|
||||
super(s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new Duplicate exception.
|
||||
*
|
||||
* @param message the message
|
||||
* @param cause the cause
|
||||
*/
|
||||
public DuplicateException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new Duplicate exception.
|
||||
*
|
||||
* @param cause the cause
|
||||
*/
|
||||
public DuplicateException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.api.exception;
|
||||
|
||||
/**
|
||||
* The type Registry client exception.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : RegistryClientException.java, v 0.1 2017-11-30 19:43 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class RegistryClientException extends RuntimeException {
|
||||
|
||||
/** UID */
|
||||
private static final long serialVersionUID = -1068018180829676315L;
|
||||
|
||||
/** Instantiates a new Registry client exception. */
|
||||
public RegistryClientException() {}
|
||||
|
||||
/**
|
||||
* Instantiates a new Registry client exception.
|
||||
*
|
||||
* @param message the message
|
||||
*/
|
||||
public RegistryClientException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new Registry client exception.
|
||||
*
|
||||
* @param message the message
|
||||
* @param cause the cause
|
||||
*/
|
||||
public RegistryClientException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new Registry client exception.
|
||||
*
|
||||
* @param cause the cause
|
||||
*/
|
||||
public RegistryClientException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.api.model;
|
||||
|
||||
/**
|
||||
* The interface Persistence data.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : ConfigData.java, v 0.1 2018-04-17 17:15 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public interface ConfigData {
|
||||
|
||||
/**
|
||||
* Gets data.
|
||||
*
|
||||
* @return the data
|
||||
*/
|
||||
String getData();
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.api.model;
|
||||
|
||||
/**
|
||||
* The interface Event.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : Event.java, v 0.1 2018-07-12 21:10 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public interface Event {}
|
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.api.model;
|
||||
|
||||
/**
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id: RegistryType.java, v 0.1 2018-03-13 15:22 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public enum RegistryType {
|
||||
PUBLISHER,
|
||||
SUBSCRIBER,
|
||||
CONFIGURATOR
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.api.model;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* The type User data.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : UserData.java, v 0.1 2017-11-23 14:37 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public interface UserData {
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>zoneData</tt>.
|
||||
*
|
||||
* @return property value of zoneData
|
||||
*/
|
||||
Map<String, List<String>> getZoneData();
|
||||
|
||||
/**
|
||||
* Gets local zone.
|
||||
*
|
||||
* @return the local zone
|
||||
*/
|
||||
String getLocalZone();
|
||||
}
|
|
@ -0,0 +1,128 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.api.registration;
|
||||
|
||||
/**
|
||||
* Base registration.
|
||||
*
|
||||
* @author yeqing.yq
|
||||
* @version $Id : BaseRegistration.java, v 0.1 2018-09-04 11:36 yeqing.yq Exp $$
|
||||
*/
|
||||
public class BaseRegistration {
|
||||
|
||||
protected String dataId;
|
||||
|
||||
protected String group;
|
||||
|
||||
protected String appName;
|
||||
|
||||
protected String instanceId;
|
||||
|
||||
protected String ip;
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>dataId</tt>.
|
||||
*
|
||||
* @return property value of dataId
|
||||
*/
|
||||
public String getDataId() {
|
||||
return dataId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>dataId</tt>.
|
||||
*
|
||||
* @param dataId value to be assigned to property dataId
|
||||
*/
|
||||
public void setDataId(String dataId) {
|
||||
this.dataId = dataId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>group</tt>.
|
||||
*
|
||||
* @return property value of group
|
||||
*/
|
||||
public String getGroup() {
|
||||
return group;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>group</tt>.
|
||||
*
|
||||
* @param group value to be assigned to property group
|
||||
*/
|
||||
public void setGroup(String group) {
|
||||
this.group = group;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>appName</tt>.
|
||||
*
|
||||
* @return property value of appName
|
||||
*/
|
||||
public String getAppName() {
|
||||
return appName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>appName</tt>.
|
||||
*
|
||||
* @param appName value to be assigned to property appName
|
||||
*/
|
||||
public void setAppName(String appName) {
|
||||
this.appName = appName;
|
||||
}
|
||||
|
||||
public String getInstanceId() {
|
||||
return instanceId;
|
||||
}
|
||||
|
||||
public void setInstanceId(String instanceId) {
|
||||
this.instanceId = instanceId;
|
||||
}
|
||||
|
||||
public String getIp() {
|
||||
return ip;
|
||||
}
|
||||
|
||||
public void setIp(String ip) {
|
||||
this.ip = ip;
|
||||
}
|
||||
|
||||
/** @see Object#toString() */
|
||||
@Override
|
||||
public String toString() {
|
||||
return "BaseRegistration{"
|
||||
+ "dataId='"
|
||||
+ dataId
|
||||
+ '\''
|
||||
+ ", group='"
|
||||
+ group
|
||||
+ '\''
|
||||
+ ", appName='"
|
||||
+ appName
|
||||
+ '\''
|
||||
+ ", instanceId='"
|
||||
+ instanceId
|
||||
+ '\''
|
||||
+ ", ip='"
|
||||
+ ip
|
||||
+ '\''
|
||||
+ '}';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.api.registration;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.ConfigDataObserver;
|
||||
|
||||
/**
|
||||
* The type Configurator registration.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : ConfiguratorRegistration.java, v 0.1 2018-04-20 11:36 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class ConfiguratorRegistration extends BaseRegistration {
|
||||
|
||||
private ConfigDataObserver configDataObserver;
|
||||
|
||||
/**
|
||||
* Instantiates a new Configurator registration.
|
||||
*
|
||||
* @param dataId the data id
|
||||
* @param configDataObserver the config data observer
|
||||
*/
|
||||
public ConfiguratorRegistration(String dataId, ConfigDataObserver configDataObserver) {
|
||||
this.dataId = dataId;
|
||||
this.configDataObserver = configDataObserver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>configDataObserver</tt>.
|
||||
*
|
||||
* @return property value of configDataObserver
|
||||
*/
|
||||
public ConfigDataObserver getConfigDataObserver() {
|
||||
return configDataObserver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>configDataObserver</tt>.
|
||||
*
|
||||
* @param configDataObserver value to be assigned to property configDataObserver
|
||||
*/
|
||||
public void setConfigDataObserver(ConfigDataObserver configDataObserver) {
|
||||
this.configDataObserver = configDataObserver;
|
||||
}
|
||||
|
||||
/** @see Object#toString() */
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ConfiguratorRegistration{"
|
||||
+ "dataId='"
|
||||
+ dataId
|
||||
+ '\''
|
||||
+ ", group='"
|
||||
+ group
|
||||
+ '\''
|
||||
+ ", appName='"
|
||||
+ appName
|
||||
+ '\''
|
||||
+ ", instanceId='"
|
||||
+ instanceId
|
||||
+ '\''
|
||||
+ ", ip='"
|
||||
+ ip
|
||||
+ '\''
|
||||
+ '}';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.api.registration;
|
||||
|
||||
/**
|
||||
* The type Publisher registration.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : PublisherRegistration.java, v 0.1 2017-11-23 15:39 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class PublisherRegistration extends BaseRegistration {
|
||||
|
||||
/**
|
||||
* Instantiates a new Publisher registration.
|
||||
*
|
||||
* @param dataId the data id
|
||||
*/
|
||||
public PublisherRegistration(String dataId) {
|
||||
this.dataId = dataId;
|
||||
}
|
||||
|
||||
/**
|
||||
* To string string.
|
||||
*
|
||||
* @return the string
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PublisherRegistration{"
|
||||
+ "dataId='"
|
||||
+ dataId
|
||||
+ '\''
|
||||
+ ", group='"
|
||||
+ group
|
||||
+ '\''
|
||||
+ ", appName='"
|
||||
+ appName
|
||||
+ '\''
|
||||
+ ", instanceId='"
|
||||
+ instanceId
|
||||
+ '\''
|
||||
+ ", ip='"
|
||||
+ ip
|
||||
+ '\''
|
||||
+ +'}';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,110 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.api.registration;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.SubscriberDataObserver;
|
||||
import com.alipay.sofa.registry.core.model.ScopeEnum;
|
||||
|
||||
/**
|
||||
* The type Subscriber registration.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : SubscriberRegistration.java, v 0.1 2017-11-23 17:05 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class SubscriberRegistration extends BaseRegistration {
|
||||
|
||||
private ScopeEnum scopeEnum;
|
||||
|
||||
private SubscriberDataObserver subscriberDataObserver;
|
||||
|
||||
/**
|
||||
* Instantiates a new Subscriber registration.
|
||||
*
|
||||
* @param dataId the data id
|
||||
* @param subscriberDataObserver the subscriber data observer
|
||||
*/
|
||||
public SubscriberRegistration(String dataId, SubscriberDataObserver subscriberDataObserver) {
|
||||
this.dataId = dataId;
|
||||
this.subscriberDataObserver = subscriberDataObserver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>scopeEnum</tt>.
|
||||
*
|
||||
* @return property value of scopeEnum
|
||||
*/
|
||||
public ScopeEnum getScopeEnum() {
|
||||
return scopeEnum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>scopeEnum</tt>.
|
||||
*
|
||||
* @param scopeEnum value to be assigned to property scopeEnum
|
||||
*/
|
||||
public void setScopeEnum(ScopeEnum scopeEnum) {
|
||||
this.scopeEnum = scopeEnum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>subscriberDataObserver</tt>.
|
||||
*
|
||||
* @return property value of subscriberDataObserver
|
||||
*/
|
||||
public SubscriberDataObserver getSubscriberDataObserver() {
|
||||
return subscriberDataObserver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>subscriberDataObserver</tt>.
|
||||
*
|
||||
* @param subscriberDataObserver value to be assigned to property subscriberDataObserver
|
||||
*/
|
||||
public void setSubscriberDataObserver(SubscriberDataObserver subscriberDataObserver) {
|
||||
this.subscriberDataObserver = subscriberDataObserver;
|
||||
}
|
||||
|
||||
/**
|
||||
* To string string.
|
||||
*
|
||||
* @return the string
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SubscriberRegistration{"
|
||||
+ "dataId='"
|
||||
+ dataId
|
||||
+ '\''
|
||||
+ ", group='"
|
||||
+ group
|
||||
+ '\''
|
||||
+ ", appName='"
|
||||
+ appName
|
||||
+ '\''
|
||||
+ ", instanceId='"
|
||||
+ instanceId
|
||||
+ '\''
|
||||
+ ", ip='"
|
||||
+ ip
|
||||
+ '\''
|
||||
+ ", scopeEnum="
|
||||
+ scopeEnum
|
||||
+ ", subscriberDataObserver="
|
||||
+ subscriberDataObserver
|
||||
+ '}';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.api.exception;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/** @author <a href="mailto:zhanggeng.zg@antfin.com">GengZhang</a> */
|
||||
public class DuplicateExceptionTest {
|
||||
|
||||
@Test
|
||||
public void testConstruct() {
|
||||
DuplicateException exception = new DuplicateException();
|
||||
Assert.assertNull(exception.getMessage());
|
||||
|
||||
exception = new DuplicateException("1");
|
||||
Assert.assertEquals("1", exception.getMessage());
|
||||
|
||||
RuntimeException runtimeException = new RuntimeException("xxx");
|
||||
exception = new DuplicateException(runtimeException);
|
||||
Assert.assertEquals("java.lang.RuntimeException: xxx", exception.getMessage());
|
||||
Assert.assertEquals(runtimeException, exception.getCause());
|
||||
|
||||
exception = new DuplicateException("1", runtimeException);
|
||||
Assert.assertEquals("1", exception.getMessage());
|
||||
Assert.assertEquals(runtimeException, exception.getCause());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.api.exception;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/** @author <a href="mailto:zhanggeng.zg@antfin.com">GengZhang</a> */
|
||||
public class RegistryClientExceptionTest {
|
||||
|
||||
@Test
|
||||
public void testConstruct() {
|
||||
RegistryClientException exception = new RegistryClientException();
|
||||
Assert.assertNull(exception.getMessage());
|
||||
|
||||
exception = new RegistryClientException("1");
|
||||
Assert.assertEquals("1", exception.getMessage());
|
||||
|
||||
RuntimeException runtimeException = new RuntimeException("xxx");
|
||||
exception = new RegistryClientException(runtimeException);
|
||||
Assert.assertEquals("java.lang.RuntimeException: xxx", exception.getMessage());
|
||||
Assert.assertEquals(runtimeException, exception.getCause());
|
||||
|
||||
exception = new RegistryClientException("1", runtimeException);
|
||||
Assert.assertEquals("1", exception.getMessage());
|
||||
Assert.assertEquals(runtimeException, exception.getCause());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.api.registration;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/** @author <a href="mailto:zhanggeng.zg@antfin.com">GengZhang</a> */
|
||||
public class BaseRegistrationTest {
|
||||
|
||||
@Test
|
||||
public void testAll() {
|
||||
BaseRegistration registration = new BaseRegistration();
|
||||
registration.setAppName("app");
|
||||
registration.setDataId("dataId");
|
||||
registration.setGroup("group");
|
||||
registration.setInstanceId("instanceId");
|
||||
registration.setIp("ip");
|
||||
Assert.assertEquals("app", registration.getAppName());
|
||||
Assert.assertEquals("dataId", registration.getDataId());
|
||||
Assert.assertEquals("group", registration.getGroup());
|
||||
Assert.assertEquals("instanceId", registration.getInstanceId());
|
||||
Assert.assertEquals("ip", registration.getIp());
|
||||
Assert.assertEquals(
|
||||
"BaseRegistration{dataId='dataId', group='group', appName='app', instanceId='instanceId', ip='ip'}",
|
||||
registration.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetAttrs() {
|
||||
BaseRegistration registration = new BaseRegistration();
|
||||
registration.setIp("127.0.0.1");
|
||||
Assert.assertEquals(registration.getIp(), "127.0.0.1");
|
||||
registration.setInstanceId("instanceId");
|
||||
Assert.assertEquals(registration.getInstanceId(), "instanceId");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.api.registration;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.ConfigDataObserver;
|
||||
import com.alipay.sofa.registry.client.api.model.ConfigData;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/** @author <a href="mailto:zhanggeng.zg@antfin.com">GengZhang</a> */
|
||||
public class ConfiguratorRegistrationTest {
|
||||
|
||||
@Test
|
||||
public void testAll() {
|
||||
ConfiguratorRegistration registration = new ConfiguratorRegistration("xxx", null);
|
||||
Assert.assertEquals("xxx", registration.getDataId());
|
||||
Assert.assertNull(registration.getConfigDataObserver());
|
||||
registration.setConfigDataObserver(
|
||||
new ConfigDataObserver() {
|
||||
@Override
|
||||
public void handleData(String dataId, ConfigData configData) {}
|
||||
});
|
||||
Assert.assertNotNull(registration.getConfigDataObserver());
|
||||
Assert.assertTrue(registration.toString().contains("xxx"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.api.registration;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/** @author <a href="mailto:zhanggeng.zg@antfin.com">GengZhang</a> */
|
||||
public class PublisherRegistrationTest {
|
||||
|
||||
@Test
|
||||
public void testAll() {
|
||||
PublisherRegistration registration = new PublisherRegistration("xxx");
|
||||
Assert.assertEquals("xxx", registration.getDataId());
|
||||
Assert.assertTrue(registration.toString().contains("xxx"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.api.registration;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.SubscriberDataObserver;
|
||||
import com.alipay.sofa.registry.client.api.model.UserData;
|
||||
import com.alipay.sofa.registry.core.model.ScopeEnum;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/** @author <a href="mailto:zhanggeng.zg@antfin.com">GengZhang</a> */
|
||||
public class SubscriberRegistrationTest {
|
||||
|
||||
@Test
|
||||
public void testAll() {
|
||||
SubscriberRegistration registration = new SubscriberRegistration("xxx", null);
|
||||
Assert.assertEquals("xxx", registration.getDataId());
|
||||
Assert.assertNull(registration.getSubscriberDataObserver());
|
||||
registration.setSubscriberDataObserver(
|
||||
new SubscriberDataObserver() {
|
||||
@Override
|
||||
public void handleData(String dataId, UserData data) {}
|
||||
});
|
||||
Assert.assertNotNull(registration.getSubscriberDataObserver());
|
||||
registration.setScopeEnum(ScopeEnum.dataCenter);
|
||||
Assert.assertEquals(ScopeEnum.dataCenter, registration.getScopeEnum());
|
||||
Assert.assertTrue(registration.toString().contains("xxx"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.core.model;
|
||||
|
||||
import java.util.Arrays;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/** @author <a href="mailto:zhanggeng.zg@antfin.com">GengZhang</a> */
|
||||
public class ReceivedConfigDataTest {
|
||||
|
||||
@Test
|
||||
public void testAll() {
|
||||
ReceivedConfigData data = new ReceivedConfigData();
|
||||
data.setDataId("dataIdd");
|
||||
data.setGroup("groupp");
|
||||
data.setInstanceId("instanceIdd");
|
||||
data.setConfiguratorRegistIds(Arrays.asList("id1", "id2"));
|
||||
data.setDataBox(new DataBox());
|
||||
data.setVersion(1234L);
|
||||
Assert.assertEquals("dataIdd", data.getDataId());
|
||||
Assert.assertEquals("groupp", data.getGroup());
|
||||
Assert.assertEquals("instanceIdd", data.getInstanceId());
|
||||
Assert.assertEquals(2, data.getConfiguratorRegistIds().size());
|
||||
Assert.assertNotNull(data.getDataBox());
|
||||
Assert.assertEquals(1234L, (long) data.getVersion());
|
||||
Assert.assertTrue(data.toString().contains("instanceIdd"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.core.model;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/** @author <a href="mailto:zhanggeng.zg@antfin.com">GengZhang</a> */
|
||||
public class ReceivedDataTest {
|
||||
|
||||
@Test
|
||||
public void testAll() {
|
||||
|
||||
ReceivedData data = new ReceivedData();
|
||||
data.setDataId("dataIdd");
|
||||
data.setGroup("groupp");
|
||||
data.setInstanceId("instanceIdd");
|
||||
data.setSubscriberRegistIds(Arrays.asList("id1", "id2"));
|
||||
data.setLocalZone("local");
|
||||
data.setScope("zone");
|
||||
data.setData(new HashMap<String, List<DataBox>>());
|
||||
data.setSegment("seg1");
|
||||
data.setVersion(1234L);
|
||||
Assert.assertEquals("dataIdd", data.getDataId());
|
||||
Assert.assertEquals("groupp", data.getGroup());
|
||||
Assert.assertEquals("instanceIdd", data.getInstanceId());
|
||||
Assert.assertEquals(2, data.getSubscriberRegistIds().size());
|
||||
Assert.assertEquals("zone", data.getScope());
|
||||
Assert.assertEquals("seg1", data.getSegment());
|
||||
Assert.assertEquals("local", data.getLocalZone());
|
||||
Assert.assertEquals(1234L, (long) data.getVersion());
|
||||
Assert.assertEquals(0, data.getData().size());
|
||||
Assert.assertTrue(data.toString().contains("instanceIdd"));
|
||||
|
||||
ReceivedData data1 = new ReceivedData(null, null, null, null, null, null, null);
|
||||
Assert.assertNull(data1.getVersion());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.core.model;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/** @author <a href="mailto:zhanggeng.zg@antfin.com">GengZhang</a> */
|
||||
public class ResultTest {
|
||||
|
||||
@Test
|
||||
public void testAll() {
|
||||
Result result = new Result();
|
||||
result.setSuccess(false);
|
||||
result.setMessage("xxx");
|
||||
Assert.assertFalse(result.isSuccess());
|
||||
Assert.assertEquals("xxx", result.getMessage());
|
||||
Assert.assertTrue(result.toString().contains("xxx"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.core.model;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
/** @author <a href="mailto:zhanggeng.zg@antfin.com">GengZhang</a> */
|
||||
public class ScopeEnumTest {
|
||||
|
||||
@Test
|
||||
public void contains() {
|
||||
Assert.assertTrue(ScopeEnum.contains("zone"));
|
||||
Assert.assertFalse(ScopeEnum.contains("xxxx"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,119 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>com.alipay.sofa</groupId>
|
||||
<artifactId>registry-client-parent</artifactId>
|
||||
<version>6.1.9</version>
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>registry-client-impl</artifactId>
|
||||
|
||||
<properties>
|
||||
<main.user.dir>../../</main.user.dir>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.alipay.sofa</groupId>
|
||||
<artifactId>registry-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alipay.sofa</groupId>
|
||||
<artifactId>registry-client-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alipay.sofa</groupId>
|
||||
<artifactId>registry-client-log</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alipay.sofa</groupId>
|
||||
<artifactId>bolt</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alipay.sofa</groupId>
|
||||
<artifactId>hessian</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.netty</groupId>
|
||||
<artifactId>netty-all</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alipay.sofa.common</groupId>
|
||||
<artifactId>sofa-common-tools</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alipay.sofa.lookout</groupId>
|
||||
<artifactId>lookout-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>ch.qos.logback</groupId>
|
||||
<artifactId>logback-classic</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-module-junit4</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.powermock</groupId>
|
||||
<artifactId>powermock-api-mockito</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>${maven.compiler.source}</source>
|
||||
<target>${maven.compiler.target}</target>
|
||||
<encoding>${project.build.sourceEncoding}</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-install-plugin</artifactId>
|
||||
<configuration>
|
||||
<skip>${module.install.skip}</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<configuration>
|
||||
<skip>${module.deploy.skip}</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.sonatype.plugins</groupId>
|
||||
<artifactId>nexus-staging-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<skipNexusStagingDeployMojo>${nexus.staging.deploy.mojo.skip}</skipNexusStagingDeployMojo>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.auth;
|
||||
|
||||
import com.alipay.sofa.registry.core.model.BaseRegister;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* The interface Auth manager.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : AuthManager.java, v 0.1 2018-04-18 12:12 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public interface AuthManager {
|
||||
|
||||
/**
|
||||
* Gets auth content.
|
||||
*
|
||||
* @param register the register
|
||||
* @return the auth content
|
||||
*/
|
||||
Map<String, String> getAuthContent(BaseRegister register);
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.auth;
|
||||
|
||||
import com.alipay.sofa.registry.core.model.BaseRegister;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id: NoopAuthManager.java, v 0.1 2018-04-18 12:13 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public final class NoopAuthManager implements AuthManager {
|
||||
|
||||
public static final AuthManager INSTANCE = new NoopAuthManager();
|
||||
|
||||
private NoopAuthManager() {}
|
||||
|
||||
/**
|
||||
* Gets auth content.
|
||||
*
|
||||
* @param register the register
|
||||
* @return the auth content
|
||||
*/
|
||||
@Override
|
||||
public Map<String, String> getAuthContent(BaseRegister register) {
|
||||
return new HashMap<String, String>();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.constants;
|
||||
|
||||
/**
|
||||
* The type Value constants.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : ValueConstants.java, v 0.1 2017-11-30 20:03 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class ValueConstants {
|
||||
|
||||
/** The constant DEFAULT_GROUP. */
|
||||
public static final String DEFAULT_GROUP = "DEFAULT_GROUP";
|
||||
|
||||
/** The constant DEFAULT_ZONE. */
|
||||
public static final String DEFAULT_ZONE = "DEFAULT_ZONE";
|
||||
|
||||
/** The constant DEFAULT_DATA_CENTER. */
|
||||
public static final String DEFAULT_DATA_CENTER = "DefaultDataCenter";
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.constants;
|
||||
|
||||
/**
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id: VersionConstants.java, v 0.1 2017-11-23 22:36 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class VersionConstants {
|
||||
|
||||
public static final long UNINITIALIZED_VERSION = 0;
|
||||
}
|
|
@ -0,0 +1,143 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.event;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.Configurator;
|
||||
import com.alipay.sofa.registry.client.api.RegistryClientConfig;
|
||||
import com.alipay.sofa.registry.client.api.model.Event;
|
||||
|
||||
/**
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id: ConfiguratorProcessEvent.java, v 0.1 2018-07-13 18:44 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class ConfiguratorProcessEvent implements Event {
|
||||
|
||||
private Configurator configurator;
|
||||
|
||||
private RegistryClientConfig config;
|
||||
|
||||
private long start;
|
||||
|
||||
private long end;
|
||||
|
||||
private Throwable throwable;
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>configurator</tt>.
|
||||
*
|
||||
* @return property value of configurator
|
||||
*/
|
||||
public Configurator getConfigurator() {
|
||||
return configurator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>configurator</tt>.
|
||||
*
|
||||
* @param configurator value to be assigned to property configurator
|
||||
*/
|
||||
public void setConfigurator(Configurator configurator) {
|
||||
this.configurator = configurator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>config</tt>.
|
||||
*
|
||||
* @return property value of config
|
||||
*/
|
||||
public RegistryClientConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>config</tt>.
|
||||
*
|
||||
* @param config value to be assigned to property config
|
||||
*/
|
||||
public void setConfig(RegistryClientConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>start</tt>.
|
||||
*
|
||||
* @return property value of start
|
||||
*/
|
||||
public long getStart() {
|
||||
return start;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>start</tt>.
|
||||
*
|
||||
* @param start value to be assigned to property start
|
||||
*/
|
||||
public void setStart(long start) {
|
||||
this.start = start;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>end</tt>.
|
||||
*
|
||||
* @return property value of end
|
||||
*/
|
||||
public long getEnd() {
|
||||
return end;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>end</tt>.
|
||||
*
|
||||
* @param end value to be assigned to property end
|
||||
*/
|
||||
public void setEnd(long end) {
|
||||
this.end = end;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>throwable</tt>.
|
||||
*
|
||||
* @return property value of throwable
|
||||
*/
|
||||
public Throwable getThrowable() {
|
||||
return throwable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>throwable</tt>.
|
||||
*
|
||||
* @param throwable value to be assigned to property throwable
|
||||
*/
|
||||
public void setThrowable(Throwable throwable) {
|
||||
this.throwable = throwable;
|
||||
}
|
||||
|
||||
/** @see Object#toString() */
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ConfiguratorProcessEvent{"
|
||||
+ "configurator="
|
||||
+ configurator
|
||||
+ ", start="
|
||||
+ start
|
||||
+ ", end="
|
||||
+ end
|
||||
+ ", throwable="
|
||||
+ throwable
|
||||
+ '}';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,153 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.event;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.EventBus;
|
||||
import com.alipay.sofa.registry.client.api.EventSubscriber;
|
||||
import com.alipay.sofa.registry.client.api.RegistryClientConfig;
|
||||
import com.alipay.sofa.registry.client.api.model.Event;
|
||||
import com.alipay.sofa.registry.client.factory.NamedThreadFactory;
|
||||
import com.alipay.sofa.registry.client.log.LoggerFactory;
|
||||
import com.alipay.sofa.registry.client.util.CommonUtils;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.concurrent.CopyOnWriteArraySet;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.LinkedBlockingDeque;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
/**
|
||||
* The type Default event bus.
|
||||
*
|
||||
* @author zhanggeng.zg
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : DefaultEventBus.java, v 0.1 2018-07-13 10:56 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class DefaultEventBus implements EventBus {
|
||||
|
||||
/** LOGGER */
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultEventBus.class);
|
||||
|
||||
private RegistryClientConfig config;
|
||||
|
||||
private ConcurrentMap<Class<? extends Event>, CopyOnWriteArraySet<EventSubscriber>>
|
||||
eventSubscriberMap =
|
||||
new ConcurrentHashMap<Class<? extends Event>, CopyOnWriteArraySet<EventSubscriber>>();
|
||||
|
||||
private Executor executor;
|
||||
|
||||
/**
|
||||
* Instantiates a new Default event bus.
|
||||
*
|
||||
* @param config the config
|
||||
*/
|
||||
public DefaultEventBus(RegistryClientConfig config) {
|
||||
this.config = config;
|
||||
this.executor =
|
||||
new ThreadPoolExecutor(
|
||||
config.getObserverThreadCoreSize(),
|
||||
config.getObserverThreadMaxSize(),
|
||||
0,
|
||||
TimeUnit.SECONDS,
|
||||
new LinkedBlockingDeque<Runnable>(config.getObserverThreadQueueLength()),
|
||||
new NamedThreadFactory("DefaultEventBusThread"));
|
||||
}
|
||||
|
||||
/** @see EventBus#register(Class, EventSubscriber) */
|
||||
@Override
|
||||
public void register(Class<? extends Event> eventClass, EventSubscriber eventSubscriber) {
|
||||
CopyOnWriteArraySet<EventSubscriber> set = eventSubscriberMap.get(eventClass);
|
||||
if (set == null) {
|
||||
set = new CopyOnWriteArraySet<EventSubscriber>();
|
||||
CopyOnWriteArraySet<EventSubscriber> old = eventSubscriberMap.putIfAbsent(eventClass, set);
|
||||
if (old != null) {
|
||||
set = old;
|
||||
}
|
||||
}
|
||||
set.add(eventSubscriber);
|
||||
LOGGER.debug("Register subscriber: {} of event: {}.", eventSubscriber, eventClass);
|
||||
}
|
||||
|
||||
/** @see EventBus#unRegister(Class, EventSubscriber) */
|
||||
@Override
|
||||
public void unRegister(Class<? extends Event> eventClass, EventSubscriber eventSubscriber) {
|
||||
CopyOnWriteArraySet<EventSubscriber> set = eventSubscriberMap.get(eventClass);
|
||||
if (set != null) {
|
||||
set.remove(eventSubscriber);
|
||||
LOGGER.debug("UnRegister subscriber: {} of event: {}.", eventSubscriber, eventClass);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Post.
|
||||
*
|
||||
* @param event the event
|
||||
*/
|
||||
@Override
|
||||
public void post(final Event event) {
|
||||
if (!isEnable()) {
|
||||
return;
|
||||
}
|
||||
CopyOnWriteArraySet<EventSubscriber> subscribers = eventSubscriberMap.get(event.getClass());
|
||||
if (null != subscribers && !subscribers.isEmpty()) {
|
||||
for (final EventSubscriber subscriber : subscribers) {
|
||||
if (subscriber.isSync()) {
|
||||
handleEvent(subscriber, event);
|
||||
} else { // 异步
|
||||
executor.execute(
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
handleEvent(subscriber, event);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Is enable boolean.
|
||||
*
|
||||
* @return the boolean
|
||||
*/
|
||||
@Override
|
||||
public boolean isEnable() {
|
||||
return null != config && config.isEventBusEnable();
|
||||
}
|
||||
|
||||
/**
|
||||
* Is enable boolean.
|
||||
*
|
||||
* @param eventClass the event class
|
||||
* @return the boolean
|
||||
*/
|
||||
@Override
|
||||
public boolean isEnable(Class<? extends Event> eventClass) {
|
||||
return isEnable() && CommonUtils.isNotEmpty(eventSubscriberMap.get(eventClass));
|
||||
}
|
||||
|
||||
private void handleEvent(final EventSubscriber subscriber, final Event event) {
|
||||
try {
|
||||
subscriber.onEvent(event);
|
||||
} catch (Throwable e) {
|
||||
LOGGER.warn("Handle {} error", event.getClass(), e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,128 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.event;
|
||||
|
||||
import com.alipay.lookout.api.Id;
|
||||
import com.alipay.lookout.api.Lookout;
|
||||
import com.alipay.lookout.api.composite.MixinMetric;
|
||||
import com.alipay.sofa.registry.client.api.Configurator;
|
||||
import com.alipay.sofa.registry.client.api.EventSubscriber;
|
||||
import com.alipay.sofa.registry.client.api.RegistryClientConfig;
|
||||
import com.alipay.sofa.registry.client.api.Subscriber;
|
||||
import com.alipay.sofa.registry.client.api.model.Event;
|
||||
import com.alipay.sofa.registry.client.util.StringUtils;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id: LookoutSubscriber.java, v 0.1 2018-07-13 20:31 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class LookoutSubscriber implements EventSubscriber {
|
||||
|
||||
/** */
|
||||
private static final String METRIC_DATA_ID_NAME = "data_id";
|
||||
|
||||
/** */
|
||||
private static final String METRIC_INSTANCE_ID_NAME = "instance_id";
|
||||
|
||||
/** */
|
||||
private static final String METRIC_COUNT_NAME = "count";
|
||||
|
||||
/** */
|
||||
private static final String METRIC_TIME_NAME = "time";
|
||||
|
||||
/** */
|
||||
private static final String METRIC_SUCCESS_NAME = "success";
|
||||
|
||||
/** */
|
||||
private static final String REGISTRY_PROCESS_SUBSCRIBER = "registry.process.subscriber";
|
||||
|
||||
/** */
|
||||
private static final String REGISTRY_PROCESS_CONFIGURATOR = "registry.process.configurator";
|
||||
|
||||
/** @see EventSubscriber#isSync() */
|
||||
@Override
|
||||
public boolean isSync() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @see EventSubscriber#onEvent(Event) */
|
||||
@Override
|
||||
public void onEvent(Event event) {
|
||||
if (null == event) {
|
||||
return;
|
||||
}
|
||||
Class eventClass = event.getClass();
|
||||
|
||||
if (eventClass == SubscriberProcessEvent.class) {
|
||||
SubscriberProcessEvent subscriberProcessEvent = (SubscriberProcessEvent) event;
|
||||
Subscriber subscriber = subscriberProcessEvent.getSubscriber();
|
||||
if (null == subscriber) {
|
||||
return;
|
||||
}
|
||||
|
||||
RegistryClientConfig config = subscriberProcessEvent.getConfig();
|
||||
if (null == config) {
|
||||
return;
|
||||
}
|
||||
|
||||
Id id =
|
||||
Lookout.registry()
|
||||
.createId(REGISTRY_PROCESS_SUBSCRIBER)
|
||||
.withTag(METRIC_DATA_ID_NAME, StringUtils.defaultString(subscriber.getDataId()))
|
||||
.withTag(METRIC_INSTANCE_ID_NAME, StringUtils.defaultString(config.getInstanceId()));
|
||||
MixinMetric mixin = Lookout.registry().mixinMetric(id);
|
||||
|
||||
mixin.counter(METRIC_COUNT_NAME).inc();
|
||||
mixin
|
||||
.timer(METRIC_TIME_NAME)
|
||||
.record(
|
||||
subscriberProcessEvent.getEnd() - subscriberProcessEvent.getStart(),
|
||||
TimeUnit.MILLISECONDS);
|
||||
|
||||
if (null == subscriberProcessEvent.getThrowable()) {
|
||||
mixin.counter(METRIC_SUCCESS_NAME).inc();
|
||||
}
|
||||
} else if (eventClass == ConfiguratorProcessEvent.class) {
|
||||
ConfiguratorProcessEvent configuratorProcessEvent = (ConfiguratorProcessEvent) event;
|
||||
Configurator configurator = configuratorProcessEvent.getConfigurator();
|
||||
if (null == configurator) {
|
||||
return;
|
||||
}
|
||||
|
||||
RegistryClientConfig config = configuratorProcessEvent.getConfig();
|
||||
|
||||
Id id =
|
||||
Lookout.registry()
|
||||
.createId(REGISTRY_PROCESS_CONFIGURATOR)
|
||||
.withTag(METRIC_DATA_ID_NAME, StringUtils.defaultString(configurator.getDataId()))
|
||||
.withTag(METRIC_INSTANCE_ID_NAME, StringUtils.defaultString(config.getInstanceId()));
|
||||
MixinMetric mixin = Lookout.registry().mixinMetric(id);
|
||||
|
||||
mixin.counter(METRIC_COUNT_NAME).inc();
|
||||
mixin
|
||||
.timer(METRIC_TIME_NAME)
|
||||
.record(
|
||||
configuratorProcessEvent.getEnd() - configuratorProcessEvent.getStart(),
|
||||
TimeUnit.MILLISECONDS);
|
||||
|
||||
if (null == configuratorProcessEvent.getThrowable()) {
|
||||
mixin.counter(METRIC_SUCCESS_NAME).inc();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,145 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.event;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.RegistryClientConfig;
|
||||
import com.alipay.sofa.registry.client.api.Subscriber;
|
||||
import com.alipay.sofa.registry.client.api.model.Event;
|
||||
|
||||
/**
|
||||
* The type Subscriber process event.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : SubscriberProcessEvent.java, v 0.1 2018-07-13 18:40 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class SubscriberProcessEvent implements Event {
|
||||
|
||||
private Subscriber subscriber;
|
||||
|
||||
private RegistryClientConfig config;
|
||||
|
||||
private long start;
|
||||
|
||||
private long end;
|
||||
|
||||
private Throwable throwable;
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>subscriber</tt>.
|
||||
*
|
||||
* @return property value of subscriber
|
||||
*/
|
||||
public Subscriber getSubscriber() {
|
||||
return subscriber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>subscriber</tt>.
|
||||
*
|
||||
* @param subscriber value to be assigned to property subscriber
|
||||
*/
|
||||
public void setSubscriber(Subscriber subscriber) {
|
||||
this.subscriber = subscriber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>config</tt>.
|
||||
*
|
||||
* @return property value of config
|
||||
*/
|
||||
public RegistryClientConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>config</tt>.
|
||||
*
|
||||
* @param config value to be assigned to property config
|
||||
*/
|
||||
public void setConfig(RegistryClientConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>start</tt>.
|
||||
*
|
||||
* @return property value of start
|
||||
*/
|
||||
public long getStart() {
|
||||
return start;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>start</tt>.
|
||||
*
|
||||
* @param start value to be assigned to property start
|
||||
*/
|
||||
public void setStart(long start) {
|
||||
this.start = start;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>end</tt>.
|
||||
*
|
||||
* @return property value of end
|
||||
*/
|
||||
public long getEnd() {
|
||||
return end;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>end</tt>.
|
||||
*
|
||||
* @param end value to be assigned to property end
|
||||
*/
|
||||
public void setEnd(long end) {
|
||||
this.end = end;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>throwable</tt>.
|
||||
*
|
||||
* @return property value of throwable
|
||||
*/
|
||||
public Throwable getThrowable() {
|
||||
return throwable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>throwable</tt>.
|
||||
*
|
||||
* @param throwable value to be assigned to property throwable
|
||||
*/
|
||||
public void setThrowable(Throwable throwable) {
|
||||
this.throwable = throwable;
|
||||
}
|
||||
|
||||
/** @see Object#toString() */
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SubscriberProcessEvent{"
|
||||
+ "subscriber="
|
||||
+ subscriber
|
||||
+ ", start="
|
||||
+ start
|
||||
+ ", end="
|
||||
+ end
|
||||
+ ", throwable="
|
||||
+ throwable
|
||||
+ '}';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.factory;
|
||||
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* The type Named thread factory.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : NamedThreadFactory.java, v 0.1 2018-03-02 21:23 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class NamedThreadFactory implements ThreadFactory {
|
||||
|
||||
private static final AtomicInteger POOL_NUMBER = new AtomicInteger(1);
|
||||
private final AtomicInteger threadNumber = new AtomicInteger(1);
|
||||
private final ThreadGroup group;
|
||||
private final String namePrefix;
|
||||
private final boolean isDaemon;
|
||||
|
||||
/**
|
||||
* Instantiates a new Named thread factory.
|
||||
*
|
||||
* @param name the name
|
||||
*/
|
||||
public NamedThreadFactory(String name) {
|
||||
this(name, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Instantiates a new Named thread factory.
|
||||
*
|
||||
* @param prefix the prefix
|
||||
* @param daemon the daemon
|
||||
*/
|
||||
public NamedThreadFactory(String prefix, boolean daemon) {
|
||||
SecurityManager s = System.getSecurityManager();
|
||||
group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
|
||||
namePrefix = prefix + "-" + POOL_NUMBER.getAndIncrement() + "-thread-";
|
||||
isDaemon = daemon;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a thread.
|
||||
*
|
||||
* @see ThreadFactory#newThread(Runnable)
|
||||
*/
|
||||
@Override
|
||||
public Thread newThread(Runnable r) {
|
||||
Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0);
|
||||
t.setDaemon(isDaemon);
|
||||
if (t.getPriority() != Thread.NORM_PRIORITY) {
|
||||
t.setPriority(Thread.NORM_PRIORITY);
|
||||
}
|
||||
return t;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.model;
|
||||
|
||||
import com.alipay.sofa.registry.core.model.DataBox;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* The type Configurator data.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : ConfiguratorData.java, v 0.1 2018-04-18 15:17 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class ConfiguratorData implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 3667107975967019132L;
|
||||
|
||||
private DataBox dataBox;
|
||||
|
||||
private Long version;
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>dataBox</tt>.
|
||||
*
|
||||
* @return property value of dataBox
|
||||
*/
|
||||
public DataBox getDataBox() {
|
||||
return dataBox;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>dataBox</tt>.
|
||||
*
|
||||
* @param dataBox value to be assigned to property dataBox
|
||||
*/
|
||||
public void setDataBox(DataBox dataBox) {
|
||||
this.dataBox = dataBox;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>version</tt>.
|
||||
*
|
||||
* @return property value of version
|
||||
*/
|
||||
public Long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>version</tt>.
|
||||
*
|
||||
* @param version value to be assigned to property version
|
||||
*/
|
||||
public void setVersion(Long version) {
|
||||
this.version = version;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,91 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.model;
|
||||
|
||||
import com.alipay.sofa.registry.core.model.DataBox;
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id: SegmentData.java, v 0.1 2018-03-12 22:13 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class SegmentData implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 3066687802211879826L;
|
||||
|
||||
private String segment;
|
||||
|
||||
private Map<String, List<DataBox>> data;
|
||||
|
||||
private Long version;
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>segment</tt>.
|
||||
*
|
||||
* @return property value of segment
|
||||
*/
|
||||
public String getSegment() {
|
||||
return segment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>segment</tt>.
|
||||
*
|
||||
* @param segment value to be assigned to property segment
|
||||
*/
|
||||
public void setSegment(String segment) {
|
||||
this.segment = segment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>data</tt>.
|
||||
*
|
||||
* @return property value of data
|
||||
*/
|
||||
public Map<String, List<DataBox>> getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>data</tt>.
|
||||
*
|
||||
* @param data value to be assigned to property data
|
||||
*/
|
||||
public void setData(Map<String, List<DataBox>> data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>version</tt>.
|
||||
*
|
||||
* @return property value of version
|
||||
*/
|
||||
public Long getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>version</tt>.
|
||||
*
|
||||
* @param version value to be assigned to property version
|
||||
*/
|
||||
public void setVersion(Long version) {
|
||||
this.version = version;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,438 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.provider;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.Register;
|
||||
import com.alipay.sofa.registry.client.api.RegistryClientConfig;
|
||||
import com.alipay.sofa.registry.client.api.registration.BaseRegistration;
|
||||
import com.alipay.sofa.registry.client.auth.AuthManager;
|
||||
import com.alipay.sofa.registry.client.constants.ValueConstants;
|
||||
import com.alipay.sofa.registry.client.constants.VersionConstants;
|
||||
import com.alipay.sofa.registry.client.util.StringUtils;
|
||||
import com.alipay.sofa.registry.core.model.BaseRegister;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReadWriteLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
|
||||
/**
|
||||
* The type Internal register.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : AbstractInternalRegister.java, v 0.1 2017-11-23 20:52 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public abstract class AbstractInternalRegister implements Register {
|
||||
|
||||
/** */
|
||||
private final AtomicLong initialVersion = new AtomicLong(VersionConstants.UNINITIALIZED_VERSION);
|
||||
/** */
|
||||
private AuthManager authManager;
|
||||
/** */
|
||||
private volatile boolean registered = false;
|
||||
/** */
|
||||
private volatile boolean enabled = true;
|
||||
/** */
|
||||
private volatile boolean refused = false;
|
||||
/** */
|
||||
private AtomicLong pubVersion = new AtomicLong(VersionConstants.UNINITIALIZED_VERSION);
|
||||
/** */
|
||||
private AtomicLong ackVersion = new AtomicLong(VersionConstants.UNINITIALIZED_VERSION);
|
||||
/** */
|
||||
private volatile long timestamp = System.currentTimeMillis();
|
||||
/** */
|
||||
private volatile int registerCount = 0;
|
||||
/** */
|
||||
private volatile String requestId = UUID.randomUUID().toString();
|
||||
|
||||
private ReadWriteLock lock = new ReentrantReadWriteLock();
|
||||
|
||||
/** The Read lock. */
|
||||
protected Lock readLock = lock.readLock();
|
||||
|
||||
/** The Write lock. */
|
||||
protected Lock writeLock = lock.writeLock();
|
||||
|
||||
/**
|
||||
* Assembly object.
|
||||
*
|
||||
* @return the object
|
||||
*/
|
||||
public abstract Object assembly();
|
||||
|
||||
/**
|
||||
* Is registered boolean.
|
||||
*
|
||||
* @return boolean boolean
|
||||
*/
|
||||
@Override
|
||||
public boolean isRegistered() {
|
||||
readLock.lock();
|
||||
try {
|
||||
return registered;
|
||||
} finally {
|
||||
readLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/** Wait to sync. */
|
||||
void waitToSync() {
|
||||
writeLock.lock();
|
||||
try {
|
||||
this.registered = false;
|
||||
this.requestId = UUID.randomUUID().toString();
|
||||
} finally {
|
||||
writeLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sync ok.
|
||||
*
|
||||
* @param requestId the request id
|
||||
* @param version the version
|
||||
* @param refused the refused
|
||||
* @return the boolean
|
||||
*/
|
||||
public boolean syncOK(String requestId, long version, boolean refused) {
|
||||
writeLock.lock();
|
||||
try {
|
||||
if (this.requestId.equals(requestId)) {
|
||||
this.registered = true;
|
||||
this.refused = refused;
|
||||
this.setAckVersion(version);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} finally {
|
||||
writeLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/** @see Register#isEnabled() */
|
||||
@Override
|
||||
public boolean isEnabled() {
|
||||
readLock.lock();
|
||||
try {
|
||||
return enabled;
|
||||
} finally {
|
||||
readLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets enabled.
|
||||
*
|
||||
* @param requestId the request id
|
||||
*/
|
||||
public void refused(String requestId) {
|
||||
writeLock.lock();
|
||||
try {
|
||||
if (this.requestId.equals(requestId)) {
|
||||
this.enabled = false;
|
||||
this.refused = true;
|
||||
}
|
||||
} finally {
|
||||
writeLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Is done boolean.
|
||||
*
|
||||
* @return boolean boolean
|
||||
*/
|
||||
public boolean isDone() {
|
||||
readLock.lock();
|
||||
try {
|
||||
return (this.isRegistered() && this.pubVersion.get() == this.ackVersion.get())
|
||||
|| this.isRefused();
|
||||
} finally {
|
||||
readLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assembly sync task sync task.
|
||||
*
|
||||
* @return the sync task
|
||||
*/
|
||||
public SyncTask assemblySyncTask() {
|
||||
readLock.lock();
|
||||
try {
|
||||
SyncTask syncTask = new SyncTask();
|
||||
syncTask.setRequestId(requestId);
|
||||
syncTask.setRequest(assembly());
|
||||
syncTask.setDone(isDone());
|
||||
return syncTask;
|
||||
} finally {
|
||||
readLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets pub version.
|
||||
*
|
||||
* @return AtomicLong pub version
|
||||
*/
|
||||
public AtomicLong getPubVersion() {
|
||||
return this.pubVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets ack version.
|
||||
*
|
||||
* @param version the ack version
|
||||
*/
|
||||
public void setAckVersion(Long version) {
|
||||
if (null == version) {
|
||||
return;
|
||||
}
|
||||
|
||||
long current = ackVersion.get();
|
||||
if (version <= current) {
|
||||
return;
|
||||
}
|
||||
|
||||
boolean result = this.ackVersion.compareAndSet(current, version);
|
||||
if (result) {
|
||||
return;
|
||||
}
|
||||
|
||||
setAckVersion(version);
|
||||
}
|
||||
|
||||
/** @see Register#reset() */
|
||||
@Override
|
||||
public void reset() {
|
||||
writeLock.lock();
|
||||
try {
|
||||
this.registered = false;
|
||||
this.registerCount = 0;
|
||||
this.timestamp = System.currentTimeMillis();
|
||||
this.ackVersion = new AtomicLong(initialVersion.longValue());
|
||||
this.requestId = UUID.randomUUID().toString();
|
||||
} finally {
|
||||
writeLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/** @see Register#unregister() */
|
||||
@Override
|
||||
public void unregister() {
|
||||
writeLock.lock();
|
||||
try {
|
||||
this.enabled = false;
|
||||
this.pubVersion.incrementAndGet();
|
||||
this.requestId = UUID.randomUUID().toString();
|
||||
this.registerCount = 0;
|
||||
} finally {
|
||||
writeLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>refused</tt>.
|
||||
*
|
||||
* @return property value of refused
|
||||
*/
|
||||
boolean isRefused() {
|
||||
readLock.lock();
|
||||
try {
|
||||
return refused;
|
||||
} finally {
|
||||
readLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>timestamp</tt>.
|
||||
*
|
||||
* @return property value of timestamp
|
||||
*/
|
||||
@Override
|
||||
public long getTimestamp() {
|
||||
readLock.lock();
|
||||
try {
|
||||
return timestamp;
|
||||
} finally {
|
||||
readLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>timestamp</tt>.
|
||||
*
|
||||
* @param timestamp value to be assigned to property timestamp
|
||||
*/
|
||||
void setTimestamp(long timestamp) {
|
||||
writeLock.lock();
|
||||
try {
|
||||
this.timestamp = timestamp;
|
||||
} finally {
|
||||
writeLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets auth signature.
|
||||
*
|
||||
* @param register the register
|
||||
*/
|
||||
void setAuthSignature(BaseRegister register) {
|
||||
// auth signature
|
||||
if (null != authManager) {
|
||||
Map<String, String> authAttributes = authManager.getAuthContent(register);
|
||||
|
||||
// merge auth attributes with exists register attributes
|
||||
Map<String, String> registerAttributes = register.getAttributes();
|
||||
if (null == registerAttributes) {
|
||||
registerAttributes = new HashMap<String, String>();
|
||||
}
|
||||
registerAttributes.putAll(authAttributes);
|
||||
register.setAttributes(registerAttributes);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>authManager</tt>.
|
||||
*
|
||||
* @param authManager value to be assigned to property authManager
|
||||
*/
|
||||
public void setAuthManager(AuthManager authManager) {
|
||||
this.authManager = authManager;
|
||||
}
|
||||
|
||||
protected void setAttributes(
|
||||
BaseRegister to, BaseRegistration from, RegistryClientConfig config) {
|
||||
if (StringUtils.isNotEmpty(from.getInstanceId())) {
|
||||
to.setInstanceId(from.getInstanceId());
|
||||
} else {
|
||||
to.setInstanceId(config.getInstanceId());
|
||||
}
|
||||
if (StringUtils.isNotEmpty(config.getZone())) {
|
||||
to.setZone(config.getZone());
|
||||
} else {
|
||||
to.setZone(ValueConstants.DEFAULT_ZONE);
|
||||
}
|
||||
if (StringUtils.isNotEmpty(from.getAppName())) {
|
||||
to.setAppName(from.getAppName());
|
||||
} else {
|
||||
to.setAppName(config.getAppName());
|
||||
}
|
||||
if (StringUtils.isNotEmpty(from.getIp())) {
|
||||
to.setIp(from.getIp());
|
||||
} else {
|
||||
to.setIp(config.getIp());
|
||||
}
|
||||
to.setDataId(from.getDataId());
|
||||
to.setGroup(from.getGroup());
|
||||
to.setVersion(this.getPubVersion().get());
|
||||
to.setTimestamp(this.getTimestamp());
|
||||
}
|
||||
|
||||
/** @see Object#toString() */
|
||||
@Override
|
||||
public String toString() {
|
||||
return "AbstractInternalRegister{"
|
||||
+ "initialVersion="
|
||||
+ initialVersion
|
||||
+ ", registered="
|
||||
+ registered
|
||||
+ ", enabled="
|
||||
+ enabled
|
||||
+ ", refused="
|
||||
+ refused
|
||||
+ ", pubVersion="
|
||||
+ pubVersion
|
||||
+ ", ackVersion="
|
||||
+ ackVersion
|
||||
+ ", timestamp="
|
||||
+ timestamp
|
||||
+ ", registerCount="
|
||||
+ registerCount
|
||||
+ ", requestId='"
|
||||
+ requestId
|
||||
+ '}';
|
||||
}
|
||||
|
||||
/** The type Sync task. */
|
||||
public static class SyncTask {
|
||||
|
||||
private String requestId;
|
||||
|
||||
private Object request;
|
||||
|
||||
private boolean done;
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>requestId</tt>.
|
||||
*
|
||||
* @return property value of requestId
|
||||
*/
|
||||
public String getRequestId() {
|
||||
return requestId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>requestId</tt>.
|
||||
*
|
||||
* @param requestId value to be assigned to property requestId
|
||||
*/
|
||||
void setRequestId(String requestId) {
|
||||
this.requestId = requestId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>request</tt>.
|
||||
*
|
||||
* @return property value of request
|
||||
*/
|
||||
public Object getRequest() {
|
||||
return request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>request</tt>.
|
||||
*
|
||||
* @param request value to be assigned to property request
|
||||
*/
|
||||
void setRequest(Object request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>done</tt>.
|
||||
*
|
||||
* @return property value of done
|
||||
*/
|
||||
public boolean isDone() {
|
||||
return done;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>done</tt>.
|
||||
*
|
||||
* @param done value to be assigned to property done
|
||||
*/
|
||||
public void setDone(boolean done) {
|
||||
this.done = done;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.provider;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.model.ConfigData;
|
||||
|
||||
/**
|
||||
* The type Default config data.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : DefaultConfigData.java, v 0.1 2018-04-18 15:29 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class DefaultConfigData implements ConfigData {
|
||||
|
||||
private String data;
|
||||
|
||||
/**
|
||||
* Instantiates a new Default config data.
|
||||
*
|
||||
* @param data the data
|
||||
*/
|
||||
public DefaultConfigData(String data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
/** @see ConfigData#getData() */
|
||||
@Override
|
||||
public String getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DefaultConfigData{" + "data='" + data + '\'' + '}';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,207 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.provider;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.ConfigDataObserver;
|
||||
import com.alipay.sofa.registry.client.api.Configurator;
|
||||
import com.alipay.sofa.registry.client.api.RegistryClientConfig;
|
||||
import com.alipay.sofa.registry.client.api.model.ConfigData;
|
||||
import com.alipay.sofa.registry.client.api.registration.ConfiguratorRegistration;
|
||||
import com.alipay.sofa.registry.client.model.ConfiguratorData;
|
||||
import com.alipay.sofa.registry.client.task.TaskEvent;
|
||||
import com.alipay.sofa.registry.client.task.Worker;
|
||||
import com.alipay.sofa.registry.core.constants.EventTypeConstants;
|
||||
import com.alipay.sofa.registry.core.model.ConfiguratorRegister;
|
||||
import com.alipay.sofa.registry.core.model.DataBox;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* The type Default configurator.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : DefaultConfigurator.java, v 0.1 2018-04-18 14:41 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class DefaultConfigurator extends AbstractInternalRegister implements Configurator {
|
||||
private final String REGIST_ID;
|
||||
|
||||
private ConfiguratorRegistration registration;
|
||||
|
||||
private ConfigDataObserver configDataObserver;
|
||||
|
||||
private RegistryClientConfig config;
|
||||
|
||||
private ConfiguratorData configuratorData;
|
||||
|
||||
private Worker worker;
|
||||
|
||||
private AtomicBoolean init = new AtomicBoolean(false);
|
||||
|
||||
/**
|
||||
* Instantiates a new Default configurator.
|
||||
*
|
||||
* @param config the config
|
||||
* @param worker the worker
|
||||
*/
|
||||
public DefaultConfigurator(
|
||||
ConfiguratorRegistration registration, RegistryClientConfig config, Worker worker) {
|
||||
if (null != registration) {
|
||||
this.configDataObserver = registration.getConfigDataObserver();
|
||||
}
|
||||
this.registration = registration;
|
||||
this.config = config;
|
||||
this.worker = worker;
|
||||
this.REGIST_ID = UUID.randomUUID().toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets data observer.
|
||||
*
|
||||
* @return the data observer
|
||||
*/
|
||||
@Override
|
||||
public ConfigDataObserver getDataObserver() {
|
||||
return configDataObserver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>configDataObserver</tt>.
|
||||
*
|
||||
* @param configDataObserver value to be assigned to property configDataObserver
|
||||
*/
|
||||
@Override
|
||||
public void setDataObserver(ConfigDataObserver configDataObserver) {
|
||||
this.configDataObserver = configDataObserver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Peek data config data.
|
||||
*
|
||||
* @return the config data
|
||||
*/
|
||||
@Override
|
||||
public ConfigData peekData() {
|
||||
if (!init.get()) {
|
||||
throw new IllegalStateException("Config data is not ready yet.");
|
||||
}
|
||||
if (null != configuratorData) {
|
||||
DataBox dataBox = configuratorData.getDataBox();
|
||||
if (null != dataBox) {
|
||||
return new DefaultConfigData(dataBox.getData());
|
||||
}
|
||||
}
|
||||
return new DefaultConfigData(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assembly object.
|
||||
*
|
||||
* @return the object
|
||||
*/
|
||||
@Override
|
||||
public Object assembly() {
|
||||
readLock.lock();
|
||||
ConfiguratorRegister register = new ConfiguratorRegister();
|
||||
try {
|
||||
register.setRegistId(REGIST_ID);
|
||||
setAttributes(register, registration, config);
|
||||
// auth signature
|
||||
setAuthSignature(register);
|
||||
|
||||
if (isEnabled()) {
|
||||
register.setEventType(EventTypeConstants.REGISTER);
|
||||
} else {
|
||||
register.setEventType(EventTypeConstants.UNREGISTER);
|
||||
}
|
||||
} finally {
|
||||
readLock.unlock();
|
||||
}
|
||||
return register;
|
||||
}
|
||||
|
||||
/**
|
||||
* Put configurator data.
|
||||
*
|
||||
* @param receivedConfigData the received config data
|
||||
*/
|
||||
public void putConfiguratorData(ConfiguratorData receivedConfigData) {
|
||||
writeLock.lock();
|
||||
try {
|
||||
if (null == receivedConfigData) {
|
||||
return;
|
||||
}
|
||||
|
||||
// default version set to zero
|
||||
if (null == receivedConfigData.getVersion()) {
|
||||
receivedConfigData.setVersion(0L);
|
||||
}
|
||||
|
||||
if (null == configuratorData
|
||||
|| receivedConfigData.getVersion() > configuratorData.getVersion()) {
|
||||
configuratorData = receivedConfigData;
|
||||
init.compareAndSet(false, true);
|
||||
}
|
||||
|
||||
} finally {
|
||||
writeLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets data id.
|
||||
*
|
||||
* @return the data id
|
||||
*/
|
||||
@Override
|
||||
public String getDataId() {
|
||||
return registration.getDataId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets group.
|
||||
*
|
||||
* @return the group
|
||||
*/
|
||||
@Override
|
||||
public String getGroup() {
|
||||
return registration.getGroup();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets regist id.
|
||||
*
|
||||
* @return the regist id
|
||||
*/
|
||||
@Override
|
||||
public String getRegistId() {
|
||||
return REGIST_ID;
|
||||
}
|
||||
|
||||
/** Unregister. */
|
||||
@Override
|
||||
public void unregister() {
|
||||
if (isEnabled()) {
|
||||
super.unregister();
|
||||
this.worker.schedule(new TaskEvent(this));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DefaultConfigurator{" + "registration=" + registration + '}';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,186 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.provider;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.ConfigDataObserver;
|
||||
import com.alipay.sofa.registry.client.api.Configurator;
|
||||
import com.alipay.sofa.registry.client.api.EventBus;
|
||||
import com.alipay.sofa.registry.client.api.RegistryClientConfig;
|
||||
import com.alipay.sofa.registry.client.api.Subscriber;
|
||||
import com.alipay.sofa.registry.client.api.SubscriberDataObserver;
|
||||
import com.alipay.sofa.registry.client.event.ConfiguratorProcessEvent;
|
||||
import com.alipay.sofa.registry.client.event.SubscriberProcessEvent;
|
||||
import com.alipay.sofa.registry.client.factory.NamedThreadFactory;
|
||||
import com.alipay.sofa.registry.client.log.LoggerFactory;
|
||||
import com.alipay.sofa.registry.client.task.ObserverHandler;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.LinkedBlockingDeque;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
/**
|
||||
* The type Default observer handler.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : DefaultObserverHandler.java, v 0.1 2018-03-15 12:02 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class DefaultObserverHandler implements ObserverHandler {
|
||||
|
||||
/** LOGGER */
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultObserverHandler.class);
|
||||
|
||||
private static final int KEEP_ALIVE_TIME = 60;
|
||||
|
||||
private ExecutorService executor;
|
||||
|
||||
private EventBus eventBus;
|
||||
|
||||
private RegistryClientConfig config;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param config the config
|
||||
*/
|
||||
public DefaultObserverHandler(RegistryClientConfig config, EventBus eventBus) {
|
||||
this.config = config;
|
||||
this.executor =
|
||||
new ThreadPoolExecutor(
|
||||
config.getObserverThreadCoreSize(),
|
||||
config.getObserverThreadMaxSize(),
|
||||
KEEP_ALIVE_TIME,
|
||||
TimeUnit.SECONDS,
|
||||
new LinkedBlockingDeque<Runnable>(config.getObserverThreadQueueLength()),
|
||||
new NamedThreadFactory("ObserverNotifyThread"));
|
||||
this.eventBus = eventBus;
|
||||
}
|
||||
|
||||
/** @see ObserverHandler#notify(Subscriber) */
|
||||
@Override
|
||||
public void notify(Subscriber subscriber) {
|
||||
executor.submit(new SubscriberNotifyTask(subscriber));
|
||||
}
|
||||
|
||||
/** @see ObserverHandler#notify(Configurator) */
|
||||
@Override
|
||||
public void notify(Configurator configurator) {
|
||||
executor.submit(new ConfiguratorNotifyTask(configurator));
|
||||
}
|
||||
|
||||
/** The type Observer notify task. */
|
||||
public class SubscriberNotifyTask implements Runnable {
|
||||
|
||||
private Subscriber subscriber;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param subscriber the subscriber
|
||||
*/
|
||||
public SubscriberNotifyTask(Subscriber subscriber) {
|
||||
this.subscriber = subscriber;
|
||||
}
|
||||
|
||||
/** @see Runnable#run() */
|
||||
@Override
|
||||
public void run() {
|
||||
// ignore empty task
|
||||
if (null == subscriber) {
|
||||
return;
|
||||
}
|
||||
|
||||
SubscriberProcessEvent event = new SubscriberProcessEvent();
|
||||
long start = System.currentTimeMillis();
|
||||
event.setStart(start);
|
||||
event.setConfig(config);
|
||||
event.setSubscriber(subscriber);
|
||||
try {
|
||||
SubscriberDataObserver dataObserver = subscriber.getDataObserver();
|
||||
if (null != dataObserver) {
|
||||
dataObserver.handleData(subscriber.getDataId(), subscriber.peekData());
|
||||
}
|
||||
LOGGER.info(
|
||||
"[notify] notify subscriber success, dataId: {}, registId:{}, cost: {}ms",
|
||||
subscriber.getDataId(),
|
||||
subscriber.getRegistId(),
|
||||
(System.currentTimeMillis() - start));
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(
|
||||
"[notify] SubscriberNotifyTask execute error, dataId: {}", subscriber.getDataId(), e);
|
||||
event.setThrowable(e);
|
||||
} finally {
|
||||
event.setEnd(System.currentTimeMillis());
|
||||
if (null != eventBus) {
|
||||
eventBus.post(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** The type Configurator notify task. */
|
||||
public class ConfiguratorNotifyTask implements Runnable {
|
||||
|
||||
private Configurator configurator;
|
||||
|
||||
/**
|
||||
* Instantiates a new Configurator notify task.
|
||||
*
|
||||
* @param configurator the configurator
|
||||
*/
|
||||
public ConfiguratorNotifyTask(Configurator configurator) {
|
||||
this.configurator = configurator;
|
||||
}
|
||||
|
||||
/** @see Runnable#run() */
|
||||
@Override
|
||||
public void run() {
|
||||
if (null == configurator) {
|
||||
return;
|
||||
}
|
||||
|
||||
ConfiguratorProcessEvent event = new ConfiguratorProcessEvent();
|
||||
long start = System.currentTimeMillis();
|
||||
event.setStart(start);
|
||||
event.setConfig(config);
|
||||
event.setConfigurator(configurator);
|
||||
try {
|
||||
ConfigDataObserver dataObserver = configurator.getDataObserver();
|
||||
if (null != dataObserver) {
|
||||
dataObserver.handleData(configurator.getDataId(), configurator.peekData());
|
||||
}
|
||||
|
||||
LOGGER.info(
|
||||
"[notify] notify configurator success, dataId: {}, registId:{}, cost: {}ms",
|
||||
configurator.getDataId(),
|
||||
configurator.getRegistId(),
|
||||
(System.currentTimeMillis() - start));
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(
|
||||
"[notify] ConfiguratorNotifyTask execute error, dataId: {}",
|
||||
configurator.getDataId(),
|
||||
e);
|
||||
event.setThrowable(e);
|
||||
} finally {
|
||||
event.setEnd(System.currentTimeMillis());
|
||||
if (null != eventBus) {
|
||||
eventBus.post(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,152 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.provider;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.Publisher;
|
||||
import com.alipay.sofa.registry.client.api.RegistryClientConfig;
|
||||
import com.alipay.sofa.registry.client.api.registration.PublisherRegistration;
|
||||
import com.alipay.sofa.registry.client.task.TaskEvent;
|
||||
import com.alipay.sofa.registry.client.task.Worker;
|
||||
import com.alipay.sofa.registry.core.constants.EventTypeConstants;
|
||||
import com.alipay.sofa.registry.core.model.DataBox;
|
||||
import com.alipay.sofa.registry.core.model.PublisherRegister;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* The type Default publisher.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : DefaultPublisher.java, v 0.1 2017-11-23 20:45 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class DefaultPublisher extends AbstractInternalRegister implements Publisher {
|
||||
|
||||
private final String REGIST_ID;
|
||||
private PublisherRegistration registration;
|
||||
private Worker worker;
|
||||
private Collection<String> dataList;
|
||||
private RegistryClientConfig config;
|
||||
|
||||
/**
|
||||
* Instantiates a new Default publisher.
|
||||
*
|
||||
* @param registration the publisher registration
|
||||
* @param worker the worker
|
||||
*/
|
||||
DefaultPublisher(PublisherRegistration registration, Worker worker, RegistryClientConfig config) {
|
||||
this.registration = registration;
|
||||
this.worker = worker;
|
||||
this.config = config;
|
||||
this.REGIST_ID = UUID.randomUUID().toString();
|
||||
}
|
||||
|
||||
/** @see Publisher#republish(String...) */
|
||||
@Override
|
||||
public void republish(String... data) {
|
||||
if (isRefused()) {
|
||||
throw new IllegalStateException(
|
||||
"Publisher is refused by server. Try to check your configuration.");
|
||||
}
|
||||
|
||||
if (!isEnabled()) {
|
||||
throw new IllegalStateException("Unregistered publisher can not be reused.");
|
||||
}
|
||||
|
||||
writeLock.lock();
|
||||
try {
|
||||
if (null != data) {
|
||||
this.dataList = Arrays.asList(data);
|
||||
}
|
||||
|
||||
this.getPubVersion().incrementAndGet();
|
||||
this.setTimestamp(System.currentTimeMillis());
|
||||
this.waitToSync();
|
||||
} finally {
|
||||
writeLock.unlock();
|
||||
}
|
||||
this.worker.schedule(new TaskEvent(this));
|
||||
}
|
||||
|
||||
/** Unregister. */
|
||||
@Override
|
||||
public void unregister() {
|
||||
if (isEnabled()) {
|
||||
super.unregister();
|
||||
this.worker.schedule(new TaskEvent(this));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assembly publisher register.
|
||||
*
|
||||
* @return the publisher register
|
||||
*/
|
||||
@Override
|
||||
public PublisherRegister assembly() {
|
||||
readLock.lock();
|
||||
PublisherRegister register;
|
||||
try {
|
||||
register = new PublisherRegister();
|
||||
register.setRegistId(REGIST_ID);
|
||||
setAttributes(register, registration, config);
|
||||
// auth signature
|
||||
setAuthSignature(register);
|
||||
|
||||
if (isEnabled()) {
|
||||
register.setEventType(EventTypeConstants.REGISTER);
|
||||
if (null != dataList) {
|
||||
List<DataBox> dataBoxes = new ArrayList<DataBox>();
|
||||
for (String data : dataList) {
|
||||
dataBoxes.add(new DataBox(data));
|
||||
}
|
||||
register.setDataList(dataBoxes);
|
||||
}
|
||||
} else {
|
||||
register.setEventType(EventTypeConstants.UNREGISTER);
|
||||
}
|
||||
} finally {
|
||||
readLock.unlock();
|
||||
}
|
||||
return register;
|
||||
}
|
||||
|
||||
/** @see Publisher#getDataId() */
|
||||
@Override
|
||||
public String getDataId() {
|
||||
return registration.getDataId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getGroup() {
|
||||
return registration.getGroup();
|
||||
}
|
||||
|
||||
/** @see Publisher#getRegistId() */
|
||||
@Override
|
||||
public String getRegistId() {
|
||||
return REGIST_ID;
|
||||
}
|
||||
|
||||
/** @see Object#toString() */
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DefaultPublisher{" + "registration=" + registration + '}' + super.toString();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,618 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.provider;
|
||||
|
||||
import static com.alipay.sofa.registry.client.constants.ValueConstants.DEFAULT_GROUP;
|
||||
|
||||
import com.alipay.remoting.ConnectionEventProcessor;
|
||||
import com.alipay.remoting.ConnectionEventType;
|
||||
import com.alipay.remoting.rpc.protocol.UserProcessor;
|
||||
import com.alipay.sofa.registry.client.api.Configurator;
|
||||
import com.alipay.sofa.registry.client.api.EventBus;
|
||||
import com.alipay.sofa.registry.client.api.Publisher;
|
||||
import com.alipay.sofa.registry.client.api.Register;
|
||||
import com.alipay.sofa.registry.client.api.RegistryClient;
|
||||
import com.alipay.sofa.registry.client.api.RegistryClientConfig;
|
||||
import com.alipay.sofa.registry.client.api.Subscriber;
|
||||
import com.alipay.sofa.registry.client.api.exception.DuplicateException;
|
||||
import com.alipay.sofa.registry.client.api.exception.RegistryClientException;
|
||||
import com.alipay.sofa.registry.client.api.model.RegistryType;
|
||||
import com.alipay.sofa.registry.client.api.registration.ConfiguratorRegistration;
|
||||
import com.alipay.sofa.registry.client.api.registration.PublisherRegistration;
|
||||
import com.alipay.sofa.registry.client.api.registration.SubscriberRegistration;
|
||||
import com.alipay.sofa.registry.client.auth.AuthManager;
|
||||
import com.alipay.sofa.registry.client.auth.NoopAuthManager;
|
||||
import com.alipay.sofa.registry.client.event.ConfiguratorProcessEvent;
|
||||
import com.alipay.sofa.registry.client.event.DefaultEventBus;
|
||||
import com.alipay.sofa.registry.client.event.LookoutSubscriber;
|
||||
import com.alipay.sofa.registry.client.event.SubscriberProcessEvent;
|
||||
import com.alipay.sofa.registry.client.log.LoggerFactory;
|
||||
import com.alipay.sofa.registry.client.remoting.ClientConnection;
|
||||
import com.alipay.sofa.registry.client.remoting.ClientConnectionCloseEventProcessor;
|
||||
import com.alipay.sofa.registry.client.remoting.ClientConnectionOpenEventProcessor;
|
||||
import com.alipay.sofa.registry.client.remoting.ReceivedConfigDataProcessor;
|
||||
import com.alipay.sofa.registry.client.remoting.ReceivedDataProcessor;
|
||||
import com.alipay.sofa.registry.client.remoting.ServerManager;
|
||||
import com.alipay.sofa.registry.client.task.ObserverHandler;
|
||||
import com.alipay.sofa.registry.client.task.SyncConfigThread;
|
||||
import com.alipay.sofa.registry.client.task.TaskEvent;
|
||||
import com.alipay.sofa.registry.client.task.WorkerThread;
|
||||
import com.alipay.sofa.registry.client.util.StringUtils;
|
||||
import com.alipay.sofa.registry.core.model.ReceivedConfigData;
|
||||
import com.alipay.sofa.registry.core.model.ReceivedData;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
/**
|
||||
* The type Default registry client.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : DefaultRegistryClient.java, v 0.1 2017-11-23 20:07 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class DefaultRegistryClient implements RegistryClient {
|
||||
|
||||
/** LOGGER */
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultRegistryClient.class);
|
||||
|
||||
private RegistryClientConfig registryClientConfig;
|
||||
|
||||
private RegisterCache registerCache;
|
||||
|
||||
private ServerManager serverManager;
|
||||
|
||||
private WorkerThread workerThread;
|
||||
|
||||
private ClientConnection client;
|
||||
|
||||
private Map<Class<?>, UserProcessor> userProcessorMap;
|
||||
|
||||
private Map<ConnectionEventType, ConnectionEventProcessor> connectionEventProcessorMap;
|
||||
|
||||
private ConcurrentMap<PublisherRegistration, Publisher> registrationPublisherMap;
|
||||
|
||||
private ConcurrentMap<SubscriberRegistration, Subscriber> registrationSubscriberMap;
|
||||
|
||||
private ConcurrentMap<ConfiguratorRegistration, Configurator> registrationConfiguratorMap;
|
||||
|
||||
private ObserverHandler observerHandler;
|
||||
|
||||
private AuthManager authManager;
|
||||
|
||||
private EventBus eventBus;
|
||||
|
||||
private LookoutSubscriber lookoutSubscriber;
|
||||
|
||||
private AtomicBoolean init = new AtomicBoolean(false);
|
||||
|
||||
/**
|
||||
* Instantiates a new Default registry client.
|
||||
*
|
||||
* @param registryClientConfig the registry client config
|
||||
*/
|
||||
public DefaultRegistryClient(RegistryClientConfig registryClientConfig) {
|
||||
// clone config to avoid configuration changes at runtime
|
||||
this.registryClientConfig = cloneConfig(registryClientConfig);
|
||||
this.registerCache = new RegisterCache();
|
||||
this.registrationPublisherMap = new ConcurrentHashMap<PublisherRegistration, Publisher>();
|
||||
this.registrationSubscriberMap = new ConcurrentHashMap<SubscriberRegistration, Subscriber>();
|
||||
this.registrationConfiguratorMap =
|
||||
new ConcurrentHashMap<ConfiguratorRegistration, Configurator>();
|
||||
}
|
||||
|
||||
private DefaultRegistryClientConfig cloneConfig(RegistryClientConfig registryClientConfig) {
|
||||
DefaultRegistryClientConfig cloneConfig = null;
|
||||
DefaultRegistryClientConfigBuilder builder = DefaultRegistryClientConfigBuilder.start();
|
||||
if (null != registryClientConfig) {
|
||||
cloneConfig =
|
||||
builder
|
||||
.setEnv(registryClientConfig.getEnv())
|
||||
.setAppName(registryClientConfig.getAppName())
|
||||
.setInstanceId(registryClientConfig.getInstanceId())
|
||||
.setDataCenter(registryClientConfig.getDataCenter())
|
||||
.setZone(registryClientConfig.getZone())
|
||||
.setRegistryEndpoint(registryClientConfig.getRegistryEndpoint())
|
||||
.setRegistryEndpointPort(registryClientConfig.getRegistryEndpointPort())
|
||||
.setConnectTimeout(registryClientConfig.getConnectTimeout())
|
||||
.setSocketTimeout(registryClientConfig.getSocketTimeout())
|
||||
.setInvokeTimeout(registryClientConfig.getInvokeTimeout())
|
||||
.setRecheckInterval(registryClientConfig.getRecheckInterval())
|
||||
.setObserverThreadCoreSize(registryClientConfig.getObserverThreadCoreSize())
|
||||
.setObserverThreadMaxSize(registryClientConfig.getObserverThreadMaxSize())
|
||||
.setObserverThreadQueueLength(registryClientConfig.getObserverThreadQueueLength())
|
||||
.setObserverCallbackTimeout(registryClientConfig.getObserverCallbackTimeout())
|
||||
.setSyncConfigRetryInterval(registryClientConfig.getSyncConfigRetryInterval())
|
||||
.setAccessKey(registryClientConfig.getAccessKey())
|
||||
.setSecretKey(registryClientConfig.getSecretKey())
|
||||
.build();
|
||||
}
|
||||
return cloneConfig;
|
||||
}
|
||||
|
||||
/** Init. */
|
||||
public void init() {
|
||||
if (!init.compareAndSet(false, true)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// init lookout subscriber
|
||||
if (null == lookoutSubscriber) {
|
||||
this.lookoutSubscriber = new LookoutSubscriber();
|
||||
}
|
||||
|
||||
// init event bus
|
||||
if (null == eventBus) {
|
||||
this.eventBus = new DefaultEventBus(registryClientConfig);
|
||||
this.eventBus.register(SubscriberProcessEvent.class, lookoutSubscriber);
|
||||
this.eventBus.register(ConfiguratorProcessEvent.class, lookoutSubscriber);
|
||||
}
|
||||
|
||||
// init server manager
|
||||
if (null == serverManager) {
|
||||
this.serverManager = new DefaultServerManager(registryClientConfig);
|
||||
}
|
||||
|
||||
// init observer handler
|
||||
if (null == observerHandler) {
|
||||
observerHandler = new DefaultObserverHandler(registryClientConfig, eventBus);
|
||||
}
|
||||
|
||||
// init auth manager
|
||||
if (null == authManager) {
|
||||
authManager = NoopAuthManager.INSTANCE;
|
||||
}
|
||||
|
||||
// init user processor
|
||||
List<UserProcessor> userProcessorList = new ArrayList<UserProcessor>();
|
||||
if (null == userProcessorMap) {
|
||||
userProcessorList.add(new ReceivedDataProcessor(registerCache, observerHandler));
|
||||
userProcessorList.add(new ReceivedConfigDataProcessor(registerCache, observerHandler));
|
||||
} else {
|
||||
UserProcessor userProcessor = userProcessorMap.get(ReceivedData.class);
|
||||
if (null == userProcessor) {
|
||||
userProcessorList.add(new ReceivedDataProcessor(registerCache, observerHandler));
|
||||
}
|
||||
userProcessor = userProcessorMap.get(ReceivedConfigData.class);
|
||||
if (null == userProcessor) {
|
||||
userProcessorList.add(new ReceivedConfigDataProcessor(registerCache, observerHandler));
|
||||
}
|
||||
userProcessorList.addAll(userProcessorMap.values());
|
||||
}
|
||||
|
||||
// init connection event processor
|
||||
if (null == connectionEventProcessorMap) {
|
||||
connectionEventProcessorMap =
|
||||
new HashMap<ConnectionEventType, ConnectionEventProcessor>(
|
||||
ConnectionEventType.values().length);
|
||||
}
|
||||
if (null == connectionEventProcessorMap.get(ConnectionEventType.CLOSE)) {
|
||||
ClientConnectionCloseEventProcessor connectionCloseEventProcessor =
|
||||
new ClientConnectionCloseEventProcessor();
|
||||
connectionEventProcessorMap.put(ConnectionEventType.CLOSE, connectionCloseEventProcessor);
|
||||
}
|
||||
if (null == connectionEventProcessorMap.get(ConnectionEventType.CONNECT)) {
|
||||
ClientConnectionOpenEventProcessor connectionOpenEventProcessor =
|
||||
new ClientConnectionOpenEventProcessor();
|
||||
connectionEventProcessorMap.put(ConnectionEventType.CONNECT, connectionOpenEventProcessor);
|
||||
}
|
||||
|
||||
// init client connection and register worker
|
||||
client =
|
||||
new ClientConnection(
|
||||
serverManager,
|
||||
userProcessorList,
|
||||
connectionEventProcessorMap,
|
||||
registerCache,
|
||||
registryClientConfig);
|
||||
|
||||
workerThread = new WorkerThread(client, registryClientConfig, registerCache);
|
||||
client.setWorker(workerThread);
|
||||
|
||||
client.init();
|
||||
|
||||
// init registry check thread
|
||||
new RegistryCheckThread().start();
|
||||
|
||||
// init sync config thread
|
||||
new SyncConfigThread(client, registerCache, registryClientConfig, observerHandler).start();
|
||||
}
|
||||
|
||||
/** @see RegistryClient#register(PublisherRegistration, String...) */
|
||||
@Override
|
||||
public Publisher register(PublisherRegistration registration, String... data) {
|
||||
if (!init.get()) {
|
||||
throw new IllegalStateException("Client needs to be initialized before using.");
|
||||
}
|
||||
|
||||
if (null == registration) {
|
||||
throw new IllegalArgumentException("Registration can not be null.");
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(registration.getDataId())) {
|
||||
throw new IllegalArgumentException("DataId can not be null");
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(registration.getGroup())) {
|
||||
registration.setGroup(DEFAULT_GROUP);
|
||||
}
|
||||
|
||||
Publisher publisher = registrationPublisherMap.get(registration);
|
||||
|
||||
if (null != publisher) {
|
||||
throwDuplicateException(registration, publisher);
|
||||
}
|
||||
|
||||
publisher = new DefaultPublisher(registration, workerThread, registryClientConfig);
|
||||
((DefaultPublisher) publisher).setAuthManager(authManager);
|
||||
|
||||
Publisher oldPublisher = registrationPublisherMap.putIfAbsent(registration, publisher);
|
||||
if (null != oldPublisher) {
|
||||
throwDuplicateException(registration, oldPublisher);
|
||||
}
|
||||
|
||||
registerCache.addRegister(publisher);
|
||||
|
||||
publisher.republish(data);
|
||||
|
||||
LOGGER.info(
|
||||
"[api] Regist publisher success, dataId: {}, group: {}, registerId: {}",
|
||||
publisher.getDataId(),
|
||||
publisher.getGroup(),
|
||||
publisher.getRegistId());
|
||||
|
||||
return publisher;
|
||||
}
|
||||
|
||||
private void throwDuplicateException(PublisherRegistration registration, Publisher publisher) {
|
||||
LOGGER.info(
|
||||
"[api] Publisher already exists, dataId: {}, group: {}, registerId: {}",
|
||||
publisher.getDataId(),
|
||||
publisher.getGroup(),
|
||||
publisher.getRegistId());
|
||||
|
||||
throw new DuplicateException(
|
||||
"Duplicate Publisher registration. (dataId: "
|
||||
+ registration.getDataId()
|
||||
+ ", group: "
|
||||
+ registration.getGroup()
|
||||
+ ")");
|
||||
}
|
||||
|
||||
/** @see RegistryClient#register(SubscriberRegistration) */
|
||||
@Override
|
||||
public Subscriber register(SubscriberRegistration registration) {
|
||||
if (!init.get()) {
|
||||
throw new IllegalStateException("Client needs to be initialized before using.");
|
||||
}
|
||||
|
||||
if (null == registration) {
|
||||
throw new IllegalArgumentException("Registration can not be null.");
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(registration.getDataId())) {
|
||||
throw new IllegalArgumentException("DataId can not be null.");
|
||||
}
|
||||
|
||||
if (null == registration.getSubscriberDataObserver()) {
|
||||
throw new IllegalArgumentException("Subscriber data observer can not be null.");
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(registration.getGroup())) {
|
||||
registration.setGroup(DEFAULT_GROUP);
|
||||
}
|
||||
|
||||
Subscriber subscriber = registrationSubscriberMap.get(registration);
|
||||
|
||||
if (null != subscriber) {
|
||||
throwDuplicateException(registration, subscriber);
|
||||
}
|
||||
|
||||
subscriber = new DefaultSubscriber(registration, workerThread, registryClientConfig);
|
||||
((DefaultSubscriber) subscriber).setAuthManager(authManager);
|
||||
|
||||
Subscriber oldSubscriber = registrationSubscriberMap.putIfAbsent(registration, subscriber);
|
||||
if (null != oldSubscriber) {
|
||||
throwDuplicateException(registration, oldSubscriber);
|
||||
}
|
||||
|
||||
registerCache.addRegister(subscriber);
|
||||
addRegisterTask(subscriber);
|
||||
|
||||
LOGGER.info(
|
||||
"[api] Regist subscriber success, dataId: {}, group: {}, scope: {}, registerId: {}",
|
||||
subscriber.getDataId(),
|
||||
subscriber.getGroup(),
|
||||
subscriber.getScopeEnum(),
|
||||
subscriber.getRegistId());
|
||||
|
||||
return subscriber;
|
||||
}
|
||||
|
||||
/** @see RegistryClient#register(ConfiguratorRegistration) */
|
||||
@Override
|
||||
public Configurator register(ConfiguratorRegistration registration) {
|
||||
if (!init.get()) {
|
||||
throw new IllegalStateException("Client needs to be initialized before using.");
|
||||
}
|
||||
|
||||
if (null == registration) {
|
||||
throw new IllegalArgumentException("Registration can not be null.");
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(registration.getDataId())) {
|
||||
throw new IllegalArgumentException("DataId can not be null.");
|
||||
}
|
||||
|
||||
if (null == registration.getConfigDataObserver()) {
|
||||
throw new IllegalArgumentException("Config data observer can not be null");
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(registration.getGroup())) {
|
||||
registration.setGroup(DEFAULT_GROUP);
|
||||
}
|
||||
|
||||
Configurator configurator = registrationConfiguratorMap.get(registration);
|
||||
|
||||
if (null != configurator) {
|
||||
throwDuplicateException(configurator);
|
||||
}
|
||||
|
||||
configurator = new DefaultConfigurator(registration, registryClientConfig, workerThread);
|
||||
((DefaultConfigurator) configurator).setAuthManager(authManager);
|
||||
|
||||
Configurator oldConfigurator =
|
||||
registrationConfiguratorMap.putIfAbsent(registration, configurator);
|
||||
if (null != oldConfigurator) {
|
||||
throwDuplicateException(configurator);
|
||||
}
|
||||
|
||||
registerCache.addRegister(configurator);
|
||||
addRegisterTask(configurator);
|
||||
|
||||
LOGGER.info(
|
||||
"[api] Regist configurator success, dataId: {}, registerId: {}",
|
||||
configurator.getDataId(),
|
||||
configurator.getRegistId());
|
||||
|
||||
return configurator;
|
||||
}
|
||||
|
||||
private void throwDuplicateException(Configurator configurator) {
|
||||
LOGGER.info(
|
||||
"[api] Configurator already exists, dataId: {}, registerId: {}",
|
||||
configurator.getDataObserver(),
|
||||
configurator.getRegistId());
|
||||
throw new DuplicateException(
|
||||
"Duplicate configurator registration. (dataId: " + configurator.getDataId() + " )");
|
||||
}
|
||||
|
||||
/** @see RegistryClient#unregister(String, String, RegistryType) */
|
||||
@Override
|
||||
public int unregister(String dataId, String group, RegistryType registryType) {
|
||||
if (StringUtils.isBlank(dataId)) {
|
||||
throw new IllegalArgumentException("dataId can not be empty");
|
||||
}
|
||||
|
||||
if (null == registryType) {
|
||||
throw new IllegalArgumentException("registry type can not be null");
|
||||
}
|
||||
|
||||
if (null == group) {
|
||||
group = DEFAULT_GROUP;
|
||||
}
|
||||
|
||||
List<Register> registers = new ArrayList<Register>();
|
||||
|
||||
if (RegistryType.PUBLISHER == registryType) {
|
||||
Collection<Publisher> publishers = registerCache.getAllPublishers();
|
||||
for (Publisher publisher : publishers) {
|
||||
if (dataId.equals(publisher.getDataId()) && group.equals(publisher.getGroup())) {
|
||||
registers.add(publisher);
|
||||
}
|
||||
}
|
||||
} else if (RegistryType.SUBSCRIBER == registryType) {
|
||||
Collection<Subscriber> subscribers = registerCache.getAllSubscribers();
|
||||
for (Subscriber subscriber : subscribers) {
|
||||
if (dataId.equals(subscriber.getDataId()) && group.equals(subscriber.getGroup())) {
|
||||
registers.add(subscriber);
|
||||
}
|
||||
}
|
||||
} else if (RegistryType.CONFIGURATOR == registryType) {
|
||||
Collection<Configurator> configurators = registerCache.getAllConfigurator();
|
||||
for (Configurator configurator : configurators) {
|
||||
if (dataId.equals(configurator.getDataId())) {
|
||||
registers.add(configurator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (Register register : registers) {
|
||||
register.unregister();
|
||||
}
|
||||
return registers.size();
|
||||
}
|
||||
|
||||
private void throwDuplicateException(SubscriberRegistration registration, Subscriber subscriber) {
|
||||
LOGGER.info(
|
||||
"[api] Subscriber already exists, dataId: {}, group: {}, scope: {}, registerId: {}",
|
||||
subscriber.getDataId(),
|
||||
subscriber.getGroup(),
|
||||
subscriber.getScopeEnum(),
|
||||
subscriber.getRegistId());
|
||||
throw new DuplicateException(
|
||||
"Duplicate subscriber registration. (dataId: "
|
||||
+ registration.getDataId()
|
||||
+ ", group: "
|
||||
+ registration.getGroup()
|
||||
+ ")");
|
||||
}
|
||||
|
||||
/**
|
||||
* Add register task.
|
||||
*
|
||||
* @param register the register
|
||||
* @throws RegistryClientException the registry client exception
|
||||
*/
|
||||
private void addRegisterTask(Register register) throws RegistryClientException {
|
||||
try {
|
||||
TaskEvent event = new TaskEvent(register);
|
||||
workerThread.schedule(event);
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("Register task schedule error, {}", register, e);
|
||||
throw new RegistryClientException("Register task schedule error", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>registerCache</tt>.
|
||||
*
|
||||
* @return property value of registerCache
|
||||
*/
|
||||
public RegisterCache getRegisterCache() {
|
||||
return registerCache;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>userProcessorMap</tt>.
|
||||
*
|
||||
* @param userProcessorMap value to be assigned to property userProcessorMap
|
||||
*/
|
||||
public void setUserProcessorMap(Map<Class<?>, UserProcessor> userProcessorMap) {
|
||||
this.userProcessorMap = userProcessorMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>connectionEventProcessorMap</tt>.
|
||||
*
|
||||
* @param connectionEventProcessorMap value to be assigned to property connectionEventProcessorMap
|
||||
*/
|
||||
public void setConnectionEventProcessorMap(
|
||||
Map<ConnectionEventType, ConnectionEventProcessor> connectionEventProcessorMap) {
|
||||
this.connectionEventProcessorMap = connectionEventProcessorMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>observerHandler</tt>.
|
||||
*
|
||||
* @param observerHandler value to be assigned to property observerHandler
|
||||
*/
|
||||
public void setObserverHandler(ObserverHandler observerHandler) {
|
||||
this.observerHandler = observerHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>serverManager</tt>.
|
||||
*
|
||||
* @param serverManager value to be assigned to property serverManager
|
||||
*/
|
||||
public void setServerManager(ServerManager serverManager) {
|
||||
this.serverManager = serverManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>authManager</tt>.
|
||||
*
|
||||
* @param authManager value to be assigned to property authManager
|
||||
*/
|
||||
public void setAuthManager(AuthManager authManager) {
|
||||
this.authManager = authManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>lookoutSubscriber</tt>.
|
||||
*
|
||||
* @param lookoutSubscriber value to be assigned to property lookoutSubscriber
|
||||
*/
|
||||
public void setLookoutSubscriber(LookoutSubscriber lookoutSubscriber) {
|
||||
this.lookoutSubscriber = lookoutSubscriber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>eventBus</tt>.
|
||||
*
|
||||
* @param eventBus value to be assigned to property eventBus
|
||||
*/
|
||||
public void setEventBus(EventBus eventBus) {
|
||||
this.eventBus = eventBus;
|
||||
}
|
||||
|
||||
/** The type Registry check thread. */
|
||||
class RegistryCheckThread extends Thread {
|
||||
|
||||
/** Instantiates a new Registry check thread. */
|
||||
public RegistryCheckThread() {
|
||||
super("RegistryClientCheckThread");
|
||||
setDaemon(true);
|
||||
}
|
||||
|
||||
/** @see Thread#run() */
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
//noinspection InfiniteLoopStatement
|
||||
while (true) {
|
||||
try {
|
||||
Thread.sleep(registryClientConfig.getRecheckInterval());
|
||||
|
||||
Collection<Publisher> allPublishers = registerCache.getAllPublishers();
|
||||
|
||||
for (Publisher publisher : allPublishers) {
|
||||
try {
|
||||
if (null != publisher && !((AbstractInternalRegister) publisher).isDone()) {
|
||||
addRegisterTask(publisher);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("Sync publisher error, {}", publisher, e);
|
||||
}
|
||||
}
|
||||
|
||||
Collection<Subscriber> allSubscribers = registerCache.getAllSubscribers();
|
||||
|
||||
for (Subscriber subscriber : allSubscribers) {
|
||||
try {
|
||||
if (null != subscriber && !((AbstractInternalRegister) subscriber).isDone()) {
|
||||
addRegisterTask(subscriber);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("Sync subscriber error, {}", subscriber, e);
|
||||
}
|
||||
}
|
||||
|
||||
Collection<Configurator> allConfigurators = registerCache.getAllConfigurator();
|
||||
|
||||
for (Configurator configurator : allConfigurators) {
|
||||
try {
|
||||
if (null != configurator && !((AbstractInternalRegister) configurator).isDone()) {
|
||||
addRegisterTask(configurator);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("Sync configurator error, {}", configurator, e);
|
||||
}
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
LOGGER.error("Execute error", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,532 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.provider;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.RegistryClientConfig;
|
||||
|
||||
/**
|
||||
* The type Default registry client config.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : DefaultRegistryClientConfig.java, v 0.1 2017-11-23 20:11 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class DefaultRegistryClientConfig implements RegistryClientConfig {
|
||||
|
||||
/** */
|
||||
private String env;
|
||||
|
||||
/** */
|
||||
private String instanceId;
|
||||
|
||||
private String ip;
|
||||
|
||||
/** */
|
||||
private String zone;
|
||||
|
||||
/** */
|
||||
private String registryEndpoint;
|
||||
|
||||
/** */
|
||||
private int registryEndpointPort;
|
||||
|
||||
/** */
|
||||
private String dataCenter;
|
||||
|
||||
/** */
|
||||
private String appName;
|
||||
|
||||
/** */
|
||||
private int connectTimeout;
|
||||
|
||||
/** */
|
||||
private int socketTimeout;
|
||||
|
||||
/** */
|
||||
private int invokeTimeout;
|
||||
|
||||
/** */
|
||||
private int recheckInterval;
|
||||
|
||||
/** */
|
||||
private int observerThreadCoreSize;
|
||||
|
||||
/** */
|
||||
private int observerThreadMaxSize;
|
||||
|
||||
/** */
|
||||
private int observerThreadQueueLength;
|
||||
|
||||
/** */
|
||||
private int observerCallbackTimeout;
|
||||
|
||||
/** */
|
||||
private int syncConfigRetryInterval;
|
||||
|
||||
/** */
|
||||
private String accessKey;
|
||||
|
||||
/** */
|
||||
private String secretKey;
|
||||
|
||||
/** */
|
||||
private String algorithm;
|
||||
|
||||
/** */
|
||||
private long authCacheInterval;
|
||||
|
||||
/** */
|
||||
private boolean eventBusEnable;
|
||||
|
||||
/**
|
||||
* Instantiates a new Default registry client config.
|
||||
*
|
||||
* @param env the env
|
||||
* @param instanceId the instance id
|
||||
* @param ip the ip
|
||||
* @param zone the zone
|
||||
* @param registryEndpoint the registry endpoint
|
||||
* @param registryEndpointPort the registry endpoint port
|
||||
* @param dataCenter the data center
|
||||
* @param appName the app name
|
||||
* @param connectTimeout the connect timeout
|
||||
* @param socketTimeout the socket timeout
|
||||
* @param invokeTimeout the invoke timeout
|
||||
* @param recheckInterval the recheck interval
|
||||
* @param observerThreadCoreSize the observer thread core size
|
||||
* @param observerThreadMaxSize the observer thread max size
|
||||
* @param observerThreadQueueLength the observer thread queue length
|
||||
* @param observerCallbackTimeout the observer callback timeout
|
||||
* @param syncConfigRetryInterval the sync config retry interval
|
||||
* @param accessKey the access key
|
||||
* @param secretKey the secret key
|
||||
* @param algorithm the algorithm
|
||||
* @param authCacheInterval the auth cache interval
|
||||
*/
|
||||
public DefaultRegistryClientConfig(
|
||||
String env,
|
||||
String instanceId,
|
||||
String ip,
|
||||
String zone,
|
||||
String registryEndpoint,
|
||||
int registryEndpointPort,
|
||||
String dataCenter,
|
||||
String appName,
|
||||
int connectTimeout,
|
||||
int socketTimeout,
|
||||
int invokeTimeout,
|
||||
int recheckInterval,
|
||||
int observerThreadCoreSize,
|
||||
int observerThreadMaxSize,
|
||||
int observerThreadQueueLength,
|
||||
int observerCallbackTimeout,
|
||||
int syncConfigRetryInterval,
|
||||
String accessKey,
|
||||
String secretKey,
|
||||
String algorithm,
|
||||
long authCacheInterval,
|
||||
boolean eventBusEnable) {
|
||||
this.env = env;
|
||||
this.instanceId = instanceId;
|
||||
this.ip = ip;
|
||||
this.zone = zone;
|
||||
this.registryEndpoint = registryEndpoint;
|
||||
this.registryEndpointPort = registryEndpointPort;
|
||||
this.dataCenter = dataCenter;
|
||||
this.appName = appName;
|
||||
this.connectTimeout = connectTimeout;
|
||||
this.socketTimeout = socketTimeout;
|
||||
this.invokeTimeout = invokeTimeout;
|
||||
this.recheckInterval = recheckInterval;
|
||||
this.observerThreadCoreSize = observerThreadCoreSize;
|
||||
this.observerThreadMaxSize = observerThreadMaxSize;
|
||||
this.observerThreadQueueLength = observerThreadQueueLength;
|
||||
this.observerCallbackTimeout = observerCallbackTimeout;
|
||||
this.syncConfigRetryInterval = syncConfigRetryInterval;
|
||||
this.accessKey = accessKey;
|
||||
this.secretKey = secretKey;
|
||||
this.algorithm = algorithm;
|
||||
this.authCacheInterval = authCacheInterval;
|
||||
this.eventBusEnable = eventBusEnable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>env</tt>.
|
||||
*
|
||||
* @return property value of env
|
||||
*/
|
||||
@Override
|
||||
public String getEnv() {
|
||||
return env;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>instanceId</tt>.
|
||||
*
|
||||
* @return property value of instanceId
|
||||
*/
|
||||
@Override
|
||||
public String getInstanceId() {
|
||||
return instanceId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIp() {
|
||||
return ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>zone</tt>.
|
||||
*
|
||||
* @return property value of zone
|
||||
*/
|
||||
@Override
|
||||
public String getZone() {
|
||||
return zone;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>registryEndpoint</tt>.
|
||||
*
|
||||
* @return property value of registryEndpoint
|
||||
*/
|
||||
@Override
|
||||
public String getRegistryEndpoint() {
|
||||
return registryEndpoint;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>registryEndpointPort</tt>.
|
||||
*
|
||||
* @return property value of registryEndpointPort
|
||||
*/
|
||||
@Override
|
||||
public int getRegistryEndpointPort() {
|
||||
return registryEndpointPort;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>dataCenter</tt>.
|
||||
*
|
||||
* @return property value of dataCenter
|
||||
*/
|
||||
@Override
|
||||
public String getDataCenter() {
|
||||
return dataCenter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>appName</tt>.
|
||||
*
|
||||
* @return property value of appName
|
||||
*/
|
||||
@Override
|
||||
public String getAppName() {
|
||||
return appName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>connectTimeout</tt>.
|
||||
*
|
||||
* @return property value of connectTimeout
|
||||
*/
|
||||
@Override
|
||||
public int getConnectTimeout() {
|
||||
return connectTimeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>socketTimeout</tt>.
|
||||
*
|
||||
* @return property value of socketTimeout
|
||||
*/
|
||||
@Override
|
||||
public int getSocketTimeout() {
|
||||
return socketTimeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>invokeTimeout</tt>.
|
||||
*
|
||||
* @return property value of invokeTimeout
|
||||
*/
|
||||
@Override
|
||||
public int getInvokeTimeout() {
|
||||
return invokeTimeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>recheckInterval</tt>.
|
||||
*
|
||||
* @return property value of recheckInterval
|
||||
*/
|
||||
@Override
|
||||
public int getRecheckInterval() {
|
||||
return recheckInterval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>observerThreadCoreSize</tt>.
|
||||
*
|
||||
* @return property value of observerThreadCoreSize
|
||||
*/
|
||||
@Override
|
||||
public int getObserverThreadCoreSize() {
|
||||
return observerThreadCoreSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>observerThreadMaxSize</tt>.
|
||||
*
|
||||
* @return property value of observerThreadMaxSize
|
||||
*/
|
||||
@Override
|
||||
public int getObserverThreadMaxSize() {
|
||||
return observerThreadMaxSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>observerThreadQueueLength</tt>.
|
||||
*
|
||||
* @return property value of observerThreadQueueLength
|
||||
*/
|
||||
@Override
|
||||
public int getObserverThreadQueueLength() {
|
||||
return observerThreadQueueLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>observerCallbackTimeout</tt>.
|
||||
*
|
||||
* @return property value of observerCallbackTimeout
|
||||
*/
|
||||
@Override
|
||||
public int getObserverCallbackTimeout() {
|
||||
return observerCallbackTimeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>syncConfigRetryInterval</tt>.
|
||||
*
|
||||
* @return property value of syncConfigRetryInterval
|
||||
*/
|
||||
@Override
|
||||
public int getSyncConfigRetryInterval() {
|
||||
return syncConfigRetryInterval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>accessKey</tt>.
|
||||
*
|
||||
* @return property value of accessKey
|
||||
*/
|
||||
@Override
|
||||
public String getAccessKey() {
|
||||
return accessKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>secretKey</tt>.
|
||||
*
|
||||
* @return property value of secretKey
|
||||
*/
|
||||
@Override
|
||||
public String getSecretKey() {
|
||||
return secretKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>algorithm</tt>.
|
||||
*
|
||||
* @return property value of algorithm
|
||||
*/
|
||||
@Override
|
||||
public String getAlgorithm() {
|
||||
return algorithm;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>algorithm</tt>.
|
||||
*
|
||||
* @param algorithm value to be assigned to property algorithm
|
||||
*/
|
||||
public void setAlgorithm(String algorithm) {
|
||||
this.algorithm = algorithm;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>authCacheInterval</tt>.
|
||||
*
|
||||
* @return property value of authCacheInterval
|
||||
*/
|
||||
@Override
|
||||
public long getAuthCacheInterval() {
|
||||
return authCacheInterval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>authCacheInterval</tt>.
|
||||
*
|
||||
* @param authCacheInterval value to be assigned to property authCacheInterval
|
||||
*/
|
||||
public void setAuthCacheInterval(long authCacheInterval) {
|
||||
this.authCacheInterval = authCacheInterval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>eventBusEnable</tt>.
|
||||
*
|
||||
* @return property value of eventBusEnable
|
||||
*/
|
||||
@Override
|
||||
public boolean isEventBusEnable() {
|
||||
return eventBusEnable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>eventBusEnable</tt>.
|
||||
*
|
||||
* @param eventBusEnable value to be assigned to property eventBusEnable
|
||||
*/
|
||||
public void setEventBusEnable(boolean eventBusEnable) {
|
||||
this.eventBusEnable = eventBusEnable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Equals boolean.
|
||||
*
|
||||
* @param o the o
|
||||
* @return the boolean
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (!(o instanceof DefaultRegistryClientConfig)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
DefaultRegistryClientConfig that = (DefaultRegistryClientConfig) o;
|
||||
|
||||
if (registryEndpointPort != that.registryEndpointPort) {
|
||||
return false;
|
||||
}
|
||||
if (env != null ? !env.equals(that.env) : that.env != null) {
|
||||
return false;
|
||||
}
|
||||
if (instanceId != null ? !instanceId.equals(that.instanceId) : that.instanceId != null) {
|
||||
return false;
|
||||
}
|
||||
if (ip != null ? !ip.equals(that.ip) : that.ip != null) {
|
||||
return false;
|
||||
}
|
||||
if (zone != null ? !zone.equals(that.zone) : that.zone != null) {
|
||||
return false;
|
||||
}
|
||||
if (registryEndpoint != null
|
||||
? !registryEndpoint.equals(that.registryEndpoint)
|
||||
: that.registryEndpoint != null) {
|
||||
return false;
|
||||
}
|
||||
if (dataCenter != null ? !dataCenter.equals(that.dataCenter) : that.dataCenter != null) {
|
||||
return false;
|
||||
}
|
||||
return appName != null ? appName.equals(that.appName) : that.appName == null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hash code int.
|
||||
*
|
||||
* @return the int
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = env != null ? env.hashCode() : 0;
|
||||
result = 31 * result + (instanceId != null ? instanceId.hashCode() : 0);
|
||||
result = 31 * result + (ip != null ? ip.hashCode() : 0);
|
||||
result = 31 * result + (zone != null ? zone.hashCode() : 0);
|
||||
result = 31 * result + (registryEndpoint != null ? registryEndpoint.hashCode() : 0);
|
||||
result = 31 * result + registryEndpointPort;
|
||||
result = 31 * result + (dataCenter != null ? dataCenter.hashCode() : 0);
|
||||
result = 31 * result + (appName != null ? appName.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* To string string.
|
||||
*
|
||||
* @return the string
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DefaultRegistryClientConfig{"
|
||||
+ "env='"
|
||||
+ env
|
||||
+ '\''
|
||||
+ ", instanceId='"
|
||||
+ instanceId
|
||||
+ ", ip='"
|
||||
+ ip
|
||||
+ '\''
|
||||
+ ", zone='"
|
||||
+ zone
|
||||
+ '\''
|
||||
+ ", registryEndpoint='"
|
||||
+ registryEndpoint
|
||||
+ '\''
|
||||
+ ", registryEndpointPort="
|
||||
+ registryEndpointPort
|
||||
+ ", dataCenter='"
|
||||
+ dataCenter
|
||||
+ '\''
|
||||
+ ", appName='"
|
||||
+ appName
|
||||
+ '\''
|
||||
+ ", connectTimeout="
|
||||
+ connectTimeout
|
||||
+ ", socketTimeout="
|
||||
+ socketTimeout
|
||||
+ ", invokeTimeout="
|
||||
+ invokeTimeout
|
||||
+ ", recheckInterval="
|
||||
+ recheckInterval
|
||||
+ ", observerThreadCoreSize="
|
||||
+ observerThreadCoreSize
|
||||
+ ", observerThreadMaxSize="
|
||||
+ observerThreadMaxSize
|
||||
+ ", observerThreadQueueLength="
|
||||
+ observerThreadQueueLength
|
||||
+ ", observerCallbackTimeout="
|
||||
+ observerCallbackTimeout
|
||||
+ ", syncConfigRetryInterval="
|
||||
+ syncConfigRetryInterval
|
||||
+ ", accessKey='"
|
||||
+ accessKey
|
||||
+ '\''
|
||||
+ ", secretKey='"
|
||||
+ secretKey
|
||||
+ '\''
|
||||
+ ", algorithm='"
|
||||
+ algorithm
|
||||
+ '\''
|
||||
+ ", authCacheInterval="
|
||||
+ authCacheInterval
|
||||
+ ", eventBusEnable="
|
||||
+ eventBusEnable
|
||||
+ '}';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,325 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.provider;
|
||||
|
||||
import static com.alipay.sofa.registry.client.constants.ValueConstants.DEFAULT_DATA_CENTER;
|
||||
import static com.alipay.sofa.registry.client.constants.ValueConstants.DEFAULT_ZONE;
|
||||
|
||||
/**
|
||||
* The type Default registry client config builder.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : DefaultRegistryClientConfig.java, v 0.1 2017-11-23 20:12 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class DefaultRegistryClientConfigBuilder {
|
||||
private String env;
|
||||
private String instanceId;
|
||||
private String ip;
|
||||
private String zone = DEFAULT_ZONE;
|
||||
private String registryEndpoint;
|
||||
private int registryEndpointPort = 9603;
|
||||
private String dataCenter = DEFAULT_DATA_CENTER;
|
||||
private String appName;
|
||||
private int connectTimeout = 3000;
|
||||
private int socketTimeout = 3000;
|
||||
private int invokeTimeout = 1000;
|
||||
private int recheckInterval = 500;
|
||||
private int observerThreadCoreSize = 5;
|
||||
private int observerThreadMaxSize = 10;
|
||||
private int observerThreadQueueLength = 1000;
|
||||
private int observerCallbackTimeout = 5000;
|
||||
private int syncConfigRetryInterval = 30000;
|
||||
private String accessKey;
|
||||
private String secretKey;
|
||||
private String algorithm = "HmacSHA256";
|
||||
private long authCacheInterval = 5 * 60 * 1000;
|
||||
private boolean eventBusEnable = true;
|
||||
|
||||
/**
|
||||
* Start default registry client config builder.
|
||||
*
|
||||
* @return the default registry client config builder
|
||||
*/
|
||||
public static DefaultRegistryClientConfigBuilder start() {
|
||||
return new DefaultRegistryClientConfigBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets env.
|
||||
*
|
||||
* @param env the env
|
||||
* @return the env
|
||||
*/
|
||||
public DefaultRegistryClientConfigBuilder setEnv(String env) {
|
||||
this.env = env;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets instance id.
|
||||
*
|
||||
* @param instanceId the instance id
|
||||
* @return the instance id
|
||||
*/
|
||||
public DefaultRegistryClientConfigBuilder setInstanceId(String instanceId) {
|
||||
this.instanceId = instanceId;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets ip.
|
||||
*
|
||||
* @param ip the ip
|
||||
*/
|
||||
public DefaultRegistryClientConfigBuilder setIp(String ip) {
|
||||
this.ip = ip;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets zone.
|
||||
*
|
||||
* @param zone the zone
|
||||
* @return the zone
|
||||
*/
|
||||
public DefaultRegistryClientConfigBuilder setZone(String zone) {
|
||||
this.zone = zone;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets registry endpoint.
|
||||
*
|
||||
* @param registryEndpoint the registry endpoint
|
||||
* @return the registry endpoint
|
||||
*/
|
||||
public DefaultRegistryClientConfigBuilder setRegistryEndpoint(String registryEndpoint) {
|
||||
this.registryEndpoint = registryEndpoint;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets registry endpoint port.
|
||||
*
|
||||
* @param registryEndpointPort the registry endpoint port
|
||||
* @return the registry endpoint port
|
||||
*/
|
||||
public DefaultRegistryClientConfigBuilder setRegistryEndpointPort(int registryEndpointPort) {
|
||||
this.registryEndpointPort = registryEndpointPort;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets data center.
|
||||
*
|
||||
* @param dataCenter the data center
|
||||
* @return the data center
|
||||
*/
|
||||
public DefaultRegistryClientConfigBuilder setDataCenter(String dataCenter) {
|
||||
this.dataCenter = dataCenter;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets app name.
|
||||
*
|
||||
* @param appName the app name
|
||||
* @return the app name
|
||||
*/
|
||||
public DefaultRegistryClientConfigBuilder setAppName(String appName) {
|
||||
this.appName = appName;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>observerThreadCoreSize</tt>.
|
||||
*
|
||||
* @param observerThreadCoreSize value to be assigned to property observerThreadCoreSize
|
||||
*/
|
||||
public DefaultRegistryClientConfigBuilder setObserverThreadCoreSize(int observerThreadCoreSize) {
|
||||
this.observerThreadCoreSize = observerThreadCoreSize;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>observerThreadMaxSize</tt>.
|
||||
*
|
||||
* @param observerThreadMaxSize value to be assigned to property observerThreadMaxSize
|
||||
*/
|
||||
public DefaultRegistryClientConfigBuilder setObserverThreadMaxSize(int observerThreadMaxSize) {
|
||||
this.observerThreadMaxSize = observerThreadMaxSize;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>observerThreadQueueLength</tt>.
|
||||
*
|
||||
* @param observerThreadQueueLength value to be assigned to property observerThreadQueueLength
|
||||
*/
|
||||
public DefaultRegistryClientConfigBuilder setObserverThreadQueueLength(
|
||||
int observerThreadQueueLength) {
|
||||
this.observerThreadQueueLength = observerThreadQueueLength;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>observerCallbackTimeout</tt>.
|
||||
*
|
||||
* @param observerCallbackTimeout value to be assigned to property observerCallbackTimeout
|
||||
*/
|
||||
public DefaultRegistryClientConfigBuilder setObserverCallbackTimeout(
|
||||
int observerCallbackTimeout) {
|
||||
this.observerCallbackTimeout = observerCallbackTimeout;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets connect timeout.
|
||||
*
|
||||
* @param connectTimeout the connect timeout
|
||||
* @return the connect timeout
|
||||
*/
|
||||
public DefaultRegistryClientConfigBuilder setConnectTimeout(int connectTimeout) {
|
||||
this.connectTimeout = connectTimeout;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets socket timeout.
|
||||
*
|
||||
* @param socketTimeout the socket timeout
|
||||
* @return the socket timeout
|
||||
*/
|
||||
public DefaultRegistryClientConfigBuilder setSocketTimeout(int socketTimeout) {
|
||||
this.socketTimeout = socketTimeout;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets invoke timeout.
|
||||
*
|
||||
* @param invokeTimeout the invoke timeout
|
||||
* @return the invoke timeout
|
||||
*/
|
||||
public DefaultRegistryClientConfigBuilder setInvokeTimeout(int invokeTimeout) {
|
||||
this.invokeTimeout = invokeTimeout;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets recheck interval.
|
||||
*
|
||||
* @param recheckInterval the recheck interval
|
||||
* @return the recheck interval
|
||||
*/
|
||||
public DefaultRegistryClientConfigBuilder setRecheckInterval(int recheckInterval) {
|
||||
this.recheckInterval = recheckInterval;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>syncConfigRetryInterval</tt>.
|
||||
*
|
||||
* @param syncConfigRetryInterval value to be assigned to property syncConfigRetryInterval
|
||||
*/
|
||||
public DefaultRegistryClientConfigBuilder setSyncConfigRetryInterval(
|
||||
int syncConfigRetryInterval) {
|
||||
this.syncConfigRetryInterval = syncConfigRetryInterval;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>accessKey</tt>.
|
||||
*
|
||||
* @param accessKey value to be assigned to property accessKey
|
||||
*/
|
||||
public DefaultRegistryClientConfigBuilder setAccessKey(String accessKey) {
|
||||
this.accessKey = accessKey;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>secretKey</tt>.
|
||||
*
|
||||
* @param secretKey value to be assigned to property secretKey
|
||||
*/
|
||||
public DefaultRegistryClientConfigBuilder setSecretKey(String secretKey) {
|
||||
this.secretKey = secretKey;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>algorithm</tt>.
|
||||
*
|
||||
* @param algorithm value to be assigned to property algorithm
|
||||
*/
|
||||
public DefaultRegistryClientConfigBuilder setAlgorithm(String algorithm) {
|
||||
this.algorithm = algorithm;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>authCacheInterval</tt>.
|
||||
*
|
||||
* @param authCacheInterval value to be assigned to property authCacheInterval
|
||||
*/
|
||||
public DefaultRegistryClientConfigBuilder setAuthCacheInterval(long authCacheInterval) {
|
||||
this.authCacheInterval = authCacheInterval;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>eventBusEnable</tt>.
|
||||
*
|
||||
* @param eventBusEnable value to be assigned to property eventBusEnable
|
||||
*/
|
||||
public DefaultRegistryClientConfigBuilder setEventBusEnable(boolean eventBusEnable) {
|
||||
this.eventBusEnable = eventBusEnable;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create default registry client config default registry client config.
|
||||
*
|
||||
* @return the default registry client config
|
||||
*/
|
||||
public DefaultRegistryClientConfig build() {
|
||||
return new DefaultRegistryClientConfig(
|
||||
env,
|
||||
instanceId,
|
||||
ip,
|
||||
zone,
|
||||
registryEndpoint,
|
||||
registryEndpointPort,
|
||||
dataCenter,
|
||||
appName,
|
||||
connectTimeout,
|
||||
socketTimeout,
|
||||
invokeTimeout,
|
||||
recheckInterval,
|
||||
observerThreadCoreSize,
|
||||
observerThreadMaxSize,
|
||||
observerThreadQueueLength,
|
||||
observerCallbackTimeout,
|
||||
syncConfigRetryInterval,
|
||||
accessKey,
|
||||
secretKey,
|
||||
algorithm,
|
||||
authCacheInterval,
|
||||
eventBusEnable);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,157 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.provider;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.RegistryClientConfig;
|
||||
import com.alipay.sofa.registry.client.log.LoggerFactory;
|
||||
import com.alipay.sofa.registry.client.remoting.ServerManager;
|
||||
import com.alipay.sofa.registry.client.remoting.ServerNode;
|
||||
import com.alipay.sofa.registry.client.util.HttpClientUtils;
|
||||
import com.alipay.sofa.registry.client.util.ServerNodeParser;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
/**
|
||||
* The type Default server manager.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : DefaultServerManager.java, v 0.1 2017-12-25 11:48 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class DefaultServerManager implements ServerManager {
|
||||
|
||||
/** The constant MIN_RETRY_INTERVAL. */
|
||||
public static final int MIN_RETRY_INTERVAL = 10000;
|
||||
/** LOGGER */
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultServerManager.class);
|
||||
|
||||
private RegistryClientConfig config;
|
||||
private Set<ServerNode> serverNodes;
|
||||
private SyncServerListThread syncServerListTask;
|
||||
|
||||
private AtomicBoolean inited = new AtomicBoolean(false);
|
||||
|
||||
/**
|
||||
* Instantiates a new Default server manager.
|
||||
*
|
||||
* @param config the config
|
||||
*/
|
||||
public DefaultServerManager(RegistryClientConfig config) {
|
||||
this.config = config;
|
||||
this.serverNodes = new HashSet<ServerNode>();
|
||||
this.syncServerListTask = new SyncServerListThread(config);
|
||||
}
|
||||
|
||||
/** @see ServerManager#getServerList() */
|
||||
@Override
|
||||
public List<ServerNode> getServerList() {
|
||||
// init once
|
||||
if (inited.compareAndSet(false, true)) {
|
||||
this.syncServerListTask.start();
|
||||
}
|
||||
// sync query when server list is empty
|
||||
if (serverNodes.isEmpty()) {
|
||||
syncServerList();
|
||||
}
|
||||
return new ArrayList<ServerNode>(serverNodes);
|
||||
}
|
||||
|
||||
/** @see ServerManager#random() */
|
||||
@Override
|
||||
public ServerNode random() {
|
||||
List<ServerNode> urls = getServerList();
|
||||
if (null == urls || urls.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
Random random = new Random();
|
||||
return urls.get(random.nextInt(urls.size()));
|
||||
}
|
||||
|
||||
private void syncServerList() {
|
||||
String url =
|
||||
String.format(
|
||||
"http://%s:%d/api/servers/query",
|
||||
config.getRegistryEndpoint(), config.getRegistryEndpointPort());
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
params.put("env", config.getEnv());
|
||||
params.put("zone", config.getZone());
|
||||
params.put("dataCenter", config.getDataCenter());
|
||||
params.put("appName", config.getAppName());
|
||||
params.put("instanceId", config.getInstanceId());
|
||||
try {
|
||||
String result = HttpClientUtils.get(url, params, config);
|
||||
if (null != result) {
|
||||
String[] servers = result.split(";");
|
||||
Set<ServerNode> tempNodes = new HashSet<ServerNode>();
|
||||
for (String server : servers) {
|
||||
try {
|
||||
ServerNode serverNode = ServerNodeParser.parse(server);
|
||||
tempNodes.add(serverNode);
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("[serverManager] parse server node error, {}", server, e);
|
||||
}
|
||||
}
|
||||
if (!tempNodes.equals(serverNodes)) {
|
||||
serverNodes = tempNodes;
|
||||
LOGGER.info("[serverManager] update nodes success, {}", tempNodes);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("[serverManager] get server list error", e);
|
||||
}
|
||||
}
|
||||
|
||||
/** The type Sync server list task. */
|
||||
class SyncServerListThread extends Thread {
|
||||
|
||||
private RegistryClientConfig config;
|
||||
|
||||
/**
|
||||
* Instantiates a new Sync server list task.
|
||||
*
|
||||
* @param config the config
|
||||
*/
|
||||
public SyncServerListThread(RegistryClientConfig config) {
|
||||
this.setName("SyncServerListThread");
|
||||
this.setDaemon(true);
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
/** @see Thread#run() */
|
||||
@SuppressWarnings("InfiniteLoopStatement")
|
||||
@Override
|
||||
public void run() {
|
||||
int retryInterval;
|
||||
while (true) {
|
||||
try {
|
||||
retryInterval = Math.max(MIN_RETRY_INTERVAL, config.getSyncConfigRetryInterval());
|
||||
Thread.sleep(retryInterval);
|
||||
|
||||
syncServerList();
|
||||
} catch (Throwable e) {
|
||||
LOGGER.error("[serverManager] sync server list task error", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,179 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.provider;
|
||||
|
||||
import com.alipay.sofa.registry.client.remoting.ServerNode;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* The type Default server node.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : DefaultServerNode.java, v 0.1 2018-03-01 17:13 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class DefaultServerNode implements ServerNode {
|
||||
|
||||
private String url;
|
||||
|
||||
private String host;
|
||||
|
||||
private int port;
|
||||
|
||||
private Properties properties;
|
||||
|
||||
/**
|
||||
* Instantiates a new Default server node.
|
||||
*
|
||||
* @param url the url
|
||||
* @param host the host
|
||||
* @param port the port
|
||||
* @param properties the properties
|
||||
*/
|
||||
public DefaultServerNode(String url, String host, int port, Properties properties) {
|
||||
this.url = url;
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets host.
|
||||
*
|
||||
* @return the host
|
||||
*/
|
||||
@Override
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets port.
|
||||
*
|
||||
* @return the port
|
||||
*/
|
||||
@Override
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets url.
|
||||
*
|
||||
* @return the url
|
||||
*/
|
||||
@Override
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets properties.
|
||||
*
|
||||
* @return the properties
|
||||
*/
|
||||
@Override
|
||||
public Properties getProperties() {
|
||||
return properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>url</tt>.
|
||||
*
|
||||
* @param url value to be assigned to property url
|
||||
*/
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>host</tt>.
|
||||
*
|
||||
* @param host value to be assigned to property host
|
||||
*/
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>port</tt>.
|
||||
*
|
||||
* @param port value to be assigned to property port
|
||||
*/
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>properties</tt>.
|
||||
*
|
||||
* @param properties value to be assigned to property properties
|
||||
*/
|
||||
public void setProperties(Properties properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
/** @see Object#equals(Object) */
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (!(o instanceof DefaultServerNode)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
DefaultServerNode that = (DefaultServerNode) o;
|
||||
|
||||
if (port != that.port) {
|
||||
return false;
|
||||
}
|
||||
if (url != null ? !url.equals(that.url) : that.url != null) {
|
||||
return false;
|
||||
}
|
||||
if (host != null ? !host.equals(that.host) : that.host != null) {
|
||||
return false;
|
||||
}
|
||||
return properties != null ? properties.equals(that.properties) : that.properties == null;
|
||||
}
|
||||
|
||||
/** @see Object#hashCode() */
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = url != null ? url.hashCode() : 0;
|
||||
result = 31 * result + (host != null ? host.hashCode() : 0);
|
||||
result = 31 * result + port;
|
||||
result = 31 * result + (properties != null ? properties.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
/** @see Object#toString() */
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DefaultServerNode{"
|
||||
+ "url='"
|
||||
+ url
|
||||
+ '\''
|
||||
+ ", host='"
|
||||
+ host
|
||||
+ '\''
|
||||
+ ", port="
|
||||
+ port
|
||||
+ ", properties="
|
||||
+ properties
|
||||
+ '}';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,293 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.provider;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.RegistryClientConfig;
|
||||
import com.alipay.sofa.registry.client.api.Subscriber;
|
||||
import com.alipay.sofa.registry.client.api.SubscriberDataObserver;
|
||||
import com.alipay.sofa.registry.client.api.model.UserData;
|
||||
import com.alipay.sofa.registry.client.api.registration.SubscriberRegistration;
|
||||
import com.alipay.sofa.registry.client.model.SegmentData;
|
||||
import com.alipay.sofa.registry.client.task.TaskEvent;
|
||||
import com.alipay.sofa.registry.client.task.Worker;
|
||||
import com.alipay.sofa.registry.client.util.CommonUtils;
|
||||
import com.alipay.sofa.registry.core.constants.EventTypeConstants;
|
||||
import com.alipay.sofa.registry.core.model.DataBox;
|
||||
import com.alipay.sofa.registry.core.model.ScopeEnum;
|
||||
import com.alipay.sofa.registry.core.model.SubscriberRegister;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* The type Default subscriber multi.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : DefaultSubscriber.java, v 0.1 2017-11-23 22:13 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class DefaultSubscriber extends AbstractInternalRegister implements Subscriber {
|
||||
|
||||
private final String REGIST_ID;
|
||||
private SubscriberDataObserver dataObserver;
|
||||
private ConcurrentHashMap<String, SegmentData> data =
|
||||
new ConcurrentHashMap<String, SegmentData>();
|
||||
private AtomicBoolean init = new AtomicBoolean(false);
|
||||
private RegistryClientConfig config;
|
||||
private SubscriberRegistration registration;
|
||||
private Worker worker;
|
||||
private volatile String localZone;
|
||||
private List<String> availableSegments = new ArrayList<String>();
|
||||
|
||||
/**
|
||||
* Instantiates a new Default subscriber multi.
|
||||
*
|
||||
* @param registration the registration
|
||||
*/
|
||||
DefaultSubscriber(
|
||||
SubscriberRegistration registration, Worker worker, RegistryClientConfig config) {
|
||||
if (null != registration) {
|
||||
this.dataObserver = registration.getSubscriberDataObserver();
|
||||
}
|
||||
this.registration = registration;
|
||||
this.worker = worker;
|
||||
this.config = config;
|
||||
this.REGIST_ID = UUID.randomUUID().toString();
|
||||
this.localZone = config.getZone();
|
||||
this.getPubVersion().incrementAndGet();
|
||||
}
|
||||
|
||||
/** @see Subscriber#getDataObserver() */
|
||||
@Override
|
||||
public SubscriberDataObserver getDataObserver() {
|
||||
return dataObserver;
|
||||
}
|
||||
|
||||
/** @see Subscriber#setDataObserver(SubscriberDataObserver) */
|
||||
@Override
|
||||
public void setDataObserver(SubscriberDataObserver dataObserver) {
|
||||
this.dataObserver = dataObserver;
|
||||
}
|
||||
|
||||
/** @see Subscriber#peekData() */
|
||||
@Override
|
||||
public UserData peekData() {
|
||||
readLock.lock();
|
||||
try {
|
||||
if (!init.get()) {
|
||||
// todo sync read from server
|
||||
return new DefaultUserData();
|
||||
}
|
||||
Set<Entry<String, SegmentData>> values = data.entrySet();
|
||||
DefaultUserData userData = new DefaultUserData();
|
||||
if (null == localZone) {
|
||||
userData.setLocalZone(config.getZone());
|
||||
} else {
|
||||
userData.setLocalZone(localZone);
|
||||
}
|
||||
Map<String, List<String>> zoneMap = new HashMap<String, List<String>>();
|
||||
for (Entry<String, SegmentData> segmentDataEntry : values) {
|
||||
String segment = segmentDataEntry.getKey();
|
||||
|
||||
// only accept available segments, when available segments is empty accept all
|
||||
if (CommonUtils.isNotEmpty(availableSegments) && !availableSegments.contains(segment)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
SegmentData segmentData = segmentDataEntry.getValue();
|
||||
|
||||
if (null == segmentData) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Map<String, List<DataBox>> data = segmentData.getData();
|
||||
for (Entry<String, List<DataBox>> entry : data.entrySet()) {
|
||||
String zone = entry.getKey();
|
||||
List<String> resultList = zoneMap.get(zone);
|
||||
if (null == resultList) {
|
||||
resultList = new ArrayList<String>();
|
||||
zoneMap.put(zone, resultList);
|
||||
}
|
||||
List<DataBox> dataList = entry.getValue();
|
||||
for (DataBox dataBox : dataList) {
|
||||
resultList.add(dataBox.getData());
|
||||
}
|
||||
}
|
||||
}
|
||||
userData.setZoneData(zoneMap);
|
||||
return userData;
|
||||
} finally {
|
||||
readLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets scope enum.
|
||||
*
|
||||
* @return the scope enum
|
||||
*/
|
||||
@Override
|
||||
public ScopeEnum getScopeEnum() {
|
||||
return registration.getScopeEnum();
|
||||
}
|
||||
|
||||
/** Unregister. */
|
||||
@Override
|
||||
public void unregister() {
|
||||
if (isEnabled()) {
|
||||
super.unregister();
|
||||
worker.schedule(new TaskEvent(this));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Assembly subscriber register.
|
||||
*
|
||||
* @return the subscriber register
|
||||
*/
|
||||
@Override
|
||||
public SubscriberRegister assembly() {
|
||||
readLock.lock();
|
||||
SubscriberRegister register;
|
||||
try {
|
||||
if (null == registration.getScopeEnum()) {
|
||||
registration.setScopeEnum(ScopeEnum.zone);
|
||||
}
|
||||
|
||||
register = new SubscriberRegister();
|
||||
register.setRegistId(REGIST_ID);
|
||||
register.setScope(registration.getScopeEnum().name());
|
||||
setAttributes(register, registration, config);
|
||||
// auth signature
|
||||
setAuthSignature(register);
|
||||
|
||||
if (isEnabled()) {
|
||||
register.setEventType(EventTypeConstants.REGISTER);
|
||||
} else {
|
||||
register.setEventType(EventTypeConstants.UNREGISTER);
|
||||
}
|
||||
} finally {
|
||||
readLock.unlock();
|
||||
}
|
||||
return register;
|
||||
}
|
||||
|
||||
public void putReceivedData(SegmentData segmentData, String localZone) {
|
||||
writeLock.lock();
|
||||
try {
|
||||
putSegmentData(segmentData);
|
||||
this.localZone = localZone;
|
||||
} finally {
|
||||
writeLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
private void putSegmentData(SegmentData segmentData) {
|
||||
if (null != segmentData) {
|
||||
|
||||
SegmentData existsData = data.putIfAbsent(segmentData.getSegment(), segmentData);
|
||||
if (null == existsData) {
|
||||
init.compareAndSet(false, true);
|
||||
return;
|
||||
}
|
||||
|
||||
if (existsData.getVersion() < segmentData.getVersion()) {
|
||||
boolean result = data.replace(segmentData.getSegment(), existsData, segmentData);
|
||||
if (!result) {
|
||||
putSegmentData(segmentData);
|
||||
}
|
||||
init.compareAndSet(false, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets data id.
|
||||
*
|
||||
* @return the data id
|
||||
*/
|
||||
@Override
|
||||
public String getDataId() {
|
||||
return registration.getDataId();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets group.
|
||||
*
|
||||
* @return the group
|
||||
*/
|
||||
@Override
|
||||
public String getGroup() {
|
||||
return registration.getGroup();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets regist id.
|
||||
*
|
||||
* @return the regist id
|
||||
*/
|
||||
@Override
|
||||
public String getRegistId() {
|
||||
return REGIST_ID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>availableSegments</tt>.
|
||||
*
|
||||
* @return property value of availableSegments
|
||||
*/
|
||||
public List<String> getAvailableSegments() {
|
||||
readLock.lock();
|
||||
try {
|
||||
return new ArrayList<String>(availableSegments);
|
||||
} finally {
|
||||
readLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>availableSegments</tt>.
|
||||
*
|
||||
* @param availableSegments value to be assigned to property availableSegments
|
||||
*/
|
||||
public void setAvailableSegments(List<String> availableSegments) {
|
||||
writeLock.lock();
|
||||
try {
|
||||
if (null == availableSegments) {
|
||||
this.availableSegments = new ArrayList<String>();
|
||||
} else {
|
||||
this.availableSegments = new ArrayList<String>(availableSegments);
|
||||
}
|
||||
} finally {
|
||||
writeLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isInited() {
|
||||
return init.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DefaultSubscriber{" + "registration=" + registration + '}' + super.toString();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,83 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.provider;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.model.UserData;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id: DefaultUserData.java, v 0.1 2017-11-30 21:01 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class DefaultUserData implements UserData {
|
||||
|
||||
/** zone, List */
|
||||
private Map<String, List<String>> zoneData = new ConcurrentHashMap<String, List<String>>();
|
||||
|
||||
/** The current client`s zone */
|
||||
private String localZone;
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>zoneData</tt>.
|
||||
*
|
||||
* @return property value of zoneData
|
||||
*/
|
||||
@Override
|
||||
public Map<String, List<String>> getZoneData() {
|
||||
return zoneData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>zoneData</tt>.
|
||||
*
|
||||
* @param zoneData value to be assigned to property zoneData
|
||||
*/
|
||||
public void setZoneData(Map<String, List<String>> zoneData) {
|
||||
this.zoneData = zoneData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>localZone</tt>.
|
||||
*
|
||||
* @return property value of localZone
|
||||
*/
|
||||
@Override
|
||||
public String getLocalZone() {
|
||||
return localZone;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>localZone</tt>.
|
||||
*
|
||||
* @param localZone value to be assigned to property localZone
|
||||
*/
|
||||
public void setLocalZone(String localZone) {
|
||||
this.localZone = localZone;
|
||||
}
|
||||
|
||||
/**
|
||||
* To string string.
|
||||
*
|
||||
* @return the string
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DefaultUserData{" + "zoneData=" + zoneData + ", localZone='" + localZone + '\'' + '}';
|
||||
}
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.provider;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.RegistryClientConfig;
|
||||
import com.alipay.sofa.registry.client.remoting.ServerManager;
|
||||
import com.alipay.sofa.registry.client.remoting.ServerNode;
|
||||
import com.alipay.sofa.registry.client.util.ServerNodeParser;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The type Direct server manager. Return server node RegistryClientConfig.getRegistryEndpoint()+
|
||||
* PORT 9600
|
||||
*
|
||||
* @author zhiqiang.li
|
||||
* @version $Id : DirectServerManager.java, v 0.1 2022-03-18 13:37 zhiqiang.li Exp $$
|
||||
*/
|
||||
public class DirectServerManager implements ServerManager {
|
||||
|
||||
private final List<ServerNode> serverNodes;
|
||||
|
||||
public DirectServerManager(RegistryClientConfig config) {
|
||||
this.serverNodes = new ArrayList<ServerNode>();
|
||||
int port = config.getRegistryEndpointPort();
|
||||
if (port == 0) {
|
||||
port = 9600;
|
||||
}
|
||||
this.serverNodes.add(
|
||||
ServerNodeParser.parse(String.format("%s:%s", config.getRegistryEndpoint(), port)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ServerNode> getServerList() {
|
||||
return serverNodes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServerNode random() {
|
||||
return serverNodes.get(0);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,151 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.provider;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.Configurator;
|
||||
import com.alipay.sofa.registry.client.api.Publisher;
|
||||
import com.alipay.sofa.registry.client.api.Subscriber;
|
||||
import com.alipay.sofa.registry.client.util.StringUtils;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* The type Register cache.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : RegisterCache.java, v 0.1 2017-11-30 18:18 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class RegisterCache {
|
||||
|
||||
/** publisher register cache Map<registId,Publisher> */
|
||||
private Map<String, Publisher> publisherMap = new ConcurrentHashMap<String, Publisher>();
|
||||
/** subscriber register cache Map<registId,Register> */
|
||||
private Map<String, Subscriber> subscriberMap = new ConcurrentHashMap<String, Subscriber>();
|
||||
/** configurator register cache Map<registId,Register> */
|
||||
private Map<String, Configurator> configuratorMap = new ConcurrentHashMap<String, Configurator>();
|
||||
|
||||
/**
|
||||
* Add register.
|
||||
*
|
||||
* @param publisher the publisher
|
||||
*/
|
||||
public void addRegister(Publisher publisher) {
|
||||
if (null == publisher || StringUtils.isEmpty(publisher.getDataId())) {
|
||||
return;
|
||||
}
|
||||
|
||||
publisherMap.put(publisher.getRegistId(), publisher);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add register.
|
||||
*
|
||||
* @param subscriber the subscriber
|
||||
*/
|
||||
public void addRegister(Subscriber subscriber) {
|
||||
if (null == subscriber || StringUtils.isEmpty(subscriber.getDataId())) {
|
||||
return;
|
||||
}
|
||||
|
||||
subscriberMap.put(subscriber.getRegistId(), subscriber);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add register.
|
||||
*
|
||||
* @param configurator the configurator
|
||||
*/
|
||||
public void addRegister(Configurator configurator) {
|
||||
if (null == configurator || StringUtils.isEmpty(configurator.getDataId())) {
|
||||
return;
|
||||
}
|
||||
|
||||
configuratorMap.put(configurator.getRegistId(), configurator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove.
|
||||
*
|
||||
* @param registId the regist id
|
||||
*/
|
||||
public void remove(String registId) {
|
||||
if (publisherMap.remove(registId) == null) {
|
||||
if (subscriberMap.remove(registId) == null) {
|
||||
configuratorMap.remove(registId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets publisher by regist id.
|
||||
*
|
||||
* @param registId the regist id
|
||||
* @return the publisher by regist id
|
||||
*/
|
||||
public Publisher getPublisherByRegistId(String registId) {
|
||||
return publisherMap.get(registId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets subscriber by regist id.
|
||||
*
|
||||
* @param registId the regist id
|
||||
* @return the subscriber by regist id
|
||||
*/
|
||||
public Subscriber getSubscriberByRegistId(String registId) {
|
||||
return subscriberMap.get(registId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets configurator by data id.
|
||||
*
|
||||
* @param registId the regist id
|
||||
* @return the configurator by data id
|
||||
*/
|
||||
public Configurator getConfiguratorByRegistId(String registId) {
|
||||
return configuratorMap.get(registId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all publishers.
|
||||
*
|
||||
* @return the all publishers
|
||||
*/
|
||||
public Collection<Publisher> getAllPublishers() {
|
||||
return new ArrayList<Publisher>(publisherMap.values());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all subscribers.
|
||||
*
|
||||
* @return the all subscribers
|
||||
*/
|
||||
public Collection<Subscriber> getAllSubscribers() {
|
||||
return new ArrayList<Subscriber>(subscriberMap.values());
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all configurator.
|
||||
*
|
||||
* @return the all configurator
|
||||
*/
|
||||
public Collection<Configurator> getAllConfigurator() {
|
||||
return new ArrayList<Configurator>(configuratorMap.values());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.remoting;
|
||||
|
||||
import com.alipay.remoting.exception.RemotingException;
|
||||
|
||||
/**
|
||||
* The interface Client.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : Client.java, v 0.1 2018-03-06 20:43 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public interface Client {
|
||||
|
||||
/** Init. */
|
||||
void init();
|
||||
|
||||
/**
|
||||
* Is connected boolean.
|
||||
*
|
||||
* @return the boolean
|
||||
*/
|
||||
boolean isConnected();
|
||||
|
||||
/**
|
||||
* Ensure connected.
|
||||
*
|
||||
* @throws InterruptedException the interrupted exception
|
||||
*/
|
||||
void ensureConnected() throws InterruptedException;
|
||||
|
||||
/**
|
||||
* Invoke sync object.
|
||||
*
|
||||
* @param request the request
|
||||
* @return the object
|
||||
* @throws RemotingException the remoting exception
|
||||
* @throws InterruptedException the interrupted exception
|
||||
*/
|
||||
Object invokeSync(Object request) throws RemotingException, InterruptedException;
|
||||
}
|
|
@ -0,0 +1,299 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.remoting;
|
||||
|
||||
import com.alipay.remoting.Connection;
|
||||
import com.alipay.remoting.ConnectionEventProcessor;
|
||||
import com.alipay.remoting.ConnectionEventType;
|
||||
import com.alipay.remoting.exception.RemotingException;
|
||||
import com.alipay.remoting.rpc.RpcClient;
|
||||
import com.alipay.remoting.rpc.protocol.UserProcessor;
|
||||
import com.alipay.sofa.registry.client.api.Configurator;
|
||||
import com.alipay.sofa.registry.client.api.Publisher;
|
||||
import com.alipay.sofa.registry.client.api.RegistryClientConfig;
|
||||
import com.alipay.sofa.registry.client.api.Subscriber;
|
||||
import com.alipay.sofa.registry.client.log.LoggerFactory;
|
||||
import com.alipay.sofa.registry.client.provider.RegisterCache;
|
||||
import com.alipay.sofa.registry.client.task.TaskEvent;
|
||||
import com.alipay.sofa.registry.client.task.Worker;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Random;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
/**
|
||||
* The type Client connection.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : ClientConnection.java, v 0.1 2018-03-01 16:44 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class ClientConnection implements Client {
|
||||
/** LOGGER */
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ClientConnection.class);
|
||||
/** The Reconnecting delay. */
|
||||
private static final int RECONNECTING_DELAY = 5000;
|
||||
|
||||
private RpcClient client;
|
||||
private ServerManager serverManager;
|
||||
private List<UserProcessor> userProcessorList;
|
||||
private Map<ConnectionEventType, ConnectionEventProcessor> connectionEventProcessorMap;
|
||||
private RegistryClientConfig config;
|
||||
private Connection clientConnection;
|
||||
private RegisterCache registerCache;
|
||||
private Worker worker;
|
||||
|
||||
/**
|
||||
* Instantiates a new Client connection.
|
||||
*
|
||||
* @param serverManager the server manager
|
||||
* @param userProcessorList the user processor list
|
||||
* @param connectionEventProcessorMap the connection event processor map
|
||||
* @param config the config
|
||||
*/
|
||||
public ClientConnection(
|
||||
ServerManager serverManager,
|
||||
List<UserProcessor> userProcessorList,
|
||||
Map<ConnectionEventType, ConnectionEventProcessor> connectionEventProcessorMap,
|
||||
RegisterCache registerCache,
|
||||
RegistryClientConfig config) {
|
||||
this.client = new RpcClient();
|
||||
this.serverManager = serverManager;
|
||||
this.userProcessorList = userProcessorList;
|
||||
this.connectionEventProcessorMap = connectionEventProcessorMap;
|
||||
this.registerCache = registerCache;
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
/** Init. */
|
||||
@Override
|
||||
public void init() {
|
||||
for (UserProcessor userProcessor : userProcessorList) {
|
||||
client.registerUserProcessor(userProcessor);
|
||||
}
|
||||
|
||||
if (null != connectionEventProcessorMap) {
|
||||
for (Entry<ConnectionEventType, ConnectionEventProcessor> entry :
|
||||
connectionEventProcessorMap.entrySet()) {
|
||||
client.addConnectionEventProcessor(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
client.init();
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure connected.
|
||||
*
|
||||
* @throws InterruptedException the interrupted exception
|
||||
*/
|
||||
@Override
|
||||
public void ensureConnected() throws InterruptedException {
|
||||
if (isConnected()) {
|
||||
return;
|
||||
}
|
||||
while (!connect()) {
|
||||
Thread.sleep(ClientConnection.RECONNECTING_DELAY);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect boolean.
|
||||
*
|
||||
* @return the boolean
|
||||
*/
|
||||
private boolean connect() {
|
||||
Random random = new Random();
|
||||
Connection connection = null;
|
||||
List<ServerNode> serverNodes = new ArrayList<ServerNode>(serverManager.getServerList());
|
||||
// shuffle server list to make server connections as discrete as possible
|
||||
Collections.shuffle(serverNodes);
|
||||
for (ServerNode serverNode : serverNodes) {
|
||||
try {
|
||||
connection = connect(serverNode);
|
||||
if (null != connection && connection.isFine()) {
|
||||
resetRegister();
|
||||
LOGGER.info("[Connect] Successfully connected to server: {}", serverNode);
|
||||
break;
|
||||
} else {
|
||||
recycle(connection);
|
||||
}
|
||||
|
||||
Thread.sleep(random.nextInt(RECONNECTING_DELAY));
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("[Connect] Failed trying connect to {}", serverNode, e);
|
||||
}
|
||||
}
|
||||
|
||||
if (null != connection && connection.isFine()) {
|
||||
clientConnection = connection;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoke sync object.
|
||||
*
|
||||
* @param request the request
|
||||
* @return the object
|
||||
* @throws RemotingException the remoting exception
|
||||
* @throws InterruptedException the interrupted exception
|
||||
*/
|
||||
@Override
|
||||
public Object invokeSync(Object request) throws RemotingException, InterruptedException {
|
||||
if (!isConnected()) {
|
||||
throw new IllegalStateException("Not connected");
|
||||
}
|
||||
|
||||
return client.invokeSync(clientConnection, request, config.getInvokeTimeout());
|
||||
}
|
||||
|
||||
private void recycle(Connection connection) {
|
||||
if (null == connection) {
|
||||
return;
|
||||
}
|
||||
|
||||
client.closeConnection(connection.getUrl());
|
||||
}
|
||||
|
||||
private Connection connect(ServerNode serverNode) {
|
||||
Connection connection = null;
|
||||
try {
|
||||
connection = client.getConnection(serverNode.getUrl(), config.getConnectTimeout());
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("[connection] Create connection error, {}", serverNode, e);
|
||||
}
|
||||
return connection;
|
||||
}
|
||||
|
||||
private void resetRegister() {
|
||||
try {
|
||||
List<TaskEvent> eventList = new ArrayList<TaskEvent>();
|
||||
|
||||
Collection<Publisher> publishers = registerCache.getAllPublishers();
|
||||
for (Publisher publisher : publishers) {
|
||||
try {
|
||||
publisher.reset();
|
||||
eventList.add(new TaskEvent(publisher));
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("[connection] Publisher reset error, {}", publisher, e);
|
||||
}
|
||||
}
|
||||
|
||||
Collection<Subscriber> subscribers = registerCache.getAllSubscribers();
|
||||
for (Subscriber subscriber : subscribers) {
|
||||
try {
|
||||
subscriber.reset();
|
||||
eventList.add(new TaskEvent(subscriber));
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("[connection] Subscriber reset error, {}", subscriber, e);
|
||||
}
|
||||
}
|
||||
|
||||
Collection<Configurator> configurators = registerCache.getAllConfigurator();
|
||||
for (Configurator configurator : configurators) {
|
||||
try {
|
||||
configurator.reset();
|
||||
eventList.add(new TaskEvent(configurator));
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("[connection] Configurator reset error, {}", configurator, e);
|
||||
}
|
||||
}
|
||||
|
||||
worker.schedule(eventList);
|
||||
LOGGER.info(
|
||||
"[reset] {} publishers and {} subscribers has been reset",
|
||||
publishers.size(),
|
||||
subscribers.size());
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("[reset] Reset register after reconnect error", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets remote address.
|
||||
*
|
||||
* @return the remote address
|
||||
*/
|
||||
public String getRemoteAddress() {
|
||||
if (null != clientConnection) {
|
||||
return clientConnection.getRemoteIP();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is connected boolean.
|
||||
*
|
||||
* @return boolean boolean
|
||||
*/
|
||||
@Override
|
||||
public boolean isConnected() {
|
||||
return clientConnection != null && clientConnection.isFine();
|
||||
}
|
||||
|
||||
/** Destroy. */
|
||||
public void destroy() {
|
||||
if (null != clientConnection) {
|
||||
clientConnection.close();
|
||||
}
|
||||
if (null != client) {
|
||||
client.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>serverManager</tt>.
|
||||
*
|
||||
* @param serverManager value to be assigned to property serverManager
|
||||
*/
|
||||
public void setServerManager(ServerManager serverManager) {
|
||||
this.serverManager = serverManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>userProcessorList</tt>.
|
||||
*
|
||||
* @param userProcessorList value to be assigned to property userProcessorList
|
||||
*/
|
||||
public void setUserProcessorList(List<UserProcessor> userProcessorList) {
|
||||
this.userProcessorList = userProcessorList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>connectionEventProcessorMap</tt>.
|
||||
*
|
||||
* @param connectionEventProcessorMap value to be assigned to property connectionEventProcessorMap
|
||||
*/
|
||||
public void setConnectionEventProcessorMap(
|
||||
Map<ConnectionEventType, ConnectionEventProcessor> connectionEventProcessorMap) {
|
||||
this.connectionEventProcessorMap = connectionEventProcessorMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>worker</tt>.
|
||||
*
|
||||
* @param worker value to be assigned to property worker
|
||||
*/
|
||||
public void setWorker(Worker worker) {
|
||||
this.worker = worker;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.remoting;
|
||||
|
||||
import com.alipay.remoting.Connection;
|
||||
import com.alipay.remoting.ConnectionEventProcessor;
|
||||
import com.alipay.sofa.registry.client.log.LoggerFactory;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
/**
|
||||
* The type Client connection close event processor.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : ClientConnectionCloseEventProcessor.java, v 0.1 2018-02-26 20:28 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class ClientConnectionCloseEventProcessor implements ConnectionEventProcessor {
|
||||
private static final Logger LOGGER =
|
||||
LoggerFactory.getLogger(ClientConnectionCloseEventProcessor.class);
|
||||
|
||||
/**
|
||||
* On event.
|
||||
*
|
||||
* @param remoteAddr the remote addr
|
||||
* @param conn the conn
|
||||
*/
|
||||
@Override
|
||||
public void onEvent(String remoteAddr, Connection conn) {
|
||||
if (null != conn) {
|
||||
LOGGER.info(
|
||||
"[connection] Client disconnected, remote address: {}, localAddress: {}",
|
||||
remoteAddr,
|
||||
conn.getLocalAddress());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.remoting;
|
||||
|
||||
import com.alipay.remoting.Connection;
|
||||
import com.alipay.remoting.ConnectionEventProcessor;
|
||||
import com.alipay.sofa.registry.client.log.LoggerFactory;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
/**
|
||||
* The type Client connection close event processor.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : ClientConnectionCloseEventProcessor.java, v 0.1 2018-02-26 20:28 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class ClientConnectionOpenEventProcessor implements ConnectionEventProcessor {
|
||||
private static final Logger LOGGER =
|
||||
LoggerFactory.getLogger(ClientConnectionOpenEventProcessor.class);
|
||||
|
||||
/**
|
||||
* On event.
|
||||
*
|
||||
* @param remoteAddr the remote addr
|
||||
* @param conn the conn
|
||||
*/
|
||||
@Override
|
||||
public void onEvent(String remoteAddr, Connection conn) {
|
||||
if (null != conn) {
|
||||
LOGGER.info(
|
||||
"[connection] Client connected, remote address: {}, localAddress: {}",
|
||||
remoteAddr,
|
||||
conn.getLocalAddress());
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,117 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.remoting;
|
||||
|
||||
import com.alipay.remoting.BizContext;
|
||||
import com.alipay.remoting.rpc.protocol.SyncUserProcessor;
|
||||
import com.alipay.sofa.registry.client.api.Configurator;
|
||||
import com.alipay.sofa.registry.client.log.LoggerFactory;
|
||||
import com.alipay.sofa.registry.client.model.ConfiguratorData;
|
||||
import com.alipay.sofa.registry.client.provider.DefaultConfigurator;
|
||||
import com.alipay.sofa.registry.client.provider.RegisterCache;
|
||||
import com.alipay.sofa.registry.client.task.ObserverHandler;
|
||||
import com.alipay.sofa.registry.core.model.ReceivedConfigData;
|
||||
import com.alipay.sofa.registry.core.model.Result;
|
||||
import java.util.List;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
/**
|
||||
* The type Received config data processor.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : ReceivedConfigDataProcessor.java, v 0.1 2018-04-18 15:40 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class ReceivedConfigDataProcessor extends SyncUserProcessor<ReceivedConfigData> {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ReceivedConfigDataProcessor.class);
|
||||
|
||||
private RegisterCache registerCache;
|
||||
|
||||
private ObserverHandler observerHandler;
|
||||
|
||||
/**
|
||||
* Instantiates a new Received config data processor.
|
||||
*
|
||||
* @param registerCache the register cache
|
||||
* @param observerHandler the observer handler
|
||||
*/
|
||||
public ReceivedConfigDataProcessor(RegisterCache registerCache, ObserverHandler observerHandler) {
|
||||
this.registerCache = registerCache;
|
||||
this.observerHandler = observerHandler;
|
||||
}
|
||||
|
||||
/** @see com.alipay.remoting.rpc.protocol.UserProcessor#handleRequest(BizContext, Object) */
|
||||
@Override
|
||||
public Object handleRequest(BizContext bizCtx, ReceivedConfigData request) {
|
||||
Result result = new Result();
|
||||
result.setSuccess(true);
|
||||
if (null == request) {
|
||||
return result;
|
||||
}
|
||||
|
||||
List<String> registIds = request.getConfiguratorRegistIds();
|
||||
try {
|
||||
|
||||
ConfiguratorData configuratorData = new ConfiguratorData();
|
||||
configuratorData.setDataBox(request.getDataBox());
|
||||
configuratorData.setVersion(request.getVersion());
|
||||
for (String registId : registIds) {
|
||||
Configurator configurator = registerCache.getConfiguratorByRegistId(registId);
|
||||
if (null == configurator) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (configurator instanceof DefaultConfigurator) {
|
||||
((DefaultConfigurator) configurator).putConfiguratorData(configuratorData);
|
||||
try {
|
||||
observerHandler.notify(configurator);
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(
|
||||
"[received] add configurator notify task error, dataId: {}, registId: {}",
|
||||
configurator.getDataId(),
|
||||
configurator.getRegistId(),
|
||||
e);
|
||||
}
|
||||
} else {
|
||||
LOGGER.warn(
|
||||
"[received] ignore unknown configurator type: {}", configurator.getClass().getName());
|
||||
}
|
||||
}
|
||||
LOGGER.info(
|
||||
"[received] receive configurator data save success, dataId: {} version: {} data:{} registIds:{}",
|
||||
request.getDataId(),
|
||||
request.getVersion(),
|
||||
request.getDataBox(),
|
||||
registIds);
|
||||
} catch (Exception e) {
|
||||
result.setSuccess(false);
|
||||
result.setMessage("");
|
||||
LOGGER.info(
|
||||
"[received] receive configurator data save failed, dataId: {} version: {} data:{}",
|
||||
request.getDataId(),
|
||||
request.getVersion(),
|
||||
request.getDataBox(),
|
||||
e);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/** @see com.alipay.remoting.rpc.protocol.UserProcessor#interest() */
|
||||
@Override
|
||||
public String interest() {
|
||||
return ReceivedConfigData.class.getName();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,131 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.remoting;
|
||||
|
||||
import com.alipay.remoting.BizContext;
|
||||
import com.alipay.remoting.rpc.protocol.SyncUserProcessor;
|
||||
import com.alipay.sofa.registry.client.api.Subscriber;
|
||||
import com.alipay.sofa.registry.client.log.LoggerFactory;
|
||||
import com.alipay.sofa.registry.client.model.SegmentData;
|
||||
import com.alipay.sofa.registry.client.provider.DefaultSubscriber;
|
||||
import com.alipay.sofa.registry.client.provider.RegisterCache;
|
||||
import com.alipay.sofa.registry.client.task.ObserverHandler;
|
||||
import com.alipay.sofa.registry.core.model.ReceivedData;
|
||||
import com.alipay.sofa.registry.core.model.Result;
|
||||
import java.util.List;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
/**
|
||||
* The type Received data multi processor.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : ReceivedDataProcessor.java, v 0.1 2018-02-27 14:54 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class ReceivedDataProcessor extends SyncUserProcessor<ReceivedData> {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(ReceivedDataProcessor.class);
|
||||
|
||||
private RegisterCache registerCache;
|
||||
|
||||
private ObserverHandler observerHandler;
|
||||
|
||||
/**
|
||||
* Instantiates a new Received data multi processor.
|
||||
*
|
||||
* @param registerCache the register cache
|
||||
*/
|
||||
public ReceivedDataProcessor(RegisterCache registerCache, ObserverHandler observerHandler) {
|
||||
this.registerCache = registerCache;
|
||||
this.observerHandler = observerHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle request object.
|
||||
*
|
||||
* @param bizCtx the biz ctx
|
||||
* @param request the request
|
||||
* @return the object
|
||||
*/
|
||||
@Override
|
||||
public Object handleRequest(BizContext bizCtx, ReceivedData request) {
|
||||
Result result = new Result();
|
||||
result.setSuccess(true);
|
||||
if (null == request || null == request.getData()) {
|
||||
return result;
|
||||
}
|
||||
try {
|
||||
List<String> registIds = request.getSubscriberRegistIds();
|
||||
|
||||
SegmentData segmentData = new SegmentData();
|
||||
segmentData.setData(request.getData());
|
||||
segmentData.setVersion(request.getVersion());
|
||||
segmentData.setSegment(request.getSegment());
|
||||
|
||||
for (String registId : registIds) {
|
||||
Subscriber subscriber = registerCache.getSubscriberByRegistId(registId);
|
||||
if (null == subscriber) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (subscriber instanceof DefaultSubscriber) {
|
||||
DefaultSubscriber defaultSubscriber = (DefaultSubscriber) subscriber;
|
||||
defaultSubscriber.putReceivedData(segmentData, request.getLocalZone());
|
||||
try {
|
||||
observerHandler.notify(subscriber);
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(
|
||||
"[received] add notify task error, dataId: {}, registId: {}",
|
||||
subscriber.getDataId(),
|
||||
subscriber.getRegistId(),
|
||||
e);
|
||||
}
|
||||
} else {
|
||||
LOGGER.warn(
|
||||
"[received] ignore unknown subscriber type: {}", subscriber.getClass().getName());
|
||||
}
|
||||
}
|
||||
|
||||
LOGGER.info(
|
||||
"[received] receive subscriber data save success, dataId: {} group: {} version: {} data:{} registIds:{}",
|
||||
request.getDataId(),
|
||||
request.getGroup(),
|
||||
request.getVersion(),
|
||||
request.getData(),
|
||||
registIds);
|
||||
} catch (Exception e) {
|
||||
result.setSuccess(false);
|
||||
result.setMessage("");
|
||||
LOGGER.info(
|
||||
"[received] receive subscriber data save failed, dataId: {} group: {} version: {} data:{}",
|
||||
request.getDataId(),
|
||||
request.getGroup(),
|
||||
request.getVersion(),
|
||||
request.getData(),
|
||||
e);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interest string.
|
||||
*
|
||||
* @return the string
|
||||
*/
|
||||
@Override
|
||||
public String interest() {
|
||||
return ReceivedData.class.getName();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.remoting;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The interface Server manager.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : ServerManager.java, v 0.1 2017-12-25 11:42 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public interface ServerManager {
|
||||
|
||||
/**
|
||||
* Gets server list.
|
||||
*
|
||||
* @return the server list
|
||||
*/
|
||||
List<ServerNode> getServerList();
|
||||
|
||||
/**
|
||||
* Random url.
|
||||
*
|
||||
* @return the url
|
||||
*/
|
||||
ServerNode random();
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.remoting;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* The interface Server node.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : ServerNode.java, v 0.1 2018-03-01 17:07 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public interface ServerNode {
|
||||
|
||||
/**
|
||||
* Gets host.
|
||||
*
|
||||
* @return the host
|
||||
*/
|
||||
String getHost();
|
||||
|
||||
/**
|
||||
* Gets port.
|
||||
*
|
||||
* @return the port
|
||||
*/
|
||||
int getPort();
|
||||
|
||||
/**
|
||||
* Gets properties.
|
||||
*
|
||||
* @return the properties
|
||||
*/
|
||||
Properties getProperties();
|
||||
|
||||
/**
|
||||
* Gets url.
|
||||
*
|
||||
* @return the url
|
||||
*/
|
||||
String getUrl();
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.task;
|
||||
|
||||
import com.alipay.sofa.registry.client.remoting.Client;
|
||||
|
||||
/**
|
||||
* The type Abstract worker thread.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : AbstractWorkerThread.java, v 0.1 2018-03-01 11:54 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public abstract class AbstractWorkerThread extends Thread implements Worker {
|
||||
|
||||
/** */
|
||||
private final Object bell = new Object();
|
||||
|
||||
/** The Client. */
|
||||
protected Client client;
|
||||
|
||||
/**
|
||||
* Instantiates a new Abstract worker thread.
|
||||
*
|
||||
* @param client the client
|
||||
*/
|
||||
public AbstractWorkerThread(Client client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
/** Notify execute signal for task thread. */
|
||||
void signal() {
|
||||
synchronized (bell) {
|
||||
bell.notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Await for next task.
|
||||
*
|
||||
* @param timeout wait timeout
|
||||
* @throws InterruptedException thread interrupted
|
||||
*/
|
||||
void await(long timeout) throws InterruptedException {
|
||||
synchronized (bell) {
|
||||
bell.wait(timeout);
|
||||
}
|
||||
}
|
||||
|
||||
/** @see Runnable#run() */
|
||||
@Override
|
||||
public void run() {
|
||||
handle();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.task;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.Configurator;
|
||||
import com.alipay.sofa.registry.client.api.Subscriber;
|
||||
|
||||
/**
|
||||
* The interface Observer handler.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : ObserverHandler.java, v 0.1 2018-03-15 12:15 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public interface ObserverHandler {
|
||||
|
||||
/**
|
||||
* Notify.
|
||||
*
|
||||
* @param subscriber the subscriber
|
||||
*/
|
||||
void notify(Subscriber subscriber);
|
||||
|
||||
/**
|
||||
* Notify.
|
||||
*
|
||||
* @param configurator the configurator
|
||||
*/
|
||||
void notify(Configurator configurator);
|
||||
}
|
|
@ -0,0 +1,141 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.task;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.RegistryClientConfig;
|
||||
import com.alipay.sofa.registry.client.api.Subscriber;
|
||||
import com.alipay.sofa.registry.client.log.LoggerFactory;
|
||||
import com.alipay.sofa.registry.client.provider.DefaultSubscriber;
|
||||
import com.alipay.sofa.registry.client.provider.RegisterCache;
|
||||
import com.alipay.sofa.registry.client.remoting.Client;
|
||||
import com.alipay.sofa.registry.core.model.SyncConfigRequest;
|
||||
import com.alipay.sofa.registry.core.model.SyncConfigResponse;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
/**
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id: SyncConfigThread.java, v 0.1 2018-03-14 23:36 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class SyncConfigThread extends Thread {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(SyncConfigThread.class);
|
||||
|
||||
private Client client;
|
||||
|
||||
private RegisterCache registerCache;
|
||||
|
||||
private RegistryClientConfig config;
|
||||
|
||||
private ObserverHandler observerHandler;
|
||||
|
||||
public SyncConfigThread(
|
||||
Client client,
|
||||
RegisterCache registerCache,
|
||||
RegistryClientConfig config,
|
||||
ObserverHandler observerHandler) {
|
||||
super("SyncConfigThread");
|
||||
this.setDaemon(true);
|
||||
this.client = client;
|
||||
this.registerCache = registerCache;
|
||||
this.config = config;
|
||||
this.observerHandler = observerHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
int retryInterval = config.getSyncConfigRetryInterval();
|
||||
//noinspection InfiniteLoopStatement
|
||||
while (true) {
|
||||
try {
|
||||
Thread.sleep(retryInterval);
|
||||
|
||||
if (!client.isConnected()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
SyncConfigRequest request = new SyncConfigRequest();
|
||||
request.setDataCenter(config.getDataCenter());
|
||||
request.setZone(config.getZone());
|
||||
Object result = client.invokeSync(request);
|
||||
|
||||
if (!(result instanceof SyncConfigResponse)) {
|
||||
LOGGER.warn("[syncConfig] unknown response type, {}", result);
|
||||
continue;
|
||||
}
|
||||
|
||||
SyncConfigResponse response = (SyncConfigResponse) result;
|
||||
if (!response.isSuccess()) {
|
||||
LOGGER.warn("[syncConfig] request failed, {}", response);
|
||||
continue;
|
||||
}
|
||||
|
||||
int interval = response.getRetryInterval();
|
||||
retryInterval = Math.max(retryInterval, interval);
|
||||
|
||||
List<String> availableSegments = response.getAvailableSegments();
|
||||
|
||||
Collection<Subscriber> allSubscribers = registerCache.getAllSubscribers();
|
||||
for (Subscriber subscriber : allSubscribers) {
|
||||
try {
|
||||
if (!(subscriber instanceof DefaultSubscriber)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
DefaultSubscriber defaultSubscriber = (DefaultSubscriber) subscriber;
|
||||
|
||||
if (!defaultSubscriber.isInited()) {
|
||||
LOGGER.info(
|
||||
"[syncConfig] DefaultSubscriber not init, {}", defaultSubscriber.getRegistId());
|
||||
continue;
|
||||
}
|
||||
List<String> nowAvailableSegments = defaultSubscriber.getAvailableSegments();
|
||||
|
||||
if (isEqualCollections(availableSegments, nowAvailableSegments)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
defaultSubscriber.setAvailableSegments(availableSegments);
|
||||
|
||||
observerHandler.notify(defaultSubscriber);
|
||||
} catch (Exception e) {
|
||||
LOGGER.error(
|
||||
"[syncConfig] try notify subscriber error, registId: {}, availableSegments: {}",
|
||||
subscriber.getRegistId(),
|
||||
availableSegments,
|
||||
e);
|
||||
}
|
||||
}
|
||||
} catch (Throwable e) {
|
||||
LOGGER.error("[syncConfig] sync config error, retryInterval: {}", retryInterval, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isEqualCollections(Collection<String> a, Collection<String> b) {
|
||||
if (null == a) {
|
||||
a = new ArrayList<String>();
|
||||
}
|
||||
|
||||
if (null == b) {
|
||||
b = new ArrayList<String>();
|
||||
}
|
||||
|
||||
return a.size() == b.size() && a.equals(b);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,150 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.task;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.Register;
|
||||
|
||||
/**
|
||||
* The type Task event.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : TaskEvent.java, v 0.1 2018-02-28 20:33 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class TaskEvent implements Comparable<TaskEvent> {
|
||||
private static final int MAX_DELAY_TIME = 1000;
|
||||
|
||||
private Register source;
|
||||
|
||||
private int sendCount;
|
||||
|
||||
private long triggerTime;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param source the source
|
||||
*/
|
||||
public TaskEvent(Register source) {
|
||||
this.source = source;
|
||||
this.triggerTime = System.currentTimeMillis();
|
||||
this.sendCount = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>source</tt>.
|
||||
*
|
||||
* @return property value of source
|
||||
*/
|
||||
public Register getSource() {
|
||||
return source;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>triggerTime</tt>.
|
||||
*
|
||||
* @param triggerTime value to be assigned to property triggerTime
|
||||
*/
|
||||
public void setTriggerTime(long triggerTime) {
|
||||
this.triggerTime = triggerTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delay time long.
|
||||
*
|
||||
* @return long long
|
||||
*/
|
||||
public long delayTime() {
|
||||
int time = sendCount * 200;
|
||||
if (time > MAX_DELAY_TIME) {
|
||||
time = MAX_DELAY_TIME;
|
||||
}
|
||||
return time - (System.currentTimeMillis() - this.triggerTime);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare to int.
|
||||
*
|
||||
* @param event the event
|
||||
* @return the int
|
||||
*/
|
||||
@Override
|
||||
public int compareTo(TaskEvent event) {
|
||||
if (this.sendCount > event.sendCount) {
|
||||
return 1;
|
||||
} else if (this.sendCount < event.sendCount) {
|
||||
return -1;
|
||||
}
|
||||
Register register1 = source;
|
||||
Register register2 = event.getSource();
|
||||
|
||||
if (register1 == null) {
|
||||
return register2 != null ? -1 : 0;
|
||||
} else {
|
||||
if (register2 == null) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
long t1 = register1.getTimestamp();
|
||||
long t2 = register2.getTimestamp();
|
||||
if (t1 > t2) {
|
||||
return 1;
|
||||
} else if (t1 < t2) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** @see Object#equals(Object) */
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (!(o instanceof TaskEvent)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
TaskEvent taskEvent = (TaskEvent) o;
|
||||
|
||||
if (sendCount != taskEvent.sendCount) {
|
||||
return false;
|
||||
}
|
||||
if (triggerTime != taskEvent.triggerTime) {
|
||||
return false;
|
||||
}
|
||||
return source != null ? source.equals(taskEvent.source) : taskEvent.source == null;
|
||||
}
|
||||
|
||||
/** @see Object#hashCode() */
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = source != null ? source.hashCode() : 0;
|
||||
result = 31 * result + sendCount;
|
||||
result = 31 * result + (int) (triggerTime ^ (triggerTime >>> 32));
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inc send count int.
|
||||
*
|
||||
* @return the int
|
||||
*/
|
||||
public int incSendCount() {
|
||||
return this.sendCount++;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.task;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.Register;
|
||||
import com.alipay.sofa.registry.client.provider.AbstractInternalRegister;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
/**
|
||||
* The type Task queue.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : TaskQueue.java, v 0.1 2018-02-28 20:44 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class TaskQueue implements Iterable<TaskEvent> {
|
||||
|
||||
private final ConcurrentMap<String, TaskEvent> taskMap =
|
||||
new ConcurrentHashMap<String, TaskEvent>();
|
||||
|
||||
/**
|
||||
* Iterator iterator.
|
||||
*
|
||||
* @return the iterator
|
||||
*/
|
||||
@Override
|
||||
public Iterator<TaskEvent> iterator() {
|
||||
List<TaskEvent> taskList = new ArrayList<TaskEvent>(taskMap.values());
|
||||
Collections.sort(taskList);
|
||||
return taskList.iterator();
|
||||
}
|
||||
|
||||
/** Delete the completed task, return task queue size. */
|
||||
public void cleanCompletedTasks() {
|
||||
List<String> taskList = new ArrayList<String>(taskMap.keySet());
|
||||
for (String key : taskList) {
|
||||
TaskEvent event = taskMap.get(key);
|
||||
AbstractInternalRegister r = (AbstractInternalRegister) event.getSource();
|
||||
if (r.isDone()) {
|
||||
taskMap.remove(key, event);
|
||||
}
|
||||
}
|
||||
taskMap.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add task event to task queue.
|
||||
*
|
||||
* @param event task event
|
||||
*/
|
||||
public void put(TaskEvent event) {
|
||||
Register register = event.getSource();
|
||||
String key = register.getRegistId();
|
||||
taskMap.put(key, event);
|
||||
}
|
||||
|
||||
/**
|
||||
* Put all.
|
||||
*
|
||||
* @param taskEvents the task events
|
||||
*/
|
||||
public void putAll(List<TaskEvent> taskEvents) {
|
||||
for (TaskEvent event : taskEvents) {
|
||||
put(event);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Is empty boolean.
|
||||
*
|
||||
* @return the boolean
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return taskMap.isEmpty();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.task;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* The interface Worker.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : Worker.java, v 0.1 2018-02-28 22:45 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public interface Worker {
|
||||
|
||||
/**
|
||||
* Schedule.
|
||||
*
|
||||
* @param event the event
|
||||
*/
|
||||
void schedule(TaskEvent event);
|
||||
|
||||
/**
|
||||
* Schedule.
|
||||
*
|
||||
* @param events the events
|
||||
*/
|
||||
void schedule(List<TaskEvent> events);
|
||||
|
||||
/** Handle. */
|
||||
void handle();
|
||||
}
|
|
@ -0,0 +1,194 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.task;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.Register;
|
||||
import com.alipay.sofa.registry.client.api.RegistryClientConfig;
|
||||
import com.alipay.sofa.registry.client.log.LoggerFactory;
|
||||
import com.alipay.sofa.registry.client.provider.AbstractInternalRegister;
|
||||
import com.alipay.sofa.registry.client.provider.AbstractInternalRegister.SyncTask;
|
||||
import com.alipay.sofa.registry.client.provider.RegisterCache;
|
||||
import com.alipay.sofa.registry.client.remoting.Client;
|
||||
import com.alipay.sofa.registry.core.model.RegisterResponse;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
/**
|
||||
* The type Worker thread.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : WorkerThread.java, v 0.1 2018-03-01 11:51 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class WorkerThread extends AbstractWorkerThread {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(WorkerThread.class);
|
||||
|
||||
/** Task queue */
|
||||
protected final TaskQueue requestQueue = new TaskQueue();
|
||||
|
||||
private RegistryClientConfig config;
|
||||
|
||||
private RegisterCache registerCache;
|
||||
|
||||
private AtomicBoolean inited = new AtomicBoolean(false);
|
||||
|
||||
/**
|
||||
* Instantiates a new Worker thread.
|
||||
*
|
||||
* @param client the client connection
|
||||
* @param config the config
|
||||
*/
|
||||
public WorkerThread(Client client, RegistryClientConfig config, RegisterCache registerCache) {
|
||||
super(client);
|
||||
this.config = config;
|
||||
this.registerCache = registerCache;
|
||||
this.setName("RegistryWorkerThread");
|
||||
this.setDaemon(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule.
|
||||
*
|
||||
* @param event the event
|
||||
*/
|
||||
@Override
|
||||
public void schedule(TaskEvent event) {
|
||||
if (inited.compareAndSet(false, true)) {
|
||||
this.start();
|
||||
}
|
||||
requestQueue.put(event);
|
||||
signal();
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule.
|
||||
*
|
||||
* @param events the events
|
||||
*/
|
||||
@Override
|
||||
public void schedule(List<TaskEvent> events) {
|
||||
if (inited.compareAndSet(false, true)) {
|
||||
this.start();
|
||||
}
|
||||
requestQueue.putAll(events);
|
||||
}
|
||||
|
||||
/** Handle. */
|
||||
@Override
|
||||
public void handle() {
|
||||
//noinspection InfiniteLoopStatement
|
||||
while (true) {
|
||||
try {
|
||||
// check connection status, try to reconnect to the server when connection lose
|
||||
client.ensureConnected();
|
||||
|
||||
if (requestQueue.isEmpty()) {
|
||||
await(config.getRecheckInterval());
|
||||
continue;
|
||||
}
|
||||
|
||||
Iterator<TaskEvent> lt = requestQueue.iterator();
|
||||
|
||||
while (lt.hasNext()) {
|
||||
client.ensureConnected();
|
||||
TaskEvent ev = lt.next();
|
||||
lt.remove();
|
||||
int sendCount = ev.incSendCount();
|
||||
|
||||
// Resent needs delay when task event is not the first time to send.
|
||||
if (sendCount != 0 && ev.delayTime() > 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
handleTask(ev);
|
||||
}
|
||||
|
||||
// Cleaning completed task, it will take more time when the registration number is large.
|
||||
requestQueue.cleanCompletedTasks();
|
||||
} catch (Throwable e) {
|
||||
LOGGER.error("[send] handle data error!", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void handleTask(TaskEvent event) {
|
||||
if (null == event) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
event.setTriggerTime(System.currentTimeMillis());
|
||||
Register register = event.getSource();
|
||||
|
||||
if (!(register instanceof AbstractInternalRegister)) {
|
||||
LOGGER.warn("[register] register type unknown, {}", register);
|
||||
return;
|
||||
}
|
||||
|
||||
AbstractInternalRegister abstractInternalRegister = (AbstractInternalRegister) register;
|
||||
|
||||
SyncTask syncTask = abstractInternalRegister.assemblySyncTask();
|
||||
String requestId = syncTask.getRequestId();
|
||||
|
||||
if (syncTask.isDone()) {
|
||||
LOGGER.info("[register] register already sync succeeded, {}", register);
|
||||
return;
|
||||
}
|
||||
|
||||
Object request = syncTask.getRequest();
|
||||
|
||||
Object result = client.invokeSync(request);
|
||||
|
||||
if (!(result instanceof RegisterResponse)) {
|
||||
LOGGER.warn("[register] result type is wrong, {}", result);
|
||||
return;
|
||||
}
|
||||
|
||||
RegisterResponse response = (RegisterResponse) result;
|
||||
if (!response.isSuccess()) {
|
||||
LOGGER.info("[register] register to server failed, {}, {}", request, response);
|
||||
return;
|
||||
}
|
||||
|
||||
boolean syncOK =
|
||||
abstractInternalRegister.syncOK(requestId, response.getVersion(), response.isRefused());
|
||||
if (!syncOK) {
|
||||
LOGGER.info(
|
||||
"[register] requestId has expired, ignore this response, {}, {}, {}",
|
||||
requestId,
|
||||
request,
|
||||
response);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!register.isEnabled()) {
|
||||
registerCache.remove(register.getRegistId());
|
||||
}
|
||||
|
||||
if (response.isRefused()) {
|
||||
LOGGER.info(
|
||||
"[register] register refused by server, {}, {}, {}", requestId, request, response);
|
||||
} else {
|
||||
LOGGER.info(
|
||||
"[register] register to server success, {}, {}, {}", requestId, request, response);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("[send] handle request failed, {}", event, e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.util;
|
||||
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* The type Common utils.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : CommonUtils.java, v 0.1 2018-07-15 22:48 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class CommonUtils {
|
||||
|
||||
/**
|
||||
* Is not empty boolean.
|
||||
*
|
||||
* @param collection the collection
|
||||
* @return the boolean
|
||||
*/
|
||||
public static boolean isNotEmpty(Collection collection) {
|
||||
return collection != null && !collection.isEmpty();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,123 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.util;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.RegistryClientConfig;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
/**
|
||||
* The type Http client utils.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : HttpClientUtils.java, v 0.1 2018-03-22 17:38 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class HttpClientUtils {
|
||||
private static final char AND = '&';
|
||||
|
||||
/**
|
||||
* Get string.
|
||||
*
|
||||
* @param url the url
|
||||
* @param params the params
|
||||
* @param config the config
|
||||
* @return the string
|
||||
* @throws Exception the exception
|
||||
*/
|
||||
public static String get(String url, Map<String, String> params, RegistryClientConfig config)
|
||||
throws Exception {
|
||||
HttpURLConnection httpURLConnection = create(getFullPath(url, params), config);
|
||||
httpURLConnection.setRequestMethod("GET");
|
||||
|
||||
BufferedReader reader = null;
|
||||
try {
|
||||
StringBuilder stringBuffer = new StringBuilder();
|
||||
|
||||
int responseCode = httpURLConnection.getResponseCode();
|
||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||
String charset = httpURLConnection.getContentEncoding();
|
||||
if (charset != null) {
|
||||
reader =
|
||||
new BufferedReader(
|
||||
new InputStreamReader(httpURLConnection.getInputStream(), charset));
|
||||
} else {
|
||||
reader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
|
||||
}
|
||||
|
||||
String strCurrentLine;
|
||||
while ((strCurrentLine = reader.readLine()) != null) {
|
||||
stringBuffer.append(strCurrentLine).append("\n");
|
||||
}
|
||||
if (stringBuffer.length() > 0 && stringBuffer.charAt(stringBuffer.length() - 1) == '\n') {
|
||||
stringBuffer.deleteCharAt(stringBuffer.length() - 1);
|
||||
}
|
||||
|
||||
return stringBuffer.toString();
|
||||
}
|
||||
} finally {
|
||||
httpURLConnection.disconnect();
|
||||
if (null != reader) {
|
||||
reader.close();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create http url connection.
|
||||
*
|
||||
* @param httpUrl the http url
|
||||
* @param config the config
|
||||
* @return the http url connection
|
||||
* @throws Exception the exception
|
||||
*/
|
||||
private static HttpURLConnection create(String httpUrl, RegistryClientConfig config)
|
||||
throws Exception {
|
||||
URL url = new URL(httpUrl);
|
||||
URLConnection urlConnection = url.openConnection();
|
||||
HttpURLConnection httpConnection = (HttpURLConnection) urlConnection;
|
||||
httpConnection.setDoOutput(true);
|
||||
httpConnection.setDoInput(true);
|
||||
httpConnection.setUseCaches(false);
|
||||
httpConnection.setRequestProperty("Content-type", "text/plain");
|
||||
httpConnection.setConnectTimeout(config.getConnectTimeout());
|
||||
httpConnection.setReadTimeout(config.getSocketTimeout());
|
||||
return httpConnection;
|
||||
}
|
||||
|
||||
private static String getFullPath(String url, Map<String, String> params) {
|
||||
StringBuilder sb = new StringBuilder(url);
|
||||
if (params != null) {
|
||||
sb.append("?");
|
||||
for (Entry<String, String> param : params.entrySet()) {
|
||||
sb.append(param.getKey());
|
||||
sb.append("=");
|
||||
sb.append(param.getValue());
|
||||
sb.append("&");
|
||||
}
|
||||
if (sb.charAt(sb.length() - 1) == AND) {
|
||||
sb.deleteCharAt(sb.length() - 1);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,134 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.util;
|
||||
|
||||
import com.alipay.sofa.registry.client.provider.DefaultServerNode;
|
||||
import com.alipay.sofa.registry.client.remoting.ServerNode;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id: ServerNodeParser.java, v 0.1 2018-03-22 20:28 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class ServerNodeParser {
|
||||
|
||||
/** symbol : */
|
||||
private static final char COLON = ':';
|
||||
|
||||
/** symbol = */
|
||||
private static final char EQUAL = '=';
|
||||
|
||||
/** symbol & */
|
||||
private static final char AND = '&';
|
||||
|
||||
/** symbol ? */
|
||||
private static final char QUES = '?';
|
||||
|
||||
/** @see com.alipay.remoting.RemotingAddressParser#parse(String) */
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
public static ServerNode parse(String url) {
|
||||
if (StringUtils.isBlank(url)) {
|
||||
throw new IllegalArgumentException(
|
||||
"Illegal format address string [" + url + "], should not be blank! ");
|
||||
}
|
||||
String ip = null;
|
||||
String port = null;
|
||||
Properties properties = null;
|
||||
|
||||
int size = url.length();
|
||||
int pos = 0;
|
||||
for (int i = 0; i < size; ++i) {
|
||||
if (COLON == url.charAt(i)) {
|
||||
ip = url.substring(pos, i);
|
||||
pos = i;
|
||||
// should not end with COLON
|
||||
if (i == size - 1) {
|
||||
throw new IllegalArgumentException(
|
||||
"Illegal format address string [" + url + "], should not end with COLON[:]! ");
|
||||
}
|
||||
break;
|
||||
}
|
||||
// must have one COLON
|
||||
if (i == size - 1) {
|
||||
throw new IllegalArgumentException(
|
||||
"Illegal format address string [" + url + "], must have one COLON[:]! ");
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = pos; i < size; ++i) {
|
||||
if (QUES == url.charAt(i)) {
|
||||
port = url.substring(pos + 1, i);
|
||||
pos = i;
|
||||
if (i == size - 1) {
|
||||
// should not end with QUES
|
||||
throw new IllegalArgumentException(
|
||||
"Illegal format address string [" + url + "], should not end with QUES[?]! ");
|
||||
}
|
||||
break;
|
||||
}
|
||||
// end without a QUES
|
||||
if (i == size - 1) {
|
||||
port = url.substring(pos + 1, i + 1);
|
||||
pos = size;
|
||||
}
|
||||
}
|
||||
|
||||
if (pos < (size - 1)) {
|
||||
properties = new Properties();
|
||||
while (pos < (size - 1)) {
|
||||
String key = null;
|
||||
String value = null;
|
||||
for (int i = pos; i < size; ++i) {
|
||||
if (EQUAL == url.charAt(i)) {
|
||||
key = url.substring(pos + 1, i);
|
||||
pos = i;
|
||||
if (i == size - 1) {
|
||||
// should not end with EQUAL
|
||||
throw new IllegalArgumentException(
|
||||
"Illegal format address string [" + url + "], should not end with EQUAL[=]! ");
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (i == size - 1) {
|
||||
// must have one EQUAL
|
||||
throw new IllegalArgumentException(
|
||||
"Illegal format address string [" + url + "], must have one EQUAL[=]! ");
|
||||
}
|
||||
}
|
||||
for (int i = pos; i < size; ++i) {
|
||||
if (AND == url.charAt(i)) {
|
||||
value = url.substring(pos + 1, i);
|
||||
pos = i;
|
||||
if (i == size - 1) {
|
||||
// should not end with AND
|
||||
throw new IllegalArgumentException(
|
||||
"Illegal format address string [" + url + "], should not end with AND[&]! ");
|
||||
}
|
||||
break;
|
||||
}
|
||||
// end without more AND
|
||||
if (i == size - 1) {
|
||||
value = url.substring(pos + 1, i + 1);
|
||||
pos = size;
|
||||
}
|
||||
}
|
||||
properties.put(key, value);
|
||||
}
|
||||
}
|
||||
return new DefaultServerNode(url, ip, Integer.parseInt(port), properties);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,116 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.util;
|
||||
|
||||
/**
|
||||
* The type String utils.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : StringUtils.java, v 0.1 2018-03-05 17:36 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class StringUtils {
|
||||
|
||||
/** The constant EMPTY. */
|
||||
public static final String EMPTY = "";
|
||||
|
||||
/**
|
||||
* Is empty boolean.
|
||||
*
|
||||
* @param cs the cs
|
||||
* @return the boolean
|
||||
*/
|
||||
public static boolean isEmpty(CharSequence cs) {
|
||||
return cs == null || cs.length() == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is not empty boolean.
|
||||
*
|
||||
* @param cs the cs
|
||||
* @return the boolean
|
||||
*/
|
||||
public static boolean isNotEmpty(CharSequence cs) {
|
||||
return !isEmpty(cs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Is blank boolean.
|
||||
*
|
||||
* @param cs the cs
|
||||
* @return the boolean
|
||||
*/
|
||||
public static boolean isBlank(CharSequence cs) {
|
||||
int strLen;
|
||||
if (cs == null || (strLen = cs.length()) == 0) {
|
||||
return true;
|
||||
}
|
||||
for (int i = 0; i < strLen; i++) {
|
||||
if (!Character.isWhitespace(cs.charAt(i))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is not blank boolean.
|
||||
*
|
||||
* @param cs the cs
|
||||
* @return the boolean
|
||||
*/
|
||||
public static boolean isNotBlank(CharSequence cs) {
|
||||
return !isBlank(cs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns either the passed in String, or if the String is {@code null}, an empty String ("").
|
||||
*
|
||||
* <pre>
|
||||
* StringUtils.defaultString(null) = ""
|
||||
* StringUtils.defaultString("") = ""
|
||||
* StringUtils.defaultString("bat") = "bat"
|
||||
* </pre>
|
||||
*
|
||||
* @param str the String to check, may be null
|
||||
* @return the passed in String, or the empty String if it was {@code null}
|
||||
* @see String#valueOf(Object) String#valueOf(Object)
|
||||
*/
|
||||
public static String defaultString(final Object str) {
|
||||
return toString(str, EMPTY);
|
||||
}
|
||||
|
||||
/**
|
||||
* To string string.
|
||||
*
|
||||
* @param o the o
|
||||
* @param defaultVal the default val
|
||||
* @return the string
|
||||
*/
|
||||
public static String toString(Object o, String defaultVal) {
|
||||
return o == null ? defaultVal : o.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* To string string.
|
||||
*
|
||||
* @param o the o
|
||||
* @return the string
|
||||
*/
|
||||
public static String toString(Object o) {
|
||||
return toString(o, null);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
|
||||
|
||||
<!-- Log4j 1.x config -->
|
||||
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
|
||||
|
||||
<!-- appender -->
|
||||
<appender name="ERROR-APPENDER" class="org.apache.log4j.DailyRollingFileAppender">
|
||||
<param name="file" value="${logging.path}/registry/common-error.log"/>
|
||||
<param name="append" value="true"/>
|
||||
<param name="encoding" value="${com.alipay.sofa.registry.client.log.encode}"/>
|
||||
<param name="threshold" value="ERROR"/>
|
||||
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
|
||||
<layout class="org.apache.log4j.PatternLayout">
|
||||
<param name="ConversionPattern" value="%d %-5p %C{1}#%L %-32t - %m%n"/>
|
||||
</layout>
|
||||
</appender>
|
||||
|
||||
<appender name="ROOT-APPENDER" class="org.apache.log4j.DailyRollingFileAppender">
|
||||
<param name="file" value="${logging.path}/registry/common-default.log"/>
|
||||
<param name="append" value="true"/>
|
||||
<param name="encoding" value="${com.alipay.sofa.registry.client.log.encode}"/>
|
||||
<param name="threshold" value="${com.alipay.sofa.registry.client.log.level}"/>
|
||||
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
|
||||
<layout class="org.apache.log4j.PatternLayout">
|
||||
<param name="ConversionPattern" value="%d %-5p %-32t - %m%n"/>
|
||||
</layout>
|
||||
<filter class="org.apache.log4j.varia.LevelRangeFilter">
|
||||
<param name="LevelMin" value="INFO"/>
|
||||
<param name="LevelMax" value="WARN"/>
|
||||
</filter>
|
||||
</appender>
|
||||
|
||||
<appender name="DEFAULT-CLIENT-APPENDER" class="org.apache.log4j.DailyRollingFileAppender">
|
||||
<param name="file" value="${logging.path}/registry/registry-client.log"/>
|
||||
<param name="append" value="true"/>
|
||||
<param name="encoding" value="${com.alipay.sofa.registry.client.log.encode}"/>
|
||||
<param name="threshold" value="${com.alipay.sofa.registry.client.log.level}"/>
|
||||
<param name="DatePattern" value="'.'yyyy-MM-dd"/>
|
||||
<layout class="org.apache.log4j.PatternLayout">
|
||||
<param name="ConversionPattern" value="%d %-5p %-32t - %m%n"/>
|
||||
</layout>
|
||||
<filter class="org.apache.log4j.varia.LevelRangeFilter">
|
||||
<param name="LevelMin" value="INFO"/>
|
||||
<param name="LevelMax" value="WARN"/>
|
||||
</filter>
|
||||
</appender>
|
||||
|
||||
<!-- logger -->
|
||||
<logger name="com.alipay.sofa.registry.client" additivity="false">
|
||||
<level value="${com.alipay.sofa.registry.client.log.level}"/>
|
||||
<appender-ref ref="DEFAULT-CLIENT-APPENDER"/>
|
||||
<appender-ref ref="ERROR-APPENDER"/>
|
||||
</logger>
|
||||
|
||||
<root>
|
||||
<level value="${com.alipay.sofa.registry.client.log.level}"/>
|
||||
<appender-ref ref="ROOT-APPENDER"/>
|
||||
<appender-ref ref="ERROR-APPENDER"/>
|
||||
</root>
|
||||
|
||||
</log4j:configuration>
|
|
@ -0,0 +1,86 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<!-- Log4j 2.x config -->
|
||||
<Configuration status="OFF">
|
||||
<properties>
|
||||
<property name="LOG_HOME">${sys:logging.path}/registry</property>
|
||||
<Property name="LOG_LEVEL">${sys:com.alipay.sofa.registry.client.log.level}</Property>
|
||||
<Property name="LOG_ENCODE">${sys:com.alipay.sofa.registry.client.log.encode}</Property>
|
||||
</properties>
|
||||
|
||||
<!-- appenders -->
|
||||
<Appenders>
|
||||
<RollingRandomAccessFile name="ERROR-APPENDER"
|
||||
|
||||
immediateFlush="false"
|
||||
fileName="${LOG_HOME}/common-error.log"
|
||||
append="true"
|
||||
filePattern="${LOG_HOME}/common-error.log.%d{yyyy-MM-dd}">
|
||||
<Filters>
|
||||
<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
|
||||
</Filters>
|
||||
<PatternLayout charset="${LOG_ENCODE}">
|
||||
<pattern>%d %-5p %C{1}#%L %-32t - %m%n</pattern>
|
||||
</PatternLayout>
|
||||
<Policies>
|
||||
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
|
||||
</Policies>
|
||||
</RollingRandomAccessFile>
|
||||
|
||||
<RollingRandomAccessFile name="ROOT-APPENDER"
|
||||
immediateFlush="false"
|
||||
fileName="${LOG_HOME}/common-default.log"
|
||||
append="true"
|
||||
filePattern="${LOG_HOME}/common-default.log.%d{yyyy-MM-dd}">
|
||||
<Filters>
|
||||
<ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL"/>
|
||||
<ThresholdFilter level="${LOG_LEVEL}" onMatch="ACCEPT" onMismatch="DENY"/>
|
||||
</Filters>
|
||||
<PatternLayout charset="${LOG_ENCODE}">
|
||||
<pattern>%d %-5p %-32t - %m%n</pattern>
|
||||
</PatternLayout>
|
||||
<Policies>
|
||||
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
|
||||
</Policies>
|
||||
</RollingRandomAccessFile>
|
||||
|
||||
<RollingRandomAccessFile name="DEFAULT-CLIENT-APPENDER"
|
||||
immediateFlush="false"
|
||||
fileName="${LOG_HOME}/registry-client.log"
|
||||
append="true"
|
||||
filePattern="${LOG_HOME}/registry-client.log.%d{yyyy-MM-dd}">
|
||||
<Filters>
|
||||
<ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL"/>
|
||||
<ThresholdFilter level="${LOG_LEVEL}" onMatch="ACCEPT" onMismatch="DENY"/>
|
||||
</Filters>
|
||||
<PatternLayout charset="${LOG_ENCODE}">
|
||||
<pattern>%d %-5p %-32t - %m%n</pattern>
|
||||
</PatternLayout>
|
||||
<Policies>
|
||||
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
|
||||
</Policies>
|
||||
</RollingRandomAccessFile>
|
||||
|
||||
<Console name="Console" target="SYSTEM_OUT">
|
||||
<ThresholdFilter level="${LOG_LEVEL}" onMatch="ACCEPT" onMismatch="DENY"/>
|
||||
<PatternLayout charset="${LOG_ENCODE}">
|
||||
<pattern>%d %-5p %-32t - %m%n</pattern>
|
||||
</PatternLayout>
|
||||
</Console>
|
||||
|
||||
</Appenders>
|
||||
|
||||
<!-- loggers -->
|
||||
<Loggers>
|
||||
|
||||
<AsyncLogger name="com.alipay.sofa.registry.client" level="${LOG_LEVEL}" additivity="false">
|
||||
<appender-ref ref="DEFAULT-CLIENT-APPENDER"/>
|
||||
<appender-ref ref="ERROR-APPENDER"/>
|
||||
</AsyncLogger>
|
||||
|
||||
<root level="${LOG_LEVEL}">
|
||||
<appender-ref ref="ROOT-APPENDER"/>
|
||||
<appender-ref ref="ERROR-APPENDER"/>
|
||||
</root>
|
||||
</Loggers>
|
||||
</Configuration>
|
|
@ -0,0 +1,84 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<!-- Logback config -->
|
||||
<configuration>
|
||||
<property scope="local" name="LOG_HOME" value="${logging.path}/registry"/>
|
||||
<property scope="local" name="LOG_LEVEL" value="${com.alipay.sofa.registry.client.log.level}"/>
|
||||
<property scope="local" name="LOG_ENCODE" value="${com.alipay.sofa.registry.client.log.encode}"/>
|
||||
|
||||
<!-- appenders -->
|
||||
<appender name="ERROR-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<append>true</append>
|
||||
<filters>
|
||||
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
|
||||
<level>ERROR</level>
|
||||
</filter>
|
||||
</filters>
|
||||
<file>${LOG_HOME}/common-error.log</file>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<FileNamePattern>${LOG_HOME}/common-error.log.%d{yyyy-MM-dd}</FileNamePattern>
|
||||
<MaxHistory>30</MaxHistory>
|
||||
</rollingPolicy>
|
||||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
|
||||
<pattern>%d %-5p %C{1}#%L %-32t - %m%n</pattern>
|
||||
<charset>${LOG_ENCODE}</charset>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<appender name="ROOT-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<append>true</append>
|
||||
<filters>
|
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||
<level>ERROR</level>
|
||||
<onMatch>DENY</onMatch>
|
||||
</filter>
|
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||
<level>${LOG_LEVEL}</level>
|
||||
<onMatch>ACCEPT</onMatch>
|
||||
</filter>
|
||||
</filters>
|
||||
<file>${LOG_HOME}/common-default.log</file>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<FileNamePattern>${LOG_HOME}/common-default.log.%d{yyyy-MM-dd}</FileNamePattern>
|
||||
<MaxHistory>30</MaxHistory>
|
||||
</rollingPolicy>
|
||||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
|
||||
<pattern>%d %-5p %-32t - %m%n</pattern>
|
||||
<charset>${LOG_ENCODE}</charset>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<appender name="DEFAULT-CLIENT-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||
<append>true</append>
|
||||
<filters>
|
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||
<level>ERROR</level>
|
||||
<onMatch>DENY</onMatch>
|
||||
</filter>
|
||||
<filter class="ch.qos.logback.classic.filter.LevelFilter">
|
||||
<level>${LOG_LEVEL}</level>
|
||||
<onMatch>ACCEPT</onMatch>
|
||||
</filter>
|
||||
</filters>
|
||||
<file>${LOG_HOME}/registry-client.log</file>
|
||||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||
<FileNamePattern>${LOG_HOME}/registry-client.log.%d{yyyy-MM-dd}</FileNamePattern>
|
||||
<MaxHistory>30</MaxHistory>
|
||||
</rollingPolicy>
|
||||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
|
||||
<pattern>%d %-5p %-32t - %m%n</pattern>
|
||||
<charset>${LOG_ENCODE}</charset>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<!-- loggers -->
|
||||
<logger name="com.alipay.sofa.registry.client" level="${LOG_LEVEL}" additivity="false">
|
||||
<appender-ref ref="DEFAULT-CLIENT-APPENDER"/>
|
||||
<appender-ref ref="ERROR-APPENDER"/>
|
||||
</logger>
|
||||
|
||||
<root level="${LOG_LEVEL}">
|
||||
<appender-ref ref="ROOT-APPENDER"/>
|
||||
<appender-ref ref="ERROR-APPENDER"/>
|
||||
</root>
|
||||
</configuration>
|
|
@ -0,0 +1,215 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client;
|
||||
|
||||
import com.alipay.remoting.BizContext;
|
||||
import com.alipay.remoting.rpc.RpcServer;
|
||||
import com.alipay.remoting.rpc.protocol.SyncUserProcessor;
|
||||
import com.alipay.sofa.registry.core.constants.EventTypeConstants;
|
||||
import com.alipay.sofa.registry.core.model.BaseRegister;
|
||||
import com.alipay.sofa.registry.core.model.ConfiguratorRegister;
|
||||
import com.alipay.sofa.registry.core.model.DataBox;
|
||||
import com.alipay.sofa.registry.core.model.PublisherRegister;
|
||||
import com.alipay.sofa.registry.core.model.RegisterResponse;
|
||||
import com.alipay.sofa.registry.core.model.Result;
|
||||
import com.alipay.sofa.registry.core.model.SubscriberRegister;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* The type Mock server.
|
||||
*
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id : MockServer.java, v 0.1 2017-12-25 22:39 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class MockServer {
|
||||
|
||||
/** LOGGER */
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(MockServer.class);
|
||||
|
||||
private RpcServer rpcServer;
|
||||
|
||||
private String ip = "127.0.0.1";
|
||||
|
||||
private int port = 9600;
|
||||
|
||||
private Map<String, PublisherRegister> publisherMap = new HashMap<String, PublisherRegister>();
|
||||
private Map<String, SubscriberRegister> subscriberMap = new HashMap<String, SubscriberRegister>();
|
||||
private Map<String, ConfiguratorRegister> configuratorMap =
|
||||
new HashMap<String, ConfiguratorRegister>();
|
||||
|
||||
/** Start. */
|
||||
public void start() {
|
||||
rpcServer = new RpcServer(port);
|
||||
rpcServer.registerUserProcessor(new MockSubscriberRegisterProcessor());
|
||||
rpcServer.registerUserProcessor(new MockPublisherRegisterProcessor());
|
||||
rpcServer.registerUserProcessor(new MockConfiguratorRegisterProcesor());
|
||||
rpcServer.start();
|
||||
}
|
||||
|
||||
/** Stop. */
|
||||
public void stop() {
|
||||
rpcServer.stop();
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>ip</tt>.
|
||||
*
|
||||
* @return property value of ip
|
||||
*/
|
||||
public String getIp() {
|
||||
return ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>ip</tt>.
|
||||
*
|
||||
* @param ip value to be assigned to property ip
|
||||
*/
|
||||
public void setIp(String ip) {
|
||||
this.ip = ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter method for property <tt>port</tt>.
|
||||
*
|
||||
* @return property value of port
|
||||
*/
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter method for property <tt>port</tt>.
|
||||
*
|
||||
* @param port value to be assigned to property port
|
||||
*/
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Query publisher by registId
|
||||
*
|
||||
* @param registId
|
||||
* @return
|
||||
*/
|
||||
public PublisherRegister queryPubliser(String registId) {
|
||||
return publisherMap.get(registId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Query subscriber by registId
|
||||
*
|
||||
* @param registId
|
||||
* @return
|
||||
*/
|
||||
public SubscriberRegister querySubscriber(String registId) {
|
||||
return subscriberMap.get(registId);
|
||||
}
|
||||
|
||||
/** The type Mock subscriber register processor. */
|
||||
class MockSubscriberRegisterProcessor extends SyncUserProcessor<SubscriberRegister> {
|
||||
|
||||
@Override
|
||||
public Object handleRequest(BizContext bizCtx, SubscriberRegister request) throws Exception {
|
||||
if ("subscribeAndRefused".equals(request.getDataId())) {
|
||||
return response(request, true);
|
||||
}
|
||||
|
||||
Result result = new Result();
|
||||
result.setSuccess(true);
|
||||
String registId = request.getRegistId();
|
||||
if (EventTypeConstants.REGISTER.equals(request.getEventType())) {
|
||||
subscriberMap.put(registId, request);
|
||||
} else if (EventTypeConstants.UNREGISTER.equals(request.getEventType())) {
|
||||
subscriberMap.remove(registId);
|
||||
}
|
||||
return response(request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String interest() {
|
||||
return SubscriberRegister.class.getName();
|
||||
}
|
||||
}
|
||||
|
||||
/** The type Mock publisher register processor. */
|
||||
class MockPublisherRegisterProcessor extends SyncUserProcessor<PublisherRegister> {
|
||||
|
||||
@Override
|
||||
public Object handleRequest(BizContext bizCtx, PublisherRegister request) throws Exception {
|
||||
if ("publishAndRefused".equals(request.getDataId())) {
|
||||
return response(request, true);
|
||||
}
|
||||
String registId = request.getRegistId();
|
||||
List<DataBox> dataList = request.getDataList();
|
||||
LOGGER.info(registId + " " + request.getEventType() + " " + dataList);
|
||||
if (EventTypeConstants.REGISTER.equals(request.getEventType())) {
|
||||
publisherMap.put(registId, request);
|
||||
} else if (EventTypeConstants.UNREGISTER.equals(request.getEventType())) {
|
||||
publisherMap.remove(registId);
|
||||
}
|
||||
return response(request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String interest() {
|
||||
return PublisherRegister.class.getName();
|
||||
}
|
||||
}
|
||||
|
||||
private RegisterResponse response(BaseRegister register) {
|
||||
return response(register, false);
|
||||
}
|
||||
|
||||
private RegisterResponse response(BaseRegister register, boolean refused) {
|
||||
RegisterResponse response = new RegisterResponse();
|
||||
response.setSuccess(true);
|
||||
response.setVersion(register.getVersion());
|
||||
response.setRegistId(register.getRegistId());
|
||||
response.setRefused(refused);
|
||||
return response;
|
||||
}
|
||||
|
||||
class MockConfiguratorRegisterProcesor extends SyncUserProcessor<ConfiguratorRegister> {
|
||||
|
||||
@Override
|
||||
public Object handleRequest(BizContext bizCtx, ConfiguratorRegister request) throws Exception {
|
||||
if ("subscribeAndRefused".equals(request.getDataId())) {
|
||||
return response(request, true);
|
||||
}
|
||||
|
||||
String registId = request.getRegistId();
|
||||
LOGGER.info("dataId: {} registId: {}", request.getDataId(), registId);
|
||||
if (EventTypeConstants.REGISTER.equals(request.getEventType())) {
|
||||
configuratorMap.put(registId, request);
|
||||
} else if (EventTypeConstants.UNREGISTER.equals(request.getEventType())) {
|
||||
configuratorMap.remove(registId);
|
||||
}
|
||||
return response(request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String interest() {
|
||||
return ConfiguratorRegister.class.getName();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.base;
|
||||
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyMapOf;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.alipay.sofa.registry.client.MockServer;
|
||||
import com.alipay.sofa.registry.client.api.RegistryClientConfig;
|
||||
import com.alipay.sofa.registry.client.provider.DefaultRegistryClient;
|
||||
import com.alipay.sofa.registry.client.provider.DefaultRegistryClientConfig;
|
||||
import com.alipay.sofa.registry.client.provider.DefaultRegistryClientConfigBuilder;
|
||||
import com.alipay.sofa.registry.client.util.HttpClientUtils;
|
||||
import org.junit.Before;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.api.mockito.PowerMockito;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
|
||||
/**
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id: BaseTest.java, v 0.1 2018-03-24 14:58 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest(HttpClientUtils.class)
|
||||
public abstract class BaseTest {
|
||||
|
||||
protected DefaultRegistryClient registryClient;
|
||||
|
||||
protected String appName = "registry-test";
|
||||
|
||||
protected String dataCenter = "HZ";
|
||||
|
||||
protected String instanceId = "testInstanceId";
|
||||
|
||||
protected MockServer mockServer;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
mockServer = new MockServer();
|
||||
mockServer.start();
|
||||
|
||||
PowerMockito.mockStatic(HttpClientUtils.class);
|
||||
when(HttpClientUtils.get(
|
||||
anyString(), anyMapOf(String.class, String.class), any(RegistryClientConfig.class)))
|
||||
.thenReturn(mockServer.getIp() + ":" + mockServer.getPort());
|
||||
|
||||
DefaultRegistryClientConfig config =
|
||||
DefaultRegistryClientConfigBuilder.start()
|
||||
.setAppName(appName)
|
||||
.setDataCenter(dataCenter)
|
||||
.setInstanceId(instanceId)
|
||||
.setRegistryEndpoint(mockServer.getIp())
|
||||
.build();
|
||||
registryClient = new DefaultRegistryClient(config);
|
||||
registryClient.init();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,89 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.event;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.RegistryClientConfig;
|
||||
import com.alipay.sofa.registry.client.provider.DefaultRegistryClientConfigBuilder;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id: DefaultEventBusTest.java, v 0.1 2018-07-15 22:31 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class DefaultEventBusTest {
|
||||
|
||||
private DefaultEventBus eventBus;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
RegistryClientConfig config =
|
||||
DefaultRegistryClientConfigBuilder.start().setEventBusEnable(true).build();
|
||||
eventBus = new DefaultEventBus(config);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isEnable0() {
|
||||
RegistryClientConfig config =
|
||||
DefaultRegistryClientConfigBuilder.start().setEventBusEnable(false).build();
|
||||
eventBus = new DefaultEventBus(config);
|
||||
assertFalse(eventBus.isEnable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isEnable1() {
|
||||
RegistryClientConfig config =
|
||||
DefaultRegistryClientConfigBuilder.start().setEventBusEnable(true).build();
|
||||
eventBus = new DefaultEventBus(config);
|
||||
assertTrue(eventBus.isEnable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void register() {
|
||||
TestEventSubscriber testEventSubscriber = new TestEventSubscriber(true);
|
||||
|
||||
try {
|
||||
assertFalse(eventBus.isEnable(TestEvent.class));
|
||||
eventBus.register(TestEvent.class, testEventSubscriber);
|
||||
assertTrue(eventBus.isEnable(TestEvent.class));
|
||||
} finally {
|
||||
eventBus.unRegister(TestEvent.class, testEventSubscriber);
|
||||
}
|
||||
assertFalse(eventBus.isEnable(TestEvent.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void post() {
|
||||
TestEventSubscriber testEventSubscriber = new TestEventSubscriber(true);
|
||||
|
||||
try {
|
||||
assertFalse(eventBus.isEnable(TestEvent.class));
|
||||
eventBus.register(TestEvent.class, testEventSubscriber);
|
||||
assertTrue(eventBus.isEnable(TestEvent.class));
|
||||
final String data = "test-data";
|
||||
eventBus.post(new TestEvent(data));
|
||||
assertEquals(data, testEventSubscriber.getCache());
|
||||
} finally {
|
||||
eventBus.unRegister(TestEvent.class, testEventSubscriber);
|
||||
}
|
||||
assertFalse(eventBus.isEnable(TestEvent.class));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You 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.
|
||||
*/
|
||||
package com.alipay.sofa.registry.client.event;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.alipay.sofa.registry.client.api.Configurator;
|
||||
import com.alipay.sofa.registry.client.api.Subscriber;
|
||||
import com.alipay.sofa.registry.client.provider.DefaultRegistryClientConfigBuilder;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author zhuoyu.sjw
|
||||
* @version $Id: LookoutSubscriberTest.java, v 0.1 2018-07-15 23:04 zhuoyu.sjw Exp $$
|
||||
*/
|
||||
public class LookoutSubscriberTest {
|
||||
|
||||
private LookoutSubscriber lookoutSubscriber;
|
||||
|
||||
@Test
|
||||
public void isSync() {
|
||||
lookoutSubscriber = new LookoutSubscriber();
|
||||
assertFalse(lookoutSubscriber.isSync());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onSubscriberProcessEvent() {
|
||||
lookoutSubscriber = new LookoutSubscriber();
|
||||
|
||||
SubscriberProcessEvent event = new SubscriberProcessEvent();
|
||||
Subscriber subscriber = mock(Subscriber.class);
|
||||
event.setSubscriber(subscriber);
|
||||
event.setConfig(DefaultRegistryClientConfigBuilder.start().setInstanceId("000001").build());
|
||||
event.setStart(System.currentTimeMillis());
|
||||
event.setEnd(System.currentTimeMillis() + 1);
|
||||
event.setThrowable(null);
|
||||
|
||||
when(subscriber.getDataId()).thenReturn("test-dataId");
|
||||
|
||||
lookoutSubscriber.onEvent(event);
|
||||
|
||||
verify(subscriber, times(1)).getDataId();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onConfiguratorProcessEvent() {
|
||||
lookoutSubscriber = new LookoutSubscriber();
|
||||
|
||||
ConfiguratorProcessEvent event = new ConfiguratorProcessEvent();
|
||||
Configurator configurator = mock(Configurator.class);
|
||||
event.setConfigurator(configurator);
|
||||
event.setConfig(DefaultRegistryClientConfigBuilder.start().setInstanceId("000001").build());
|
||||
event.setStart(System.currentTimeMillis());
|
||||
event.setEnd(System.currentTimeMillis() + 1);
|
||||
event.setThrowable(null);
|
||||
|
||||
when(configurator.getDataId()).thenReturn("test-dataId");
|
||||
|
||||
lookoutSubscriber.onEvent(event);
|
||||
|
||||
verify(configurator, times(1)).getDataId();
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue