diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index beff4ec..cc3762f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -45,9 +45,9 @@ jobs: CORE_API_DIR="${RUNNER_TEMP}/shenyu-core-api" git init "${CORE_API_DIR}" git -C "${CORE_API_DIR}" remote add origin https://github.com/apache/shenyu.git - git -C "${CORE_API_DIR}" fetch --depth 1 origin 91bd8d0c18fae8a1b297a3a752ab3282643bc7f9 + git -C "${CORE_API_DIR}" fetch --depth 1 origin 971354fef10260e46e09ed951e0bff4d9b5e4040 git -C "${CORE_API_DIR}" checkout --detach FETCH_HEAD - "${CORE_API_DIR}/mvnw" -B -ntp -f "${CORE_API_DIR}/pom.xml" -pl shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-common,shenyu-web,shenyu-admin-listener/shenyu-admin-listener-api,shenyu-registry/shenyu-registry-api,shenyu-plugin/shenyu-plugin-global,shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide,shenyu-plugin/shenyu-plugin-uri,shenyu-plugin/shenyu-plugin-httpclient,shenyu-plugin/shenyu-plugin-response -am install -DskipTests -Dmaven.javadoc.skip=true -Drat.skip=true -Djacoco.skip=true -DskipRemoteResources=true + "${CORE_API_DIR}/mvnw" -B -ntp -f "${CORE_API_DIR}/pom.xml" -pl shenyu-sync-data-center/shenyu-sync-data-api,shenyu-registry/shenyu-registry-core,shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-common,shenyu-web,shenyu-admin-listener/shenyu-admin-listener-api,shenyu-registry/shenyu-registry-api,shenyu-plugin/shenyu-plugin-global,shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide,shenyu-plugin/shenyu-plugin-uri,shenyu-plugin/shenyu-plugin-httpclient,shenyu-plugin/shenyu-plugin-response -am install -DskipTests -Dmaven.javadoc.skip=true -Drat.skip=true -Djacoco.skip=true -DskipRemoteResources=true - name: Prepare original-suite core APIs run: | CORE_API_DIR="${RUNNER_TEMP}/shenyu-core-api" @@ -62,6 +62,10 @@ jobs: bash script/plugin-store/prepare-core-artifacts.sh "${CORE_API_DIR}" - name: Build, test, check style, and verify licenses run: mvn -B -ntp clean verify -Dapi.version=1.44 + - name: Verify the complete external Apollo stack + run: | + mvn -B -ntp -pl shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo,shenyu-extension/shenyu-extension-apollo/shenyu-sync-data-apollo,shenyu-extension/shenyu-extension-apollo/shenyu-registry-apollo,shenyu-extension/shenyu-extension-apollo/shenyu-spring-boot-starter-sync-data-apollo -am clean verify -Dapi.version=1.44 + python3 script/plugin-store/check-apollo-packaging.py - name: Compile preserved original test and example assets run: mvn -B -ntp -Pintegration-tests,e2e,examples test-compile -DskipTests -Dapi.version=1.44 - name: Verify store-owned SQL installation diff --git a/README.md b/README.md index 996610d..1c2e466 100644 --- a/README.md +++ b/README.md @@ -266,3 +266,7 @@ starter plus gateway plus backend end-to-end fixture is not included in this cha In order of registration, More access companies are welcome to register at [https://github.com/apache/shenyu/issues/68](https://github.com/apache/shenyu/issues/68) (For open source users only) . All Users : [Known Users](https://shenyu.apache.org/community/user-registration) + +### Apollo integration + +The [Apollo extension](shenyu-extension/shenyu-extension-apollo/README.md) builds its listener, sync adapter, registry and starter together. See [staged migration](docs/plugin-store-apollo.md) for the one-release deprecation and publication contract. diff --git a/docs/plugin-store-apollo.md b/docs/plugin-store-apollo.md new file mode 100644 index 0000000..1c6bd65 --- /dev/null +++ b/docs/plugin-store-apollo.md @@ -0,0 +1,210 @@ + +# Apollo plugin-store migration + +## Status and deprecation cycle + +Apollo is currently an optional, supported in-tree integration. Issue [#7599](https://github.com/apache/shenyu/issues/7599) +proposes transferring its registry, data-sync, Admin listener and gateway starter to +[plugin-store](https://github.com/apache/shenyu-plugin-store), extending [#7306](https://github.com/apache/shenyu/issues/7306). +This guide is the advance deprecation notice: the in-tree modules remain available +for the release carrying this notice. No removal release has been selected. +The separate removal PR is a draft and must not be merged in that deprecation release. + +| Stage | Required action | +| --- | --- | +| Agreement | Obtain and link the dev-list decision approving Apollo externalization. | +| Deprecation release | Ship this notice and migration guide in a core release while retaining all four in-tree modules. Record that released version publicly. | +| Receiving parity | Build/test all four modules together in plugin-store, verify external SPI loading and unchanged sync/registry configuration. | +| Publication | Announce compatible core/store versions, publish external artifacts before cutover, and reserve one publisher per Maven group/artifact/version. | +| Later core release | After the deprecation release has shipped, merge the separate removal, update the release inventory, and announce the rollback window. | + +Do not treat creating PRs, merging documentation, or producing local snapshot JARs +as completing the dev-list decision, a released deprecation cycle, or artifact publication. +Keep the issue open until all stages and their release evidence are complete. + +## Artifact ownership and installation + +The receiving `shenyu-extension-apollo` reactor owns these artifacts together: + +- `shenyu-admin-listener-apollo` +- `shenyu-sync-data-apollo` +- `shenyu-registry-apollo` +- `shenyu-spring-boot-starter-sync-data-apollo` + +The preparation branch uses store `2.7.1-SNAPSHOT` against core `2.7.2-SNAPSHOT`. +These are development coordinates, not an announced release compatibility matrix. +For deployment, select the published store release declared compatible with your core release. +Use the core version for retained APIs and the store version for **all four** Apollo artifacts. +Keep exactly one provider version on each runtime classpath; the in-tree and external +implementations retain identical packages and must not coexist. + +Admin needs: + +```xml + + org.apache.shenyu + shenyu-admin-listener-apollo + ${store.version} + +``` + +Gateway data synchronization needs: + +```xml + + org.apache.shenyu + shenyu-spring-boot-starter-sync-data-apollo + ${store.version} + +``` + +Applications using Apollo instance registration or SDK instance discovery additionally need: + +```xml + + org.apache.shenyu + shenyu-registry-apollo + ${store.version} + +``` + +Gateway registration also requires the retained core `shenyu-spring-boot-starter-registry`; +SDK discovery requires the appropriate SDK starter. Apollo was never included in the +compatibility registry starter, so it remains an explicit provider dependency. +For binary distributions, resolve the complete runtime dependency closure into `lib/`, +replace old Apollo JARs, and retain the extension's licensing information and dependency notices. + +Shading continues to package Apollo client dependencies, but excludes retained ShenYu +API artifacts and sibling extension modules. API classes come from the application's core artifacts, preserving +class identity for external SPI loading. The published POM retains runtime dependencies. + +## Enable Admin and gateway synchronization + +Admin keeps its existing property prefix and portal publishing settings: + +```yaml +shenyu: + sync: + websocket: + enabled: false + apollo: + appId: shenyu + meta: http://apollo-config-service:8080 + portalUrl: http://apollo-portal:8070 + env: DEV + clusterName: default + namespace: application + token: ${APOLLO_OPENAPI_TOKEN} + operatorUser: apollo +``` + +Gateway keeps the same reading settings; remove or disable the previous sync channel: + +```yaml +shenyu: + namespace: 649330b6-c2d7-4edc-be8e-8a54df9eb385 + sync: + websocket: + urls: false + apollo: + appId: shenyu + meta: http://apollo-config-service:8080 + env: DEV + clusterName: default + namespace: application + accessKey: ${APOLLO_ACCESS_KEY:} +``` + +The portal OpenAPI token and config-service access key serve different APIs. +Ensure the publishing app, environment, cluster and Apollo namespace match the reader. +The preserved reader calls Apollo `ConfigService.getAppConfig()`, so the example uses +`application`. This migration does not add arbitrary namespace selection. +The ShenYu namespace ID is a separate tenant prefix inside Apollo item keys. +The Apollo client sets JVM-wide app/meta/cluster properties; preserve the existing +process-level configuration when using sync and registry together. + +## Registry and SPI contract + +The external JAR continues to publish: + +```text +META-INF/shenyu/org.apache.shenyu.registry.api.ShenyuInstanceRegisterRepository +apollo=org.apache.shenyu.registry.apollo.ApolloInstanceRegisterRepository +``` + +`ShenyuInstanceRegisterRepositoryFactory.loadRepository("apollo")` resolves that +resource without any factory or core runtime change, and preserves the non-singleton provider. +Registry configuration remains: + +```yaml +shenyu: + register: + enabled: true + registerType: apollo + serverLists: http://apollo-config-service:8080 + props: + appId: shenyu + portalUrl: http://apollo-portal:8070 + token: ${APOLLO_OPENAPI_TOKEN} + env: DEV + clusterName: default + namespace: application +``` + +SDK discovery uses the same `apollo` type and registry settings via its existing SDK +configuration; it does not replace backend HTTP annotation/API registration. +Instance registration writes persistent portal items and publishes the namespace. +Existing shutdown removes listeners; it does not deregister persistent instance items. +Preserve that behavior during migration and apply your existing instance-removal procedure. + +## Stored keys and shared compatibility shim + +| Data | Existing Apollo item key | +| --- | --- | +| Plugin | `.plugin.` | +| Selector | `.selector..` | +| Rule | `.rule...` | +| Authentication | `.auth.` | +| Metadata | `.meta.` | +| Proxy selector | `.proxy.selector..` | +| Discovery upstream | `.discovery..` | +| Instance | `/shenyu/register/instance//:` | + +The existing `.list` index items under each category/group are retained alongside the data keys. +JSON DTOs, key prefixes, namespace IDs and the `apollo` type remain unchanged. +`org.apache.shenyu.common.constant.ApolloPathConstants` stays in core as a small +constants/path compatibility shim with no Apollo SDK dependency. Do not duplicate that +class in store JARs or migrate/delete existing keyspaces as part of this transfer. + +The client versions remain `apollo-client`, `apollo-openapi` and `apollo-core` `2.1.0`. +Source and the Apollo clients are Apache-2.0; preserve licenses when assembling custom packages. +Existing unit tests, real Apollo `DefaultConfig` listener filtering tests and starter +configuration tests move with their modules. Those tests do not claim a full live Apollo +portal/config-service deployment; validate your staging deployment before production cutover. + +## Upgrade and rollback + +1. After publication and release gates pass, back up the Admin database and Apollo + application/namespace configuration, recording exact core/store versions and deployed JARs. +2. Verify the external artifacts in staging using unchanged namespace, endpoints, tokens, + keys and registry properties. Check initial sync, create/update/delete and instance updates. +3. Roll Admin and gateways with complete external dependency closures, then confirm + subsequent changes propagate and existing instance discovery still works. +4. Retain the last in-tree release for the announced rollback window. To roll back, + restore its images/JARs and configuration and remove the external Apollo versions. + Reuse the same Admin database and Apollo keyspace; do not clear either during rollback. diff --git a/examples/apollo/admin/application.yml b/examples/apollo/admin/application.yml new file mode 100644 index 0000000..2ca96c3 --- /dev/null +++ b/examples/apollo/admin/application.yml @@ -0,0 +1,28 @@ +# 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. + +shenyu: + sync: + apollo: + appId: shenyu + meta: http://apollo-config-service:8080 + env: DEV + clusterName: default + namespace: application + portalUrl: http://apollo-portal:8070 + token: ${APOLLO_OPENAPI_TOKEN} + operatorUser: apollo + websocket: + enabled: false diff --git a/examples/apollo/gateway/application.yml b/examples/apollo/gateway/application.yml new file mode 100644 index 0000000..4da20c8 --- /dev/null +++ b/examples/apollo/gateway/application.yml @@ -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. + +shenyu: + sync: + apollo: + appId: shenyu + meta: http://apollo-config-service:8080 + env: DEV + clusterName: default + namespace: application + accessKey: ${APOLLO_ACCESS_KEY:} + websocket: + urls: false diff --git a/examples/apollo/legacy/shenyu-admin/src/main/resources/application.yml b/examples/apollo/legacy/shenyu-admin/src/main/resources/application.yml new file mode 100644 index 0000000..790ce2d --- /dev/null +++ b/examples/apollo/legacy/shenyu-admin/src/main/resources/application.yml @@ -0,0 +1,23 @@ +# 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. + +# apollo: +# meta: http://localhost:8080 +# appId: shenyu +# portalUrl: http://localhost:8070 +# env: dev +# clusterName: test +# namespace: application +# token: 0fff5645fc74ee5e0d63a6389433c8c8afc0beea31eed0279ecc1c8961d12da9 diff --git a/examples/apollo/legacy/shenyu-bootstrap/src/main/resources/application.yml b/examples/apollo/legacy/shenyu-bootstrap/src/main/resources/application.yml new file mode 100644 index 0000000..8a3fb0b --- /dev/null +++ b/examples/apollo/legacy/shenyu-bootstrap/src/main/resources/application.yml @@ -0,0 +1,21 @@ +# 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. + +# apollo: +# appId: shenyu +# meta: http://localhost:8080 +# env: dev +# clusterName: test +# namespace: application diff --git a/examples/apollo/legacy/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-apache-dubbo/src/main/resources/application.yml b/examples/apollo/legacy/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-apache-dubbo/src/main/resources/application.yml new file mode 100644 index 0000000..8a3fb0b --- /dev/null +++ b/examples/apollo/legacy/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-apache-dubbo/src/main/resources/application.yml @@ -0,0 +1,21 @@ +# 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. + +# apollo: +# appId: shenyu +# meta: http://localhost:8080 +# env: dev +# clusterName: test +# namespace: application diff --git a/examples/apollo/legacy/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-http/src/main/resources/application.yml b/examples/apollo/legacy/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-http/src/main/resources/application.yml new file mode 100644 index 0000000..8a3fb0b --- /dev/null +++ b/examples/apollo/legacy/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-http/src/main/resources/application.yml @@ -0,0 +1,21 @@ +# 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. + +# apollo: +# appId: shenyu +# meta: http://localhost:8080 +# env: dev +# clusterName: test +# namespace: application diff --git a/examples/apollo/legacy/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-websocket/src/main/resources/application.yml b/examples/apollo/legacy/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-websocket/src/main/resources/application.yml new file mode 100644 index 0000000..8a3fb0b --- /dev/null +++ b/examples/apollo/legacy/shenyu-integrated-test/shenyu-integrated-test-k8s-ingress-websocket/src/main/resources/application.yml @@ -0,0 +1,21 @@ +# 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. + +# apollo: +# appId: shenyu +# meta: http://localhost:8080 +# env: dev +# clusterName: test +# namespace: application diff --git a/script/plugin-store/check-apollo-packaging.py b/script/plugin-store/check-apollo-packaging.py new file mode 100644 index 0000000..c590de8 --- /dev/null +++ b/script/plugin-store/check-apollo-packaging.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 +# 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. + +import argparse +from pathlib import Path +import zipfile + + +MODULES = { + "shenyu-admin-listener-apollo": ("org/apache/shenyu/admin/config/", "org/apache/shenyu/admin/listener/apollo/"), + "shenyu-sync-data-apollo": ("org/apache/shenyu/sync/data/apollo/",), + "shenyu-registry-apollo": ("org/apache/shenyu/registry/apollo/",), + "shenyu-spring-boot-starter-sync-data-apollo": ("org/apache/shenyu/springboot/sync/data/apollo/",), +} + + +def check(root, version): + for module, owned in MODULES.items(): + artifact = root / "shenyu-extension/shenyu-extension-apollo" / module / "target" / f"{module}-{version}.jar" + with zipfile.ZipFile(artifact) as jar: + names = jar.namelist() + classes = [name for name in names if name.startswith("org/apache/shenyu/") and name.endswith(".class")] + if not classes: + raise AssertionError(f"{artifact}: missing the extension's implementation classes") + unexpected = [name for name in classes if not name.startswith(owned)] + if unexpected: + raise AssertionError(f"{artifact}: embeds core or sibling classes: {unexpected[:5]}") + if module == "shenyu-registry-apollo": + registration = jar.read("META-INF/shenyu/org.apache.shenyu.registry.api.ShenyuInstanceRegisterRepository").decode() + if "apollo=org.apache.shenyu.registry.apollo.ApolloInstanceRegisterRepository" not in registration: + raise AssertionError(f"{artifact}: missing the historical Apollo SPI key") + if module in {"shenyu-admin-listener-apollo", "shenyu-spring-boot-starter-sync-data-apollo"}: + imports = jar.read("META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports").decode() + expected = "org.apache.shenyu.admin.config.ApolloSyncConfiguration" if module == "shenyu-admin-listener-apollo" else "org.apache.shenyu.springboot.sync.data.apollo.ApolloSyncDataConfiguration" + if expected not in imports: + raise AssertionError(f"{artifact}: missing starter auto-configuration registration") + print(f"{module}: implementation, registrations and core API boundary verified") + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Check final Apollo extension JAR ownership and runtime registrations.") + parser.add_argument("--store-root", type=Path, default=Path(__file__).resolve().parents[2]) + parser.add_argument("--version", default="2.7.1-SNAPSHOT") + args = parser.parse_args() + check(args.store_root, args.version) diff --git a/shenyu-extension/pom.xml b/shenyu-extension/pom.xml index 97c23fc..cfe4bb9 100644 --- a/shenyu-extension/pom.xml +++ b/shenyu-extension/pom.xml @@ -27,5 +27,6 @@ shenyu-extension-consul shenyu-extension-polaris + shenyu-extension-apollo diff --git a/shenyu-extension/shenyu-extension-apollo/LICENSE b/shenyu-extension/shenyu-extension-apollo/LICENSE new file mode 100644 index 0000000..290d079 --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/LICENSE @@ -0,0 +1,224 @@ + 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. + +======================================================================= +Apache ShenYu Subcomponents: + +The Apache ShenYu project contains subcomponents with separate copyright +notices and license terms. Your use of the source code for these +subcomponents is subject to the terms and conditions of the following +licenses. + +======================================================================== +Apache 2.0 licenses +======================================================================== + +The following components are provided under the Apache License. See project link for details. +The text of each license is the standard Apache 2.0 license. + + Maven Wrapper(mvnw, mvnw.cmd files in root path), https://github.com/apache/maven-wrapper Apache 2.0 + + +Apollo client dependencies use the Apache License, Version 2.0: + apollo-client 2.1.0: https://github.com/apolloconfig/apollo/blob/v2.1.0/LICENSE + apollo-openapi 2.1.0: https://github.com/apolloconfig/apollo/blob/v2.1.0/LICENSE + apollo-core 2.1.0: https://github.com/apolloconfig/apollo/blob/v2.1.0/LICENSE diff --git a/shenyu-extension/shenyu-extension-apollo/NOTICE b/shenyu-extension/shenyu-extension-apollo/NOTICE new file mode 100644 index 0000000..4f0daa2 --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/NOTICE @@ -0,0 +1,5 @@ +Apache ShenYu Apollo Extension +Copyright 2026 The Apache Software Foundation + +This product includes software developed at +The Apache Software Foundation (http://www.apache.org/). diff --git a/shenyu-extension/shenyu-extension-apollo/README.md b/shenyu-extension/shenyu-extension-apollo/README.md new file mode 100644 index 0000000..cdeb1e3 --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/README.md @@ -0,0 +1,49 @@ + +# ShenYu Apollo extension + +This reactor owns the Admin listener, data-sync adapter, registry provider and sync +starter together. See [installation, compatibility and staged cutover](../../docs/plugin-store-apollo.md). + +Prepare compatible core API artifacts with the core checkout's Maven wrapper: + +```bash +/path/to/shenyu/mvnw -B -ntp -f /path/to/shenyu/pom.xml \ + -pl shenyu-admin-listener/shenyu-admin-listener-api,shenyu-sync-data-center/shenyu-sync-data-api,shenyu-registry/shenyu-registry-core \ + -am install -DskipTests -Dmaven.javadoc.skip=true -DskipRemoteResources=true +``` + +CI pins core commit `971354fef10260e46e09ed951e0bff4d9b5e4040`. Build/test all four children: + +```bash +mvn -B -ntp -pl shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo,shenyu-extension/shenyu-extension-apollo/shenyu-sync-data-apollo,shenyu-extension/shenyu-extension-apollo/shenyu-registry-apollo,shenyu-extension/shenyu-extension-apollo/shenyu-spring-boot-starter-sync-data-apollo -am clean verify +``` + +After packaging, run `python3 script/plugin-store/check-apollo-packaging.py` to +verify each final JAR owns its implementation and runtime registration resources +without embedding core or sibling API classes. CI runs this check. + +No live Apollo account or server is needed by the unit/SDK listener-filtering fixtures. +The real SDK `DefaultConfig` test checks asynchronous interested-key prefix handling; +Admin OpenAPI boundaries use mocks. Full portal/config-service deployment is a separate +staging release gate and is not claimed by this test suite. + +The shared `ApolloPathConstants` class remains owned by core, preserving downstream +API compatibility; shaded extension JARs exclude core APIs and sibling providers. +The initial receiving coordinates use store `2.7.1-SNAPSHOT` against core `2.7.2-SNAPSHOT`. +Publish only after agreeing on release ownership and compatible versions; main removal +requires dev-list consensus plus a released deprecation cycle and rollback window. diff --git a/shenyu-extension/shenyu-extension-apollo/pom.xml b/shenyu-extension/shenyu-extension-apollo/pom.xml new file mode 100644 index 0000000..b7510a0 --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/pom.xml @@ -0,0 +1,55 @@ + + + + + org.apache.shenyu + shenyu-extension + 2.7.1-SNAPSHOT + + 4.0.0 + pom + shenyu-extension-apollo + + + 2.1.0 + + + + + + com.ctrip.framework.apollo + apollo-client + ${apollo.version} + + + com.ctrip.framework.apollo + apollo-openapi + ${apollo.version} + + + + + + shenyu-admin-listener-apollo + shenyu-sync-data-apollo + shenyu-registry-apollo + shenyu-spring-boot-starter-sync-data-apollo + + diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/pom.xml b/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/pom.xml new file mode 100644 index 0000000..df818d3 --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/pom.xml @@ -0,0 +1,87 @@ + + + + + 4.0.0 + + org.apache.shenyu + shenyu-extension-apollo + 2.7.1-SNAPSHOT + + + shenyu-admin-listener-apollo + + + + com.ctrip.framework.apollo + apollo-openapi + + + + org.apache.shenyu + shenyu-admin-listener-api + ${shenyu.version} + + + + org.springframework.boot + spring-boot-starter-web + provided + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.apache.maven.plugins + maven-shade-plugin + ${maven-shade-plugin.version} + + false + + + org.apache.shenyu:shenyu-common + org.apache.shenyu:shenyu-spi + org.apache.shenyu:shenyu-admin-listener-api + org.apache.shenyu:shenyu-sync-data-api + org.apache.shenyu:shenyu-registry-api + org.apache.shenyu:shenyu-registry-core + + + + + + + + + package + + shade + + + + + + + diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/main/java/org/apache/shenyu/admin/config/ApolloSyncConfiguration.java b/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/main/java/org/apache/shenyu/admin/config/ApolloSyncConfiguration.java new file mode 100644 index 0000000..755c794 --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/main/java/org/apache/shenyu/admin/config/ApolloSyncConfiguration.java @@ -0,0 +1,76 @@ +/* + * 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 org.apache.shenyu.admin.config; + +import org.apache.shenyu.admin.config.properties.ApolloProperties; +import org.apache.shenyu.admin.listener.DataChangedInit; +import org.apache.shenyu.admin.listener.DataChangedListener; +import org.apache.shenyu.admin.listener.apollo.ApolloClient; +import org.apache.shenyu.admin.listener.apollo.ApolloDataChangedInit; +import org.apache.shenyu.admin.listener.apollo.ApolloDataChangedListener; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * the type apollo listener. + */ +@Configuration +@ConditionalOnProperty(prefix = "shenyu.sync.apollo", name = "meta") +@EnableConfigurationProperties(ApolloProperties.class) +public class ApolloSyncConfiguration { + + + /** + * init Consul client. + * + * @param apolloProperties the apollo properties + * @return apollo client + */ + @Bean + public ApolloClient apolloClient(final ApolloProperties apolloProperties) { + return new ApolloClient(apolloProperties); + } + + /** + * Config event listener data changed listener. + * + * @param apolloClient the apollo client + * @return the data changed listener + */ + @Bean + @ConditionalOnMissingBean(ApolloDataChangedListener.class) + public DataChangedListener apolloDataChangeListener(final ApolloClient apolloClient) { + return new ApolloDataChangedListener(apolloClient); + } + + /** + * apollo data init. + * + * @param apolloClient the apollo client + * @return the apollo data init + */ + @Bean + @ConditionalOnMissingBean(ApolloDataChangedInit.class) + public DataChangedInit apolloDataChangeInit(final ApolloClient apolloClient) { + return new ApolloDataChangedInit(apolloClient); + } + +} diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/main/java/org/apache/shenyu/admin/config/properties/ApolloProperties.java b/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/main/java/org/apache/shenyu/admin/config/properties/ApolloProperties.java new file mode 100644 index 0000000..b1a100f --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/main/java/org/apache/shenyu/admin/config/properties/ApolloProperties.java @@ -0,0 +1,196 @@ +/* + * 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 org.apache.shenyu.admin.config.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("shenyu.sync.apollo") +public class ApolloProperties { + + /** + * appId. + */ + private String appId; + + /** + * apollo config service url. + */ + private String meta; + + /** + * portal url. + * e.g. localhost:8080 + */ + private String portalUrl; + + /** + * env. + * e.g. ENV + */ + private String env; + + /** + * cluster name. + */ + private String clusterName; + + /** + * namespace name. + */ + private String namespace; + + /** + * open api use token. + */ + private String token; + + /** + * operatorUser. + */ + private String operatorUser; + + /** + * get appId. + * @return appId + */ + public String getAppId() { + return appId; + } + + /** + * set app id. + * @param appId app id + */ + public void setAppId(final String appId) { + this.appId = appId; + } + + /** + * get config service url. + * @return config service url + */ + public String getMeta() { + return meta; + } + + /** + * set config service url. + * @param meta config service url + */ + public void setMeta(final String meta) { + this.meta = meta; + } + + /** + * get portal url. + * @return portal url + */ + public String getPortalUrl() { + return portalUrl; + } + + /** + * set portal url. + * @param portalUrl portal url + */ + public void setPortalUrl(final String portalUrl) { + this.portalUrl = portalUrl; + } + + /** + * get env. + * @return env + */ + public String getEnv() { + return env; + } + + /** + * set env. + * @param env env + */ + public void setEnv(final String env) { + this.env = env; + } + + /** + * get cluster name. + * @return cluster name + */ + public String getClusterName() { + return clusterName; + } + + /** + * set cluster name. + * @param clusterName cluster name + */ + public void setClusterName(final String clusterName) { + this.clusterName = clusterName; + } + + /** + * get namespace name. + * @return namespace name + */ + public String getNamespace() { + return namespace; + } + + /** + * set namespace name. + * @param namespace namespace name + */ + public void setNamespace(final String namespace) { + this.namespace = namespace; + } + + /** + * get token. + * @return token + */ + public String getToken() { + return token; + } + + /** + * set token. + * @param token token + */ + public void setToken(final String token) { + this.token = token; + } + + /** + * operatorUser. + * + * @return OperatorUser + */ + public String getOperatorUser() { + return operatorUser; + } + + /** + * set operatorUser. + * + * @param operatorUser operatorUser + */ + public void setOperatorUser(final String operatorUser) { + this.operatorUser = operatorUser; + } +} diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/main/java/org/apache/shenyu/admin/listener/apollo/ApolloClient.java b/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/main/java/org/apache/shenyu/admin/listener/apollo/ApolloClient.java new file mode 100644 index 0000000..580b942 --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/main/java/org/apache/shenyu/admin/listener/apollo/ApolloClient.java @@ -0,0 +1,149 @@ +/* + * 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 org.apache.shenyu.admin.listener.apollo; + +import com.ctrip.framework.apollo.openapi.client.ApolloOpenApiClient; +import com.ctrip.framework.apollo.openapi.dto.NamespaceReleaseDTO; +import com.ctrip.framework.apollo.openapi.dto.OpenItemDTO; +import org.apache.shenyu.admin.config.properties.ApolloProperties; +import org.apache.shenyu.common.exception.ShenyuException; +import org.apache.shenyu.common.utils.GsonUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Date; +import java.util.Objects; +import java.util.Optional; + +/** + * apollo open-api client. + */ +public class ApolloClient { + + private static final Logger LOG = LoggerFactory.getLogger(ApolloClient.class); + + private final String operatorUser; + + private final ApolloProperties apolloConfig; + + private final ApolloOpenApiClient apolloOpenApiClient; + + public ApolloClient(final ApolloProperties apolloConfig) { + this.apolloConfig = apolloConfig; + + this.apolloOpenApiClient = ApolloOpenApiClient + .newBuilder() + .withPortalUrl(apolloConfig.getPortalUrl()) + .withToken(apolloConfig.getToken()) + .build(); + operatorUser = Optional.ofNullable(apolloConfig.getOperatorUser()).orElse("apollo"); + } + + /** + * get item value. + * @param key item key + * @return item value + */ + public String getItemValue(final String key) { + OpenItemDTO openItemDTO = this.apolloOpenApiClient.getItem( + apolloConfig.getAppId(), + apolloConfig.getEnv(), + apolloConfig.getClusterName(), + apolloConfig.getNamespace(), + key); + if (Objects.isNull(openItemDTO)) { + return null; + } + if (openItemDTO.getKey().equals("timeout")) { + LOG.error("apollo client getItemValue time out"); + throw new ShenyuException("apollo client getItemValue time out"); + } + return openItemDTO.getValue(); + } + + + /** + * removeItemKey. + * + * @param key key + */ + public void removeItemKey(final String key) { + this.apolloOpenApiClient.removeItem( + apolloConfig.getAppId(), + apolloConfig.getEnv(), + apolloConfig.getClusterName(), + apolloConfig.getNamespace(), + key, operatorUser); + } + + /** + * create or update item into namespace. + * @param key item key + * @param value item value + * @param comment item comment + */ + public void createOrUpdateItem(final String key, final Object value, final String comment) { + this.createOrUpdateItem(key, GsonUtils.getInstance().toJson(value), comment); + } + + /** + * create or update item into namespace. + * @param key item key + * @param value item value + * @param comment item comment + */ + public void createOrUpdateItem(final String key, final String value, final String comment) { + OpenItemDTO openItemDTO = new OpenItemDTO(); + openItemDTO.setKey(key); + openItemDTO.setValue(value); + openItemDTO.setComment(comment); + openItemDTO.setDataChangeCreatedBy(operatorUser); + openItemDTO.setDataChangeLastModifiedBy(operatorUser); + Date now = new Date(); + openItemDTO.setDataChangeCreatedTime(now); + openItemDTO.setDataChangeLastModifiedTime(now); + + this.apolloOpenApiClient.createOrUpdateItem( + apolloConfig.getAppId(), + apolloConfig.getEnv(), + apolloConfig.getClusterName(), + apolloConfig.getNamespace(), + openItemDTO + ); + } + + /** + * publish item list in namespace. + * @param releaseTitle publish release title + * @param releaseComment publish release comment + */ + public void publishNamespace(final String releaseTitle, final String releaseComment) { + NamespaceReleaseDTO namespaceReleaseDTO = new NamespaceReleaseDTO(); + namespaceReleaseDTO.setReleaseTitle(releaseTitle); + namespaceReleaseDTO.setReleaseComment(releaseComment); + namespaceReleaseDTO.setReleasedBy(operatorUser); + + this.apolloOpenApiClient.publishNamespace( + apolloConfig.getAppId(), + apolloConfig.getEnv(), + apolloConfig.getClusterName(), + apolloConfig.getNamespace(), + namespaceReleaseDTO + ); + } +} diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/main/java/org/apache/shenyu/admin/listener/apollo/ApolloDataChangedInit.java b/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/main/java/org/apache/shenyu/admin/listener/apollo/ApolloDataChangedInit.java new file mode 100644 index 0000000..ee1090b --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/main/java/org/apache/shenyu/admin/listener/apollo/ApolloDataChangedInit.java @@ -0,0 +1,65 @@ +/* + * 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 org.apache.shenyu.admin.listener.apollo; + +import org.apache.shenyu.admin.listener.AbstractDataChangedInit; +import org.apache.shenyu.admin.listener.utils.NodeDataPathUtils; +import org.apache.shenyu.common.constant.ApolloPathConstants; + +import java.util.Objects; +import java.util.stream.Stream; + +/** + * the type apollo data change init. + * + * @since 2.6.0 + */ +public class ApolloDataChangedInit extends AbstractDataChangedInit { + private final ApolloClient apolloClient; + + /** + * Instantiates a new apollo data changed init. + * + * @param apolloClient the apollo client + */ + public ApolloDataChangedInit(final ApolloClient apolloClient) { + this.apolloClient = apolloClient; + } + + /** + * not exist. + * + * @return true if not exist + */ + @Override + protected boolean notExist() { + return Stream.of(ApolloPathConstants.PLUGIN_DATA_ID, ApolloPathConstants.AUTH_DATA_ID, ApolloPathConstants.META_DATA_ID, ApolloPathConstants.PROXY_SELECTOR_DATA_ID) + .map(NodeDataPathUtils::appendListStuff) + .allMatch(this::dataIdNotExist); + } + + /** + * Data id not exist boolean. + * + * @param pluginDataId the plugin data id + * @return the boolean + */ + private boolean dataIdNotExist(final String pluginDataId) { + return Objects.isNull(apolloClient.getItemValue(pluginDataId)); + } +} diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/main/java/org/apache/shenyu/admin/listener/apollo/ApolloDataChangedListener.java b/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/main/java/org/apache/shenyu/admin/listener/apollo/ApolloDataChangedListener.java new file mode 100644 index 0000000..eab59e1 --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/main/java/org/apache/shenyu/admin/listener/apollo/ApolloDataChangedListener.java @@ -0,0 +1,63 @@ +/* + * 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 org.apache.shenyu.admin.listener.apollo; + +import org.apache.shenyu.admin.listener.AbstractNodeDataChangedListener; +import org.apache.shenyu.common.constant.ApolloPathConstants; + +/** + * use apollo to push data changes. + * + * @since 2.6.0 + */ +public class ApolloDataChangedListener extends AbstractNodeDataChangedListener { + private final ApolloClient apolloClient; + + /** + * Instantiates a new apollo data changed listener. + * + * @param apolloClient the apollo client + */ + public ApolloDataChangedListener(final ApolloClient apolloClient) { + super(new ChangeData(ApolloPathConstants.PLUGIN_DATA_ID, + ApolloPathConstants.SELECTOR_DATA_ID, + ApolloPathConstants.RULE_DATA_ID, + ApolloPathConstants.AUTH_DATA_ID, + ApolloPathConstants.META_DATA_ID, + ApolloPathConstants.PROXY_SELECTOR_DATA_ID, + ApolloPathConstants.DISCOVERY_DATA_ID)); + this.apolloClient = apolloClient; + } + + @Override + public void doPublishConfig(final String dataId, final Object data) { + this.apolloClient.createOrUpdateItem(dataId, data, "create config data"); + this.apolloClient.publishNamespace("publish config data", ""); + } + + @Override + public String getConfig(final String dataId) { + return this.apolloClient.getItemValue(dataId); + } + + @Override + public void doDelConfig(final String dataId) { + this.apolloClient.removeItemKey(dataId); + this.apolloClient.publishNamespace("delete config data", ""); + } +} diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/main/resources/META-INF/spring.factories b/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..f210012 --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/main/resources/META-INF/spring.factories @@ -0,0 +1,19 @@ +# +# 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. +# + +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +org.apache.shenyu.admin.config.ApolloSyncConfiguration diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/main/resources/META-INF/spring.provides b/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/main/resources/META-INF/spring.provides new file mode 100644 index 0000000..9893ff1 --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/main/resources/META-INF/spring.provides @@ -0,0 +1,18 @@ +# +# 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. +# + +provides: shenyu-admin-listener-apollo diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 0000000..cda1dd6 --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1,18 @@ +# +# 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. +# + +org.apache.shenyu.admin.config.ApolloSyncConfiguration diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/test/java/org/apache/shenyu/admin/config/ApolloSyncConfigurationTest.java b/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/test/java/org/apache/shenyu/admin/config/ApolloSyncConfigurationTest.java new file mode 100644 index 0000000..362aa64 --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/test/java/org/apache/shenyu/admin/config/ApolloSyncConfigurationTest.java @@ -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 org.apache.shenyu.admin.config; + +import org.apache.shenyu.admin.config.properties.ApolloProperties; +import org.apache.shenyu.admin.listener.DataChangedInit; +import org.apache.shenyu.admin.listener.DataChangedListener; +import org.apache.shenyu.admin.service.SyncDataService; +import org.junit.jupiter.api.Test; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; + +/** + * Auto-configuration contracts of the external Apollo Admin listener. + */ +class ApolloSyncConfigurationTest { + + private final ApplicationContextRunner runner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(ApolloSyncConfiguration.class)) + .withBean(SyncDataService.class, () -> mock(SyncDataService.class)); + + @Test + void createsAdminBeansAndBindsExistingProperties() { + runner.withPropertyValues("shenyu.sync.apollo.meta=http://127.0.0.1:8080", + "shenyu.sync.apollo.portalUrl=http://127.0.0.1:8070", "shenyu.sync.apollo.appId=shenyu", + "shenyu.sync.apollo.env=DEV", "shenyu.sync.apollo.clusterName=default", + "shenyu.sync.apollo.namespace=application", "shenyu.sync.apollo.token=test-token") + .run(context -> { + assertThat(context).hasNotFailed().hasSingleBean(DataChangedListener.class).hasSingleBean(DataChangedInit.class); + assertThat(context.getBean(ApolloProperties.class).getAppId()).isEqualTo("shenyu"); + assertThat(context.getBean(ApolloProperties.class).getNamespace()).isEqualTo("application"); + }); + } + + @Test + void backsOffWhenApolloMetaIsAbsent() { + runner.run(context -> assertThat(context).hasNotFailed().doesNotHaveBean(DataChangedListener.class).doesNotHaveBean(DataChangedInit.class)); + } +} diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/test/java/org/apache/shenyu/admin/listener/apollo/ApolloClientTest.java b/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/test/java/org/apache/shenyu/admin/listener/apollo/ApolloClientTest.java new file mode 100644 index 0000000..feb1651 --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/test/java/org/apache/shenyu/admin/listener/apollo/ApolloClientTest.java @@ -0,0 +1,93 @@ +/* + * 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 org.apache.shenyu.admin.listener.apollo; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.apache.shenyu.admin.config.properties.ApolloProperties; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@ContextConfiguration(classes = {ApolloProperties.class}) +@RunWith(SpringJUnit4ClassRunner.class) +public class ApolloClientTest { + + @MockBean + private ApolloClient apolloClient; + + /** + * Method under test: {@link ApolloClient#ApolloClient(ApolloProperties)}. + */ + @Test + public void testConstructor() { + ApolloProperties apolloConfig = new ApolloProperties(); + apolloConfig.setAppId("42"); + apolloConfig.setClusterName("Cluster Name"); + apolloConfig.setEnv("Env"); + apolloConfig.setMeta("Meta"); + apolloConfig.setNamespace("Namespace"); + apolloConfig.setPortalUrl("http://localhost:8080"); + apolloConfig.setToken("ABC123"); + new ApolloClient(apolloConfig); + assertEquals("42", apolloConfig.getAppId()); + assertEquals("ABC123", apolloConfig.getToken()); + assertEquals("http://localhost:8080", apolloConfig.getPortalUrl()); + assertEquals("Namespace", apolloConfig.getNamespace()); + assertEquals("Meta", apolloConfig.getMeta()); + assertEquals("Env", apolloConfig.getEnv()); + assertEquals("Cluster Name", apolloConfig.getClusterName()); + } + + /** + * Method under test: {@link ApolloClient#getItemValue(String)}. + */ + @Test + public void testGetItemValue() { + when(apolloClient.getItemValue(Mockito.any())).thenReturn("42"); + assertEquals("42", apolloClient.getItemValue("Key")); + verify(apolloClient).getItemValue(Mockito.any()); + } + + /** + * Method under test: {@link ApolloClient#createOrUpdateItem(String, Object, String)}. + */ + @Test + public void testCreateOrUpdateItem() { + doNothing().when(apolloClient) + .createOrUpdateItem(Mockito.any(), Mockito.any(), Mockito.any()); + apolloClient.createOrUpdateItem("Key", (Object) "Value", "Comment"); + verify(apolloClient).createOrUpdateItem(Mockito.any(), Mockito.any(), Mockito.any()); + } + + /** + * Method under test: {@link ApolloClient#publishNamespace(String, String)}. + */ + @Test + public void testPublishNamespace() { + doNothing().when(apolloClient).publishNamespace(Mockito.any(), Mockito.any()); + apolloClient.publishNamespace("Dr", "1.0.2"); + verify(apolloClient).publishNamespace(Mockito.any(), Mockito.any()); + } +} diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/test/java/org/apache/shenyu/admin/listener/apollo/ApolloDataChangedInitTest.java b/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/test/java/org/apache/shenyu/admin/listener/apollo/ApolloDataChangedInitTest.java new file mode 100644 index 0000000..3a02d13 --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/test/java/org/apache/shenyu/admin/listener/apollo/ApolloDataChangedInitTest.java @@ -0,0 +1,102 @@ +/* + * 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 org.apache.shenyu.admin.listener.apollo; + +import org.apache.shenyu.admin.config.properties.ApolloProperties; +import org.apache.shenyu.admin.listener.utils.NodeDataPathUtils; +import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.junit.runner.RunWith; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.apache.shenyu.common.constant.ApolloPathConstants.AUTH_DATA_ID; +import static org.apache.shenyu.common.constant.ApolloPathConstants.META_DATA_ID; +import static org.apache.shenyu.common.constant.ApolloPathConstants.PLUGIN_DATA_ID; +import static org.apache.shenyu.common.constant.ApolloPathConstants.PROXY_SELECTOR_DATA_ID; +import static org.junit.Assert.assertNotNull; +import static org.mockito.Mockito.when; + +@ContextConfiguration(classes = {ApolloDataChangedInit.class}) +@RunWith(SpringJUnit4ClassRunner.class) +public class ApolloDataChangedInitTest { + + @MockBean + private ApolloClient apolloClient; + + @MockBean + private ApolloDataChangedInit apolloDataChangedInit; + + /** + * Method under test: {@link ApolloDataChangedInit#ApolloDataChangedInit(ApolloClient)}. + */ + @Test + public void testConstructor() { + ApolloProperties apolloConfig = new ApolloProperties(); + apolloConfig.setAppId("42"); + apolloConfig.setClusterName("Cluster Name"); + apolloConfig.setEnv("Env"); + apolloConfig.setMeta("Meta"); + apolloConfig.setNamespace("Namespace"); + apolloConfig.setPortalUrl("http://localhost:8080"); + apolloConfig.setToken("ABC123"); + ApolloClient apolloClient = new ApolloClient(apolloConfig); + ApolloDataChangedInit actualApolloDataChangedInit = new ApolloDataChangedInit(apolloClient); + assertNotNull(actualApolloDataChangedInit); + } + + /** + * Method under test: {@link ApolloDataChangedInit#notExist()}. + */ + @Test + public void testNotExist() { + + when(apolloClient.getItemValue(join(PLUGIN_DATA_ID))).thenReturn(PLUGIN_DATA_ID); + boolean pluginExist = apolloDataChangedInit.notExist(); + Assertions.assertFalse(pluginExist, "plugin exist."); + when(apolloClient.getItemValue(join(PLUGIN_DATA_ID))).thenReturn(null); + + when(apolloClient.getItemValue(join(AUTH_DATA_ID))).thenReturn(AUTH_DATA_ID); + boolean authExist = apolloDataChangedInit.notExist(); + Assertions.assertFalse(authExist, "auth exist."); + when(apolloClient.getItemValue(join(AUTH_DATA_ID))).thenReturn(null); + + when(apolloClient.getItemValue(join(META_DATA_ID))).thenReturn(META_DATA_ID); + boolean metaDataExist = apolloDataChangedInit.notExist(); + Assertions.assertFalse(metaDataExist, "metadata exist."); + when(apolloClient.getItemValue(join(META_DATA_ID))).thenReturn(null); + + when(apolloClient.getItemValue(join(PROXY_SELECTOR_DATA_ID))).thenReturn(PROXY_SELECTOR_DATA_ID); + boolean selectorDataExist = apolloDataChangedInit.notExist(); + Assertions.assertFalse(selectorDataExist, "selector exist."); + } + + @Test + public void testAllExist() { + when(apolloClient.getItemValue(join(PLUGIN_DATA_ID))).thenReturn(META_DATA_ID); + when(apolloClient.getItemValue(join(AUTH_DATA_ID))).thenReturn(META_DATA_ID); + when(apolloClient.getItemValue(join(META_DATA_ID))).thenReturn(META_DATA_ID); + when(apolloClient.getItemValue(join(PROXY_SELECTOR_DATA_ID))).thenReturn(META_DATA_ID); + Assertions.assertFalse(apolloDataChangedInit.notExist(), "some key not exist."); + } + + private String join(final String text) { + return NodeDataPathUtils.appendListStuff(text); + } +} diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/test/java/org/apache/shenyu/admin/listener/apollo/ApolloDataChangedListenerTest.java b/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/test/java/org/apache/shenyu/admin/listener/apollo/ApolloDataChangedListenerTest.java new file mode 100644 index 0000000..09fa89d --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-admin-listener-apollo/src/test/java/org/apache/shenyu/admin/listener/apollo/ApolloDataChangedListenerTest.java @@ -0,0 +1,92 @@ +/* + * 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 org.apache.shenyu.admin.listener.apollo; + +import org.apache.shenyu.admin.config.properties.ApolloProperties; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ContextConfiguration(classes = {ApolloDataChangedListener.class}) +@RunWith(SpringJUnit4ClassRunner.class) +public class ApolloDataChangedListenerTest { + + @MockBean + private ApolloClient apolloClient; + + @Autowired + private ApolloDataChangedListener apolloDataChangedListener; + + /** + * Method under test: {@link ApolloDataChangedListener#ApolloDataChangedListener(ApolloClient)}. + */ + @Test + public void testConstructor() { + ApolloProperties apolloConfig = new ApolloProperties(); + apolloConfig.setAppId("42"); + apolloConfig.setClusterName("Cluster Name"); + apolloConfig.setEnv("Env"); + apolloConfig.setMeta("Meta"); + apolloConfig.setNamespace("Namespace"); + apolloConfig.setPortalUrl("http://localhost:8080"); + apolloConfig.setToken("ABC123"); + new ApolloDataChangedListener(new ApolloClient(apolloConfig)); + } + + /** + * Method under test: {@link ApolloDataChangedListener#doPublishConfig(String, Object)}. + */ + @Test + public void testPublishConfig() { + doNothing().when(apolloClient) + .createOrUpdateItem(Mockito.any(), Mockito.any(), Mockito.any()); + doNothing().when(apolloClient).publishNamespace(Mockito.any(), Mockito.any()); + apolloDataChangedListener.doPublishConfig("42", "Data"); + verify(apolloClient).createOrUpdateItem(Mockito.any(), Mockito.any(), Mockito.any()); + verify(apolloClient).publishNamespace(Mockito.any(), Mockito.any()); + } + + /** + * Method under test: {@link ApolloDataChangedListener#getConfig(String)}. + */ + @Test + public void testGetConfig() { + when(apolloClient.getItemValue(Mockito.any())).thenReturn("42"); + assertEquals("42", apolloDataChangedListener.getConfig("42")); + verify(apolloClient).getItemValue(Mockito.any()); + } + + /** + * Method under test: {@link ApolloDataChangedListener#getConfig(String)}. + */ + @Test + public void testGetConfig2() { + when(apolloClient.getItemValue(Mockito.any())).thenReturn(""); + assertEquals("", apolloDataChangedListener.getConfig("42")); + verify(apolloClient).getItemValue(Mockito.any()); + } +} diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-registry-apollo/pom.xml b/shenyu-extension/shenyu-extension-apollo/shenyu-registry-apollo/pom.xml new file mode 100644 index 0000000..a15b1d2 --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-registry-apollo/pom.xml @@ -0,0 +1,91 @@ + + + + + + org.apache.shenyu + shenyu-extension-apollo + 2.7.1-SNAPSHOT + + 4.0.0 + shenyu-registry-apollo + + + + org.apache.shenyu + shenyu-registry-api + ${shenyu.version} + + + + com.ctrip.framework.apollo + apollo-client + + + + com.ctrip.framework.apollo + apollo-openapi + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.apache.shenyu + shenyu-registry-core + ${shenyu.version} + test + + + + + + + org.apache.maven.plugins + maven-shade-plugin + ${maven-shade-plugin.version} + + false + + + org.apache.shenyu:shenyu-common + org.apache.shenyu:shenyu-spi + org.apache.shenyu:shenyu-admin-listener-api + org.apache.shenyu:shenyu-sync-data-api + org.apache.shenyu:shenyu-registry-api + org.apache.shenyu:shenyu-registry-core + + + + + + + + + package + + shade + + + + + + + diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-registry-apollo/src/main/java/org/apache/shenyu/registry/apollo/ApolloClient.java b/shenyu-extension/shenyu-extension-apollo/shenyu-registry-apollo/src/main/java/org/apache/shenyu/registry/apollo/ApolloClient.java new file mode 100644 index 0000000..23b6915 --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-registry-apollo/src/main/java/org/apache/shenyu/registry/apollo/ApolloClient.java @@ -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 org.apache.shenyu.registry.apollo; + +import com.ctrip.framework.apollo.openapi.client.ApolloOpenApiClient; +import com.ctrip.framework.apollo.openapi.dto.NamespaceReleaseDTO; +import com.ctrip.framework.apollo.openapi.dto.OpenItemDTO; +import org.apache.shenyu.common.utils.GsonUtils; + +import java.util.Date; + +/** + * apollo open-api client. + */ +public class ApolloClient { + + private static final String DEFAULT_USER = "apollo"; + + private final ApolloConfig apolloConfig; + + private final ApolloOpenApiClient apolloOpenApiClient; + + public ApolloClient(final ApolloConfig apolloConfig) { + this.apolloConfig = apolloConfig; + + this.apolloOpenApiClient = ApolloOpenApiClient + .newBuilder() + .withPortalUrl(apolloConfig.getPortalUrl()) + .withToken(apolloConfig.getToken()) + .build(); + } + + /** + * get item value. + * @param key item key + * @return item value + */ + public String getItemValue(final String key) { + OpenItemDTO openItemDTO = this.apolloOpenApiClient.getItem( + apolloConfig.getAppId(), + apolloConfig.getEnv(), + apolloConfig.getClusterName(), + apolloConfig.getNamespace(), + key + ); + return openItemDTO.getValue(); + } + + /** + * create or update item into namespace. + * @param key item key + * @param value item value + * @param comment item comment + */ + public void createOrUpdateItem(final String key, final Object value, final String comment) { + this.createOrUpdateItem(key, GsonUtils.getInstance().toJson(value), comment); + } + + /** + * create or update item into namespace. + * @param key item key + * @param value item value + * @param comment item comment + */ + public void createOrUpdateItem(final String key, final String value, final String comment) { + OpenItemDTO openItemDTO = new OpenItemDTO(); + openItemDTO.setKey(key); + openItemDTO.setValue(value); + openItemDTO.setComment(comment); + openItemDTO.setDataChangeCreatedBy(DEFAULT_USER); + openItemDTO.setDataChangeLastModifiedBy(DEFAULT_USER); + Date now = new Date(); + openItemDTO.setDataChangeCreatedTime(now); + openItemDTO.setDataChangeLastModifiedTime(now); + + this.apolloOpenApiClient.createOrUpdateItem( + apolloConfig.getAppId(), + apolloConfig.getEnv(), + apolloConfig.getClusterName(), + apolloConfig.getNamespace(), + openItemDTO + ); + } + + /** + * remove item from namespace. + * @param key item key + */ + public void removeItem(final String key) { + this.apolloOpenApiClient.removeItem( + apolloConfig.getAppId(), + apolloConfig.getEnv(), + apolloConfig.getClusterName(), + apolloConfig.getNamespace(), + key, + DEFAULT_USER + ); + } + + /** + * publish item list in namespace. + * @param releaseTitle publish release title + * @param releaseComment publish release comment + */ + public void publishNamespace(final String releaseTitle, final String releaseComment) { + NamespaceReleaseDTO namespaceReleaseDTO = new NamespaceReleaseDTO(); + namespaceReleaseDTO.setReleaseTitle(releaseTitle); + namespaceReleaseDTO.setReleaseComment(releaseComment); + namespaceReleaseDTO.setReleasedBy(DEFAULT_USER); + + this.apolloOpenApiClient.publishNamespace( + apolloConfig.getAppId(), + apolloConfig.getEnv(), + apolloConfig.getClusterName(), + apolloConfig.getNamespace(), + namespaceReleaseDTO + ); + } +} diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-registry-apollo/src/main/java/org/apache/shenyu/registry/apollo/ApolloConfig.java b/shenyu-extension/shenyu-extension-apollo/shenyu-registry-apollo/src/main/java/org/apache/shenyu/registry/apollo/ApolloConfig.java new file mode 100644 index 0000000..d53f6ee --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-registry-apollo/src/main/java/org/apache/shenyu/registry/apollo/ApolloConfig.java @@ -0,0 +1,149 @@ +/* + * 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 org.apache.shenyu.registry.apollo; + +public class ApolloConfig { + + /** + * appId. + */ + private String appId; + + /** + * portal url. + * e.g. localhost:8080 + */ + private String portalUrl; + + /** + * env. + * e.g. ENV + */ + private String env; + + /** + * cluster name. + */ + private String clusterName; + + /** + * namespace name. + */ + private String namespace; + + /** + * open api use token. + */ + private String token; + + /** + * get appId. + * @return appId + */ + public String getAppId() { + return appId; + } + + /** + * set app id. + * @param appId app id + */ + public void setAppId(final String appId) { + this.appId = appId; + } + + /** + * get portal url. + * @return portal url + */ + public String getPortalUrl() { + return portalUrl; + } + + /** + * set portal url. + * @param portalUrl portal url + */ + public void setPortalUrl(final String portalUrl) { + this.portalUrl = portalUrl; + } + + /** + * get env. + * @return env + */ + public String getEnv() { + return env; + } + + /** + * set env. + * @param env env + */ + public void setEnv(final String env) { + this.env = env; + } + + /** + * get cluster name. + * @return cluster name + */ + public String getClusterName() { + return clusterName; + } + + /** + * set cluster name. + * @param clusterName cluster name + */ + public void setClusterName(final String clusterName) { + this.clusterName = clusterName; + } + + /** + * get namespace name. + * @return namespace name + */ + public String getNamespace() { + return namespace; + } + + /** + * set namespace name. + * @param namespace namespace name + */ + public void setNamespace(final String namespace) { + this.namespace = namespace; + } + + /** + * get token. + * @return token + */ + public String getToken() { + return token; + } + + /** + * set token. + * @param token token + */ + public void setToken(final String token) { + this.token = token; + } +} diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-registry-apollo/src/main/java/org/apache/shenyu/registry/apollo/ApolloInstanceRegisterRepository.java b/shenyu-extension/shenyu-extension-apollo/shenyu-registry-apollo/src/main/java/org/apache/shenyu/registry/apollo/ApolloInstanceRegisterRepository.java new file mode 100644 index 0000000..94e8c22 --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-registry-apollo/src/main/java/org/apache/shenyu/registry/apollo/ApolloInstanceRegisterRepository.java @@ -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 org.apache.shenyu.registry.apollo; + +import com.ctrip.framework.apollo.Config; +import com.ctrip.framework.apollo.ConfigChangeListener; +import com.ctrip.framework.apollo.ConfigService; +import com.ctrip.framework.apollo.core.ConfigConsts; +import com.google.common.collect.Maps; +import org.apache.shenyu.common.constant.Constants; +import org.apache.shenyu.common.utils.GsonUtils; +import org.apache.shenyu.registry.api.ShenyuInstanceRegisterRepository; +import org.apache.shenyu.registry.api.config.RegisterConfig; +import org.apache.shenyu.registry.api.entity.InstanceEntity; +import org.apache.shenyu.registry.api.path.InstancePathConstants; +import org.apache.shenyu.spi.Join; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.URI; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Properties; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Function; +import java.util.stream.Collectors; + +/** + * The type apollo instance register repository. + */ +@Join(isSingleton = false) +public class ApolloInstanceRegisterRepository implements ShenyuInstanceRegisterRepository { + + private static final String APOLLO_CLUSTER = "apollo.cluster"; + + private static final String PROP_APP_ID = "app.id"; + + private static final String PROP_APOLLO_META = "apollo.meta"; + + private static final String APOLLO_NAMESPACE = "apollo.bootstrap.namespace"; + + private static final Logger LOGGER = LoggerFactory.getLogger(ApolloInstanceRegisterRepository.class); + + private ApolloClient apolloClient; + + private final Map configChangeListenerMap = Maps.newConcurrentMap(); + + private final Map> watcherInstanceRegisterMap = new ConcurrentHashMap<>(); + + private String namespace; + + private Config configService; + + @Override + public void init(final RegisterConfig config) { + Properties properties = config.getProps(); + String meta = config.getServerLists(); + String appId = properties.getProperty("appId", "shenyu"); + String clusterName = properties.getProperty("clusterName", ConfigConsts.CLUSTER_NAME_DEFAULT); + String namespace = properties.getProperty("namespace", ConfigConsts.NAMESPACE_APPLICATION); + Optional.ofNullable(appId).ifPresent(x -> System.setProperty(PROP_APP_ID, x)); + Optional.ofNullable(meta).ifPresent(x -> System.setProperty(PROP_APOLLO_META, x)); + Optional.ofNullable(clusterName).ifPresent(x -> System.setProperty(APOLLO_CLUSTER, x)); + Optional.ofNullable(namespace).ifPresent(x -> System.setProperty(APOLLO_NAMESPACE, x)); + Optional.ofNullable(namespace).ifPresent(x -> System.setProperty(APOLLO_NAMESPACE, x)); + this.namespace = namespace; + this.configService = ConfigService.getAppConfig(); + buildClient(config); + } + + private void buildClient(final RegisterConfig config) { + // init apollo client + Properties properties = config.getProps(); + String appId = properties.getProperty("appId"); + String token = properties.getProperty("token"); + String env = properties.getProperty("env", "DEV"); + String clusterName = properties.getProperty("clusterName", ConfigConsts.CLUSTER_NAME_DEFAULT); + String namespace = properties.getProperty("namespace", ConfigConsts.NAMESPACE_APPLICATION); + String portalUrl = properties.getProperty("portalUrl", "http://localhost:8070"); + ApolloConfig apolloConfig = new ApolloConfig(); + apolloConfig.setAppId(appId); + apolloConfig.setPortalUrl(portalUrl); + apolloConfig.setToken(token); + apolloConfig.setEnv(env); + apolloConfig.setClusterName(clusterName); + apolloConfig.setNamespace(namespace); + this.apolloClient = new ApolloClient(apolloConfig); + } + + @Override + public void persistInstance(final InstanceEntity instance) { + String instanceNodeName = buildInstanceNodeName(instance); + String instancePath = InstancePathConstants.buildInstanceParentPath(instance.getAppName()); + String realNode = InstancePathConstants.buildRealNode(instancePath, instanceNodeName); + String nodeData = GsonUtils.getInstance().toJson(instance); + apolloClient.createOrUpdateItem(realNode, nodeData, "register instance"); + apolloClient.publishNamespace(namespace, "publish instance"); + LOGGER.info("apollo instance register success: {}", nodeData); + } + + @Override + public List selectInstances(final String selectKey) { + final String watchKey = InstancePathConstants.buildInstanceParentPath(selectKey); + + final Function, List> getInstanceRegisterFun = childrenList -> + childrenList.values().stream().map(x -> { + InstanceEntity instanceEntity = GsonUtils.getInstance().fromJson(x, InstanceEntity.class); + instanceEntity.setUri(getURI(x, instanceEntity.getPort(), instanceEntity.getHost())); + return instanceEntity; + }).collect(Collectors.toList()); + Map childrenList = new ConcurrentHashMap<>(); + + final List cachedInstances = watcherInstanceRegisterMap.get(selectKey); + if (Objects.nonNull(cachedInstances)) { + return cachedInstances; + } + + configService.getPropertyNames().forEach(key -> { + if (key.startsWith(watchKey)) { + String itemValue = apolloClient.getItemValue(key); + childrenList.put(key, itemValue); + } + }); + ConfigChangeListener configChangeListener = changeEvent -> { + Set keys = changeEvent.changedKeys(); + keys.forEach(key -> { + if (key.startsWith(watchKey)) { + switch (changeEvent.getChange(key).getChangeType()) { + case ADDED: + case MODIFIED: + childrenList.put(key, changeEvent.getChange(key).getNewValue()); + LOGGER.info("apollo instance register add or update {}", changeEvent.getChange(key).getNewValue()); + break; + case DELETED: + childrenList.remove(key); + LOGGER.info("apollo instance register delete success: {}", changeEvent.getChange(key).getOldValue()); + break; + default: + break; + } + watcherInstanceRegisterMap.put(selectKey, getInstanceRegisterFun.apply(childrenList)); + } + }); + }; + configService.addChangeListener(configChangeListener); + configChangeListenerMap.put(watchKey, configChangeListener); + final List instanceEntities = getInstanceRegisterFun.apply(childrenList); + watcherInstanceRegisterMap.put(selectKey, instanceEntities); + return instanceEntities; + } + + private URI getURI(final String instanceRegisterJsonStr, final int port, final String host) { + String scheme = (instanceRegisterJsonStr.contains("https") || instanceRegisterJsonStr.contains("HTTPS")) ? "https" : "http"; + String uri = String.format("%s://%s:%s", scheme, host, port); + return URI.create(uri); + } + + private String buildInstanceNodeName(final InstanceEntity instance) { + String host = instance.getHost(); + int port = instance.getPort(); + return String.join(Constants.COLONS, host, Integer.toString(port)); + } + + @Override + public void close() { + configChangeListenerMap.forEach((key, value) -> configService.removeChangeListener(value)); + } +} diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-registry-apollo/src/main/resources/META-INF/shenyu/org.apache.shenyu.registry.api.ShenyuInstanceRegisterRepository b/shenyu-extension/shenyu-extension-apollo/shenyu-registry-apollo/src/main/resources/META-INF/shenyu/org.apache.shenyu.registry.api.ShenyuInstanceRegisterRepository new file mode 100644 index 0000000..201b521 --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-registry-apollo/src/main/resources/META-INF/shenyu/org.apache.shenyu.registry.api.ShenyuInstanceRegisterRepository @@ -0,0 +1,17 @@ +# 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. + +apollo=org.apache.shenyu.registry.apollo.ApolloInstanceRegisterRepository diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-registry-apollo/src/test/java/org/apache/shenyu/registry/apollo/ApolloClientTest.java b/shenyu-extension/shenyu-extension-apollo/shenyu-registry-apollo/src/test/java/org/apache/shenyu/registry/apollo/ApolloClientTest.java new file mode 100644 index 0000000..85d94b1 --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-registry-apollo/src/test/java/org/apache/shenyu/registry/apollo/ApolloClientTest.java @@ -0,0 +1,97 @@ +/* + * 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 org.apache.shenyu.registry.apollo; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mockito; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@ContextConfiguration(classes = {ApolloConfig.class}) +@RunWith(SpringJUnit4ClassRunner.class) +public class ApolloClientTest { + + @MockBean + private ApolloClient apolloClient; + + @Test + public void testConstructor() { + ApolloConfig apolloConfig = new ApolloConfig(); + apolloConfig.setAppId("42"); + apolloConfig.setClusterName("Cluster Name"); + apolloConfig.setEnv("Env"); + apolloConfig.setNamespace("Namespace"); + apolloConfig.setPortalUrl("http://localhost:8080"); + apolloConfig.setToken("ABC123"); + new ApolloClient(apolloConfig); + assertEquals("42", apolloConfig.getAppId()); + assertEquals("ABC123", apolloConfig.getToken()); + assertEquals("http://localhost:8080", apolloConfig.getPortalUrl()); + assertEquals("Namespace", apolloConfig.getNamespace()); + assertEquals("Env", apolloConfig.getEnv()); + assertEquals("Cluster Name", apolloConfig.getClusterName()); + } + + /** + * Method under test: {@link ApolloClient#getItemValue(String)}. + */ + @Test + public void testGetItemValue() { + when(apolloClient.getItemValue(Mockito.any())).thenReturn("42"); + assertEquals("42", apolloClient.getItemValue("Key")); + verify(apolloClient).getItemValue(Mockito.any()); + } + + /** + * Method under test: {@link ApolloClient#createOrUpdateItem(String, Object, String)}. + */ + @Test + public void testCreateOrUpdateItem() { + doNothing().when(apolloClient) + .createOrUpdateItem(Mockito.any(), Mockito.any(), Mockito.any()); + apolloClient.createOrUpdateItem("Key", (Object) "Value", "Comment"); + verify(apolloClient).createOrUpdateItem(Mockito.any(), Mockito.any(), Mockito.any()); + } + + /** + * Method under test: {@link ApolloClient#removeItem(String)}. + */ + @Test + public void testRemoveItem() { + String keyToRemove = "KeyToRemove"; + apolloClient.removeItem(keyToRemove); + verify(apolloClient).removeItem(keyToRemove); + } + + /** + * Method under test: {@link ApolloClient#publishNamespace(String, String)}. + */ + @Test + public void testPublishNamespace() { + doNothing().when(apolloClient).publishNamespace(Mockito.any(), Mockito.any()); + apolloClient.publishNamespace("Dr", "1.0.2"); + verify(apolloClient).publishNamespace(Mockito.any(), Mockito.any()); + } +} diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-registry-apollo/src/test/java/org/apache/shenyu/registry/apollo/ApolloConfigTest.java b/shenyu-extension/shenyu-extension-apollo/shenyu-registry-apollo/src/test/java/org/apache/shenyu/registry/apollo/ApolloConfigTest.java new file mode 100644 index 0000000..880fc6a --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-registry-apollo/src/test/java/org/apache/shenyu/registry/apollo/ApolloConfigTest.java @@ -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 org.apache.shenyu.registry.apollo; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class ApolloConfigTest { + /** + * Methods under test. + * + *
    + *
  • default or parameterless constructor of {@link ApolloConfig} + *
  • {@link ApolloConfig#setAppId(String)} + *
  • {@link ApolloConfig#setClusterName(String)} + *
  • {@link ApolloConfig#setEnv(String)} + *
  • {@link ApolloConfig#setNamespace(String)} + *
  • {@link ApolloConfig#setPortalUrl(String)} + *
  • {@link ApolloConfig#setToken(String)} + *
  • {@link ApolloConfig#getAppId()} + *
  • {@link ApolloConfig#getClusterName()} + *
  • {@link ApolloConfig#getEnv()} + *
  • {@link ApolloConfig#getNamespace()} + *
  • {@link ApolloConfig#getPortalUrl()} + *
  • {@link ApolloConfig#getToken()} + *
+ */ + @Test + public void testConstructor() { + ApolloConfig actualApolloConfig = new ApolloConfig(); + actualApolloConfig.setAppId("42"); + actualApolloConfig.setClusterName("Cluster Name"); + actualApolloConfig.setEnv("Env"); + actualApolloConfig.setNamespace("Namespace"); + actualApolloConfig.setPortalUrl("https://example.org/example"); + actualApolloConfig.setToken("ABC123"); + assertEquals("42", actualApolloConfig.getAppId()); + assertEquals("Cluster Name", actualApolloConfig.getClusterName()); + assertEquals("Env", actualApolloConfig.getEnv()); + assertEquals("Namespace", actualApolloConfig.getNamespace()); + assertEquals("https://example.org/example", actualApolloConfig.getPortalUrl()); + assertEquals("ABC123", actualApolloConfig.getToken()); + } +} diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-registry-apollo/src/test/java/org/apache/shenyu/registry/apollo/ApolloInstanceRegisterRepositoryTest.java b/shenyu-extension/shenyu-extension-apollo/shenyu-registry-apollo/src/test/java/org/apache/shenyu/registry/apollo/ApolloInstanceRegisterRepositoryTest.java new file mode 100644 index 0000000..bc31e1c --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-registry-apollo/src/test/java/org/apache/shenyu/registry/apollo/ApolloInstanceRegisterRepositoryTest.java @@ -0,0 +1,109 @@ +/* + * 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 org.apache.shenyu.registry.apollo; + +import com.ctrip.framework.apollo.Config; +import com.ctrip.framework.apollo.ConfigChangeListener; +import com.ctrip.framework.apollo.enums.PropertyChangeType; +import com.ctrip.framework.apollo.model.ConfigChange; +import com.ctrip.framework.apollo.model.ConfigChangeEvent; +import org.apache.shenyu.common.utils.GsonUtils; +import org.apache.shenyu.registry.api.entity.InstanceEntity; +import org.apache.shenyu.registry.api.path.InstancePathConstants; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; +import org.springframework.test.util.ReflectionTestUtils; + +import java.net.URI; +import java.util.Collections; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public final class ApolloInstanceRegisterRepositoryTest { + + private ApolloInstanceRegisterRepository repository; + + private ConfigChangeListener listener; + + private String instanceKey; + + private String originalValue; + + @BeforeEach + public void setUp() { + repository = new ApolloInstanceRegisterRepository(); + Config config = mock(Config.class); + ApolloClient client = mock(ApolloClient.class); + ReflectionTestUtils.setField(repository, "configService", config); + ReflectionTestUtils.setField(repository, "apolloClient", client); + instanceKey = InstancePathConstants.buildRealNode(InstancePathConstants.buildInstanceParentPath("service"), "instance"); + originalValue = instanceJson("127.0.0.1", 8080); + when(config.getPropertyNames()).thenReturn(Collections.singleton(instanceKey)); + when(client.getItemValue(instanceKey)).thenReturn(originalValue); + assertEquals("127.0.0.1", repository.selectInstances("service").get(0).getHost()); + ArgumentCaptor captor = ArgumentCaptor.forClass(ConfigChangeListener.class); + verify(config).addChangeListener(captor.capture()); + listener = captor.getValue(); + } + + @Test + public void testModifiedInstanceRefreshesCache() { + publishChange(instanceKey, originalValue, instanceJson("127.0.0.2", 9090), PropertyChangeType.MODIFIED); + List instances = repository.selectInstances("service"); + assertEquals(1, instances.size()); + assertEquals("127.0.0.2", instances.get(0).getHost()); + assertEquals(9090, instances.get(0).getPort()); + assertEquals(URI.create("http://127.0.0.2:9090"), instances.get(0).getUri()); + } + + @Test + public void testAddedAndDeletedInstancesRefreshCache() { + String addedKey = instanceKey + "-second"; + String addedValue = instanceJson("127.0.0.2", 9090); + publishChange(addedKey, null, addedValue, PropertyChangeType.ADDED); + assertEquals(2, repository.selectInstances("service").size()); + publishChange(instanceKey, originalValue, null, PropertyChangeType.DELETED); + assertEquals("127.0.0.2", repository.selectInstances("service").get(0).getHost()); + publishChange(addedKey, addedValue, null, PropertyChangeType.DELETED); + assertTrue(repository.selectInstances("service").isEmpty()); + } + + @Test + public void testUnrelatedServiceChangeDoesNotAffectCache() { + String unrelatedKey = InstancePathConstants.buildRealNode(InstancePathConstants.buildInstanceParentPath("another-service"), "instance"); + publishChange(unrelatedKey, originalValue, instanceJson("127.0.0.2", 9090), PropertyChangeType.MODIFIED); + List instances = repository.selectInstances("service"); + assertEquals(1, instances.size()); + assertEquals("127.0.0.1", instances.get(0).getHost()); + } + + private void publishChange(final String key, final String oldValue, final String newValue, final PropertyChangeType type) { + ConfigChange change = new ConfigChange("application", key, oldValue, newValue, type); + listener.onChange(new ConfigChangeEvent("application", Collections.singletonMap(key, change))); + } + + private String instanceJson(final String host, final int port) { + return GsonUtils.getInstance().toJson(InstanceEntity.builder().appName("service").host(host).port(port).build()); + } +} diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-registry-apollo/src/test/java/org/apache/shenyu/registry/apollo/RegistrySpiTest.java b/shenyu-extension/shenyu-extension-apollo/shenyu-registry-apollo/src/test/java/org/apache/shenyu/registry/apollo/RegistrySpiTest.java new file mode 100644 index 0000000..436a184 --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-registry-apollo/src/test/java/org/apache/shenyu/registry/apollo/RegistrySpiTest.java @@ -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 org.apache.shenyu.registry.apollo; + +import org.apache.shenyu.registry.api.ShenyuInstanceRegisterRepository; +import org.apache.shenyu.spi.ExtensionLoader; +import org.apache.shenyu.registry.core.ShenyuInstanceRegisterRepositoryFactory; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotSame; + +class RegistrySpiTest { + + @Test + void discoversPublishedRegistryKey() { + ShenyuInstanceRegisterRepository repository = ExtensionLoader.getExtensionLoader(ShenyuInstanceRegisterRepository.class).getJoin("apollo"); + assertEquals(ApolloInstanceRegisterRepository.class, repository.getClass()); + } + + @Test + void existingFactoryLoadsExternalApolloProvider() { + ShenyuInstanceRegisterRepository repository = ShenyuInstanceRegisterRepositoryFactory.loadRepository("apollo"); + assertEquals(ApolloInstanceRegisterRepository.class, repository.getClass()); + assertNotSame(repository, ShenyuInstanceRegisterRepositoryFactory.loadRepository("apollo")); + } +} diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-spring-boot-starter-sync-data-apollo/pom.xml b/shenyu-extension/shenyu-extension-apollo/shenyu-spring-boot-starter-sync-data-apollo/pom.xml new file mode 100644 index 0000000..f23bcdc --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-spring-boot-starter-sync-data-apollo/pom.xml @@ -0,0 +1,85 @@ + + + + + + org.apache.shenyu + shenyu-extension-apollo + 2.7.1-SNAPSHOT + + 4.0.0 + + shenyu-spring-boot-starter-sync-data-apollo + + + + org.apache.shenyu + shenyu-sync-data-apollo + ${project.version} + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-autoconfigure + + + org.springframework.boot + spring-boot-starter-web + test + + + + + + + org.apache.maven.plugins + maven-shade-plugin + ${maven-shade-plugin.version} + + false + + + org.apache.shenyu:shenyu-common + org.apache.shenyu:shenyu-spi + org.apache.shenyu:shenyu-admin-listener-api + org.apache.shenyu:shenyu-sync-data-api + org.apache.shenyu:shenyu-registry-api + org.apache.shenyu:shenyu-registry-core + org.apache.shenyu:shenyu-sync-data-apollo + + + + + + + + + package + + shade + + + + + + + diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-spring-boot-starter-sync-data-apollo/src/main/java/org/apache/shenyu/springboot/sync/data/apollo/ApolloSyncDataConfiguration.java b/shenyu-extension/shenyu-extension-apollo/shenyu-spring-boot-starter-sync-data-apollo/src/main/java/org/apache/shenyu/springboot/sync/data/apollo/ApolloSyncDataConfiguration.java new file mode 100644 index 0000000..ea926cc --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-spring-boot-starter-sync-data-apollo/src/main/java/org/apache/shenyu/springboot/sync/data/apollo/ApolloSyncDataConfiguration.java @@ -0,0 +1,115 @@ +/* + * 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 org.apache.shenyu.springboot.sync.data.apollo; + +import com.ctrip.framework.apollo.Config; +import com.ctrip.framework.apollo.ConfigService; +import org.apache.shenyu.common.config.ShenyuConfig; +import org.apache.shenyu.sync.data.api.AuthDataSubscriber; +import org.apache.shenyu.sync.data.api.MetaDataSubscriber; +import org.apache.shenyu.sync.data.api.PluginDataSubscriber; +import org.apache.shenyu.sync.data.api.ProxySelectorDataSubscriber; +import org.apache.shenyu.sync.data.api.DiscoveryUpstreamDataSubscriber; +import org.apache.shenyu.sync.data.apollo.ApolloDataService; +import org.apache.shenyu.sync.data.apollo.config.ApolloConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.Collections; +import java.util.List; +import java.util.Optional; + +/** + * Apollo sync data configuration for spring boot. + */ +@Configuration +@ConditionalOnClass(ApolloSyncDataConfiguration.class) +@ConditionalOnProperty(prefix = "shenyu.sync.apollo", name = "meta") +public class ApolloSyncDataConfiguration { + + private static final Logger LOGGER = LoggerFactory.getLogger(ApolloSyncDataConfiguration.class); + + private static final String APOLLO_CLUSTER = "apollo.cluster"; + + private static final String PROP_APP_ID = "app.id"; + + private static final String PROP_APOLLO_META = "apollo.meta"; + + private static final String PROP_APOLLO_SECRET = "apollo.access-key"; + + private static final String APOLLO_NAMESPACE = "apollo.bootstrap.namespace"; + + /** + * Apollo config apollo config. + * @param configService the config service + * @param pluginSubscriber the plugin subscriber + * @param metaSubscribers the meta subscribers + * @param authSubscribers the auth subscribers + * @param proxySelectorDataSubscriber the proxySelector subscribers + * @param discoveryUpstreamDataSubscribers the discoveryUpstream subscribers + * @param shenyuConfig the shenyu config + * + * @return the apollo config + */ + @Bean + public ApolloDataService apolloSyncDataService(final ObjectProvider configService, + final ObjectProvider pluginSubscriber, + final ObjectProvider> metaSubscribers, + final ObjectProvider> authSubscribers, + final ObjectProvider> proxySelectorDataSubscriber, + final ObjectProvider> discoveryUpstreamDataSubscribers, + final ObjectProvider shenyuConfig) { + LOGGER.info("you use apollo sync shenyu data......."); + return new ApolloDataService(configService.getIfAvailable(), pluginSubscriber.getIfAvailable(), + metaSubscribers.getIfAvailable(Collections::emptyList), authSubscribers.getIfAvailable(Collections::emptyList), proxySelectorDataSubscriber.getIfAvailable(Collections::emptyList), + discoveryUpstreamDataSubscribers.getIfAvailable(), shenyuConfig.getIfAvailable()); + } + + /** + * Apollo config config. + * + * @param apolloConfig the apollo config + * @return the config + */ + @Bean + public Config apolloConfigService(final ApolloConfig apolloConfig) { + Optional.ofNullable(apolloConfig.getAppId()).ifPresent(appId -> System.setProperty(PROP_APP_ID, appId)); + Optional.ofNullable(apolloConfig.getMeta()).ifPresent(meta -> System.setProperty(PROP_APOLLO_META, meta)); + Optional.ofNullable(apolloConfig.getClusterName()).ifPresent(cluster -> System.setProperty(APOLLO_CLUSTER, cluster)); + Optional.ofNullable(apolloConfig.getNamespace()).ifPresent(namespace -> System.setProperty(APOLLO_NAMESPACE, namespace)); + Optional.ofNullable(apolloConfig.getAccessKey()).ifPresent(accessKey -> System.setProperty(PROP_APOLLO_SECRET, accessKey)); + return ConfigService.getAppConfig(); + } + + /** + * apollo config. + * + * @return the apollo config + */ + @Bean + @ConfigurationProperties(prefix = "shenyu.sync.apollo") + public ApolloConfig apolloConfig() { + return new ApolloConfig(); + } +} diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-spring-boot-starter-sync-data-apollo/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/shenyu-extension/shenyu-extension-apollo/shenyu-spring-boot-starter-sync-data-apollo/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 0000000..e2cf78e --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-spring-boot-starter-sync-data-apollo/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1,18 @@ +# +# 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. +# + +org.apache.shenyu.springboot.sync.data.apollo.ApolloSyncDataConfiguration diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-spring-boot-starter-sync-data-apollo/src/test/java/org/apache/shenyu/springboot/sync/data/apollo/ApolloExternalStarterTest.java b/shenyu-extension/shenyu-extension-apollo/shenyu-spring-boot-starter-sync-data-apollo/src/test/java/org/apache/shenyu/springboot/sync/data/apollo/ApolloExternalStarterTest.java new file mode 100644 index 0000000..ee86d95 --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-spring-boot-starter-sync-data-apollo/src/test/java/org/apache/shenyu/springboot/sync/data/apollo/ApolloExternalStarterTest.java @@ -0,0 +1,88 @@ +/* + * 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 org.apache.shenyu.springboot.sync.data.apollo; + +import com.ctrip.framework.apollo.Config; +import com.ctrip.framework.apollo.ConfigService; +import org.apache.shenyu.common.config.ShenyuConfig; +import org.apache.shenyu.sync.data.api.PluginDataSubscriber; +import org.apache.shenyu.sync.data.api.SyncDataService; +import org.apache.shenyu.sync.data.apollo.ApolloDataService; +import org.apache.shenyu.sync.data.apollo.config.ApolloConfig; +import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; + +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.when; + +/** + * External gateway starter activation and binding without a remote Apollo server. + */ +class ApolloExternalStarterTest { + + private final ApplicationContextRunner runner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(ConfigurationPropertiesAutoConfiguration.class, ApolloSyncDataConfiguration.class)) + .withBean(ShenyuConfig.class) + .withBean(PluginDataSubscriber.class, () -> mock(PluginDataSubscriber.class)); + + @Test + void createsGatewayServiceUsingThePublishedPropertyPrefix() { + List keys = List.of("app.id", "apollo.meta", "apollo.cluster", "apollo.bootstrap.namespace", "apollo.access-key"); + Map previous = new HashMap<>(); + keys.forEach(key -> previous.put(key, System.getProperty(key))); + Config config = mock(Config.class); + when(config.getPropertyNames()).thenReturn(Collections.emptySet()); + try (MockedStatic service = mockStatic(ConfigService.class)) { + service.when(ConfigService::getAppConfig).thenReturn(config); + runner.withPropertyValues("shenyu.sync.apollo.meta=http://127.0.0.1:8080", + "shenyu.sync.apollo.appId=shenyu", "shenyu.sync.apollo.env=DEV", + "shenyu.sync.apollo.clusterName=default", "shenyu.sync.apollo.namespace=application", + "shenyu.sync.apollo.accessKey=test-access-key") + .run(context -> { + assertThat(context).hasNotFailed().hasSingleBean(SyncDataService.class); + assertThat(context.getBean(SyncDataService.class)).isInstanceOf(ApolloDataService.class); + assertThat(context.getBean(ApolloConfig.class).getAccessKey()).isEqualTo("test-access-key"); + assertThat(context.getBean(Config.class)).isSameAs(config); + }); + } finally { + previous.forEach((key, value) -> { + if (Objects.isNull(value)) { + System.clearProperty(key); + } else { + System.setProperty(key, value); + } + }); + } + } + + @Test + void backsOffWhenApolloMetaIsAbsent() { + runner.run(context -> assertThat(context).hasNotFailed().doesNotHaveBean(SyncDataService.class).doesNotHaveBean(ApolloConfig.class)); + } +} diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-spring-boot-starter-sync-data-apollo/src/test/java/org/apache/shenyu/springboot/sync/data/apollo/ApolloSyncDataConfigurationTest.java b/shenyu-extension/shenyu-extension-apollo/shenyu-spring-boot-starter-sync-data-apollo/src/test/java/org/apache/shenyu/springboot/sync/data/apollo/ApolloSyncDataConfigurationTest.java new file mode 100644 index 0000000..e82ca02 --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-spring-boot-starter-sync-data-apollo/src/test/java/org/apache/shenyu/springboot/sync/data/apollo/ApolloSyncDataConfigurationTest.java @@ -0,0 +1,86 @@ +/* + * 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 org.apache.shenyu.springboot.sync.data.apollo; + +import com.ctrip.framework.apollo.ConfigService; + +import org.apache.shenyu.common.config.ShenyuConfig; +import org.apache.shenyu.sync.data.api.SyncDataService; +import org.apache.shenyu.sync.data.apollo.ApolloDataService; +import org.apache.shenyu.sync.data.apollo.config.ApolloConfig; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Answers.CALLS_REAL_METHODS; + +@ExtendWith(SpringExtension.class) +@MockBean(ConfigService.class) +@EnableAutoConfiguration +@SpringBootTest( + classes = { + ApolloSyncDataConfiguration.class + }, + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + properties = { + "shenyu.sync.apollo.meta=http://localhost:8080", + "shenyu.sync.apollo.appId=shenyu", + "shenyu.sync.apollo.env=dev", + "shenyu.sync.apollo.clusterName=test", + "shenyu.sync.apollo.namespace=application" + + }) +@MockBean(name = "shenyuConfig", value = ShenyuConfig.class, answer = CALLS_REAL_METHODS) +class ApolloSyncDataConfigurationTest { + + @Autowired + private ApolloConfig apolloConfig; + + @Autowired + private SyncDataService syncDataService; + + /** + * case to test {@link ApolloSyncDataConfiguration} to register bean {@link ApolloSyncDataConfiguration}. + */ + @Test + public void testApolloSyncDataConfigurationRegisterBeanSyncDataService() { + assertNotNull(syncDataService); + assertTrue(syncDataService instanceof ApolloDataService); + } + + /** + * case to test {@link ApolloSyncDataConfiguration} to register bean {@link ApolloSyncDataConfiguration}. + */ + @Test + public void testApolloSyncDataConfigurationRegisterBeanApolloConfig() { + assertThat(apolloConfig.getMeta(), is("http://localhost:8080")); + assertThat(apolloConfig.getAppId(), is("shenyu")); + assertThat(apolloConfig.getEnv(), is("dev")); + assertThat(apolloConfig.getClusterName(), is("test")); + assertThat(apolloConfig.getNamespace(), is("application")); + + } +} diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-sync-data-apollo/pom.xml b/shenyu-extension/shenyu-extension-apollo/shenyu-sync-data-apollo/pom.xml new file mode 100644 index 0000000..f77c1ea --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-sync-data-apollo/pom.xml @@ -0,0 +1,40 @@ + + + + + + org.apache.shenyu + shenyu-extension-apollo + 2.7.1-SNAPSHOT + + 4.0.0 + shenyu-sync-data-apollo + + + + org.apache.shenyu + shenyu-sync-data-api + ${shenyu.version} + + + com.ctrip.framework.apollo + apollo-client + + + + diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-sync-data-apollo/src/main/java/org/apache/shenyu/sync/data/apollo/ApolloDataService.java b/shenyu-extension/shenyu-extension-apollo/shenyu-sync-data-apollo/src/main/java/org/apache/shenyu/sync/data/apollo/ApolloDataService.java new file mode 100644 index 0000000..6a4fe8f --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-sync-data-apollo/src/main/java/org/apache/shenyu/sync/data/apollo/ApolloDataService.java @@ -0,0 +1,174 @@ +/* + * 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 org.apache.shenyu.sync.data.apollo; + +import com.ctrip.framework.apollo.Config; +import com.ctrip.framework.apollo.ConfigChangeListener; +import com.ctrip.framework.apollo.enums.PropertyChangeType; +import com.ctrip.framework.apollo.model.ConfigChange; +import org.apache.commons.lang3.ObjectUtils; +import org.apache.shenyu.common.config.ShenyuConfig; +import org.apache.shenyu.common.constant.ApolloPathConstants; +import org.apache.shenyu.common.constant.DefaultNodeConstants; +import org.apache.shenyu.sync.data.api.AuthDataSubscriber; +import org.apache.shenyu.sync.data.api.DiscoveryUpstreamDataSubscriber; +import org.apache.shenyu.sync.data.api.MetaDataSubscriber; +import org.apache.shenyu.sync.data.api.PluginDataSubscriber; +import org.apache.shenyu.sync.data.api.ProxySelectorDataSubscriber; +import org.apache.shenyu.sync.data.api.SyncDataService; +import org.apache.shenyu.sync.data.core.AbstractNodeDataSyncService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import java.util.function.Consumer; + +public class ApolloDataService extends AbstractNodeDataSyncService implements SyncDataService { + + private static final Logger LOG = LoggerFactory.getLogger(ApolloDataService.class); + + private final Config configService; + + private ConfigChangeListener watchConfigChangeListener; + + /** + * Instantiates a new Nacos sync data service. + * + * @param configService the config service + * @param pluginDataSubscriber the plugin data subscriber + * @param metaDataSubscribers the meta data subscribers + * @param authDataSubscribers the auth data subscribers + * @param proxySelectorDataSubscribers the proxy selector data subscribers + * @param discoveryUpstreamDataSubscribers the discovery upstream data subscribers + * @param shenyuConfig the shenyu config + */ + public ApolloDataService(final Config configService, final PluginDataSubscriber pluginDataSubscriber, + final List metaDataSubscribers, + final List authDataSubscribers, + final List proxySelectorDataSubscribers, + final List discoveryUpstreamDataSubscribers, + final ShenyuConfig shenyuConfig) { + super(new ChangeData(ApolloPathConstants.PLUGIN_DATA_ID, + ApolloPathConstants.SELECTOR_DATA_ID, + ApolloPathConstants.RULE_DATA_ID, + ApolloPathConstants.AUTH_DATA_ID, + ApolloPathConstants.META_DATA_ID, + ApolloPathConstants.PROXY_SELECTOR_DATA_ID, + ApolloPathConstants.DISCOVERY_DATA_ID), + pluginDataSubscriber, + metaDataSubscribers, + authDataSubscribers, + proxySelectorDataSubscribers, + discoveryUpstreamDataSubscribers, + shenyuConfig); + this.configService = configService; + + startWatch(); + apolloWatchPrefixes(shenyuConfig.getNamespace()); + } + + private void apolloWatchPrefixes(final String namespaceId) { + // apollo change keys are namespace-prefixed, e.g. {namespaceId}.plugin.{pluginName} + final String namespacePrefix = namespaceId + DefaultNodeConstants.JOIN_POINT; + final ConfigChangeListener listener = changeEvent -> changeEvent.changedKeys().forEach(changeKey -> { + try { + final ConfigChange configChange = changeEvent.getChange(changeKey); + if (Objects.isNull(configChange)) { + LOG.error("apollo watchPrefixes error configChange is null {}", changeKey); + return; + } + final String newValue = configChange.getNewValue(); + // Skip list index keys. + if (changeKey.endsWith(DefaultNodeConstants.POINT_LIST)) { + return; + } + // check prefix + if (changeKey.startsWith(namespacePrefix + ApolloPathConstants.PLUGIN_DATA_ID)) { + if (PropertyChangeType.DELETED.equals(configChange.getChangeType())) { + unCachePluginData(changeKey); + } else { + cachePluginData(newValue); + } + } else if (changeKey.startsWith(namespacePrefix + ApolloPathConstants.SELECTOR_DATA_ID)) { + if (PropertyChangeType.DELETED.equals(configChange.getChangeType())) { + unCacheSelectorData(changeKey); + } else { + cacheSelectorData(newValue); + } + } else if (changeKey.startsWith(namespacePrefix + ApolloPathConstants.RULE_DATA_ID)) { + if (PropertyChangeType.DELETED.equals(configChange.getChangeType())) { + unCacheRuleData(changeKey); + } else { + cacheRuleData(newValue); + } + } else if (changeKey.startsWith(namespacePrefix + ApolloPathConstants.AUTH_DATA_ID)) { + if (PropertyChangeType.DELETED.equals(configChange.getChangeType())) { + unCacheAuthData(changeKey); + } else { + cacheAuthData(newValue); + } + } else if (changeKey.startsWith(namespacePrefix + ApolloPathConstants.META_DATA_ID)) { + if (PropertyChangeType.DELETED.equals(configChange.getChangeType())) { + unCacheMetaData(changeKey); + } else { + cacheMetaData(newValue); + } + } else if (changeKey.startsWith(namespacePrefix + ApolloPathConstants.PROXY_SELECTOR_DATA_ID)) { + if (PropertyChangeType.DELETED.equals(configChange.getChangeType())) { + unCacheProxySelectorData(changeKey); + } else { + cacheProxySelectorData(newValue); + } + } else if (changeKey.startsWith(namespacePrefix + ApolloPathConstants.DISCOVERY_DATA_ID)) { + if (PropertyChangeType.DELETED.equals(configChange.getChangeType())) { + unCacheDiscoveryUpstreamData(changeKey); + } else { + cacheDiscoveryUpstreamData(newValue); + } + } + } catch (Exception e) { + LOG.error("apollo sync listener change key handler error", e); + } + }); + watchConfigChangeListener = listener; + configService.addChangeListener(listener, Collections.emptySet(), ApolloPathConstants.pathKeySet(namespaceId)); + + } + + @Override + protected void doRemoveListener(final String key) { + // No need to implement + } + + @Override + protected String getServiceConfig(final String key, final Consumer updateHandler, final Consumer deleteHandler) { + return configService.getProperty(key, null); + } + + /** + * close. + */ + @Override + public void close() { + if (!ObjectUtils.isEmpty(watchConfigChangeListener)) { + configService.removeChangeListener(watchConfigChangeListener); + } + } +} diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-sync-data-apollo/src/main/java/org/apache/shenyu/sync/data/apollo/config/ApolloConfig.java b/shenyu-extension/shenyu-extension-apollo/shenyu-sync-data-apollo/src/main/java/org/apache/shenyu/sync/data/apollo/config/ApolloConfig.java new file mode 100644 index 0000000..42bcbcc --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-sync-data-apollo/src/main/java/org/apache/shenyu/sync/data/apollo/config/ApolloConfig.java @@ -0,0 +1,155 @@ +/* + * 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 org.apache.shenyu.sync.data.apollo.config; + +public class ApolloConfig { + + /** + * appId. + */ + private String appId; + + /** + * apollo config service url. + */ + private String meta; + + /** + * env. + * e.g. ENV + */ + private String env; + + /** + * cluster name. + */ + private String clusterName; + + /** + * namespace name. + */ + private String namespace; + + /** + * accessKey. + */ + private String accessKey; + + /** + * constructor. + */ + public ApolloConfig() { + } + + /** + * getSecretKey. + * @return secretKey + */ + public String getAccessKey() { + return accessKey; + } + + /** + * set secretKey. + * @param accessKey accessKey + */ + public void setAccessKey(final String accessKey) { + this.accessKey = accessKey; + } + + /** + * get appId. + * @return appId + */ + public String getAppId() { + return appId; + } + + /** + * set appId. + * @param appId appId + */ + public void setAppId(final String appId) { + this.appId = appId; + } + + /** + * get meta. + * @return meta + */ + public String getMeta() { + return meta; + } + + /** + * set meta. + * @param meta meta + */ + public void setMeta(final String meta) { + this.meta = meta; + } + + /** + * get env. + * @return env + */ + public String getEnv() { + return env; + } + + /** + * set env. + * @param env env + */ + public void setEnv(final String env) { + this.env = env; + } + + /** + * get clusterName. + * @return clusterName + */ + public String getClusterName() { + return clusterName; + } + + /** + * set clusterName. + * @param clusterName clusterName + */ + public void setClusterName(final String clusterName) { + this.clusterName = clusterName; + } + + /** + * get namespace. + * @return namespace + */ + public String getNamespace() { + return namespace; + } + + /** + * set namespace. + * @param namespace namespace + */ + public void setNamespace(final String namespace) { + this.namespace = namespace; + } + +} diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-sync-data-apollo/src/test/java/org/apache/shenyu/sync/data/apollo/ApolloDataServiceIntegrationTest.java b/shenyu-extension/shenyu-extension-apollo/shenyu-sync-data-apollo/src/test/java/org/apache/shenyu/sync/data/apollo/ApolloDataServiceIntegrationTest.java new file mode 100644 index 0000000..eb99b83 --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-sync-data-apollo/src/test/java/org/apache/shenyu/sync/data/apollo/ApolloDataServiceIntegrationTest.java @@ -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 org.apache.shenyu.sync.data.apollo; + +import com.ctrip.framework.apollo.enums.ConfigSourceType; +import com.ctrip.framework.apollo.internals.ConfigRepository; +import com.ctrip.framework.apollo.internals.DefaultConfig; +import org.apache.shenyu.common.config.ShenyuConfig; +import org.apache.shenyu.common.dto.PluginData; +import org.apache.shenyu.sync.data.api.AuthDataSubscriber; +import org.apache.shenyu.sync.data.api.DiscoveryUpstreamDataSubscriber; +import org.apache.shenyu.sync.data.api.MetaDataSubscriber; +import org.apache.shenyu.sync.data.api.PluginDataSubscriber; +import org.apache.shenyu.sync.data.api.ProxySelectorDataSubscriber; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.Collections; +import java.util.Properties; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Integration test case for {@link ApolloDataService} that flows through apollo-client's real + * change listener filtering (interested keys and prefixes are matched in {@link com.ctrip.framework.apollo.internals.AbstractConfig}). + */ +@ExtendWith(MockitoExtension.class) +class ApolloDataServiceIntegrationTest { + + private static final String NAMESPACE = "shenyu"; + + @Mock + private PluginDataSubscriber pluginDataSubscriber; + + @Mock + private MetaDataSubscriber metaDataSubscriber; + + @Mock + private AuthDataSubscriber authDataSubscriber; + + @Mock + private ProxySelectorDataSubscriber proxySelectorDataSubscriber; + + @Mock + private DiscoveryUpstreamDataSubscriber discoveryUpstreamDataSubscriber; + + @Test + void testIncrementalChangeEventFlowsThroughApolloPrefixFiltering() throws InterruptedException { + final DefaultConfig defaultConfig = createDefaultConfig(); + + final CountDownLatch latch = new CountDownLatch(1); + doAnswer(invocation -> { + latch.countDown(); + return null; + }).when(pluginDataSubscriber).onSubscribe(any()); + + new ApolloDataService(defaultConfig, + pluginDataSubscriber, + Collections.singletonList(metaDataSubscriber), + Collections.singletonList(authDataSubscriber), + Collections.singletonList(proxySelectorDataSubscriber), + Collections.singletonList(discoveryUpstreamDataSubscriber), + createShenyuConfig()); + + final Properties changedProperties = new Properties(); + changedProperties.setProperty(NAMESPACE + ".plugin.divide", "{\"id\":\"1\",\"name\":\"divide\"}"); + defaultConfig.onRepositoryChange(NAMESPACE, changedProperties); + + assertTrue(latch.await(5, TimeUnit.SECONDS), + "incremental change event should reach the subscriber through apollo prefix filtering"); + } + + @Test + void testDeletedEventFlowsThroughApolloPrefixFiltering() throws InterruptedException { + final DefaultConfig defaultConfig = createDefaultConfig(); + final Properties initialProperties = new Properties(); + initialProperties.setProperty(NAMESPACE + ".plugin.divide", "{\"id\":\"1\",\"name\":\"divide\"}"); + defaultConfig.onRepositoryChange(NAMESPACE, initialProperties); + + final CountDownLatch latch = new CountDownLatch(1); + doAnswer(invocation -> { + latch.countDown(); + return null; + }).when(pluginDataSubscriber).unSubscribe(any()); + + new ApolloDataService(defaultConfig, + pluginDataSubscriber, + Collections.singletonList(metaDataSubscriber), + Collections.singletonList(authDataSubscriber), + Collections.singletonList(proxySelectorDataSubscriber), + Collections.singletonList(discoveryUpstreamDataSubscriber), + createShenyuConfig()); + + defaultConfig.onRepositoryChange(NAMESPACE, new Properties()); + + assertTrue(latch.await(5, TimeUnit.SECONDS), + "deleted event should reach the subscriber through apollo prefix filtering"); + ArgumentCaptor captor = ArgumentCaptor.forClass(PluginData.class); + verify(pluginDataSubscriber).unSubscribe(captor.capture()); + assertEquals("divide", captor.getValue().getName()); + } + + private DefaultConfig createDefaultConfig() { + final ConfigRepository configRepository = mock(ConfigRepository.class); + when(configRepository.getConfig()).thenReturn(new Properties()); + when(configRepository.getSourceType()).thenReturn(ConfigSourceType.LOCAL); + return new DefaultConfig(NAMESPACE, configRepository); + } + + private ShenyuConfig createShenyuConfig() { + final ShenyuConfig shenyuConfig = new ShenyuConfig(); + shenyuConfig.setNamespace(NAMESPACE); + return shenyuConfig; + } +} diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-sync-data-apollo/src/test/java/org/apache/shenyu/sync/data/apollo/ApolloDataServiceTest.java b/shenyu-extension/shenyu-extension-apollo/shenyu-sync-data-apollo/src/test/java/org/apache/shenyu/sync/data/apollo/ApolloDataServiceTest.java new file mode 100644 index 0000000..e607b56 --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-sync-data-apollo/src/test/java/org/apache/shenyu/sync/data/apollo/ApolloDataServiceTest.java @@ -0,0 +1,462 @@ +/* + * 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 org.apache.shenyu.sync.data.apollo; + +import com.ctrip.framework.apollo.Config; +import com.ctrip.framework.apollo.ConfigChangeListener; +import com.ctrip.framework.apollo.enums.PropertyChangeType; +import com.ctrip.framework.apollo.model.ConfigChange; +import com.ctrip.framework.apollo.model.ConfigChangeEvent; +import org.apache.shenyu.common.config.ShenyuConfig; +import org.apache.shenyu.common.constant.ApolloPathConstants; +import org.apache.shenyu.common.dto.AppAuthData; +import org.apache.shenyu.common.dto.PluginData; +import org.apache.shenyu.common.dto.ProxySelectorData; +import org.apache.shenyu.common.dto.RuleData; +import org.apache.shenyu.common.dto.SelectorData; +import org.apache.shenyu.sync.data.api.AuthDataSubscriber; +import org.apache.shenyu.sync.data.api.DiscoveryUpstreamDataSubscriber; +import org.apache.shenyu.sync.data.api.MetaDataSubscriber; +import org.apache.shenyu.sync.data.api.PluginDataSubscriber; +import org.apache.shenyu.sync.data.api.ProxySelectorDataSubscriber; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.EnumSource; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.Collections; +import java.util.HashSet; +import java.util.Objects; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anySet; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; +import static org.mockito.Mockito.when; + +/** + * Test case for {@link ApolloDataService}. + */ +@ExtendWith(MockitoExtension.class) +class ApolloDataServiceTest { + + private static final String NAMESPACE = "shenyu"; + + @Mock + private Config configService; + + @Mock + private PluginDataSubscriber pluginDataSubscriber; + + @Mock + private MetaDataSubscriber metaDataSubscriber; + + @Mock + private AuthDataSubscriber authDataSubscriber; + + @Mock + private ProxySelectorDataSubscriber proxySelectorDataSubscriber; + + @Mock + private DiscoveryUpstreamDataSubscriber discoveryUpstreamDataSubscriber; + + private ShenyuConfig shenyuConfig; + + private ConfigChangeListener capturedListener; + + @BeforeEach + void setUp() { + shenyuConfig = new ShenyuConfig(); + shenyuConfig.setNamespace(NAMESPACE); + + lenient().when(configService.getProperty(anyString(), any())).thenReturn("[]"); + } + + @Test + void testConstructorAndClose() { + final ApolloDataService apolloDataService = new ApolloDataService( + configService, + pluginDataSubscriber, + Collections.singletonList(metaDataSubscriber), + Collections.singletonList(authDataSubscriber), + Collections.singletonList(proxySelectorDataSubscriber), + Collections.singletonList(discoveryUpstreamDataSubscriber), + shenyuConfig + ); + + ArgumentCaptor listenerCaptor = ArgumentCaptor.forClass(ConfigChangeListener.class); + verify(configService).addChangeListener(listenerCaptor.capture(), anySet(), eq(ApolloPathConstants.pathKeySet(NAMESPACE))); + + capturedListener = listenerCaptor.getValue(); + assertNotNull(capturedListener); + + apolloDataService.close(); + verify(configService).removeChangeListener(capturedListener); + } + + @Test + void testCloseWithNullListener() { + lenient().when(configService.getProperty(anyString(), any())).thenReturn(null); + + ApolloDataService apolloDataService = new ApolloDataService( + configService, + null, + Collections.emptyList(), + Collections.emptyList(), + Collections.emptyList(), + Collections.emptyList(), + shenyuConfig + ); + + apolloDataService.close(); + verify(configService, times(1)).removeChangeListener(any()); + } + + @Test + void testPluginDataChange() { + ApolloDataService apolloDataService = createApolloDataService(); + + ConfigChangeEvent event = mockConfigChangeEvent( + NAMESPACE + "." + ApolloPathConstants.PLUGIN_DATA_ID + ".test-plugin", + "{\"id\":\"1\",\"name\":\"test\"}", + PropertyChangeType.MODIFIED + ); + + capturedListener.onChange(event); + verify(pluginDataSubscriber, times(1)).onSubscribe(any()); + } + + @Test + void testPluginDataAdd() { + ApolloDataService apolloDataService = createApolloDataService(); + + ConfigChangeEvent event = mockConfigChangeEvent( + NAMESPACE + "." + ApolloPathConstants.PLUGIN_DATA_ID + ".new-plugin", + "{\"id\":\"2\",\"name\":\"new\"}", + PropertyChangeType.ADDED + ); + + capturedListener.onChange(event); + verify(pluginDataSubscriber, times(1)).onSubscribe(any()); + } + + @Test + void testPluginDataDeleted() { + ApolloDataService apolloDataService = createApolloDataService(); + + ConfigChangeEvent event = mockConfigChangeEvent( + NAMESPACE + "." + ApolloPathConstants.PLUGIN_DATA_ID + ".test-plugin", + null, + PropertyChangeType.DELETED + ); + + capturedListener.onChange(event); + ArgumentCaptor captor = ArgumentCaptor.forClass(PluginData.class); + verify(pluginDataSubscriber, times(1)).unSubscribe(captor.capture()); + assertEquals("test-plugin", captor.getValue().getName()); + } + + @ParameterizedTest + @EnumSource(PropertyChangeType.class) + void testPluginDataKeyEndingWithList(final PropertyChangeType changeType) { + createApolloDataService(); + + final ConfigChangeEvent event = mockConfigChangeEvent( + NAMESPACE + "." + ApolloPathConstants.PLUGIN_DATA_ID + ".blacklist", + PropertyChangeType.DELETED.equals(changeType) ? null : "{\"name\":\"blacklist\"}", + changeType + ); + + capturedListener.onChange(event); + final ArgumentCaptor captor = ArgumentCaptor.forClass(PluginData.class); + if (PropertyChangeType.DELETED.equals(changeType)) { + verify(pluginDataSubscriber).unSubscribe(captor.capture()); + } else { + verify(pluginDataSubscriber).onSubscribe(captor.capture()); + } + assertEquals("blacklist", captor.getValue().getName()); + } + + @Test + void testSelectorDataChange() { + ApolloDataService apolloDataService = createApolloDataService(); + + ConfigChangeEvent event = mockConfigChangeEvent( + NAMESPACE + "." + ApolloPathConstants.SELECTOR_DATA_ID + ".test-plugin.test-selector", + "{\"id\":\"1\",\"name\":\"test\"}", + PropertyChangeType.MODIFIED + ); + + capturedListener.onChange(event); + verify(pluginDataSubscriber, times(1)).onSelectorSubscribe(any()); + } + + @ParameterizedTest + @EnumSource(PropertyChangeType.class) + void testSelectorDataKeyEndingWithList(final PropertyChangeType changeType) { + createApolloDataService(); + + final ConfigChangeEvent event = mockConfigChangeEvent( + NAMESPACE + "." + ApolloPathConstants.SELECTOR_DATA_ID + ".test-plugin.selector-whitelist", + PropertyChangeType.DELETED.equals(changeType) ? null : "{\"id\":\"selector-whitelist\",\"pluginName\":\"test-plugin\"}", + changeType + ); + + capturedListener.onChange(event); + final ArgumentCaptor captor = ArgumentCaptor.forClass(SelectorData.class); + if (PropertyChangeType.DELETED.equals(changeType)) { + verify(pluginDataSubscriber).unSelectorSubscribe(captor.capture()); + } else { + verify(pluginDataSubscriber).onSelectorSubscribe(captor.capture()); + } + assertEquals("selector-whitelist", captor.getValue().getId()); + assertEquals("test-plugin", captor.getValue().getPluginName()); + } + + @Test + void testRuleDataChange() { + ApolloDataService apolloDataService = createApolloDataService(); + + ConfigChangeEvent event = mockConfigChangeEvent( + NAMESPACE + "." + ApolloPathConstants.RULE_DATA_ID + ".test-plugin.test-selector.test-rule", + "{\"id\":\"1\",\"name\":\"test\"}", + PropertyChangeType.MODIFIED + ); + + capturedListener.onChange(event); + verify(pluginDataSubscriber, times(1)).onRuleSubscribe(any()); + } + + @Test + void testRuleDataDeleted() { + ApolloDataService apolloDataService = createApolloDataService(); + + ConfigChangeEvent event = mockConfigChangeEvent( + NAMESPACE + "." + ApolloPathConstants.RULE_DATA_ID + ".test-plugin.test-selector.test-rule", + null, + PropertyChangeType.DELETED + ); + + capturedListener.onChange(event); + ArgumentCaptor captor = ArgumentCaptor.forClass(RuleData.class); + verify(pluginDataSubscriber, times(1)).unRuleSubscribe(captor.capture()); + assertEquals("test-plugin", captor.getValue().getPluginName()); + assertEquals("test-selector", captor.getValue().getSelectorId()); + assertEquals("test-rule", captor.getValue().getId()); + } + + @Test + void testAuthDataChange() { + ApolloDataService apolloDataService = createApolloDataService(); + + ConfigChangeEvent event = mockConfigChangeEvent( + NAMESPACE + "." + ApolloPathConstants.AUTH_DATA_ID + ".test-auth", + "{\"appKey\":\"test\"}", + PropertyChangeType.MODIFIED + ); + + capturedListener.onChange(event); + verify(authDataSubscriber, times(1)).onSubscribe(any()); + } + + @ParameterizedTest + @EnumSource(PropertyChangeType.class) + void testAuthDataKeyEndingWithList(final PropertyChangeType changeType) { + createApolloDataService(); + + final ConfigChangeEvent event = mockConfigChangeEvent( + NAMESPACE + "." + ApolloPathConstants.AUTH_DATA_ID + ".whitelist", + PropertyChangeType.DELETED.equals(changeType) ? null : "{\"appKey\":\"whitelist\"}", + changeType + ); + + capturedListener.onChange(event); + final ArgumentCaptor captor = ArgumentCaptor.forClass(AppAuthData.class); + if (PropertyChangeType.DELETED.equals(changeType)) { + verify(authDataSubscriber).unSubscribe(captor.capture()); + } else { + verify(authDataSubscriber).onSubscribe(captor.capture()); + } + assertEquals("whitelist", captor.getValue().getAppKey()); + } + + @Test + void testMetaDataChange() { + ApolloDataService apolloDataService = createApolloDataService(); + + ConfigChangeEvent event = mockConfigChangeEvent( + NAMESPACE + "." + ApolloPathConstants.META_DATA_ID + ".test-meta", + "{\"id\":\"1\",\"path\":\"/test\"}", + PropertyChangeType.MODIFIED + ); + + capturedListener.onChange(event); + verify(metaDataSubscriber, times(1)).onSubscribe(any()); + } + + @Test + void testProxySelectorDataChange() { + ApolloDataService apolloDataService = createApolloDataService(); + + ConfigChangeEvent event = mockConfigChangeEvent( + NAMESPACE + "." + ApolloPathConstants.PROXY_SELECTOR_DATA_ID + ".test-plugin.test-proxy", + "{\"id\":\"1\",\"name\":\"test\"}", + PropertyChangeType.MODIFIED + ); + + capturedListener.onChange(event); + verify(proxySelectorDataSubscriber, times(1)).onSubscribe(any()); + } + + @Test + void testProxySelectorDataDeleted() { + ApolloDataService apolloDataService = createApolloDataService(); + + ConfigChangeEvent event = mockConfigChangeEvent( + NAMESPACE + "." + ApolloPathConstants.PROXY_SELECTOR_DATA_ID + ".test-plugin.test-proxy", + null, + PropertyChangeType.DELETED + ); + + capturedListener.onChange(event); + ArgumentCaptor captor = ArgumentCaptor.forClass(ProxySelectorData.class); + verify(proxySelectorDataSubscriber, times(1)).unSubscribe(captor.capture()); + assertEquals("test-plugin", captor.getValue().getPluginName()); + // Older snapshot cores put the key identity in name; current cores use id. + String keyIdentity = captor.getValue().getId(); + assertEquals("test-proxy", Objects.isNull(keyIdentity) ? captor.getValue().getName() : keyIdentity); + } + + @Test + void testDiscoveryDataChange() { + ApolloDataService apolloDataService = createApolloDataService(); + + ConfigChangeEvent event = mockConfigChangeEvent( + NAMESPACE + "." + ApolloPathConstants.DISCOVERY_DATA_ID + ".test-plugin.test-discovery", + "{\"name\":\"test\"}", + PropertyChangeType.MODIFIED + ); + + capturedListener.onChange(event); + verify(discoveryUpstreamDataSubscriber, times(1)).onSubscribe(any()); + } + + @ParameterizedTest + @EnumSource(PropertyChangeType.class) + void testListKeySkipped(final PropertyChangeType changeType) { + ApolloDataService apolloDataService = createApolloDataService(); + + ConfigChangeEvent event = mockConfigChangeEvent( + NAMESPACE + "." + ApolloPathConstants.PLUGIN_DATA_ID + ".list", + "[\"test-plugin\"]", + changeType + ); + + capturedListener.onChange(event); + verifyNoInteractions(pluginDataSubscriber); + } + + @Test + void testChangeKeyWithoutNamespacePrefixIgnored() { + ApolloDataService apolloDataService = createApolloDataService(); + + ConfigChangeEvent event = mockConfigChangeEvent( + "other-namespace." + ApolloPathConstants.PLUGIN_DATA_ID + ".test-plugin", + "{\"id\":\"1\",\"name\":\"test\"}", + PropertyChangeType.MODIFIED + ); + + capturedListener.onChange(event); + verifyNoInteractions(pluginDataSubscriber); + } + + @Test + void testNullConfigChange() { + final ApolloDataService apolloDataService = createApolloDataService(); + + ConfigChangeEvent event = mock(ConfigChangeEvent.class); + Set keys = new HashSet<>(); + keys.add("test-key"); + when(event.changedKeys()).thenReturn(keys); + when(event.getChange("test-key")).thenReturn(null); + + capturedListener.onChange(event); + // Should handle null config change gracefully + assertNotNull(apolloDataService); + } + + @Test + void testExceptionInChangeHandler() { + final ApolloDataService apolloDataService = createApolloDataService(); + + ConfigChangeEvent event = mock(ConfigChangeEvent.class); + Set keys = new HashSet<>(); + keys.add(NAMESPACE + "." + ApolloPathConstants.PLUGIN_DATA_ID + ".test"); + when(event.changedKeys()).thenReturn(keys); + when(event.getChange(anyString())).thenThrow(new RuntimeException("Test exception")); + + capturedListener.onChange(event); + // Should handle exception gracefully + assertNotNull(apolloDataService); + } + + private ApolloDataService createApolloDataService() { + ApolloDataService service = new ApolloDataService( + configService, + pluginDataSubscriber, + Collections.singletonList(metaDataSubscriber), + Collections.singletonList(authDataSubscriber), + Collections.singletonList(proxySelectorDataSubscriber), + Collections.singletonList(discoveryUpstreamDataSubscriber), + shenyuConfig + ); + + ArgumentCaptor listenerCaptor = ArgumentCaptor.forClass(ConfigChangeListener.class); + verify(configService).addChangeListener(listenerCaptor.capture(), anySet(), eq(ApolloPathConstants.pathKeySet(NAMESPACE))); + capturedListener = listenerCaptor.getValue(); + + return service; + } + + private ConfigChangeEvent mockConfigChangeEvent(final String key, final String newValue, final PropertyChangeType changeType) { + ConfigChangeEvent event = mock(ConfigChangeEvent.class); + ConfigChange configChange = mock(ConfigChange.class); + + Set keys = new HashSet<>(); + keys.add(key); + + when(event.changedKeys()).thenReturn(keys); + when(event.getChange(key)).thenReturn(configChange); + when(configChange.getNewValue()).thenReturn(newValue); + lenient().when(configChange.getChangeType()).thenReturn(changeType); + + return event; + } +} diff --git a/shenyu-extension/shenyu-extension-apollo/shenyu-sync-data-apollo/src/test/java/org/apache/shenyu/sync/data/apollo/config/ApolloConfigTest.java b/shenyu-extension/shenyu-extension-apollo/shenyu-sync-data-apollo/src/test/java/org/apache/shenyu/sync/data/apollo/config/ApolloConfigTest.java new file mode 100644 index 0000000..7656892 --- /dev/null +++ b/shenyu-extension/shenyu-extension-apollo/shenyu-sync-data-apollo/src/test/java/org/apache/shenyu/sync/data/apollo/config/ApolloConfigTest.java @@ -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 org.apache.shenyu.sync.data.apollo.config; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class ApolloConfigTest { + + /** + * Methods under test. + * + *
    + *
  • default or parameterless constructor of {@link ApolloConfig} + *
  • {@link ApolloConfig#setAccessKey(String)} + *
  • {@link ApolloConfig#setAppId(String)} + *
  • {@link ApolloConfig#setClusterName(String)} + *
  • {@link ApolloConfig#setEnv(String)} + *
  • {@link ApolloConfig#setMeta(String)} + *
  • {@link ApolloConfig#setNamespace(String)} + *
  • {@link ApolloConfig#getAccessKey()} + *
  • {@link ApolloConfig#getAppId()} + *
  • {@link ApolloConfig#getClusterName()} + *
  • {@link ApolloConfig#getEnv()} + *
  • {@link ApolloConfig#getMeta()} + *
  • {@link ApolloConfig#getNamespace()} + *
+ */ + @Test + public void testConstructor() { + ApolloConfig actualApolloConfig = new ApolloConfig(); + actualApolloConfig.setAccessKey("EXAMPLEakiAIOSFODNN7"); + actualApolloConfig.setAppId("42"); + actualApolloConfig.setClusterName("Cluster Name"); + actualApolloConfig.setEnv("Env"); + actualApolloConfig.setMeta("Meta"); + actualApolloConfig.setNamespace("Namespace"); + assertEquals("EXAMPLEakiAIOSFODNN7", actualApolloConfig.getAccessKey()); + assertEquals("42", actualApolloConfig.getAppId()); + assertEquals("Cluster Name", actualApolloConfig.getClusterName()); + assertEquals("Env", actualApolloConfig.getEnv()); + assertEquals("Meta", actualApolloConfig.getMeta()); + assertEquals("Namespace", actualApolloConfig.getNamespace()); + } +}