From 8bf73dd4fee574440e24dcde26a44d333f3e0fd7 Mon Sep 17 00:00:00 2001 From: yangjj-iso <3202137046@qq.com> Date: Wed, 21 Oct 2026 19:53:48 +0800 Subject: [PATCH 1/4] feat(eureka): receive external registry and starter with live lifecycle checks --- .github/workflows/eureka-provider.yml | 108 ++++++ docs/plugin-store-eureka.md | 158 ++++++++ shenyu-extension/pom.xml | 1 + .../e2e/ExternalProcess.java | 36 ++ .../shenyu-extension-eureka/e2e/run-local.py | 301 ++++++++++++++ .../shenyu-extension-eureka/pom.xml | 32 ++ .../shenyu-registry-eureka/pom.xml | 159 ++++++++ .../EurekaInstanceRegisterRepository.java | 366 ++++++++++++++++++ ...istry.api.ShenyuInstanceRegisterRepository | 17 + .../EurekaInstanceRegisterRepositoryTest.java | 175 +++++++++ .../shenyu/registry/eureka/EurekaLiveIT.java | 77 ++++ .../registry/eureka/RegistrySpiTest.java | 33 ++ .../pom.xml | 50 +++ .../EurekaRegistryAutoConfiguration.java | 53 +++ ...ot.autoconfigure.AutoConfiguration.imports | 17 + .../EurekaRegistryAutoConfigurationTest.java | 82 ++++ 16 files changed, 1665 insertions(+) create mode 100644 .github/workflows/eureka-provider.yml create mode 100644 docs/plugin-store-eureka.md create mode 100644 shenyu-extension/shenyu-extension-eureka/e2e/ExternalProcess.java create mode 100644 shenyu-extension/shenyu-extension-eureka/e2e/run-local.py create mode 100644 shenyu-extension/shenyu-extension-eureka/pom.xml create mode 100644 shenyu-extension/shenyu-extension-eureka/shenyu-registry-eureka/pom.xml create mode 100644 shenyu-extension/shenyu-extension-eureka/shenyu-registry-eureka/src/main/java/org/apache/shenyu/registry/eureka/EurekaInstanceRegisterRepository.java create mode 100644 shenyu-extension/shenyu-extension-eureka/shenyu-registry-eureka/src/main/resources/META-INF/shenyu/org.apache.shenyu.registry.api.ShenyuInstanceRegisterRepository create mode 100644 shenyu-extension/shenyu-extension-eureka/shenyu-registry-eureka/src/test/java/org/apache/shenyu/registry/eureka/EurekaInstanceRegisterRepositoryTest.java create mode 100644 shenyu-extension/shenyu-extension-eureka/shenyu-registry-eureka/src/test/java/org/apache/shenyu/registry/eureka/EurekaLiveIT.java create mode 100644 shenyu-extension/shenyu-extension-eureka/shenyu-registry-eureka/src/test/java/org/apache/shenyu/registry/eureka/RegistrySpiTest.java create mode 100644 shenyu-extension/shenyu-extension-eureka/shenyu-spring-boot-starter-registry-eureka/pom.xml create mode 100644 shenyu-extension/shenyu-extension-eureka/shenyu-spring-boot-starter-registry-eureka/src/main/java/org/apache/shenyu/plugin/store/eureka/EurekaRegistryAutoConfiguration.java create mode 100644 shenyu-extension/shenyu-extension-eureka/shenyu-spring-boot-starter-registry-eureka/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports create mode 100644 shenyu-extension/shenyu-extension-eureka/shenyu-spring-boot-starter-registry-eureka/src/test/java/org/apache/shenyu/plugin/store/eureka/EurekaRegistryAutoConfigurationTest.java diff --git a/.github/workflows/eureka-provider.yml b/.github/workflows/eureka-provider.yml new file mode 100644 index 0000000..69a0462 --- /dev/null +++ b/.github/workflows/eureka-provider.yml @@ -0,0 +1,108 @@ +# 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. + +name: Eureka provider lifecycle + +on: + pull_request: + paths: + - "shenyu-extension/shenyu-extension-eureka/**" + - "pom.xml" + - ".github/workflows/eureka-provider.yml" + workflow_dispatch: + +jobs: + live-provider: + runs-on: ubuntu-latest + timeout-minutes: 45 + steps: + - uses: actions/checkout@v4 + with: + path: store + - uses: actions/checkout@v4 + with: + repository: apache/shenyu + ref: 87c22dde7 + path: core + - uses: actions/setup-java@v4 + with: + distribution: temurin + java-version: "17" + cache: maven + - name: Install retained core APIs and lifecycle + working-directory: core + run: >- + ./mvnw -B -ntp -pl shenyu-spring-boot-starter/shenyu-spring-boot-starter-registry -am install + -DskipTests -DskipRemoteResources=true -Djacoco.skip=true -Dmaven.javadoc.skip=true + - name: Build disposable real Eureka server + working-directory: core + run: >- + ./mvnw -B -ntp -f shenyu-examples/shenyu-examples-eureka/pom.xml package + -DskipTests -DskipRemoteResources=true -Djacoco.skip=true -Dmaven.javadoc.skip=true -Ddocker.skip=true + - name: Prepare real Admin and gateway classpaths + working-directory: core + run: >- + ./mvnw -B -ntp -pl shenyu-admin,shenyu-bootstrap,shenyu-spring-boot-starter/shenyu-spring-boot-starter-registry -am + package dependency:build-classpath -DskipTests -Dmdep.outputFile=target/eureka-classpath.txt -DincludeScope=runtime + -DskipRemoteResources=true -Djacoco.skip=true -Dmaven.javadoc.skip=true + - name: Start Eureka and await readiness + shell: bash + run: | + nohup java -Xms128m -Xmx384m -jar core/shenyu-examples/shenyu-examples-eureka/target/shenyu-examples-eureka.jar \ + --server.port=8761 --eureka.client.register-with-eureka=false --eureka.client.fetch-registry=false \ + --eureka.server.enable-self-preservation=false > eureka-server.log 2>&1 & + echo "$!" > eureka-server.pid + for attempt in $(seq 1 90); do + if curl --fail --silent http://127.0.0.1:8761/eureka/apps >/dev/null; then + exit 0 + fi + sleep 1 + done + cat eureka-server.log + exit 1 + - name: Verify external provider and starter + run: >- + core/mvnw -B -ntp -f store/pom.xml + -pl shenyu-extension/shenyu-extension-eureka/shenyu-spring-boot-starter-registry-eureka -am + verify dependency:build-classpath apache-rat:check -Peureka-live + -Dmdep.outputFile=target/eureka-classpath.txt -DincludeScope=runtime + -Deureka.test.url=http://127.0.0.1:8761/eureka/ + -Deureka.client.refresh.interval=1 + -Deureka.appinfo.initial.replicate.time=1 -Deureka.appinfo.replicate.interval=1 + -DskipRemoteResources=true -Djacoco.skip=true -Dmaven.javadoc.skip=true + - name: Verify external Eureka through real Admin and gateway + run: >- + python store/shenyu-extension/shenyu-extension-eureka/e2e/run-local.py + --core core --store store --java "$JAVA_HOME/bin/java" + --eureka http://127.0.0.1:8761/eureka --output store/target/eureka-e2e + - name: Stop disposable server + if: always() + shell: bash + run: | + if [ -f eureka-server.pid ]; then + kill "$(cat eureka-server.pid)" || true + fi + - uses: actions/upload-artifact@v4 + if: always() + with: + name: eureka-provider-results + retention-days: 7 + path: | + eureka-server.log + store/shenyu-extension/shenyu-extension-eureka/**/target/surefire-reports/ + store/shenyu-extension/shenyu-extension-eureka/**/target/failsafe-reports/ + store/target/eureka-e2e/*.log + store/target/eureka-e2e/result.json + diff --git a/docs/plugin-store-eureka.md b/docs/plugin-store-eureka.md new file mode 100644 index 0000000..fe4eb2b --- /dev/null +++ b/docs/plugin-store-eureka.md @@ -0,0 +1,158 @@ + + +# Eureka plugin-store migration + +Prepared cutover for apache/shenyu#7596. The receiving extension owns +shenyu-registry-eureka and shenyu-spring-boot-starter-registry-eureka. +The main-repository removal must remain a draft until compatible artifacts are +published and the complete Admin/gateway deployment gate below passes. +Source preparation, local snapshot installation and this guide are not publication. + +## Compatibility and ownership + +Development validation uses store 2.7.1-SNAPSHOT, core 2.7.2-SNAPSHOT at 87c22dde7, +Microsoft OpenJDK 17.0.18 and Netflix eureka-client/eureka-client-jersey3 2.0.2. +The disposable server uses ShenYu's Spring Cloud Eureka example (4.1.2). +These are development coordinates and test scope, not a released compatibility matrix. + +The provider keeps its packages, non-singleton @Join annotation, eureka type key, +SPI resource, registration/discovery/lease behavior and polling-failure recovery. +No core factory dispatch or enum/SQL key changes are needed. It resolves through +ShenyuInstanceRegisterRepositoryFactory with an external provider JAR. +This transfer does not redesign Eureka instance identity, authentication or refresh policy. +In particular, current identity is derived by Eureka's configured instance builder; +do not treat the single-identity update test as certification for multiple replicas +using an identical default identity. + +The registry adapter is independent of Spring Cloud backend service discovery. +The generic main-repository Eureka server example and Spring Cloud transport E2E +fixtures remain usable while their broader test ownership is reviewed. They are not +default Admin/bootstrap registry dependencies and do not justify retaining the in-tree +registry provider. + +## Install + +Use a published store version explicitly announced compatible with the selected +core release. Keep one owner per Maven group/artifact/version. The same provider +coordinates/packages cannot coexist from both repositories on a runtime classpath. + +For gateway instance registration, add: + +```xml + + org.apache.shenyu + shenyu-spring-boot-starter-registry-eureka + ${store.version} + +``` + +The external starter retains the core registry lifecycle and explicitly excludes +the core starter's old transitive Eureka provider. It contributes its external +provider instead. Other retained compatibility providers may remain on that +starter's classpath; exclude those explicitly if building a minimal assembly. + +Admin discovery and SDK deployments that already own their registration/discovery +wiring need the provider rather than the gateway starter: + +```xml + + org.apache.shenyu + shenyu-registry-eureka + ${store.version} + +``` + +Resolve the complete runtime dependency closure, including Eureka's Jersey 3 +transport, when installing into lib/ distributions. Do not copy only the provider +JAR or replace retained core API/SPI classes with bundled duplicates. +The source remains ASF/Apache-2.0. Preserve Netflix/Jersey and resolved third-party +licenses/notices in custom binary distributions. + +## Enable and verify + +Gateway configuration retains its keys: + +```yaml +shenyu: + register: + enabled: true + registerType: eureka + serverLists: http://eureka:8761/eureka/ + props: + name: shenyu-gateway + host: gateway +``` + +The listener uses the actually bound server port unless props.port explicitly +overrides it. Existing SDK register-type=eureka and server-list settings retain +their current meaning; HTTP annotation registration to Admin is a separate flow. +Do not change Admin's HTTP client-registration transport to eureka merely to +enable the instance/discovery adapter. + +The starter tests cover disabled-by-default wiring, external SPI construction, +configuration binding and one registration at the bound port. Unit tests retain +the source module's polling and shutdown checks. The opt-in eureka-live Maven +profile launches no fake service: it requires -Deureka.test.url pointing at a +real disposable server and verifies registration, initial discovery, an instance +update/refresh, deregistration and watch shutdown. + +Example verification from a checkout with the core Maven wrapper available: + +```text +core/mvnw -f store/pom.xml -pl shenyu-extension/shenyu-extension-eureka/shenyu-spring-boot-starter-registry-eureka -am test +core/mvnw -f store/pom.xml -pl shenyu-extension/shenyu-extension-eureka/shenyu-registry-eureka -am verify -Peureka-live -Deureka.test.url=http://127.0.0.1:8761/eureka/ +``` + +The live profile must fail if its server URL is missing/unreachable; ordinary unit +tests do not silently substitute for it. Its CI lane builds the server and core +API artifacts from a pinned main commit, then runs the receiving provider. + +## Cutover gates + +1. Merge/review the receiving code and confirm published artifact ownership and + compatible core/store versions without duplicate coordinates. +2. Run the live provider lane and a complete disposable Eureka + Admin + bootstrap + environment using only the external runtime provider. Verify gateway instance + registration at its bound address, discovery, config refresh, deregistration, + healthy sibling behavior and restart/rollback. Record image/JAR digests and logs. +3. Package main Admin/bootstrap after removal and inspect dependency trees and + release licenses. Keep other registry implementations and SDK boundary tests green. +4. Publish external artifacts first, then merge the main removal in the coordinated + release and announce migration/rollback versions. Do not close #7596 before the + complete deployment and publication evidence exists. + +Local evidence: six unit/SPI/starter tests pass with Checkstyle and RAT. The real +server lifecycle test also passes locally. Full Admin/bootstrap/Eureka container +E2E has not been run: the local Docker Linux-engine pipe was unavailable even +after a desktop launch attempt. That gate is outstanding, not certified by the +provider-only test. + +## Upgrade and rollback + +Back up the Admin database, registry configuration and exact old images/JARs. +Install the complete external closure in staging, remove the in-tree provider +version, and keep the eureka key and application/instance configuration unchanged. +Confirm discovery refresh and route/instance removal after a process dies; do not +infer success from a Maven build or an HTTP registration acknowledgement alone. + +For rollback, stop/drain the new processes and restore the last core distribution +that included the provider, removing the external copy. Preserve database/type +keys and Eureka applications. Clean up or expire obsolete instance records using +the deployment's existing lease/identity procedure. Do not clear the whole registry +or run two versions with identical classes in the same JVM. + diff --git a/shenyu-extension/pom.xml b/shenyu-extension/pom.xml index 97c23fc..3fb8a7d 100644 --- a/shenyu-extension/pom.xml +++ b/shenyu-extension/pom.xml @@ -27,5 +27,6 @@ shenyu-extension-consul shenyu-extension-polaris + shenyu-extension-eureka diff --git a/shenyu-extension/shenyu-extension-eureka/e2e/ExternalProcess.java b/shenyu-extension/shenyu-extension-eureka/e2e/ExternalProcess.java new file mode 100644 index 0000000..11247b7 --- /dev/null +++ b/shenyu-extension/shenyu-extension-eureka/e2e/ExternalProcess.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.util.Arrays; +import org.springframework.boot.SpringApplication; +import org.springframework.context.ConfigurableApplicationContext; + +/** + * A disposable process controlled over stdin, including graceful shutdown on Windows. + */ +public final class ExternalProcess { + private ExternalProcess() { + } + + public static void main(final String[] args) throws Exception { + ConfigurableApplicationContext context = SpringApplication.run(Class.forName(args[0]), Arrays.copyOfRange(args, 1, args.length)); + System.in.read(); + context.close(); + System.exit(0); + } +} + diff --git a/shenyu-extension/shenyu-extension-eureka/e2e/run-local.py b/shenyu-extension/shenyu-extension-eureka/e2e/run-local.py new file mode 100644 index 0000000..0906353 --- /dev/null +++ b/shenyu-extension/shenyu-extension-eureka/e2e/run-local.py @@ -0,0 +1,301 @@ +# 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 +import hashlib +import http.server +import json +import os +from pathlib import Path +import re +import secrets +import subprocess +import threading +import time +import urllib.error +import urllib.request +import uuid + +NAMESPACE = "649330b6-c2d7-4edc-be8e-8a54df9eb385" +APP = "SHENYU-EXTERNAL-" + uuid.uuid4().hex.upper() +GATEWAY_APP = APP + "-GATEWAY" +PREFIX = "/external-eureka" + +def request(url, data=None, token=None, method=None): + headers = {"Accept": "application/json"} + if data is not None: + headers["Content-Type"] = "application/json" + if token: + headers["X-Access-Token"] = token + req = urllib.request.Request(url, data=None if data is None else json.dumps(data).encode(), headers=headers, method=method) + with urllib.request.urlopen(req, timeout=4) as response: + return response.status, response.read().decode() + +def wait_for(description, check, timeout=90): + end = time.monotonic() + timeout + last = None + while time.monotonic() < end: + try: + result = check() + if result: + print(description + ": passed", flush=True) + return result + except (OSError, ValueError, KeyError, urllib.error.HTTPError) as error: + last = type(error).__name__ + time.sleep(0.2) + raise AssertionError(description + " timed out; last error=" + str(last)) + +def jar(module): + candidates = [p for p in (module / "target").glob("*.jar") + if not p.name.startswith("original-") and not p.name.endswith(("-sources.jar", "-javadoc.jar", "-shaded.jar"))] + assert len(candidates) == 1, candidates + return candidates[0] + +def dependencies(module): + text = (module / "target/eureka-classpath.txt").read_text().strip() + return [Path(p.replace(chr(92), "/")) for p in text.split(os.pathsep) if p] + +def artifact(path): + if "repository" in path.parts: + return path.parent.parent.name + return re.sub(r"-[0-9].*$", "", path.stem) + +def exists(path): + return path.exists() or (os.name == "nt" and Path(chr(92) * 2 + "?" + chr(92) + str(path.resolve())).exists()) + +def classpath(core, store, main): + extension = store / "shenyu-extension/shenyu-extension-eureka" + registry = extension / "shenyu-registry-eureka" + starter = extension / "shenyu-spring-boot-starter-registry-eureka" + retained = core / "shenyu-spring-boot-starter/shenyu-spring-boot-starter-registry" + ordered = [jar(core / main)] + dependencies(core / main) + extra = [jar(registry)] + dependencies(registry) + if main == "shenyu-bootstrap": + ordered += [jar(retained)] + dependencies(retained) + extra = [jar(starter), jar(registry)] + dependencies(starter) + selected = {} + for path in ordered: + if artifact(path) != "shenyu-registry-eureka": + selected.setdefault(artifact(path), path) + for path in extra: + selected.setdefault(artifact(path), path) + assert selected["shenyu-registry-eureka"].resolve() == jar(registry).resolve() + assert all(exists(path) for path in selected.values()), [str(path) for path in selected.values() if not exists(path)] + return os.pathsep.join(str(path.resolve()).replace("\\", "/") for path in selected.values()) + +def backend(label): + class Handler(http.server.BaseHTTPRequestHandler): + def do_GET(self): + body = label.encode() + self.send_response(200) + self.send_header("Content-Length", str(len(body))) + self.end_headers() + self.wfile.write(body) + def log_message(self, *args): + pass + server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), Handler) + threading.Thread(target=server.serve_forever, daemon=True).start() + return server + +def announce(eureka, port, identity="external-backend"): + payload = {"instance": {"instanceId": identity, "hostName": "127.0.0.1", "app": APP, + "ipAddr": "127.0.0.1", "status": "UP", "port": {"$": port, "@enabled": "true"}, + "dataCenterInfo": {"@class": "com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo", "name": "MyOwn"}, + "leaseInfo": {"renewalIntervalInSecs": 30, "durationInSecs": 180}, + "metadata": {"weight": "100"}, "vipAddress": APP, "secureVipAddress": APP}} + status, _ = request(eureka + "/apps/" + APP, payload) + assert status in (200, 204) + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("--core", type=Path, required=True) + parser.add_argument("--store", type=Path, required=True) + parser.add_argument("--java", type=Path, required=True) + parser.add_argument("--eureka", default="http://127.0.0.1:18761/eureka") + parser.add_argument("--output", type=Path, required=True) + args = parser.parse_args() + core, store = args.core.resolve(), args.store.resolve() + output = args.output.resolve() + output.mkdir(parents=True, exist_ok=True) + admin = "http://127.0.0.1:18765" + gateway = "http://127.0.0.1:19195" + processes = [] + logs = [] + sync_token = secrets.token_hex(32) + selector_id = None + first, second, sibling = backend("first"), backend("second"), backend("healthy-sibling") + + def write_args(path, arguments): + path.write_text("\n".join('"' + value.replace("\\", "\\\\").replace('"', '\\"') + '"' for value in arguments)) + + def stop(process): + if process.poll() is not None: + return + process.stdin.write(b"x") + process.stdin.flush() + try: + process.wait(timeout=20) + except subprocess.TimeoutExpired: + process.kill() + process.wait(timeout=5) + + def start(name, module, main_class, properties): + cp = classpath(core, store, module) + helper = output / "helper" + helper.mkdir(exist_ok=True) + if not (helper / "ExternalProcess.class").exists(): + javac = args.java.parent / ("javac.exe" if os.name == "nt" else "javac") + compile_args = output / "javac.args" + write_args(compile_args, ["-cp", cp, "-d", str(helper), str(Path(__file__).with_name("ExternalProcess.java"))]) + subprocess.run([str(javac), "@" + str(compile_args)], check=True) + cp = str(helper) + os.pathsep + cp + arguments = ["-Xms128m", "-Xmx768m", "-Deureka.client.refresh.interval=1", + "-Deureka.appinfo.initial.replicate.time=1", "-Deureka.appinfo.replicate.interval=1", + "-Xlog:class+load=info:file=" + name + "-classes.log", "-cp", cp, "ExternalProcess", main_class] + properties + argfile = output / (name + ".args") + write_args(argfile, arguments) + log = (output / (name + ".log")).open("w") + logs.append(log) + process = subprocess.Popen([str(args.java), "@" + str(argfile)], cwd=output, stdin=subprocess.PIPE, stdout=log, stderr=subprocess.STDOUT) + processes.append(process) + return process + + def token(): + if processes[0].poll() is not None: + raise RuntimeError("Admin exited before readiness; inspect admin.log") + _, response = request(admin + "/platform/login", {"userName": "admin", "password": "123456"}) + data = json.loads(response) + return data.get("data", {}).get("token") + + def send(path, body, auth): + status, response = request(admin + path, body, auth) + if path.startswith("/shenyu-client/"): + assert status == 200 and response == "success", (status, response) + else: + assert status == 200 and json.loads(response)["code"] == 200, (status, response) + + def provider_loaded(name): + text = (output / (name + "-classes.log")).read_text(errors="replace") + lines = [line for line in text.splitlines() if "org.apache.shenyu.registry.eureka.EurekaInstanceRegisterRepository source:" in line] + return bool(lines) and all("shenyu-extension-eureka" in line for line in lines) + + def gateway_registered(): + data = json.loads(request(args.eureka + "/apps/" + GATEWAY_APP)[1]) + instances = data["application"]["instance"] + if isinstance(instances, dict): + instances = [instances] + return any(int(instance["port"]["$"]) == 19195 for instance in instances) + + def gateway_deregistered(): + try: + request(args.eureka + "/apps/" + GATEWAY_APP) + return False + except urllib.error.HTTPError as error: + return error.code == 404 + + def selector_state(): + nonlocal selector_id + if selector_id is None: + pending = [json.loads(request(admin + "/selector?pluginId=5&namespaceId=" + NAMESPACE + + "¤tPage=1&pageSize=100", token=auth)[1])] + while pending: + item = pending.pop() + if isinstance(item, dict): + if item.get("name") == PREFIX: + selector_id = item["id"] + break + pending.extend(item.values()) + elif isinstance(item, list): + pending.extend(item) + if selector_id is None: + return set() + data = json.loads(request(admin + "/selector/" + selector_id, token=auth)[1])["data"] + return {row.get("url", row.get("upstreamUrl")) for row in data.get("discoveryUpstreams", []) if row.get("status") == 0} + + def start_gateway(): + return start("gateway", "shenyu-bootstrap", "org.apache.shenyu.bootstrap.ShenyuBootstrapApplication", [ + "--server.port=19195", "--shenyu.sync.websocket.urls=ws://127.0.0.1:18765/websocket", + "--shenyu.sync.websocket.token=" + sync_token, + "--shenyu.heartbeat.serverLists=http://127.0.0.1:18765", "--shenyu.register.enabled=true", + "--shenyu.register.registerType=eureka", "--shenyu.register.serverLists=" + args.eureka + "/", + "--shenyu.register.props.name=" + GATEWAY_APP, "--shenyu.register.props.host=127.0.0.1"]) + + def route_removed(): + try: + status, body = request(gateway + PREFIX + "/hello") + return status != 200 or json.loads(body).get("code") == -119 + except urllib.error.HTTPError as error: + return error.code in (404, 502, 503) + + try: + start("admin", "shenyu-admin", "org.apache.shenyu.admin.ShenyuAdminBootstrap", [ + "--server.port=18765", "--spring.profiles.active=h2", + "--spring.datasource.url=jdbc:h2:mem:external-eureka-e2e;DB_CLOSE_DELAY=-1;MODE=MySQL", + "--shenyu.jwt.secretKey=" + secrets.token_hex(32), + "--shenyu.sync.websocket.token=" + sync_token, "--shenyu.register.enabled=true"]) + auth = wait_for("Admin authenticated readiness", token) + bootstrap = start_gateway() + wait_for("Gateway external provider origin", lambda: provider_loaded("gateway")) + wait_for("Gateway bound-port registration", gateway_registered) + announce(args.eureka, first.server_port) + send("/shenyu-client/register-metadata", {"appName": "external-eureka", "contextPath": PREFIX, + "path": PREFIX + "/**", "ruleName": "external-eureka-route", "rpcType": "http", "enabled": True, "registerMetaData": False, + "host": "127.0.0.1", "port": first.server_port, "pluginNames": ["divide"], + "timeMillis": int(time.time() * 1000), + "addPrefixed": False, "namespaceId": NAMESPACE}, auth) + wait_for("Registered selector readiness", lambda: PREFIX in request(admin + + "/selector?pluginId=5&namespaceId=" + NAMESPACE + "¤tPage=1&pageSize=100", token=auth)[1]) + send("/shenyu-client/register-discoveryConfig", {"selectorName": PREFIX, "pluginName": "divide", + "name": "external-eureka", "discoveryType": "eureka", "serverList": args.eureka + "/", + "listenerNode": APP, "handler": "{}", "namespaceId": NAMESPACE, "props": {}}, auth) + wait_for("Admin external provider origin", lambda: provider_loaded("admin")) + wait_for("Admin discovery to gateway HTTP routing", lambda: request(gateway + PREFIX + "/hello")[1] == "first") + announce(args.eureka, second.server_port) + wait_for("Eureka update refreshes gateway routing", lambda: request(gateway + PREFIX + "/hello")[1] == "second") + wait_for("Old endpoint is removed after identity-preserving update", lambda: + "127.0.0.1:" + str(second.server_port) in selector_state() + and "127.0.0.1:" + str(first.server_port) not in selector_state()) + stop(bootstrap) + wait_for("Gateway context shutdown deregisters its instance", gateway_deregistered) + bootstrap = start_gateway() + wait_for("Gateway restart restores discovered route", lambda: request(gateway + PREFIX + "/hello")[1] == "second") + announce(args.eureka, sibling.server_port, "healthy-sibling") + wait_for("Healthy sibling enters discovery", lambda: "127.0.0.1:" + str(sibling.server_port) in selector_state()) + request(args.eureka + "/apps/" + APP + "/external-backend", method="DELETE") + wait_for("Deregistration preserves healthy sibling routing", lambda: request(gateway + PREFIX + "/hello")[1] == "healthy-sibling") + request(args.eureka + "/apps/" + APP + "/healthy-sibling", method="DELETE") + wait_for("Eureka deregistration removes gateway route", route_removed) + first.shutdown() + second.shutdown() + result = {"core": subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=core, text=True).strip(), + "store": subprocess.check_output(["git", "rev-parse", "HEAD"], cwd=store, text=True).strip(), + "assertions": ["admin-ready", "external-provider-origin-admin", "external-provider-origin-gateway", + "gateway-bound-port", "discovery-proxy", "discovery-refresh", "old-endpoint-removal", + "gateway-context-deregistration", "gateway-restart", "healthy-sibling", "deregistration"], + "backendApplication": APP, "gatewayApplication": GATEWAY_APP, + "externalRegistryJarSha256": hashlib.sha256(jar(store / "shenyu-extension/shenyu-extension-eureka/shenyu-registry-eureka").read_bytes()).hexdigest()} + (output / "result.json").write_text(json.dumps(result, indent=2)) + finally: + for process in reversed(processes): + stop(process) + for server in (first, second, sibling): + server.shutdown() + server.server_close() + for log in logs: + log.close() + +if __name__ == "__main__": + main() + diff --git a/shenyu-extension/shenyu-extension-eureka/pom.xml b/shenyu-extension/shenyu-extension-eureka/pom.xml new file mode 100644 index 0000000..b882dbc --- /dev/null +++ b/shenyu-extension/shenyu-extension-eureka/pom.xml @@ -0,0 +1,32 @@ + + + + 4.0.0 + + org.apache.shenyu + shenyu-extension + 2.7.1-SNAPSHOT + + shenyu-extension-eureka + pom + + shenyu-registry-eureka + shenyu-spring-boot-starter-registry-eureka + + + diff --git a/shenyu-extension/shenyu-extension-eureka/shenyu-registry-eureka/pom.xml b/shenyu-extension/shenyu-extension-eureka/shenyu-registry-eureka/pom.xml new file mode 100644 index 0000000..9ecc79a --- /dev/null +++ b/shenyu-extension/shenyu-extension-eureka/shenyu-registry-eureka/pom.xml @@ -0,0 +1,159 @@ + + + + + + org.apache.shenyu + shenyu-extension-eureka + 2.7.1-SNAPSHOT + + 4.0.0 + shenyu-registry-eureka + + + + org.awaitility + awaitility + ${awaitility.version} + test + + + org.apache.shenyu + shenyu-registry-api + ${shenyu.version} + + + + com.netflix.eureka + eureka-client + 2.0.2 + + + + com.netflix.eureka + eureka-client-jersey3 + 2.0.2 + + + + + + org.apache.maven.plugins + maven-shade-plugin + ${maven-shade-plugin.version} + + + package + shade + + false + + + com.netflix.eureka:* + org.glassfish.jersey.*:* + org.glassfish.hk2:* + org.glassfish.hk2.external:* + jakarta.ws.rs:jakarta.ws.rs-api + jakarta.inject:jakarta.inject-api + jakarta.annotation:jakarta.annotation-api + org.javassist:javassist + + + + + com.netflix.discovery.shared.transport.jersey3 + org.apache.shenyu.store.eureka.shaded.netflix.jersey3 + + + org.glassfish.jersey + org.apache.shenyu.store.eureka.shaded.jersey + + + org.glassfish.hk2 + org.apache.shenyu.store.eureka.shaded.hk2 + + + org.jvnet.hk2 + org.apache.shenyu.store.eureka.shaded.jvnet.hk2 + + + jakarta.ws.rs + org.apache.shenyu.store.eureka.shaded.jaxrs + + + jakarta.inject + org.apache.shenyu.store.eureka.shaded.inject + + + jakarta.annotation + org.apache.shenyu.store.eureka.shaded.annotation + + + javassist + org.apache.shenyu.store.eureka.shaded.javassist + + + + + *:* + + META-INF/*.SF + META-INF/*.RSA + META-INF/*.DSA + + + + + + + + + + + + + + + + + eureka-live + + + + org.apache.maven.plugins + maven-failsafe-plugin + 3.2.5 + + + **/EurekaLiveIT.java + + + + + + integration-test + verify + + + + + + + + + diff --git a/shenyu-extension/shenyu-extension-eureka/shenyu-registry-eureka/src/main/java/org/apache/shenyu/registry/eureka/EurekaInstanceRegisterRepository.java b/shenyu-extension/shenyu-extension-eureka/shenyu-registry-eureka/src/main/java/org/apache/shenyu/registry/eureka/EurekaInstanceRegisterRepository.java new file mode 100644 index 0000000..2d511a9 --- /dev/null +++ b/shenyu-extension/shenyu-extension-eureka/shenyu-registry-eureka/src/main/java/org/apache/shenyu/registry/eureka/EurekaInstanceRegisterRepository.java @@ -0,0 +1,366 @@ +/* + * 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.eureka; + +import com.google.gson.JsonObject; +import com.netflix.appinfo.ApplicationInfoManager; +import com.netflix.appinfo.DataCenterInfo; +import com.netflix.appinfo.EurekaInstanceConfig; +import com.netflix.appinfo.InstanceInfo; +import com.netflix.appinfo.LeaseInfo; +import com.netflix.appinfo.MyDataCenterInstanceConfig; +import com.netflix.appinfo.RefreshableInstanceConfig; +import com.netflix.appinfo.UniqueIdentifier; +import com.netflix.appinfo.providers.Archaius1VipAddressResolver; +import com.netflix.appinfo.providers.EurekaConfigBasedInstanceInfoProvider; +import com.netflix.discovery.DefaultEurekaClientConfig; +import com.netflix.discovery.DiscoveryClient; +import com.netflix.discovery.EurekaClient; +import com.netflix.discovery.shared.transport.jersey3.Jersey3TransportClientFactories; +import org.apache.commons.lang.StringUtils; +import org.apache.shenyu.common.concurrent.ShenyuThreadFactory; +import org.apache.shenyu.common.exception.ShenyuException; +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.event.ChangedEventListener; +import org.apache.shenyu.spi.Join; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.net.URI; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +@Join(isSingleton = false) +public class EurekaInstanceRegisterRepository implements ShenyuInstanceRegisterRepository { + + private static final Logger LOGGER = LoggerFactory.getLogger(EurekaInstanceRegisterRepository.class); + + private EurekaClient eurekaClient; + + private EurekaClient registerDiscoveryClient; + + private DefaultEurekaClientConfig eurekaClientConfig; + + private EurekaInstanceConfig eurekaInstanceConfig; + + private final ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(10, ShenyuThreadFactory.create("scheduled-eureka-watcher", true)); + + private final ConcurrentMap> listenerThreadsMap = new ConcurrentHashMap<>(); + + private final ConcurrentMap> instanceListMap = new ConcurrentHashMap<>(); + + @Override + public void init(final RegisterConfig config) { + eurekaInstanceConfig = new MyDataCenterInstanceConfig(); + eurekaClientConfig = new DefaultEurekaClientConfig() { + @Override + public List getEurekaServerServiceUrls(final String zone) { + return Arrays.asList(config.getServerLists().split(",")); + } + + @Override + public boolean shouldFetchRegistry() { + return false; + } + }; + + DefaultEurekaClientConfig eurekaClientNotRegisterEurekaConfig = new DefaultEurekaClientConfig() { + @Override + public List getEurekaServerServiceUrls(final String zone) { + return Arrays.asList(config.getServerLists().split(",")); + } + + @Override + public boolean shouldRegisterWithEureka() { + return false; + } + }; + LOGGER.info("eureka registry init..."); + eurekaClient = new DiscoveryClient(new ApplicationInfoManager(eurekaInstanceConfig, + new EurekaConfigBasedInstanceInfoProvider(eurekaInstanceConfig).get()), eurekaClientNotRegisterEurekaConfig, + new Jersey3TransportClientFactories()); + } + + @Override + public void persistInstance(final InstanceEntity instance) { + + InstanceInfo.Builder instanceInfoBuilder = instanceInfoBuilder(); + InstanceInfo instanceInfo = instanceInfoBuilder + .setAppName(instance.getAppName()) + .setIPAddr(instance.getHost()) + .setHostName(instance.getHost()) + .setPort(instance.getPort()) + .setLastDirtyTimestamp(System.currentTimeMillis()) + .setStatus(InstanceInfo.InstanceStatus.UP) + .build(); + LeaseInfo.Builder leaseInfoBuilder = LeaseInfo.Builder.newBuilder() + .setRenewalIntervalInSecs(eurekaInstanceConfig.getLeaseRenewalIntervalInSeconds()) + .setDurationInSecs(eurekaInstanceConfig.getLeaseExpirationDurationInSeconds()); + instanceInfo.setLeaseInfo(leaseInfoBuilder.build()); + ApplicationInfoManager applicationInfoManager = new ApplicationInfoManager(eurekaInstanceConfig, instanceInfo); + registerDiscoveryClient = new DiscoveryClient(applicationInfoManager, eurekaClientConfig, new Jersey3TransportClientFactories()); + LOGGER.info("eureka registry persistInstance success: {}", instanceInfo); + } + + @Override + public List selectInstances(final String selectKey) { + return getInstances(selectKey); + } + + @Override + public boolean serviceExists(final String key) { + try { + List instances = eurekaClient.getInstancesByVipAddressAndAppName(null, key, true); + return !instances.isEmpty(); + } catch (Exception e) { + throw new ShenyuException(e); + } + } + + @Override + public void watchInstances(final String key, final ChangedEventListener listener) { + try { + List initialInstances = eurekaClient.getInstancesByVipAddressAndAppName(null, key, true); + instanceListMap.put(key, initialInstances); + for (InstanceInfo instance : initialInstances) { + listener.onEvent(key, buildUpstreamJsonFromInstance(instance), ChangedEventListener.Event.ADDED); + } + ScheduledFuture scheduledFuture = executorService.scheduleAtFixedRate(() -> { + try { + List previousInstances = instanceListMap.get(key); + List currentInstances = eurekaClient.getInstancesByVipAddressAndAppName(null, key, true); + compareInstances(new HashSet<>(previousInstances), new HashSet<>(currentInstances), listener); + instanceListMap.put(key, currentInstances); + } catch (Exception e) { + // an exception escaping a fixed-rate task suppresses all later executions, + // so a single polling failure must only be logged to keep the watch alive + LOGGER.error("eureka registry eurekaDiscoveryService watch key: {} error", key, e); + } + }, 0, 1, TimeUnit.SECONDS); + listenerThreadsMap.put(key, scheduledFuture); + LOGGER.info("eureka registry subscribed to eureka updates for key: {}", key); + } catch (Exception e) { + LOGGER.error("eureka registry error watching key: {}", key, e); + throw new ShenyuException(e); + } + } + + @Override + public void unWatchInstances(final String key) { + try { + ScheduledFuture scheduledFuture = listenerThreadsMap.get(key); + if (Objects.nonNull(scheduledFuture)) { + scheduledFuture.cancel(true); + listenerThreadsMap.remove(key); + LOGGER.info("eureka registry unwatch key {} successfully", key); + } + } catch (Exception e) { + LOGGER.error("eureka registry error removing eureka watch task for key {} {}", key, e.getMessage(), e); + throw new ShenyuException(e); + } + } + + private void compareInstances(final Set previousInstances, final Set currentInstances, final ChangedEventListener listener) { + Set addedInstances = currentInstances.stream() + .filter(item -> !previousInstances.contains(item)) + .collect(Collectors.toSet()); + if (!addedInstances.isEmpty()) { + for (InstanceInfo instance : addedInstances) { + listener.onEvent(instance.getAppName(), buildUpstreamJsonFromInstance(instance), ChangedEventListener.Event.ADDED); + } + } + + Set deletedInstances = previousInstances.stream() + .filter(item -> !currentInstances.contains(item)) + .collect(Collectors.toSet()); + if (!deletedInstances.isEmpty()) { + for (InstanceInfo instance : deletedInstances) { + instance.setStatus(InstanceInfo.InstanceStatus.DOWN); + listener.onEvent(instance.getAppName(), buildUpstreamJsonFromInstance(instance), ChangedEventListener.Event.DELETED); + } + } + + Set updatedInstances = currentInstances.stream() + .filter(currentInstance -> previousInstances.stream() + .anyMatch(previousInstance -> currentInstance.getInstanceId().equals(previousInstance.getInstanceId()) + && !buildUpstreamJsonFromInstance(currentInstance).equals(buildUpstreamJsonFromInstance(previousInstance)))) + .collect(Collectors.toSet()); + if (!updatedInstances.isEmpty()) { + for (InstanceInfo instance : updatedInstances) { + InstanceInfo previous = previousInstances.stream().filter(old -> old.getInstanceId().equals(instance.getInstanceId())).findFirst().orElseThrow(); + if (!Objects.equals(previous.getIPAddr(), instance.getIPAddr()) || previous.getPort() != instance.getPort()) { + JsonObject removed = GsonUtils.getInstance().fromJson(buildUpstreamJsonFromInstance(previous), JsonObject.class); + removed.addProperty("status", 1); + listener.onEvent(previous.getAppName(), removed.toString(), ChangedEventListener.Event.DELETED); + listener.onEvent(instance.getAppName(), buildUpstreamJsonFromInstance(instance), ChangedEventListener.Event.ADDED); + } else { + listener.onEvent(instance.getAppName(), buildUpstreamJsonFromInstance(instance), ChangedEventListener.Event.UPDATED); + } + } + } + } + + private String buildUpstreamJsonFromInstance(final InstanceInfo instanceInfo) { + JsonObject upstreamJson = new JsonObject(); + upstreamJson.addProperty("url", instanceInfo.getIPAddr() + ":" + instanceInfo.getPort()); + upstreamJson.addProperty("weight", instanceInfo.getMetadata().get("weight")); + boolean secure = instanceInfo.isPortEnabled(InstanceInfo.PortType.SECURE); + String scheme = secure ? "https://" : "http://"; + upstreamJson.addProperty("protocol", Optional.ofNullable(instanceInfo.getMetadata().get("protocol")).orElse(scheme)); + upstreamJson.addProperty("props", instanceInfo.getMetadata().get("props")); + if (instanceInfo.getStatus() == InstanceInfo.InstanceStatus.UP) { + upstreamJson.addProperty("status", 0); + } else if (instanceInfo.getStatus() == InstanceInfo.InstanceStatus.DOWN) { + upstreamJson.addProperty("status", 1); + } + return GsonUtils.getInstance().toJson(upstreamJson); + } + + private List getInstances(final String selectKey) { + List instances = eurekaClient.getInstancesByVipAddressAndAppName(null, selectKey, true); + return instances.stream() + .map(i -> InstanceEntity.builder() + .appName(i.getAppName()).host(i.getHostName()).port(i.getPort()).uri(getURI(i)) + .build() + ).collect(Collectors.toList()); + } + + private URI getURI(final InstanceInfo instance) { + boolean secure = instance.isPortEnabled(InstanceInfo.PortType.SECURE); + String scheme = secure ? "https" : "http"; + int port = instance.getPort(); + if (port <= 0) { + port = secure ? 443 : 80; + } + String uri = String.format("%s://%s:%s", scheme, instance.getIPAddr(), port); + return URI.create(uri); + } + + @Override + public void close() { + try { + for (ScheduledFuture scheduledFuture : listenerThreadsMap.values()) { + scheduledFuture.cancel(true); + } + listenerThreadsMap.clear(); + if (Objects.nonNull(eurekaClient)) { + eurekaClient.getApplicationInfoManager().setInstanceStatus(InstanceInfo.InstanceStatus.DOWN); + eurekaClient.shutdown(); + } + if (Objects.nonNull(registerDiscoveryClient)) { + registerDiscoveryClient.getApplicationInfoManager().setInstanceStatus(InstanceInfo.InstanceStatus.DOWN); + registerDiscoveryClient.shutdown(); + } + LOGGER.info("eureka registry shutting down..."); + } catch (Exception e) { + LOGGER.error("eureka registry shutting down error", e); + throw new ShenyuException(e); + } + } + + /** + * Gets the instance information from the config instance and returns it after setting the appropriate status. + * ref: com.netflix.appinfo.providers.EurekaConfigBasedInstanceInfoProvider#get + * + * @return InstanceInfo instance to be registered with eureka server + */ + public InstanceInfo.Builder instanceInfoBuilder() { + // Builder the instance information to be registered with eureka server + final InstanceInfo.Builder builder = InstanceInfo.Builder.newBuilder(new Archaius1VipAddressResolver()); + + // set the appropriate id for the InstanceInfo, falling back to datacenter Id if applicable, else hostname + String instanceId = eurekaInstanceConfig.getInstanceId(); + if (StringUtils.isEmpty(instanceId)) { + DataCenterInfo dataCenterInfo = eurekaInstanceConfig.getDataCenterInfo(); + if (dataCenterInfo instanceof UniqueIdentifier) { + instanceId = ((UniqueIdentifier) dataCenterInfo).getId(); + } else { + instanceId = eurekaInstanceConfig.getHostName(false); + } + } + + String defaultAddress; + if (eurekaInstanceConfig instanceof RefreshableInstanceConfig) { + // Refresh AWS data center info, and return up to date address + defaultAddress = ((RefreshableInstanceConfig) eurekaInstanceConfig).resolveDefaultAddress(false); + } else { + defaultAddress = eurekaInstanceConfig.getHostName(false); + } + + // fail safe + if (StringUtils.isEmpty(defaultAddress)) { + defaultAddress = eurekaInstanceConfig.getIpAddress(); + } + + builder.setNamespace(eurekaInstanceConfig.getNamespace()) + .setInstanceId(instanceId) + .setAppName(eurekaInstanceConfig.getAppname()) + .setAppGroupName(eurekaInstanceConfig.getAppGroupName()) + .setDataCenterInfo(eurekaInstanceConfig.getDataCenterInfo()) + .setIPAddr(eurekaInstanceConfig.getIpAddress()) + .setHostName(defaultAddress) + .setPort(eurekaInstanceConfig.getNonSecurePort()) + .enablePort(InstanceInfo.PortType.UNSECURE, eurekaInstanceConfig.isNonSecurePortEnabled()) + .setSecurePort(eurekaInstanceConfig.getSecurePort()) + .enablePort(InstanceInfo.PortType.SECURE, eurekaInstanceConfig.getSecurePortEnabled()) + .setVIPAddress(eurekaInstanceConfig.getVirtualHostName()) + .setSecureVIPAddress(eurekaInstanceConfig.getSecureVirtualHostName()) + .setHomePageUrl(eurekaInstanceConfig.getHomePageUrlPath(), eurekaInstanceConfig.getHomePageUrl()) + .setStatusPageUrl(eurekaInstanceConfig.getStatusPageUrlPath(), eurekaInstanceConfig.getStatusPageUrl()) + .setASGName(eurekaInstanceConfig.getASGName()) + .setHealthCheckUrls(eurekaInstanceConfig.getHealthCheckUrlPath(), + eurekaInstanceConfig.getHealthCheckUrl(), eurekaInstanceConfig.getSecureHealthCheckUrl()); + + // Start off with the STARTING state to avoid traffic + if (!eurekaInstanceConfig.isInstanceEnabledOnit()) { + InstanceInfo.InstanceStatus initialStatus = InstanceInfo.InstanceStatus.STARTING; + LOGGER.info("Setting initial instance status as: {}", initialStatus); + builder.setStatus(initialStatus); + } else { + LOGGER.info("Setting initial instance status as: {}. This may be too early for the instance to advertise " + + "itself as available. You would instead want to control this via a healthcheck handler.", + InstanceInfo.InstanceStatus.UP); + } + + // Add any user-specific metadata information + for (Map.Entry mapEntry : eurekaInstanceConfig.getMetadataMap().entrySet()) { + String key = mapEntry.getKey(); + String value = mapEntry.getValue(); + // only add the metadata if the value is present + if (StringUtils.isNotEmpty(value)) { + builder.add(key, value); + } + } + return builder; + } +} diff --git a/shenyu-extension/shenyu-extension-eureka/shenyu-registry-eureka/src/main/resources/META-INF/shenyu/org.apache.shenyu.registry.api.ShenyuInstanceRegisterRepository b/shenyu-extension/shenyu-extension-eureka/shenyu-registry-eureka/src/main/resources/META-INF/shenyu/org.apache.shenyu.registry.api.ShenyuInstanceRegisterRepository new file mode 100644 index 0000000..a0bf57c --- /dev/null +++ b/shenyu-extension/shenyu-extension-eureka/shenyu-registry-eureka/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. + +eureka=org.apache.shenyu.registry.eureka.EurekaInstanceRegisterRepository diff --git a/shenyu-extension/shenyu-extension-eureka/shenyu-registry-eureka/src/test/java/org/apache/shenyu/registry/eureka/EurekaInstanceRegisterRepositoryTest.java b/shenyu-extension/shenyu-extension-eureka/shenyu-registry-eureka/src/test/java/org/apache/shenyu/registry/eureka/EurekaInstanceRegisterRepositoryTest.java new file mode 100644 index 0000000..989133d --- /dev/null +++ b/shenyu-extension/shenyu-extension-eureka/shenyu-registry-eureka/src/test/java/org/apache/shenyu/registry/eureka/EurekaInstanceRegisterRepositoryTest.java @@ -0,0 +1,175 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shenyu.registry.eureka; + +import com.netflix.appinfo.ApplicationInfoManager; +import com.netflix.appinfo.InstanceInfo; +import com.netflix.discovery.DiscoveryClient; +import com.netflix.discovery.EurekaClient; +import com.netflix.discovery.EurekaEventListener; +import org.apache.shenyu.registry.api.config.RegisterConfig; +import org.apache.shenyu.registry.api.entity.InstanceEntity; +import org.apache.shenyu.registry.api.event.ChangedEventListener; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.MockedConstruction; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.contains; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.nullable; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockConstruction; +import static org.mockito.Mockito.timeout; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.verifyNoInteractions; + +public final class EurekaInstanceRegisterRepositoryTest { + + private EurekaInstanceRegisterRepository repository; + + private final InstanceEntity instance = new InstanceEntity("shenyu-instances", "shenyu-host", 9195); + + private final Map instanceStorage = new HashMap<>(); + + private final Map eurekaEventStorage = new HashMap<>(); + + private MockedConstruction discoveryClientMockedConstruction; + + @BeforeEach + public void setUp() throws Exception { + repository = new EurekaInstanceRegisterRepository(); + Class clazz = repository.getClass(); + + Field eurekaClientField = clazz.getDeclaredField("eurekaClient"); + eurekaClientField.setAccessible(true); + eurekaClientField.set(repository, mockEurekaClient()); + + RegisterConfig registerConfig = new RegisterConfig(); + registerConfig.setServerLists(""); + repository.init(registerConfig); + + // mock the function discoveryClient#register(). + discoveryClientMockedConstruction = mockConstruction(DiscoveryClient.class, (mock, context) -> { + InstanceInfo.Builder builder = repository.instanceInfoBuilder(); + builder.setAppName(instance.getAppName()) + .setIPAddr(instance.getHost()) + .setHostName(instance.getHost()) + .setPort(instance.getPort()) + .setStatus(InstanceInfo.InstanceStatus.UP); + InstanceInfo instanceInfo = builder.build(); + instanceStorage.put(instanceInfo.getAppName(), instanceInfo); + }); + } + + private EurekaClient mockEurekaClient() { + DiscoveryClient discoveryClient = mock(DiscoveryClient.class); + + doAnswer(invocationOnMock -> { + eurekaEventStorage.clear(); + return null; + }).when(discoveryClient).shutdown(); + + return discoveryClient; + } + + @Test + public void testInstanceIdentityEqualityDoesNotHideUpstreamUpdates() throws Exception { + final InstanceInfo previous = InstanceInfo.Builder.newBuilder().setInstanceId("same-instance").setAppName("test") + .setIPAddr("127.0.0.1").setPort(8080).setStatus(InstanceInfo.InstanceStatus.UP).build(); + final InstanceInfo unchanged = InstanceInfo.Builder.newBuilder().setInstanceId("same-instance").setAppName("test") + .setIPAddr("127.0.0.1").setPort(8080).setStatus(InstanceInfo.InstanceStatus.UP).build(); + final InstanceInfo changed = InstanceInfo.Builder.newBuilder().setInstanceId("same-instance").setAppName("test") + .setIPAddr("127.0.0.1").setPort(8081).setStatus(InstanceInfo.InstanceStatus.UP).build(); + assertEquals(previous, changed, "Eureka equality compares identity, not the upstream payload"); + Method compare = repository.getClass().getDeclaredMethod("compareInstances", Set.class, Set.class, ChangedEventListener.class); + compare.setAccessible(true); + ChangedEventListener listener = mock(ChangedEventListener.class); + compare.invoke(repository, Set.of(previous), Set.of(unchanged), listener); + verifyNoInteractions(listener); + compare.invoke(repository, Set.of(previous), Set.of(changed), listener); + verify(listener).onEvent(eq("TEST"), contains(":8080"), eq(ChangedEventListener.Event.DELETED)); + verify(listener).onEvent(eq("TEST"), contains(":8081"), eq(ChangedEventListener.Event.ADDED)); + } + + @Test + public void persistInstance() { + repository.persistInstance(instance); + assertTrue(instanceStorage.containsKey(instance.getAppName().toUpperCase())); + InstanceInfo instanceInfo = instanceStorage.get(instance.getAppName().toUpperCase()); + assertEquals(instance.getHost(), instanceInfo.getHostName()); + assertEquals(instance.getPort(), instanceInfo.getPort()); + assertEquals(instance.getAppName().toUpperCase(), instanceInfo.getAppName()); + } + + @Test + public void testSelectInstancesAndWatcher() { + repository.selectInstances(instance.getAppName()); + repository.close(); + assertTrue(eurekaEventStorage.isEmpty()); + } + + @Test + public void testWatchInstancesKeepsPollingAfterFailure() throws Exception { + InstanceInfo info = mock(InstanceInfo.class); + when(info.getAppName()).thenReturn(instance.getAppName()); + when(info.getIPAddr()).thenReturn("10.0.0.1"); + when(info.getPort()).thenReturn(8080); + when(info.getInstanceId()).thenReturn("instance-1"); + when(info.getMetadata()).thenReturn(new HashMap<>()); + when(info.isPortEnabled(InstanceInfo.PortType.SECURE)).thenReturn(false); + when(info.getStatus()).thenReturn(InstanceInfo.InstanceStatus.UP); + + EurekaClient failingThenRecovering = mock(EurekaClient.class); + when(failingThenRecovering.getApplicationInfoManager()).thenReturn(mock(ApplicationInfoManager.class)); + when(failingThenRecovering.getInstancesByVipAddressAndAppName(nullable(String.class), eq(instance.getAppName()), anyBoolean())) + .thenReturn(new ArrayList<>()) + .thenThrow(new RuntimeException("eureka temporarily unavailable")) + .thenReturn(Collections.singletonList(info)); + + Field eurekaClientField = repository.getClass().getDeclaredField("eurekaClient"); + eurekaClientField.setAccessible(true); + eurekaClientField.set(repository, failingThenRecovering); + + ChangedEventListener listener = mock(ChangedEventListener.class); + repository.watchInstances(instance.getAppName(), listener); + + verify(listener, timeout(5000).atLeastOnce()) + .onEvent(eq(instance.getAppName()), anyString(), eq(ChangedEventListener.Event.ADDED)); + repository.close(); + } + + @AfterEach + public void clear() { + discoveryClientMockedConstruction.close(); + } +} diff --git a/shenyu-extension/shenyu-extension-eureka/shenyu-registry-eureka/src/test/java/org/apache/shenyu/registry/eureka/EurekaLiveIT.java b/shenyu-extension/shenyu-extension-eureka/shenyu-registry-eureka/src/test/java/org/apache/shenyu/registry/eureka/EurekaLiveIT.java new file mode 100644 index 0000000..d00273e --- /dev/null +++ b/shenyu-extension/shenyu-extension-eureka/shenyu-registry-eureka/src/test/java/org/apache/shenyu/registry/eureka/EurekaLiveIT.java @@ -0,0 +1,77 @@ +/* + * 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.eureka; + +import org.apache.shenyu.registry.api.config.RegisterConfig; +import org.apache.shenyu.registry.api.entity.InstanceEntity; +import org.apache.shenyu.registry.api.event.ChangedEventListener; +import org.junit.jupiter.api.Test; + +import java.time.Duration; +import java.util.UUID; +import java.util.concurrent.ConcurrentLinkedQueue; + +import static org.awaitility.Awaitility.await; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +/** + * Opt-in real Eureka registration, refresh, deregistration and shutdown checks. + */ +class EurekaLiveIT { + + @Test + void externalProviderRegistersDiscoversRefreshesAndDeregisters() { + String url = System.getProperty("eureka.test.url"); + assertNotNull(url, "The eureka-live profile requires -Deureka.test.url=http://server:8761/eureka/"); + final String application = "SHENYU-STORE-" + UUID.randomUUID(); + final EurekaInstanceRegisterRepository first = repository(url); + final EurekaInstanceRegisterRepository second = repository(url); + final EurekaInstanceRegisterRepository discovery = repository(url); + final ConcurrentLinkedQueue removals = new ConcurrentLinkedQueue<>(); + try { + first.persistInstance(new InstanceEntity(application, "127.0.0.1", 19195)); + await().atMost(Duration.ofSeconds(90)).until(() -> discovery.selectInstances(application).stream() + .anyMatch(instance -> instance.getPort() == 19195)); + discovery.watchInstances(application, (key, value, event) -> { + if (event == ChangedEventListener.Event.DELETED) { + removals.add(value); + } + }); + second.persistInstance(new InstanceEntity(application, "127.0.0.1", 19196)); + await().atMost(Duration.ofSeconds(90)).until(() -> discovery.selectInstances(application).stream() + .anyMatch(instance -> instance.getPort() == 19196)); + second.close(); + await().atMost(Duration.ofSeconds(90)).until(() -> removals.stream().anyMatch(value -> value.contains("19196"))); + discovery.unWatchInstances(application); + } finally { + first.close(); + second.close(); + discovery.close(); + } + } + + private EurekaInstanceRegisterRepository repository(final String url) { + RegisterConfig config = new RegisterConfig(); + config.setServerLists(url); + config.setRegisterType("eureka"); + EurekaInstanceRegisterRepository repository = new EurekaInstanceRegisterRepository(); + repository.init(config); + return repository; + } +} + diff --git a/shenyu-extension/shenyu-extension-eureka/shenyu-registry-eureka/src/test/java/org/apache/shenyu/registry/eureka/RegistrySpiTest.java b/shenyu-extension/shenyu-extension-eureka/shenyu-registry-eureka/src/test/java/org/apache/shenyu/registry/eureka/RegistrySpiTest.java new file mode 100644 index 0000000..ea1f39c --- /dev/null +++ b/shenyu-extension/shenyu-extension-eureka/shenyu-registry-eureka/src/test/java/org/apache/shenyu/registry/eureka/RegistrySpiTest.java @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shenyu.registry.eureka; + +import org.apache.shenyu.registry.api.ShenyuInstanceRegisterRepository; +import org.apache.shenyu.spi.ExtensionLoader; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class RegistrySpiTest { + + @Test + void discoversPublishedRegistryKey() { + ShenyuInstanceRegisterRepository repository = ExtensionLoader.getExtensionLoader(ShenyuInstanceRegisterRepository.class).getJoin("eureka"); + assertEquals(EurekaInstanceRegisterRepository.class, repository.getClass()); + } +} diff --git a/shenyu-extension/shenyu-extension-eureka/shenyu-spring-boot-starter-registry-eureka/pom.xml b/shenyu-extension/shenyu-extension-eureka/shenyu-spring-boot-starter-registry-eureka/pom.xml new file mode 100644 index 0000000..b808b76 --- /dev/null +++ b/shenyu-extension/shenyu-extension-eureka/shenyu-spring-boot-starter-registry-eureka/pom.xml @@ -0,0 +1,50 @@ + + + + 4.0.0 + + org.apache.shenyu + shenyu-extension-eureka + 2.7.1-SNAPSHOT + + shenyu-spring-boot-starter-registry-eureka + + + org.apache.shenyu + shenyu-registry-eureka + ${project.version} + + + org.apache.shenyu + shenyu-spring-boot-starter-registry + ${shenyu.version} + + + org.apache.shenyu + shenyu-registry-eureka + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + diff --git a/shenyu-extension/shenyu-extension-eureka/shenyu-spring-boot-starter-registry-eureka/src/main/java/org/apache/shenyu/plugin/store/eureka/EurekaRegistryAutoConfiguration.java b/shenyu-extension/shenyu-extension-eureka/shenyu-spring-boot-starter-registry-eureka/src/main/java/org/apache/shenyu/plugin/store/eureka/EurekaRegistryAutoConfiguration.java new file mode 100644 index 0000000..b2f80d3 --- /dev/null +++ b/shenyu-extension/shenyu-extension-eureka/shenyu-spring-boot-starter-registry-eureka/src/main/java/org/apache/shenyu/plugin/store/eureka/EurekaRegistryAutoConfiguration.java @@ -0,0 +1,53 @@ +/* + * 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.plugin.store.eureka; + +import org.apache.shenyu.springboot.starter.registry.ShenyuRegistryConfiguration; +import org.apache.shenyu.registry.api.ShenyuInstanceRegisterRepository; +import org.apache.shenyu.registry.api.config.RegisterConfig; +import org.apache.shenyu.registry.core.ShenyuInstanceRegisterRepositoryFactory; +import org.springframework.beans.factory.DisposableBean; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Import; + +/** + * Enable the retained registry lifecycle with the external Eureka SPI provider. + */ +@AutoConfiguration +@Import(ShenyuRegistryConfiguration.class) +public class EurekaRegistryAutoConfiguration { + + /** + * Close the registration provider when its application context stops. + * + * @param config bound registry configuration + * @return owned provider cleanup + */ + @Bean + @ConditionalOnProperty(name = "shenyu.register.enabled", havingValue = "true") + public DisposableBean eurekaRegistryLifecycle(final RegisterConfig config) { + if (!"eureka".equals(config.getRegisterType())) { + return () -> { }; + } + ShenyuInstanceRegisterRepository repository = ShenyuInstanceRegisterRepositoryFactory.newAndInitInstance(config); + return repository::close; + } +} + diff --git a/shenyu-extension/shenyu-extension-eureka/shenyu-spring-boot-starter-registry-eureka/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/shenyu-extension/shenyu-extension-eureka/shenyu-spring-boot-starter-registry-eureka/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 0000000..a41af2b --- /dev/null +++ b/shenyu-extension/shenyu-extension-eureka/shenyu-spring-boot-starter-registry-eureka/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -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. + +org.apache.shenyu.plugin.store.eureka.EurekaRegistryAutoConfiguration + diff --git a/shenyu-extension/shenyu-extension-eureka/shenyu-spring-boot-starter-registry-eureka/src/test/java/org/apache/shenyu/plugin/store/eureka/EurekaRegistryAutoConfigurationTest.java b/shenyu-extension/shenyu-extension-eureka/shenyu-spring-boot-starter-registry-eureka/src/test/java/org/apache/shenyu/plugin/store/eureka/EurekaRegistryAutoConfigurationTest.java new file mode 100644 index 0000000..69f6bd1 --- /dev/null +++ b/shenyu-extension/shenyu-extension-eureka/shenyu-spring-boot-starter-registry-eureka/src/test/java/org/apache/shenyu/plugin/store/eureka/EurekaRegistryAutoConfigurationTest.java @@ -0,0 +1,82 @@ +/* + * 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.plugin.store.eureka; + +import org.apache.shenyu.registry.api.config.RegisterConfig; +import org.apache.shenyu.registry.api.entity.InstanceEntity; +import org.apache.shenyu.registry.eureka.EurekaInstanceRegisterRepository; +import org.apache.shenyu.springboot.starter.registry.RegistryListener; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.MockedConstruction; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.boot.web.context.WebServerInitializedEvent; +import org.springframework.boot.web.server.WebServer; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockConstruction; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * The external starter resolves the provider and preserves registration lifecycle. + */ +class EurekaRegistryAutoConfigurationTest { + + private final ApplicationContextRunner runner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(ConfigurationPropertiesAutoConfiguration.class, EurekaRegistryAutoConfiguration.class)); + + @Test + void disabledByDefault() { + runner.run(context -> { + org.assertj.core.api.Assertions.assertThat(context).doesNotHaveBean(RegisterConfig.class); + org.assertj.core.api.Assertions.assertThat(context).doesNotHaveBean(RegistryListener.class); + }); + } + + @Test + void externalProviderRegistersTheBoundPortOnce() { + try (MockedConstruction construction = mockConstruction(EurekaInstanceRegisterRepository.class)) { + runner.withPropertyValues("shenyu.register.enabled=true", "shenyu.register.registerType=eureka", + "shenyu.register.serverLists=http://eureka:8761/eureka/", "shenyu.register.props.host=127.0.0.1") + .run(context -> { + org.assertj.core.api.Assertions.assertThat(context).hasSingleBean(RegisterConfig.class).hasSingleBean(RegistryListener.class); + assertEquals(1, construction.constructed().size()); + EurekaInstanceRegisterRepository provider = construction.constructed().get(0); + RegisterConfig config = context.getBean(RegisterConfig.class); + assertEquals("eureka", config.getRegisterType()); + verify(provider).init(config); + RegistryListener listener = context.getBean(RegistryListener.class); + WebServerInitializedEvent event = mock(WebServerInitializedEvent.class); + WebServer server = mock(WebServer.class); + when(event.getWebServer()).thenReturn(server); + when(server.getPort()).thenReturn(9197); + listener.onApplicationEvent(event); + listener.onApplicationEvent(event); + ArgumentCaptor instance = ArgumentCaptor.forClass(InstanceEntity.class); + verify(provider).persistInstance(instance.capture()); + assertEquals(9197, instance.getValue().getPort()); + }); + verify(construction.constructed().get(0)).close(); + } + } +} + From 2e10c449d1408b59f3f80e2809c0f8a1df914985 Mon Sep 17 00:00:00 2001 From: yangjj-iso <3202137046@qq.com> Date: Wed, 21 Oct 2026 20:00:01 +0800 Subject: [PATCH 2/4] docs(eureka): record complete external runtime parity evidence --- docs/plugin-store-eureka.md | 45 ++++++++++++++----- .../e2e/results/windows-jdk17.json | 25 +++++++++++ 2 files changed, 59 insertions(+), 11 deletions(-) create mode 100644 shenyu-extension/shenyu-extension-eureka/e2e/results/windows-jdk17.json diff --git a/docs/plugin-store-eureka.md b/docs/plugin-store-eureka.md index fe4eb2b..7fa5979 100644 --- a/docs/plugin-store-eureka.md +++ b/docs/plugin-store-eureka.md @@ -25,13 +25,16 @@ Source preparation, local snapshot installation and this guide are not publicati ## Compatibility and ownership -Development validation uses store 2.7.1-SNAPSHOT, core 2.7.2-SNAPSHOT at 87c22dde7, +Development validation uses store 2.7.1-SNAPSHOT, core 2.7.2-SNAPSHOT at cutover commit cfc267c8b (base 87c22dde7), Microsoft OpenJDK 17.0.18 and Netflix eureka-client/eureka-client-jersey3 2.0.2. The disposable server uses ShenYu's Spring Cloud Eureka example (4.1.2). These are development coordinates and test scope, not a released compatibility matrix. The provider keeps its packages, non-singleton @Join annotation, eureka type key, -SPI resource, registration/discovery/lease behavior and polling-failure recovery. +SPI resource and existing lease/registration behavior. Runtime parity testing also +corrects identity-only update comparison: changed upstream payloads produce updates; +an IP/port change removes the old URL before adding its replacement. Polling-failure +recovery remains covered. No core factory dispatch or enum/SQL key changes are needed. It resolves through ShenyuInstanceRegisterRepositoryFactory with an external provider JAR. This transfer does not redesign Eureka instance identity, authentication or refresh policy. @@ -61,7 +64,8 @@ For gateway instance registration, add: ``` -The external starter retains the core registry lifecycle and explicitly excludes +The external starter retains bound-port registration and owns provider cleanup on +application-context shutdown. It explicitly excludes the core starter's old transitive Eureka provider. It contributes its external provider instead. Other retained compatibility providers may remain on that starter's classpath; exclude those explicitly if building a minimal assembly. @@ -77,8 +81,10 @@ wiring need the provider rather than the gateway starter: ``` -Resolve the complete runtime dependency closure, including Eureka's Jersey 3 -transport, when installing into lib/ distributions. Do not copy only the provider +The provider JAR isolates Jersey 3 transport, HK2 and JAX-RS classes under private +packages. This avoids the older Jersey providers found on the full Bootstrap +classpath while retaining shared ShenYu API/SPI class identity. Resolve the complete +runtime dependency closure when installing into lib/ distributions. Do not copy only the provider JAR or replace retained core API/SPI classes with bundled duplicates. The source remains ASF/Apache-2.0. Preserve Netflix/Jersey and resolved third-party licenses/notices in custom binary distributions. @@ -136,11 +142,29 @@ API artifacts from a pinned main commit, then runs the receiving provider. release and announce migration/rollback versions. Do not close #7596 before the complete deployment and publication evidence exists. -Local evidence: six unit/SPI/starter tests pass with Checkstyle and RAT. The real -server lifecycle test also passes locally. Full Admin/bootstrap/Eureka container -E2E has not been run: the local Docker Linux-engine pipe was unavailable even -after a desktop launch attempt. That gate is outstanding, not certified by the -provider-only test. +Local evidence on 2026-10-21: seven unit/SPI/starter tests pass with Checkstyle +and RAT, and a separate real-server provider lifecycle test passes. The complete +local process E2E passes 13 checkpoints using actual Eureka, H2 Admin and bootstrap +JVMs with the external provider JAR. It verifies class origins in both server +processes, bound-port registration, discovery/proxying, endpoint refresh and old +endpoint removal, graceful gateway deregistration, gateway restart, healthy sibling +preservation and final route removal. The harness uses a stdin-controlled Spring +context for graceful shutdown on Windows as well as Linux. + +The runtime source pair is core cfc267c8b and extension 8bf73dd; the tested registry +JAR SHA-256 is e5fefda48d00b17cf505549e100479b87c04a5d83ca7c0ef6c3547225fc8fc31. +The receiving repository retains the machine-readable process result under its +Eureka e2e/results directory. The CI lane is configured to repeat the live provider +and complete process tests; its configuration alone is not a passing CI result. +Docker image/container execution was not run. + +To reproduce the full process harness, first package core Admin, bootstrap and the +retained registry starter with dependency:build-classpath, setting +-Dmdep.outputFile=target/eureka-classpath.txt -DincludeScope=runtime. Package the +external Eureka starter reactor with the same classpath goal. Start the real Eureka +example server, then run e2e/run-local.py with --core, --store, --java, --eureka and +--output paths. The workflow records the complete wrapper commands and retains +redacted process/class-origin logs plus result.json. ## Upgrade and rollback @@ -155,4 +179,3 @@ that included the provider, removing the external copy. Preserve database/type keys and Eureka applications. Clean up or expire obsolete instance records using the deployment's existing lease/identity procedure. Do not clear the whole registry or run two versions with identical classes in the same JVM. - diff --git a/shenyu-extension/shenyu-extension-eureka/e2e/results/windows-jdk17.json b/shenyu-extension/shenyu-extension-eureka/e2e/results/windows-jdk17.json new file mode 100644 index 0000000..0585792 --- /dev/null +++ b/shenyu-extension/shenyu-extension-eureka/e2e/results/windows-jdk17.json @@ -0,0 +1,25 @@ +{ + "core": "cfc267c8b7766baa7580f5a97e11ca9ea1a385c2", + "store": "8bf73dd4fee574440e24dcde26a44d333f3e0fd7", + "assertions": [ + "admin-ready", + "external-provider-origin-admin", + "external-provider-origin-gateway", + "gateway-bound-port", + "discovery-proxy", + "discovery-refresh", + "old-endpoint-removal", + "gateway-context-deregistration", + "gateway-restart", + "healthy-sibling", + "deregistration" + ], + "backendApplication": "SHENYU-EXTERNAL-A923F853650A43C489A865C2B7A02D25", + "gatewayApplication": "SHENYU-EXTERNAL-A923F853650A43C489A865C2B7A02D25-GATEWAY", + "externalRegistryJarSha256": "e5fefda48d00b17cf505549e100479b87c04a5d83ca7c0ef6c3547225fc8fc31", + "date": "2026-10-21", + "runtime": "Microsoft OpenJDK 17.0.18, Windows amd64", + "database": "H2 in-memory", + "sync": "WebSocket", + "checkpointsPassed": 13 +} From 0921f1a43b8819f855c47da049a379c169089575 Mon Sep 17 00:00:00 2001 From: yangjj-iso <3202137046@qq.com> Date: Wed, 21 Oct 2026 20:01:29 +0800 Subject: [PATCH 3/4] ci(eureka): pin core revision and run its wrapper from core --- .github/workflows/eureka-provider.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/eureka-provider.yml b/.github/workflows/eureka-provider.yml index 69a0462..ab5b1a9 100644 --- a/.github/workflows/eureka-provider.yml +++ b/.github/workflows/eureka-provider.yml @@ -34,7 +34,7 @@ jobs: - uses: actions/checkout@v4 with: repository: apache/shenyu - ref: 87c22dde7 + ref: 87c22dde7be14c1c3755440a4d8b082a86d10a87 path: core - uses: actions/setup-java@v4 with: @@ -73,8 +73,9 @@ jobs: cat eureka-server.log exit 1 - name: Verify external provider and starter + working-directory: core run: >- - core/mvnw -B -ntp -f store/pom.xml + ./mvnw -B -ntp -f ../store/pom.xml -pl shenyu-extension/shenyu-extension-eureka/shenyu-spring-boot-starter-registry-eureka -am verify dependency:build-classpath apache-rat:check -Peureka-live -Dmdep.outputFile=target/eureka-classpath.txt -DincludeScope=runtime From be07c121085f219f46578515156e7a0bb20befdf Mon Sep 17 00:00:00 2001 From: redyyang <3202137046@qq.com> Date: Fri, 6 Nov 2026 05:08:52 -0800 Subject: [PATCH 4/4] ci(eureka): validate against the current core cutover --- .github/workflows/eureka-provider.yml | 5 +- docs/plugin-store-eureka.md | 34 ++++++++++-- .../e2e/results/linux-jdk21-current-core.json | 53 +++++++++++++++++++ 3 files changed, 85 insertions(+), 7 deletions(-) create mode 100644 shenyu-extension/shenyu-extension-eureka/e2e/results/linux-jdk21-current-core.json diff --git a/.github/workflows/eureka-provider.yml b/.github/workflows/eureka-provider.yml index ab5b1a9..f5f3d37 100644 --- a/.github/workflows/eureka-provider.yml +++ b/.github/workflows/eureka-provider.yml @@ -34,7 +34,7 @@ jobs: - uses: actions/checkout@v4 with: repository: apache/shenyu - ref: 87c22dde7be14c1c3755440a4d8b082a86d10a87 + ref: 31d90ba4022c3c49bbdd2aa507cb4e1f5fc8b87f path: core - uses: actions/setup-java@v4 with: @@ -55,7 +55,7 @@ jobs: working-directory: core run: >- ./mvnw -B -ntp -pl shenyu-admin,shenyu-bootstrap,shenyu-spring-boot-starter/shenyu-spring-boot-starter-registry -am - package dependency:build-classpath -DskipTests -Dmdep.outputFile=target/eureka-classpath.txt -DincludeScope=runtime + install dependency:build-classpath -DskipTests -Dmdep.outputFile=target/eureka-classpath.txt -DincludeScope=runtime -DskipRemoteResources=true -Djacoco.skip=true -Dmaven.javadoc.skip=true - name: Start Eureka and await readiness shell: bash @@ -78,6 +78,7 @@ jobs: ./mvnw -B -ntp -f ../store/pom.xml -pl shenyu-extension/shenyu-extension-eureka/shenyu-spring-boot-starter-registry-eureka -am verify dependency:build-classpath apache-rat:check -Peureka-live + -Dshenyu.version=2.7.3-SNAPSHOT -Dmdep.outputFile=target/eureka-classpath.txt -DincludeScope=runtime -Deureka.test.url=http://127.0.0.1:8761/eureka/ -Deureka.client.refresh.interval=1 diff --git a/docs/plugin-store-eureka.md b/docs/plugin-store-eureka.md index 7fa5979..524c755 100644 --- a/docs/plugin-store-eureka.md +++ b/docs/plugin-store-eureka.md @@ -25,11 +25,19 @@ Source preparation, local snapshot installation and this guide are not publicati ## Compatibility and ownership -Development validation uses store 2.7.1-SNAPSHOT, core 2.7.2-SNAPSHOT at cutover commit cfc267c8b (base 87c22dde7), +Initial development validation used store 2.7.1-SNAPSHOT, core 2.7.2-SNAPSHOT at cutover commit cfc267c8b (base 87c22dde7), Microsoft OpenJDK 17.0.18 and Netflix eureka-client/eureka-client-jersey3 2.0.2. The disposable server uses ShenYu's Spring Cloud Eureka example (4.1.2). These are development coordinates and test scope, not a released compatibility matrix. +The current Eureka lifecycle workflow pins the paired removal commit +31d90ba4022c3c49bbdd2aa507cb4e1f5fc8b87f from apache/shenyu#7960 and explicitly +builds the receiving modules with `-Dshenyu.version=2.7.3-SNAPSHOT`. Store artifact +coordinates remain 2.7.1-SNAPSHOT. The workflow installs the selected core reactor +before resolving the Admin/gateway runtime classpaths, so validation uses that +source pair rather than an older cached core snapshot. This source-pair update +does not establish a published compatibility matrix or an upstream CI pass. + The provider keeps its packages, non-singleton @Join annotation, eureka type key, SPI resource and existing lease/registration behavior. Runtime parity testing also corrects identity-only update comparison: changed upstream payloads produce updates; @@ -120,8 +128,9 @@ update/refresh, deregistration and watch shutdown. Example verification from a checkout with the core Maven wrapper available: ```text -core/mvnw -f store/pom.xml -pl shenyu-extension/shenyu-extension-eureka/shenyu-spring-boot-starter-registry-eureka -am test -core/mvnw -f store/pom.xml -pl shenyu-extension/shenyu-extension-eureka/shenyu-registry-eureka -am verify -Peureka-live -Deureka.test.url=http://127.0.0.1:8761/eureka/ +cd core +./mvnw -f ../store/pom.xml -pl shenyu-extension/shenyu-extension-eureka/shenyu-spring-boot-starter-registry-eureka -am test -Dshenyu.version=2.7.3-SNAPSHOT +./mvnw -f ../store/pom.xml -pl shenyu-extension/shenyu-extension-eureka/shenyu-registry-eureka -am verify -Dshenyu.version=2.7.3-SNAPSHOT -Peureka-live -Deureka.test.url=http://127.0.0.1:8761/eureka/ ``` The live profile must fail if its server URL is missing/unreachable; ordinary unit @@ -158,10 +167,25 @@ Eureka e2e/results directory. The CI lane is configured to repeat the live provi and complete process tests; its configuration alone is not a passing CI result. Docker image/container execution was not run. -To reproduce the full process harness, first package core Admin, bootstrap and the +Current-core local evidence on 2026-11-06: store runtime code at 0921f1a43 and +core removal commit 31d90ba40 pass seven unit/SPI/starter tests, one live provider +test and all 13 full-process checkpoints on OpenJDK 21.0.13/Linux. The selected +163-module Admin/bootstrap reactor builds with Checkstyle and RAT; its tests are +compiled but skipped. Neither default Admin nor bootstrap runtime classpath +contains the Eureka provider or Netflix Eureka dependencies before the external +closure is added. The machine-readable result and provider JAR digest are in +`shenyu-extension/shenyu-extension-eureka/e2e/results/linux-jdk21-current-core.json`. +The disposable server was compiled locally with `-Dmaven.compiler.release=` +because the available runtime lacks `ct.sym`; Java 17 source/target settings +remain unchanged. The committed workflow still uses Temurin 17. This local +process result does not replace Docker IT/E2E, upstream workflow approval or +compatible artifact publication. + +To reproduce the full process harness, first install core Admin, bootstrap and the retained registry starter with dependency:build-classpath, setting -Dmdep.outputFile=target/eureka-classpath.txt -DincludeScope=runtime. Package the -external Eureka starter reactor with the same classpath goal. Start the real Eureka +external Eureka starter reactor with the same classpath goal and +`-Dshenyu.version=2.7.3-SNAPSHOT`. Start the real Eureka example server, then run e2e/run-local.py with --core, --store, --java, --eureka and --output paths. The workflow records the complete wrapper commands and retains redacted process/class-origin logs plus result.json. diff --git a/shenyu-extension/shenyu-extension-eureka/e2e/results/linux-jdk21-current-core.json b/shenyu-extension/shenyu-extension-eureka/e2e/results/linux-jdk21-current-core.json new file mode 100644 index 0000000..6138024 --- /dev/null +++ b/shenyu-extension/shenyu-extension-eureka/e2e/results/linux-jdk21-current-core.json @@ -0,0 +1,53 @@ +{ + "core": "31d90ba4022c3c49bbdd2aa507cb4e1f5fc8b87f", + "store": "0921f1a43b8819f855c47da049a379c169089575", + "assertions": [ + "admin-ready", + "external-provider-origin-admin", + "external-provider-origin-gateway", + "gateway-bound-port", + "discovery-proxy", + "discovery-refresh", + "old-endpoint-removal", + "gateway-context-deregistration", + "gateway-restart", + "healthy-sibling", + "deregistration" + ], + "backendApplication": "SHENYU-EXTERNAL-B261412C36194E9F948B22B85B068068", + "gatewayApplication": "SHENYU-EXTERNAL-B261412C36194E9F948B22B85B068068-GATEWAY", + "externalRegistryJarSha256": "9b2c226c6749af681eae31c788a4333af4f71583f624ae49bbeb59d952fedd5f", + "date": "2026-11-06", + "runtime": "OpenJDK 21.0.13, Linux amd64", + "database": "H2 in-memory", + "sync": "WebSocket", + "checkpointsPassed": 13, + "checkpoints": [ + "Admin authenticated readiness", + "Gateway external provider origin", + "Gateway bound-port registration", + "Registered selector readiness", + "Admin external provider origin", + "Admin discovery to gateway HTTP routing", + "Eureka update refreshes gateway routing", + "Old endpoint is removed after identity-preserving update", + "Gateway context shutdown deregisters its instance", + "Gateway restart restores discovered route", + "Healthy sibling enters discovery", + "Deregistration preserves healthy sibling routing", + "Eureka deregistration removes gateway route" + ], + "coreVersion": "2.7.3-SNAPSHOT", + "storeVersion": "2.7.1-SNAPSHOT", + "unitTestsPassed": 7, + "liveProviderTestsPassed": 1, + "defaultCoreEurekaDependencies": { + "shenyu-admin": [], + "shenyu-bootstrap": [] + }, + "notes": [ + "The recorded store revision identifies unchanged runtime code; only workflow and documentation changes were present during this run.", + "The disposable Eureka example used -Dmaven.compiler.release= locally because this runtime lacks ct.sym; Java 17 source/target settings were retained. The committed workflow uses Temurin 17.", + "Process-based local validation only; Docker deployment, artifact publication and upstream CI approval are not established." + ] +}