From 2b1a740e21ab8b9f25e6da7ebc4d6c518ffcbdf3 Mon Sep 17 00:00:00 2001 From: yangjj-iso <3202137046@qq.com> Date: Wed, 14 Oct 2026 19:10:23 +0800 Subject: [PATCH 1/6] feat(etcd): receive the complete sync and registry integration --- .github/workflows/build.yml | 10 +- README.md | 4 + examples/etcd/admin/application.yml | 23 + examples/etcd/gateway/application.yml | 30 + examples/etcd/gateway/pom.xml | 68 ++ .../compose/sync/shenyu-sync-etcd-eureka.yml | 132 ++++ .../compose/sync/shenyu-sync-etcd.yml | 128 ++++ .../shenyu-e2e-case/k8s/shenyu-etcd.yml | 67 ++ .../k8s/sync/shenyu-admin-etcd.yml | 114 +++ .../k8s/sync/shenyu-bootstrap-etcd.yml | 95 +++ .../shenyu-e2e-case/k8s/sync/shenyu-cm.yml | 576 +++++++++++++++ .../k8s/shenyu-cm.yml | 531 ++++++++++++++ .../shenyu-examples-springcloud/README.md | 41 ++ script/plugin-store/build-etcd-runtime.sh | 74 ++ script/plugin-store/etcd-deployment-e2e.py | 81 +++ shenyu-extension/pom.xml | 1 + .../shenyu-extension-etcd/LICENSE | 229 ++++++ shenyu-extension/shenyu-extension-etcd/NOTICE | 13 + .../shenyu-extension-etcd/README.md | 163 +++++ .../shenyu-extension-etcd/pom.xml | 52 ++ .../shenyu-admin-listener-etcd/pom.xml | 55 ++ .../admin/config/EtcdSyncConfiguration.java | 68 ++ .../listener/etcd/EtcdDataChangedInit.java | 52 ++ .../etcd/EtcdDataDataChangedListener.java | 49 ++ .../main/resources/META-INF/spring.factories | 19 + .../main/resources/META-INF/spring.provides | 18 + ...ot.autoconfigure.AutoConfiguration.imports | 18 + .../config/EtcdSyncConfigurationTest.java | 62 ++ .../etcd/EtcdDataChangedInitTest.java | 63 ++ .../etcd/EtcdDataDataChangedListenerTest.java | 141 ++++ .../shenyu-infra-etcd/pom.xml | 88 +++ .../etcd/autoconfig/ConditionOnSyncEtcd.java | 39 ++ .../etcd/autoconfig/EtcdConfiguration.java | 70 ++ .../infra/etcd/autoconfig/EtcdProperties.java | 43 ++ .../shenyu/infra/etcd/client/EtcdClient.java | 549 +++++++++++++++ .../shenyu/infra/etcd/config/EtcdConfig.java | 173 +++++ .../main/resources/META-INF/spring.factories | 19 + ...ot.autoconfigure.AutoConfiguration.imports | 18 + .../infra/etcd/client/EtcdClientTest.java | 270 +++++++ .../properties/AbstractConfigurationTest.java | 69 ++ .../etcd/properties/EtcdPropertiesTest.java | 74 ++ .../shenyu-registry-etcd/pom.xml | 74 ++ .../etcd/EtcdInstanceRegisterRepository.java | 172 +++++ ...istry.api.ShenyuInstanceRegisterRepository | 17 + .../shenyu/registry/etcd/EtcdClientTest.java | 148 ++++ .../EtcdInstanceRegisterRepositoryTest.java | 239 +++++++ .../shenyu/registry/etcd/RegistrySpiTest.java | 33 + .../pom.xml | 134 ++++ .../data/etcd/EtcdSyncDataConfiguration.java | 91 +++ ...ot.autoconfigure.AutoConfiguration.imports | 18 + .../sync/data/etcd/EtcdDeploymentIT.java | 151 ++++ .../sync/data/etcd/EtcdRealServerTest.java | 656 ++++++++++++++++++ .../etcd/EtcdSyncDataConfigurationTest.java | 70 ++ .../sync/data/etcd/EtcdTestServer.java | 141 ++++ .../shenyu-sync-data-etcd/pom.xml | 42 ++ .../sync/data/etcd/EtcdSyncDataService.java | 114 +++ .../data/etcd/EtcdSyncDataServiceTest.java | 73 ++ 57 files changed, 6560 insertions(+), 2 deletions(-) create mode 100644 examples/etcd/admin/application.yml create mode 100644 examples/etcd/gateway/application.yml create mode 100644 examples/etcd/gateway/pom.xml create mode 100644 examples/etcd/legacy/shenyu-e2e/shenyu-e2e-case/compose/sync/shenyu-sync-etcd-eureka.yml create mode 100644 examples/etcd/legacy/shenyu-e2e/shenyu-e2e-case/compose/sync/shenyu-sync-etcd.yml create mode 100644 examples/etcd/legacy/shenyu-e2e/shenyu-e2e-case/k8s/shenyu-etcd.yml create mode 100644 examples/etcd/legacy/shenyu-e2e/shenyu-e2e-case/k8s/sync/shenyu-admin-etcd.yml create mode 100644 examples/etcd/legacy/shenyu-e2e/shenyu-e2e-case/k8s/sync/shenyu-bootstrap-etcd.yml create mode 100644 examples/etcd/legacy/shenyu-e2e/shenyu-e2e-case/k8s/sync/shenyu-cm.yml create mode 100644 examples/etcd/legacy/shenyu-e2e/shenyu-e2e-case/shenyu-e2e-case-spring-cloud/k8s/shenyu-cm.yml create mode 100644 examples/etcd/legacy/shenyu-examples/shenyu-examples-springcloud/README.md create mode 100644 script/plugin-store/build-etcd-runtime.sh create mode 100644 script/plugin-store/etcd-deployment-e2e.py create mode 100644 shenyu-extension/shenyu-extension-etcd/LICENSE create mode 100644 shenyu-extension/shenyu-extension-etcd/NOTICE create mode 100644 shenyu-extension/shenyu-extension-etcd/README.md create mode 100644 shenyu-extension/shenyu-extension-etcd/pom.xml create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/pom.xml create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/main/java/org/apache/shenyu/admin/config/EtcdSyncConfiguration.java create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/main/java/org/apache/shenyu/admin/listener/etcd/EtcdDataChangedInit.java create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/main/java/org/apache/shenyu/admin/listener/etcd/EtcdDataDataChangedListener.java create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/main/resources/META-INF/spring.factories create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/main/resources/META-INF/spring.provides create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/test/java/org/apache/shenyu/admin/config/EtcdSyncConfigurationTest.java create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/test/java/org/apache/shenyu/admin/listener/etcd/EtcdDataChangedInitTest.java create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/test/java/org/apache/shenyu/admin/listener/etcd/EtcdDataDataChangedListenerTest.java create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/pom.xml create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/main/java/org/apache/shenyu/infra/etcd/autoconfig/ConditionOnSyncEtcd.java create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/main/java/org/apache/shenyu/infra/etcd/autoconfig/EtcdConfiguration.java create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/main/java/org/apache/shenyu/infra/etcd/autoconfig/EtcdProperties.java create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/main/java/org/apache/shenyu/infra/etcd/client/EtcdClient.java create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/main/java/org/apache/shenyu/infra/etcd/config/EtcdConfig.java create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/main/resources/META-INF/spring.factories create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/test/java/org/apache/shenyu/infra/etcd/client/EtcdClientTest.java create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/test/java/org/apache/shenyu/infra/etcd/properties/AbstractConfigurationTest.java create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/test/java/org/apache/shenyu/infra/etcd/properties/EtcdPropertiesTest.java create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-registry-etcd/pom.xml create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-registry-etcd/src/main/java/org/apache/shenyu/registry/etcd/EtcdInstanceRegisterRepository.java create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-registry-etcd/src/main/resources/META-INF/shenyu/org.apache.shenyu.registry.api.ShenyuInstanceRegisterRepository create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-registry-etcd/src/test/java/org/apache/shenyu/registry/etcd/EtcdClientTest.java create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-registry-etcd/src/test/java/org/apache/shenyu/registry/etcd/EtcdInstanceRegisterRepositoryTest.java create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-registry-etcd/src/test/java/org/apache/shenyu/registry/etcd/RegistrySpiTest.java create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/pom.xml create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/main/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdSyncDataConfiguration.java create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/test/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdDeploymentIT.java create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/test/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdRealServerTest.java create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/test/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdSyncDataConfigurationTest.java create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/test/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdTestServer.java create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-sync-data-etcd/pom.xml create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-sync-data-etcd/src/main/java/org/apache/shenyu/sync/data/etcd/EtcdSyncDataService.java create mode 100644 shenyu-extension/shenyu-extension-etcd/shenyu-sync-data-etcd/src/test/java/org/apache/shenyu/sync/data/etcd/EtcdSyncDataServiceTest.java diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index beff4ec..f62e9aa 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 5aa9517a8fb513c3afaa3606cfe8bfefb32fc5ad 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-infra/shenyu-infra-common,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" @@ -112,3 +112,9 @@ jobs: SHENYU_CORE_SOURCE: "${{ runner.temp }}/shenyu-core-api" run: bash script/plugin-store/run-broker-original-suites.sh logging-rabbitmq all + - name: Verify external etcd with full Admin and gateway deployments + env: + SHENYU_CORE_SOURCE: "${{ runner.temp }}/shenyu-core-api" + run: | + bash script/plugin-store/build-etcd-runtime.sh + mvn -B -ntp -pl shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd -am verify -Petcd-e2e -Dapi.version=1.44 diff --git a/README.md b/README.md index 996610d..a7df0fe 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) + +### etcd integration + +The [etcd extension](shenyu-extension/shenyu-extension-etcd/README.md) owns its admin listener, sync adapter, registry provider, starter and shared client together. diff --git a/examples/etcd/admin/application.yml b/examples/etcd/admin/application.yml new file mode 100644 index 0000000..adee772 --- /dev/null +++ b/examples/etcd/admin/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. + +shenyu: + sync: + etcd: + url: http://etcd-1:2379,http://etcd-2:2379 + sessionTimeout: 30000 + connectionTimeout: 30000 + websocket: + enabled: false diff --git a/examples/etcd/gateway/application.yml b/examples/etcd/gateway/application.yml new file mode 100644 index 0000000..1e8e2e7 --- /dev/null +++ b/examples/etcd/gateway/application.yml @@ -0,0 +1,30 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +shenyu: + sync: + etcd: + url: http://etcd-1:2379,http://etcd-2:2379 + sessionTimeout: 30000 + connectionTimeout: 30000 + websocket: + urls: false + register: + enabled: true + registerType: etcd + serverLists: http://etcd-1:2379,http://etcd-2:2379 + props: + etcdTimeout: "3000" + etcdTTL: "5" diff --git a/examples/etcd/gateway/pom.xml b/examples/etcd/gateway/pom.xml new file mode 100644 index 0000000..45a5e3d --- /dev/null +++ b/examples/etcd/gateway/pom.xml @@ -0,0 +1,68 @@ + + + + 4.0.0 + org.apache.shenyu.examples + shenyu-etcd-gateway-runtime + 1.0-SNAPSHOT + pom + + 2.7.2-SNAPSHOT + 2.7.1-SNAPSHOT + + + + org.apache.shenyu + shenyu-spring-boot-starter-sync-data-etcd + ${store.version} + + + org.apache.shenyu + shenyu-registry-etcd + ${store.version} + + + org.apache.shenyu + shenyu-spring-boot-starter-registry + ${shenyu.version} + + + org.apache.shenyu + shenyu-registry-zookeeper + + + org.apache.shenyu + shenyu-registry-etcd + + + org.apache.shenyu + shenyu-registry-nacos + + + org.apache.shenyu + shenyu-registry-eureka + + + org.apache.shenyu + shenyu-registry-kubernetes + + + + + diff --git a/examples/etcd/legacy/shenyu-e2e/shenyu-e2e-case/compose/sync/shenyu-sync-etcd-eureka.yml b/examples/etcd/legacy/shenyu-e2e/shenyu-e2e-case/compose/sync/shenyu-sync-etcd-eureka.yml new file mode 100644 index 0000000..549294d --- /dev/null +++ b/examples/etcd/legacy/shenyu-e2e/shenyu-e2e-case/compose/sync/shenyu-sync-etcd-eureka.yml @@ -0,0 +1,132 @@ +# 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. + +version: '3.9' + +services: + shenyu-etcd: + image: rancher/hardened-etcd:v3.5.21-k3s1-build20250910 + container_name: shenyu-etcd + command: ["etcd"] + environment: + - ALLOW_NONE_AUTHENTICATION=yes + - ETCD_ADVERTISE_CLIENT_URLS=http://shenyu-etcd:2379 + - TZ=Asia/Shanghai + ports: + - "32379:2379" + - "2380:2380" + healthcheck: + test: ["CMD", "etcdctl", "endpoint", "health"] + interval: 10s + timeout: 5s + retries: 10 + start_period: 30s + restart: always + networks: + - shenyu + + shenyu-mysql: + image: mysql:8.0 + container_name: shenyu-mysql + environment: + - MYSQL_ROOT_PASSWORD=shenyue2e + - MYSQL_USER=shenyue2e + - MYSQL_PASSWORD=shenyue2e + ports: + - "30306:3306" + volumes: + - /tmp/shenyu-e2e/mysql/schema/schema.sql:/docker-entrypoint-initdb.d/schema.sql + healthcheck: + test: [ "CMD", "mysql", "-u", "root", "-pshenyue2e", "-e", "SELECT 1" ] + interval: 10s + timeout: 5s + retries: 10 + start_period: 30s + deploy: + resources: + limits: + cpus: '1' + memory: 768M + reservations: + cpus: '0.5' + memory: 512M + restart: always + networks: + - shenyu + + shenyu-admin: + image: apache/shenyu-admin:latest + container_name: shenyu-admin + command: [ "-Xmx768m", "-Xms768m" ] + ports: + - "31095:9095" + environment: + - TZ=Asia/Beijing + - SPRING_PROFILES_ACTIVE=mysql + - shenyu.database.dialect=mysql + - shenyu.database.init_enable=true + - spring.datasource.username=shenyue2e + - spring.datasource.password=shenyue2e + - spring.datasource.url=jdbc:mysql://shenyu-mysql:3306/shenyu?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNull + - shenyu.sync.etcd.url=http://shenyu-etcd:2379 + - shenyu.jwt.secretKey=shenyu-e2e-jwt-secret-key-2024 + volumes: + - /tmp/shenyu-e2e/mysql/driver:/opt/shenyu-admin/ext-lib + healthcheck: + test: [ "CMD-SHELL", "wget -q -O - http://shenyu-admin:9095/actuator/health | grep UP || exit 1" ] + interval: 10s + timeout: 5s + retries: 15 + start_period: 30s + restart: always + depends_on: + shenyu-mysql: + condition: service_healthy + shenyu-etcd: + condition: service_healthy + networks: + - shenyu + + shenyu-bootstrap: + image: apache/shenyu-bootstrap:latest + container_name: shenyu-bootstrap + ports: + - "31195:9195" + environment: + - TZ=Asia/Beijing + - shenyu.sync.etcd.url=http://shenyu-etcd:2379 + - spring.cloud.discovery.enabled=true + - eureka.client.enabled=true + - eureka.client.serviceUrl.defaultZone=http://shenyu-examples-eureka:8761/eureka/ + - eureka.instance.prefer-ip-address=true + healthcheck: + test: [ "CMD-SHELL", "wget -q -O - http://shenyu-bootstrap:9195/actuator/health | grep UP || exit 1" ] + interval: 10s + timeout: 5s + retries: 15 + start_period: 30s + restart: always + depends_on: + shenyu-admin: + condition: service_healthy + networks: + - shenyu + +networks: + shenyu: + name: shenyu + driver: bridge + external: true diff --git a/examples/etcd/legacy/shenyu-e2e/shenyu-e2e-case/compose/sync/shenyu-sync-etcd.yml b/examples/etcd/legacy/shenyu-e2e/shenyu-e2e-case/compose/sync/shenyu-sync-etcd.yml new file mode 100644 index 0000000..bf2df79 --- /dev/null +++ b/examples/etcd/legacy/shenyu-e2e/shenyu-e2e-case/compose/sync/shenyu-sync-etcd.yml @@ -0,0 +1,128 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +version: '3.9' + +services: + shenyu-etcd: + image: rancher/hardened-etcd:v3.5.21-k3s1-build20250910 + container_name: shenyu-etcd + command: ["etcd"] + environment: + - ALLOW_NONE_AUTHENTICATION=yes + - ETCD_ADVERTISE_CLIENT_URLS=http://shenyu-etcd:2379 + - TZ=Asia/Shanghai + ports: + - "32379:2379" + - "2380:2380" + healthcheck: + test: ["CMD", "etcdctl", "endpoint", "health"] + interval: 10s + timeout: 5s + retries: 10 + start_period: 30s + restart: always + networks: + - shenyu + + shenyu-mysql: + image: mysql:8.0 + container_name: shenyu-mysql + environment: + - MYSQL_ROOT_PASSWORD=shenyue2e + - MYSQL_USER=shenyue2e + - MYSQL_PASSWORD=shenyue2e + ports: + - "30306:3306" + volumes: + - /tmp/shenyu-e2e/mysql/schema/schema.sql:/docker-entrypoint-initdb.d/schema.sql + healthcheck: + test: [ "CMD", "mysql", "-u", "root", "-pshenyue2e", "-e", "SELECT 1" ] + interval: 10s + timeout: 5s + retries: 10 + start_period: 30s + deploy: + resources: + limits: + cpus: '1' + memory: 768M + reservations: + cpus: '0.5' + memory: 512M + restart: always + networks: + - shenyu + + shenyu-admin: + image: apache/shenyu-admin:latest + container_name: shenyu-admin + command: [ "-Xmx768m", "-Xms768m" ] + ports: + - "31095:9095" + environment: + - TZ=Asia/Beijing + - SPRING_PROFILES_ACTIVE=mysql + - shenyu.database.dialect=mysql + - shenyu.database.init_enable=true + - spring.datasource.username=shenyue2e + - spring.datasource.password=shenyue2e + - spring.datasource.url=jdbc:mysql://shenyu-mysql:3306/shenyu?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNull + - shenyu.sync.etcd.url=http://shenyu-etcd:2379 + - shenyu.jwt.secretKey=shenyu-e2e-jwt-secret-key-2024 + volumes: + - /tmp/shenyu-e2e/mysql/driver:/opt/shenyu-admin/ext-lib + healthcheck: + test: [ "CMD-SHELL", "wget -q -O - http://shenyu-admin:9095/actuator/health | grep UP || exit 1" ] + interval: 10s + timeout: 5s + retries: 15 + start_period: 30s + restart: always + depends_on: + shenyu-mysql: + condition: service_healthy + shenyu-etcd: + condition: service_healthy + networks: + - shenyu + + shenyu-bootstrap: + image: apache/shenyu-bootstrap:latest + container_name: shenyu-bootstrap + ports: + - "31195:9195" + environment: + - TZ=Asia/Beijing + - shenyu.sync.etcd.url=http://shenyu-etcd:2379 + healthcheck: + test: [ "CMD-SHELL", "wget -q -O - http://shenyu-bootstrap:9195/actuator/health | grep UP || exit 1" ] + interval: 10s + timeout: 5s + retries: 15 + start_period: 30s + restart: always + depends_on: + shenyu-admin: + condition: service_healthy + networks: + - shenyu + +networks: + shenyu: + name: shenyu + driver: bridge + external: true diff --git a/examples/etcd/legacy/shenyu-e2e/shenyu-e2e-case/k8s/shenyu-etcd.yml b/examples/etcd/legacy/shenyu-e2e/shenyu-e2e-case/k8s/shenyu-etcd.yml new file mode 100644 index 0000000..15e4302 --- /dev/null +++ b/examples/etcd/legacy/shenyu-e2e/shenyu-e2e-case/k8s/shenyu-etcd.yml @@ -0,0 +1,67 @@ +# 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. + +apiVersion: apps/v1 +kind: Deployment +metadata: + name: shenyu-etcd + labels: + app: shenyu-etcd +spec: + replicas: 1 + selector: + matchLabels: + app: shenyu-etcd + strategy: { } + template: + metadata: + labels: + app: shenyu-etcd + spec: + containers: + - image: rancher/hardened-etcd:v3.5.21-k3s1-build20250910 + name: shenyu-etcd + command: ["etcd"] + resources: { } + env: + - name: ALLOW_NONE_AUTHENTICATION + value: "yes" + - name: ETCD_ADVERTISE_CLIENT_URLS + value: http://shenyu-etcd:2379 + - name: TZ + value: Asia/Shanghai + ports: + - containerPort: 2379 + name: client + - containerPort: 2380 + name: server + restartPolicy: Always + +--- +apiVersion: v1 +kind: Service +metadata: + name: shenyu-etcd + labels: + app: shenyu-etcd +spec: + type: NodePort + selector: + app: shenyu-etcd + ports: + - port: 2379 + targetPort: 2379 + nodePort: 32379 diff --git a/examples/etcd/legacy/shenyu-e2e/shenyu-e2e-case/k8s/sync/shenyu-admin-etcd.yml b/examples/etcd/legacy/shenyu-e2e/shenyu-e2e-case/k8s/sync/shenyu-admin-etcd.yml new file mode 100644 index 0000000..fa6bb3d --- /dev/null +++ b/examples/etcd/legacy/shenyu-e2e/shenyu-e2e-case/k8s/sync/shenyu-admin-etcd.yml @@ -0,0 +1,114 @@ +# 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. + +apiVersion: apps/v1 +kind: Deployment +metadata: + name: shenyu-admin +spec: + selector: + matchLabels: + app: shenyu-admin + replicas: 1 + template: + metadata: + labels: + app: shenyu-admin + spec: + containers: + - name: shenyu-admin + image: apache/shenyu-admin:latest + args: + - -Xmx768m -Xms768m + imagePullPolicy: IfNotPresent + ports: + - containerPort: 9095 + livenessProbe: + initialDelaySeconds: 30 + periodSeconds: 10 + timeoutSeconds: 5 + successThreshold: 1 + failureThreshold: 3 + httpGet: + port: 9095 + path: /actuator/health + readinessProbe: + initialDelaySeconds: 30 + periodSeconds: 10 + timeoutSeconds: 5 + successThreshold: 1 + failureThreshold: 3 + httpGet: + port: 9095 + path: /actuator/health + env: + - name: 'TZ' + value: 'Asia/Beijing' + - name: SPRING_PROFILES_ACTIVE + value: mysql, admin-sync-etcd + - name: SHENYU_JWT_SECRETKEY + value: shenyu-e2e-jwt-secret-key-2024 + volumeMounts: + - name: mysql-connector-volume + mountPath: /opt/shenyu-admin/ext-lib + - name: shenyu-admin-application + mountPath: /opt/shenyu-admin/conf/application.yml + subPath: shenyu-admin-application.yml + - name: shenyu-admin-application-mysql + mountPath: /opt/shenyu-admin/conf/application-mysql.yml + subPath: shenyu-admin-application-mysql.yml + - name: shenyu-admin-sync-etcd + mountPath: /opt/shenyu-admin/conf/application-admin-sync-etcd.yml + subPath: application-admin-sync-etcd.yml + restartPolicy: Always + volumes: + - name: mysql-connector-volume + hostPath: + path: /tmp/shenyu-e2e/mysql/driver + - name: shenyu-admin-application + configMap: + name: shenyu-configmap + items: + - key: shenyu-admin-application.yml + path: shenyu-admin-application.yml + - name: shenyu-admin-application-mysql + configMap: + name: shenyu-configmap + items: + - key: shenyu-admin-application-mysql.yml + path: shenyu-admin-application-mysql.yml + - name: shenyu-admin-sync-etcd + configMap: + name: shenyu-configmap + items: + - key: application-admin-sync-etcd.yml + path: application-admin-sync-etcd.yml +--- +apiVersion: v1 +kind: Service +metadata: + name: shenyu-admin + labels: + app: shenyu-admin +spec: + type: NodePort + selector: + app: shenyu-admin + ports: + - name: "9095" + port: 9095 + targetPort: 9095 + nodePort: 31095 diff --git a/examples/etcd/legacy/shenyu-e2e/shenyu-e2e-case/k8s/sync/shenyu-bootstrap-etcd.yml b/examples/etcd/legacy/shenyu-e2e/shenyu-e2e-case/k8s/sync/shenyu-bootstrap-etcd.yml new file mode 100644 index 0000000..420f675 --- /dev/null +++ b/examples/etcd/legacy/shenyu-e2e/shenyu-e2e-case/k8s/sync/shenyu-bootstrap-etcd.yml @@ -0,0 +1,95 @@ +# 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-bootstrap +apiVersion: apps/v1 +kind: Deployment +metadata: + name: shenyu-bootstrap +spec: + selector: + matchLabels: + app: shenyu-bootstrap + replicas: 1 + template: + metadata: + labels: + app: shenyu-bootstrap + spec: + containers: + - name: shenyu-bootstrap + image: apache/shenyu-bootstrap:latest + imagePullPolicy: IfNotPresent + ports: + - containerPort: 9195 + livenessProbe: + initialDelaySeconds: 30 + periodSeconds: 10 + timeoutSeconds: 5 + successThreshold: 1 + failureThreshold: 3 + httpGet: + port: 9195 + path: /actuator/health + readinessProbe: + initialDelaySeconds: 30 + periodSeconds: 10 + timeoutSeconds: 5 + successThreshold: 1 + failureThreshold: 3 + httpGet: + port: 9195 + path: /actuator/health + env: + - name: TZ + value: Asia/Beijing + - name: SPRING_PROFILES_ACTIVE + value: bootstrap-sync-etcd + volumeMounts: + - name: shenyu-bootstrap-application + mountPath: /opt/shenyu-bootstrap/conf/application.yml + subPath: shenyu-bootstrap-application.yml + - name: shenyu-bootstrap-sync-etcd + mountPath: /opt/shenyu-bootstrap/conf/application-bootstrap-sync-etcd.yml + subPath: application-bootstrap-sync-etcd.yml + volumes: + - name: shenyu-bootstrap-application + configMap: + name: shenyu-configmap + items: + - key: shenyu-bootstrap-application.yml + path: shenyu-bootstrap-application.yml + - name: shenyu-bootstrap-sync-etcd + configMap: + name: shenyu-configmap + items: + - key: application-bootstrap-sync-etcd.yml + path: application-bootstrap-sync-etcd.yml + +--- +apiVersion: v1 +kind: Service +metadata: + name: shenyu-bootstrap +spec: + selector: + app: shenyu-bootstrap + type: NodePort + ports: + - protocol: TCP + port: 9195 + targetPort: 9195 + nodePort: 31195 diff --git a/examples/etcd/legacy/shenyu-e2e/shenyu-e2e-case/k8s/sync/shenyu-cm.yml b/examples/etcd/legacy/shenyu-e2e/shenyu-e2e-case/k8s/sync/shenyu-cm.yml new file mode 100644 index 0000000..a4852ad --- /dev/null +++ b/examples/etcd/legacy/shenyu-e2e/shenyu-e2e-case/k8s/sync/shenyu-cm.yml @@ -0,0 +1,576 @@ +# 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. + +#apiVersion: v1 +#kind: Namespace +#metadata: +# name: shenyu +# labels: +# name: shenyu +#--- + +apiVersion: v1 +kind: ConfigMap +metadata: + name: shenyu-configmap + labels: + app: shenyu-configmap +data: + shenyu-admin-application.yml: | + server: + port: 9095 + address: 0.0.0.0 + + spring: + profiles: + active: h2 + thymeleaf: + cache: true + encoding: utf-8 + enabled: true + prefix: classpath:/static/ + suffix: .html + mvc: + pathmatch: + matching-strategy: ant_path_matcher + jackson: + time-zone: GMT+8 # GMT , Asia/Shanghai + messages: + basename: message/i18n + + management: + health: + mail: + enabled: off + endpoints: + web: + exposure: + include: + - 'health' + - 'prometheus' + enabled-by-default: true + + mybatis: + config-location: classpath:/mybatis/mybatis-config.xml + mapper-locations: classpath:/mappers/*.xml + type-handlers-package: org.apache.shenyu.admin.mybatis.handler + + shenyu: + register: + registerType: http #http #zookeeper #etcd #nacos #consul + serverLists: #localhost:2181 #http://localhost:2379 #localhost:8848 + props: + sessionTimeout: 5000 + connectionTimeout: 2000 + checked: true + zombieCheckThreads: 10 + zombieCheckTimes: 5 + scheduledTime: 10 + nacosNameSpace: ShenyuRegisterCenter + ldap: + enabled: false + url: ldap://xxxx:xxx + bind-dn: cn=xxx,dc=xxx,dc=xxx + password: xxxx + base-dn: ou=xxx,dc=xxx,dc=xxx + object-class: person + login-field: cn + jwt: + secret-key: shenyu-e2e-jwt-secret-key-2024 + expired-seconds: 86400000 + shiro: + white-list: + - / + - /favicon.* + - /static/** + - /index** + - /platform/login + - /websocket + - /error + - /actuator/health + - /actuator/health/** # /actuator/health/readiness /actuator/health/liveness + - /actuator/prometheus + - /swagger-ui.html + - /swagger-ui/** + - /webjars/** + - /v3/api-docs/** + - /csrf + - /alert/report + dashboard: + core: + onlySuperAdminPermission: + - system:manager:add + - system:manager:edit + - system:manager:delete + - system:role:add + - system:role:edit + - system:role:delete + - system:resource:addButton + - system:resource:addMenu + - system:resource:editButton + - system:resource:editMenu + - system:resource:deleteButton + - system:resource:deleteMenu + + springdoc: + api-docs: + enabled: true + swagger-ui: + enabled: true + tags-sorter: alpha + packages-to-scan: org.apache.shenyu.admin.controller + paths-to-match: /** + + logging: + level: + root: info + org.springframework.boot: info + org.apache.ibatis: info + org.apache.shenyu.bonuspoint: info + org.apache.shenyu.lottery: info + org.apache.shenyu: info + org.apache.shenyu.admin.listener.http: debug + # org.apache.shenyu.admin.utils.HttpUtils: debug + + + shenyu-admin-application-mysql.yml: | + shenyu: + database: + dialect: mysql + init_enable: true + + spring: + datasource: + url: jdbc:mysql://shenyu-mysql:3306/shenyu?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNull + username: root + password: shenyue2e + driver-class-name: com.mysql.cj.jdbc.Driver + hikari: + connection-timeout: 30000 + minimum-idle: 5 + maximum-pool-size: 20 + auto-commit: true + idle-timeout: 600000 + max-lifetime: 1800000 + connection-test-query: SELECT 1 + connection-init-sql: SET NAMES utf8mb4 + validation-timeout: 800 + mail: + # Attention: this is mail server address. + host: smtp.qq.com + username: shenyu@apache.com + # Attention: this is not email account password, this requires an email authorization code + password: your-password + #Attention: Tencent mail smtps 465,smtp 587 + port: 587 + properties: + mail: + smtp: + socketFactoryClass: javax.net.ssl.SSLSocketFactory + ssl: + enable: true + + shenyu-bootstrap-application.yml: | + server: + port: 9195 + address: 0.0.0.0 + compression: + enabled: true + minResponseSize: 1MB # If the response data is greater than 1MB, enable compression. + + spring: + main: + allow-bean-definition-overriding: true + application: + name: shenyu-bootstrap + codec: + max-in-memory-size: 2MB + cloud: + discovery: + enabled: false + nacos: + discovery: + server-addr: 127.0.0.1:8848 # Spring Cloud Alibaba Dubbo use this. + enabled: false + namespace: ShenyuRegisterCenter + + # if you want use ribbon please config every server. + #springCloud-test: + # ribbon: + # NIWSServerListClassName: com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList + + eureka: + client: + enabled: false + serviceUrl: + defaultZone: http://localhost:8761/eureka/ + instance: + prefer-ip-address: true + + # security: + # oauth2: + # client: + # registration: + # : + # client-id: + # client-secret: + # provider: + # : + # authorization-uri: + # token-uri: + # user-info-uri: + # jwk-set-uri: + + management: + health: + redis: + enabled: false + elasticsearch: + enabled: false + endpoint: + health: + enabled: true + show-details: always + endpoints: + web: + exposure: + include: "*" # or health,info + + + shenyu: + selectorMatchCache: + ## selector L1 cache + cache: + enabled: false + initialCapacity: 10000 # initial capacity in cache + maximumSize: 10000 # max size in cache + ruleMatchCache: + ## rule L1 cache + cache: + enabled: true + initialCapacity: 10000 # initial capacity in cache + maximumSize: 65536 # max size in cache + netty: + http: + # set to false, user can custom the netty tcp server config. + webServerFactoryEnabled: true + selectCount: 1 + workerCount: 8 + accessLog: false + serverSocketChannel: + soRcvBuf: 87380 + soBackLog: 128 + soReuseAddr: false + connectTimeoutMillis: 10000 + writeBufferHighWaterMark: 65536 + writeBufferLowWaterMark: 32768 + writeSpinCount: 16 + autoRead: false + allocType: "pooled" + messageSizeEstimator: 8 + singleEventExecutorPerGroup: true + socketChannel: + soKeepAlive: false + soReuseAddr: false + soLinger: -1 + tcpNoDelay: true + soRcvBuf: 87380 + soSndBuf: 16384 + ipTos: 0 + allowHalfClosure: false + connectTimeoutMillis: 10000 + writeBufferHighWaterMark: 65536 + writeBufferLowWaterMark: 32768 + writeSpinCount: 16 + autoRead: false + allocType: "pooled" + messageSizeEstimator: 8 + singleEventExecutorPerGroup: true + sni: + enabled: false + mod: k8s #manul + defaultK8sSecretNamespace: shenyu-ingress + defaultK8sSecretName: default-cert + # mod: manual + # certificates: + # - domain: 'localhost' + # keyCertChainFile: '/Users/zhukunshuai/Desktop/cert/example.com+1.pem' + # keyFile: '/Users/zhukunshuai/Desktop/cert/example.com+1-key.pem' + # - domain: 'example.com' + # keyCertChainFile: '/Users/zhukunshuai/Desktop/cert/example.com+1.pem' + # keyFile: '/Users/zhukunshuai/Desktop/cert/example.com+1-key.pem' + # httpclient: + # strategy: webClient # netty + # connectTimeout: 45000 + # responseTimeout: 3000 + # readerIdleTime: 3000 + # writerIdleTime: 3000 + # allIdleTime: 3000 + # readTimeout: 3000 + # writeTimeout: 3000 + # wiretap: false + # keepAlive: false + # maxInMemorySize: 1 #1mb + # pool: + # type: ELASTIC + # name: proxy + # maxConnections: 16 + # acquireTimeout: 45000 + # maxIdleTime: 3000 # unit: ms + # maxLifeTime: # unit: ms + # evictionInterval: # unit: ms + # metrics: false + # proxy: + # host: + # port: + # username: + # password: + # nonProxyHostsPattern: + # ssl: + # useInsecureTrustManager: true + # keyStoreType: PKCS12 + # keyStorePath: classpath:keystore.p12 + # keyStorePassword: 123456 + # keyStoreProvider: + # keyPassword: 123456 + # trustedX509Certificates: + # handshakeTimeout: + # closeNotifyFlushTimeout: + # closeNotifyReadTimeout: + # defaultConfigurationType: + # threadPool: + # prefix: shenyu + # selectCount: 1 + # workerCount: 8 + # daemon: true + duplicate-response-header: + strategy: RETAIN_FIRST + headers: + - Access-Control-Allow-Origin + register: + enabled: false + registerType: zookeeper #etcd #consul + serverLists: localhost:2181 #http://localhost:2379 #localhost:8848 + props: + cross: + enabled: true + allowedHeaders: + allowedMethods: "*" + allowedAnyOrigin: true # the same of Access-Control-Allow-Origin: "*" + # allowedOrigin: + # format : schema://prefix spacer domain + # Access-Control-Allow-Origin: "http://a.apache.org,http://b.apache.org" + # spacer: "." + # domain: apache.org + # prefixes: + # - a # a.apache.org + # - b # b.apache.org + # origins: + # - c.apache.org + # - d.apache.org + # - http://e.apache.org + # originRegex: ^http(|s)://(.*\.|)abc.com$ + allowedExpose: "" + maxAge: "18000" + allowCredentials: true + + switchConfig: + local: true + collapseSlashes: false + file: + enabled: true + maxSize : 10 + exclude: + enabled: false + paths: + - /favicon.ico + fallback: + enabled: false + paths: + - /fallback/resilience4j + - /fallback/sentinel + health: + enabled: true + paths: + - /actuator + - /health_check + alert: + enabled: true + admins: localhost:9095 + extPlugin: + path: + enabled: true + threads: 1 + scheduleTime: 300 + scheduleDelay: 30 + scheduler: + enabled: false + type: fixed + threads: 16 + upstreamCheck: + enabled: false + poolSize: 10 + timeout: 3000 + healthyThreshold: 1 + unhealthyThreshold: 1 + interval: 5000 + printEnabled: true + printInterval: 60000 + springCloudCache: + enabled: false + ribbon: + serverListRefreshInterval: 10000 + metrics: + enabled: false + name : prometheus + host: 127.0.0.1 + port: 8090 + jmxConfig: + props: + jvm_enabled: true + # plugins: + # rate-limiter.enabled: false + local: + enabled: false + sha512Key: "BA3253876AED6BC22D4A6FF53D8406C6AD864195ED144AB5C87621B6C233B548BAEAE6956DF346EC8C17F5EA10F35EE3CBC514797ED7DDD3145464E2A0BAB413" + websocket: + enableProxyPing: false + # sharedPool: + # enable: true + # prefix: "shenyu-shared" + # corePoolSize: 200 + # maximumPoolSize: 2000 + # keepAliveTime: 60000 + # # 1GB + # maxWorkQueueMemory: 1073741824 + # # 256MB + # maxFreeMemory: 268435456 + + logging: + level: + root: info + org.springframework.boot: info + org.apache.ibatis: info + org.apache.shenyu.bonuspoint: info + org.apache.shenyu.lottery: info + org.apache.shenyu: info + org.springframework.http.server.reactive: info + org.springframework.web.reactive: info + reactor.ipc.netty: info + reactor.netty: info + org.apache.shenyu.sync.data.http: debug + + application-admin-sync-websocket.yml: | + shenyu: + sync: + websocket: + enabled: true + messageMaxSize: 10240 + allowOrigins: ws://localhost:9095;ws://localhost:9195; + token: shenyu-sync-token + + application-admin-sync-http.yml: | + shenyu: + sync: + http: + enabled: true + + application-admin-sync-zookeeper.yml: | + shenyu: + sync: + zookeeper: + url: shenyu-zookeeper:2181 + sessionTimeoutMilliseconds: 5000 + connectionTimeoutMilliseconds: 2000 + + application-admin-sync-etcd.yml: | + shenyu: + sync: + etcd: + url: http://shenyu-etcd:2379 + + application-admin-sync-nacos.yml: | + shenyu: + sync: + nacos: + url: shenyu-nacos.default.svc.cluster.local:8848 + namespace: 1c10d748-af86-43b9-8265-75f487d20c6c + username: + password: + acm: + enabled: false + endpoint: acm.aliyun.com + namespace: + accessKey: + secretKey: + + application-bootstrap-sync-websocket.yml: | + management: + endpoint: + health: + probes: + enabled: true + group: + readiness: + include: readinessState,websocketSync + liveness: + include: livenessState + shenyu: + sync: + websocket: + # Requires an Admin supporting the initial synchronization protocol. + initial-sync-readiness: true + urls: ws://shenyu-admin:9095/websocket + token: shenyu-sync-token + allowOrigin: ws://localhost:9195 + + application-bootstrap-sync-http.yml: | + shenyu: + sync: + http: + url: http://shenyu-admin.default.svc.cluster.local:9095 + username: admin + password: 123456 + enabled: true + + application-bootstrap-sync-zookeeper.yml: | + shenyu: + sync: + zookeeper: + url: shenyu-zookeeper:2181 + sessionTimeoutMilliseconds: 5000 + connectionTimeoutMilliseconds: 2000 + + application-bootstrap-sync-etcd.yml: | + shenyu: + sync: + etcd: + url: http://shenyu-etcd:2379 + + application-bootstrap-sync-nacos.yml: | + shenyu: + sync: + nacos: + url: shenyu-nacos.default.svc.cluster.local:8848 + namespace: 1c10d748-af86-43b9-8265-75f487d20c6c + username: + password: + acm: + enabled: false + endpoint: acm.aliyun.com + namespace: + accessKey: + secretKey: diff --git a/examples/etcd/legacy/shenyu-e2e/shenyu-e2e-case/shenyu-e2e-case-spring-cloud/k8s/shenyu-cm.yml b/examples/etcd/legacy/shenyu-e2e/shenyu-e2e-case/shenyu-e2e-case-spring-cloud/k8s/shenyu-cm.yml new file mode 100644 index 0000000..d491699 --- /dev/null +++ b/examples/etcd/legacy/shenyu-e2e/shenyu-e2e-case/shenyu-e2e-case-spring-cloud/k8s/shenyu-cm.yml @@ -0,0 +1,531 @@ +# 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. + +#apiVersion: v1 +#kind: Namespace +#metadata: +# name: shenyu +# labels: +# name: shenyu +#--- + +apiVersion: v1 +kind: ConfigMap +metadata: + name: shenyu-configmap + labels: + app: shenyu-configmap +data: + shenyu-admin-application.yml: | + server: + port: 9095 + address: 0.0.0.0 + + spring: + profiles: + active: h2 + thymeleaf: + cache: true + encoding: utf-8 + enabled: true + prefix: classpath:/static/ + suffix: .html + mvc: + pathmatch: + matching-strategy: ant_path_matcher + jackson: + time-zone: GMT+8 # GMT , Asia/Shanghai + messages: + basename: message/i18n + + + management: + health: + mail: + enabled: off + endpoints: + web: + exposure: + include: + - 'health' + - 'prometheus' + enabled-by-default: true + + mybatis: + config-location: classpath:/mybatis/mybatis-config.xml + mapper-locations: classpath:/mappers/*.xml + type-handlers-package: org.apache.shenyu.admin.mybatis.handler + + shenyu: + register: + registerType: http #http #zookeeper #etcd #nacos #consul + serverLists: #localhost:2181 #http://localhost:2379 #localhost:8848 + props: + sessionTimeout: 5000 + connectionTimeout: 2000 + checked: true + zombieCheckThreads: 10 + zombieCheckTimes: 5 + scheduledTime: 10 + nacosNameSpace: ShenyuRegisterCenter + ldap: + enabled: false + url: ldap://xxxx:xxx + bind-dn: cn=xxx,dc=xxx,dc=xxx + password: xxxx + base-dn: ou=xxx,dc=xxx,dc=xxx + object-class: person + login-field: cn + jwt: + expired-seconds: 86400000 + shiro: + white-list: + - / + - /favicon.* + - /static/** + - /index** + - /platform/login + - /websocket + - /error + - /actuator/health + - /actuator/health/** # /actuator/health/readiness /actuator/health/liveness + - /actuator/prometheus + - /swagger-ui.html + - /swagger-ui/** + - /webjars/** + - /v3/api-docs/** + - /csrf + - /alert/report + dashboard: + core: + onlySuperAdminPermission: + - system:manager:add + - system:manager:edit + - system:manager:delete + - system:role:add + - system:role:edit + - system:role:delete + - system:resource:addButton + - system:resource:addMenu + - system:resource:editButton + - system:resource:editMenu + - system:resource:deleteButton + - system:resource:deleteMenu + + springdoc: + api-docs: + enabled: true + swagger-ui: + enabled: true + tags-sorter: alpha + packages-to-scan: org.apache.shenyu.admin.controller + paths-to-match: /** + + logging: + level: + root: info + org.springframework.boot: info + org.apache.ibatis: info + org.apache.shenyu.bonuspoint: info + org.apache.shenyu.lottery: info + org.apache.shenyu: info + # org.apache.shenyu.admin.utils.HttpUtils: debug + + + shenyu-admin-application-mysql.yml: | + shenyu: + database: + dialect: mysql + init_enable: true + + spring: + datasource: + url: jdbc:mysql://shenyu-mysql:3306/shenyu?useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNull + username: root + password: shenyue2e + driver-class-name: com.mysql.cj.jdbc.Driver + hikari: + connection-timeout: 30000 + minimum-idle: 5 + maximum-pool-size: 20 + auto-commit: true + idle-timeout: 600000 + max-lifetime: 1800000 + connection-test-query: SELECT 1 + connection-init-sql: SET NAMES utf8mb4 + validation-timeout: 800 + mail: + # Attention: this is mail server address. + host: smtp.qq.com + username: shenyu@apache.com + # Attention: this is not email account password, this requires an email authorization code + password: your-password + #Attention: Tencent mail smtps 465,smtp 587 + port: 587 + properties: + mail: + smtp: + socketFactoryClass: javax.net.ssl.SSLSocketFactory + ssl: + enable: true + + shenyu-bootstrap-application.yml: | + server: + port: 9195 + address: 0.0.0.0 + compression: + enabled: true + minResponseSize: 1MB # If the response data is greater than 1MB, enable compression. + + spring: + main: + allow-bean-definition-overriding: true + application: + name: shenyu-bootstrap + codec: + max-in-memory-size: 2MB + cloud: + discovery: + enabled: true + nacos: + discovery: + server-addr: 127.0.0.1:8848 # Spring Cloud Alibaba Dubbo use this. + enabled: false + namespace: ShenyuRegisterCenter + + # if you want use ribbon please config every server. + #springCloud-test: + # ribbon: + # NIWSServerListClassName: com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList + + eureka: + client: + enabled: true + serviceUrl: + defaultZone: http://shenyu-examples-eureka:8761/eureka/ + instance: + prefer-ip-address: true + + management: + health: + redis: + enabled: false + elasticsearch: + enabled: false + endpoint: + health: + enabled: true + show-details: always + endpoints: + web: + exposure: + include: "*" # or health,info + + shenyu: + selectorMatchCache: + ## selector L1 cache + cache: + enabled: false + initialCapacity: 10000 # initial capacity in cache + maximumSize: 10000 # max size in cache + ruleMatchCache: + ## rule L1 cache + cache: + enabled: true + initialCapacity: 10000 # initial capacity in cache + maximumSize: 65536 # max size in cache + netty: + http: + # set to false, user can custom the netty tcp server config. + webServerFactoryEnabled: true + selectCount: 1 + workerCount: 8 + accessLog: false + serverSocketChannel: + soRcvBuf: 87380 + soBackLog: 128 + soReuseAddr: false + connectTimeoutMillis: 10000 + writeBufferHighWaterMark: 65536 + writeBufferLowWaterMark: 32768 + writeSpinCount: 16 + autoRead: false + allocType: "pooled" + messageSizeEstimator: 8 + singleEventExecutorPerGroup: true + socketChannel: + soKeepAlive: false + soReuseAddr: false + soLinger: -1 + tcpNoDelay: true + soRcvBuf: 87380 + soSndBuf: 16384 + ipTos: 0 + allowHalfClosure: false + connectTimeoutMillis: 10000 + writeBufferHighWaterMark: 65536 + writeBufferLowWaterMark: 32768 + writeSpinCount: 16 + autoRead: false + allocType: "pooled" + messageSizeEstimator: 8 + singleEventExecutorPerGroup: true + sni: + enabled: false + mod: k8s #manul + defaultK8sSecretNamespace: shenyu-ingress + defaultK8sSecretName: default-cert + # mod: manual + # certificates: + # - domain: 'localhost' + # keyCertChainFile: '/Users/zhukunshuai/Desktop/cert/example.com+1.pem' + # keyFile: '/Users/zhukunshuai/Desktop/cert/example.com+1-key.pem' + # - domain: 'example.com' + # keyCertChainFile: '/Users/zhukunshuai/Desktop/cert/example.com+1.pem' + # keyFile: '/Users/zhukunshuai/Desktop/cert/example.com+1-key.pem' + # httpclient: + # strategy: webClient # netty + # connectTimeout: 45000 + # responseTimeout: 3000 + # readerIdleTime: 3000 + # writerIdleTime: 3000 + # allIdleTime: 3000 + # readTimeout: 3000 + # writeTimeout: 3000 + # wiretap: false + # keepAlive: false + # maxInMemorySize: 1 #1mb + # pool: + # type: ELASTIC + # name: proxy + # maxConnections: 16 + # acquireTimeout: 45000 + # maxIdleTime: 3000 + # proxy: + # host: + # port: + # username: + # password: + # nonProxyHostsPattern: + # ssl: + # useInsecureTrustManager: true + # keyStoreType: PKCS12 + # keyStorePath: classpath:keystore.p12 + # keyStorePassword: 123456 + # keyStoreProvider: + # keyPassword: 123456 + # trustedX509Certificates: + # handshakeTimeout: + # closeNotifyFlushTimeout: + # closeNotifyReadTimeout: + # defaultConfigurationType: + # threadPool: + # prefix: shenyu + # selectCount: 1 + # workerCount: 8 + # daemon: true + register: + enabled: false + registerType: zookeeper #etcd #consul + serverLists: localhost:2181 #http://localhost:2379 #localhost:8848 + props: + cross: + enabled: true + allowedHeaders: + allowedMethods: "*" + allowedAnyOrigin: true # the same of Access-Control-Allow-Origin: "*" + # allowedOrigin: + # format : schema://prefix spacer domain + # Access-Control-Allow-Origin: "http://a.apache.org,http://b.apache.org" + # spacer: "." + # domain: apache.org + # prefixes: + # - a # a.apache.org + # - b # b.apache.org + # origins: + # - c.apache.org + # - d.apache.org + # - http://e.apache.org + # originRegex: ^http(|s)://(.*\.|)abc.com$ + allowedExpose: "" + maxAge: "18000" + allowCredentials: true + + switchConfig: + local: true + collapseSlashes: false + file: + enabled: true + maxSize: 10 + exclude: + enabled: false + paths: + - /favicon.ico + fallback: + enabled: false + paths: + - /fallback/resilience4j + - /fallback/sentinel + health: + enabled: true + paths: + - /actuator + - /health_check + alert: + enabled: true + reportUrl: http://localhost:9095/alert/report + extPlugin: + path: + enabled: true + threads: 1 + scheduleTime: 300 + scheduleDelay: 30 + scheduler: + enabled: false + type: fixed + threads: 16 + upstreamCheck: + enabled: false + poolSize: 10 + timeout: 3000 + healthyThreshold: 1 + unhealthyThreshold: 1 + interval: 5000 + printEnabled: true + printInterval: 60000 + springCloudCache: + enabled: false + ribbon: + serverListRefreshInterval: 10000 + metrics: + enabled: false + name: prometheus + host: 127.0.0.1 + port: 8090 + jmxConfig: + props: + jvm_enabled: true + # plugins: + # rate-limiter.enabled: false + local: + enabled: false + sha512Key: "BA3253876AED6BC22D4A6FF53D8406C6AD864195ED144AB5C87621B6C233B548BAEAE6956DF346EC8C17F5EA10F35EE3CBC514797ED7DDD3145464E2A0BAB413" + websocket: + enableProxyPing: false + + logging: + level: + root: info + org.springframework.boot: info + org.apache.ibatis: info + org.apache.shenyu.bonuspoint: info + org.apache.shenyu.lottery: info + org.apache.shenyu: info + org.springframework.http.server.reactive: info + org.springframework.web.reactive: info + reactor.ipc.netty: info + reactor.netty: info + com.alibaba: debug + + application-admin-sync-websocket.yml: | + shenyu: + sync: + websocket: + enabled: true + messageMaxSize: 10240 + allowOrigins: ws://localhost:9095;ws://localhost:9195; + token: shenyu-sync-token + + application-admin-sync-http.yml: | + shenyu: + sync: + http: + enabled: true + + application-admin-sync-zookeeper.yml: | + shenyu: + sync: + zookeeper: + url: shenyu-zookeeper:2181 + sessionTimeoutMilliseconds: 5000 + connectionTimeoutMilliseconds: 2000 + + application-admin-sync-etcd.yml: | + shenyu: + sync: + etcd: + url: http://shenyu-etcd:2379 + + application-admin-sync-nacos.yml: | + shenyu: + sync: + nacos: + url: shenyu-nacos.default.svc.cluster.local:8848 + namespace: 1c10d748-af86-43b9-8265-75f487d20c6c + username: + password: + acm: + enabled: false + endpoint: acm.aliyun.com + namespace: + accessKey: + secretKey: + + application-bootstrap-sync-websocket.yml: | + shenyu: + sync: + websocket: + urls: ws://shenyu-admin:9095/websocket + token: shenyu-sync-token + allowOrigin: ws://localhost:9195 + + application-bootstrap-sync-http.yml: | + shenyu: + sync: + http: + url: http://shenyu-admin:9095 + username: admin + password: 123456 + enabled: true + + application-bootstrap-sync-zookeeper.yml: | + shenyu: + sync: + zookeeper: + url: shenyu-zookeeper:2181 + sessionTimeoutMilliseconds: 5000 + connectionTimeoutMilliseconds: 2000 + + application-bootstrap-sync-etcd.yml: | + shenyu: + sync: + etcd: + url: http://shenyu-etcd:2379 + + application-bootstrap-sync-nacos.yml: | + shenyu: + sync: + nacos: + url: shenyu-nacos.default.svc.cluster.local:8848 + namespace: 1c10d748-af86-43b9-8265-75f487d20c6c + username: + password: + acm: + enabled: false + endpoint: acm.aliyun.com + namespace: + accessKey: + secretKey: diff --git a/examples/etcd/legacy/shenyu-examples/shenyu-examples-springcloud/README.md b/examples/etcd/legacy/shenyu-examples/shenyu-examples-springcloud/README.md new file mode 100644 index 0000000..f83b1a2 --- /dev/null +++ b/examples/etcd/legacy/shenyu-examples/shenyu-examples-springcloud/README.md @@ -0,0 +1,41 @@ +# Shenyu example SpringCloud +*** +## Run +first you should run eureka: shenyu-example-eureka + +Then you can run server with http/zookeeper/etcd register center + +### run with etcd register center +#### Shenyu-Admin config +add etcd register center dependency in shenyu-admin: + +```xml + + org.apache.shenyu + shenyu-register-server-etcd + ${project.version} + +``` + +set etcd register config in shenyu-admin: + +```yaml +shenyu: + register: + registerType: etcd + serverLists : http://localhost:2379 +``` + +#### shenyu-examples-springcloud config +set etcd register config: + +```yaml +shenyu: + client: + registerType: etcd + serverLists: http://localhost:2379 + props: + contextPath: /springcloud +``` + +run ShenyuTestSpringCloudApplication diff --git a/script/plugin-store/build-etcd-runtime.sh b/script/plugin-store/build-etcd-runtime.sh new file mode 100644 index 0000000..b69884e --- /dev/null +++ b/script/plugin-store/build-etcd-runtime.sh @@ -0,0 +1,74 @@ +#!/usr/bin/env bash +# +# 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. + +set -euo pipefail + +REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +CORE_SOURCE="${SHENYU_CORE_SOURCE:?Set SHENYU_CORE_SOURCE to the compatible core checkout}" +CORE_VERSION="${SHENYU_CORE_VERSION:-2.7.2-SNAPSHOT}" +EXTENSION="${REPO_DIR}/shenyu-extension/shenyu-extension-etcd" +mkdir -p "${REPO_DIR}/target" +RUNTIME_DIR="$(mktemp -d "${REPO_DIR}/target/etcd-runtime.XXXXXX")" + +"${CORE_SOURCE}/mvnw" -B -ntp -f "${CORE_SOURCE}/pom.xml" -Prelease -pl shenyu-dist/shenyu-admin-dist,shenyu-dist/shenyu-bootstrap-dist -am package -DskipTests -Dmaven.javadoc.skip=true -Djacoco.skip=true -DskipRemoteResources=true +"${CORE_SOURCE}/mvnw" -B -ntp -f "${CORE_SOURCE}/pom.xml" \ + -pl shenyu-spring-boot-starter/shenyu-spring-boot-starter-registry -am install \ + -DskipTests -Dmaven.javadoc.skip=true -Djacoco.skip=true -DskipRemoteResources=true +mvn -B -ntp -f "${REPO_DIR}/pom.xml" -pl shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd -am install -DskipTests + +for role in admin bootstrap; do + mkdir -p "${RUNTIME_DIR}/${role}/app" "${RUNTIME_DIR}/${role}/external" + tar -xzf "${CORE_SOURCE}/shenyu-dist/shenyu-${role}-dist/target/apache-shenyu-${CORE_VERSION}-${role}-bin.tar.gz" -C "${RUNTIME_DIR}/${role}/app" --strip-components=1 + # Remove old ownership even when building from the pre-cutover pinned core. + find "${RUNTIME_DIR}/${role}/app/lib" -maxdepth 1 -type f \( -name 'shenyu-*-etcd-*.jar' -o -name 'jetcd-*.jar' \) -delete + if [[ "${role}" == "admin" ]]; then + MODULES=(shenyu-admin-listener-etcd shenyu-registry-etcd) + else + MODULES=(shenyu-spring-boot-starter-sync-data-etcd shenyu-registry-etcd) + fi + for module in "${MODULES[@]}"; do + mvn -B -ntp -f "${EXTENSION}/${module}/pom.xml" dependency:copy-dependencies -DincludeScope=runtime -DoutputDirectory="${RUNTIME_DIR}/${role}/external" + cp "${EXTENSION}/${module}/target/${module}-2.7.1-SNAPSHOT.jar" "${RUNTIME_DIR}/${role}/external/" + done + if [[ "${role}" == "bootstrap" ]]; then + mvn -B -ntp -f "${REPO_DIR}/examples/etcd/gateway/pom.xml" dependency:copy-dependencies \ + -DincludeScope=runtime -DoutputDirectory="${RUNTIME_DIR}/${role}/external" + fi + cp "${RUNTIME_DIR}/${role}/external/"*.jar "${RUNTIME_DIR}/${role}/app/lib/" + cp "${EXTENSION}/NOTICE" "${RUNTIME_DIR}/${role}/app/ETCD-NOTICE" + cp "${EXTENSION}/LICENSE" "${RUNTIME_DIR}/${role}/app/ETCD-LICENSE" + # Prove the runtime implementation is the store build, including the shared client. + for artifact in shenyu-infra-etcd shenyu-registry-etcd; do + cmp "${EXTENSION}/${artifact}/target/${artifact}-2.7.1-SNAPSHOT.jar" \ + "${RUNTIME_DIR}/${role}/app/lib/${artifact}-2.7.1-SNAPSHOT.jar" + done + if find "${RUNTIME_DIR}/${role}/app/lib" -maxdepth 1 -type f -name 'shenyu-*-etcd-2.7.2-SNAPSHOT.jar' | grep -q .; then + echo "Duplicate in-tree etcd ownership remains in ${role}" >&2 + exit 1 + fi + cp "${CORE_SOURCE}/shenyu-dist/shenyu-${role}-dist/docker/entrypoint.sh" "${RUNTIME_DIR}/${role}/entrypoint.sh" + chmod +x "${RUNTIME_DIR}/${role}/entrypoint.sh" + cat > "${RUNTIME_DIR}/${role}/Dockerfile" <= 400: + return + time.sleep(0.5) + raise AssertionError(f"route {path}: status={status}, body={body!r}, expected matched={expected}") + + +def prepare(): + token = API.login() + API.enable_namespace_plugin(token, "5", "divide") + selector = API.create_selector(token) + rule = API.create_rule(token, selector) + API.bind_discovery_upstream(token, selector) + STATE.write_text(json.dumps({"selector": selector, "rule": rule}), encoding="utf-8") + + +def verify(): + state = json.loads(STATE.read_text(encoding="utf-8")) + token = API.login() + # Data exists before the gateway starts: this is initial snapshot coverage. + await_route("/http/initial", True) + updated = { + "id": state["rule"], "name": "plugin-store-http-route", "selectorId": state["selector"], + "matchMode": 0, "enabled": True, "loged": False, "matchRestful": False, "sort": 1, + "namespaceId": API.NAMESPACE, "handle": json.dumps({"loadBalance": "hash", "retry": 0, "timeout": 3000}), + "ruleConditions": [{"paramType": "uri", "operator": "=", "paramName": "/", "paramValue": "/http/updated"}], + } + API.admin_result("PUT", "/rule/" + state["rule"], updated, token) + await_route("/http/updated", True) + await_route("/http/initial", False) + API.admin_result("DELETE", "/rule/batch", {"ids": [state["rule"]], "namespaceId": API.NAMESPACE}, token) + await_route("/http/updated", False) + API.admin_result("DELETE", "/selector/batch", {"ids": [state["selector"]], "namespaceId": API.NAMESPACE}, token) + + +if __name__ == "__main__": + {"prepare": prepare, "verify": verify}[sys.argv[1]]() diff --git a/shenyu-extension/pom.xml b/shenyu-extension/pom.xml index 97c23fc..2fffe3f 100644 --- a/shenyu-extension/pom.xml +++ b/shenyu-extension/pom.xml @@ -27,5 +27,6 @@ shenyu-extension-consul shenyu-extension-polaris + shenyu-extension-etcd diff --git a/shenyu-extension/shenyu-extension-etcd/LICENSE b/shenyu-extension/shenyu-extension-etcd/LICENSE new file mode 100644 index 0000000..d565b83 --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/LICENSE @@ -0,0 +1,229 @@ + 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 + + +Third-party etcd client components (Apache License, Version 2.0): + jetcd-api 0.7.3: https://github.com/etcd-io/jetcd + jetcd-common 0.7.3: https://github.com/etcd-io/jetcd + jetcd-core 0.7.3: https://github.com/etcd-io/jetcd + jetcd-grpc 0.7.3: https://github.com/etcd-io/jetcd + +The disposable etcd 3.5.21 server is a test environment, not included in the +published extension JARs. Custom distributions must inventory all resolved +runtime dependencies and preserve their accompanying notices. diff --git a/shenyu-extension/shenyu-extension-etcd/NOTICE b/shenyu-extension/shenyu-extension-etcd/NOTICE new file mode 100644 index 0000000..267eeb6 --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/NOTICE @@ -0,0 +1,13 @@ +Apache ShenYu etcd Extension +Copyright 2026 The Apache Software Foundation + +This product includes software developed at +The Apache Software Foundation (http://www.apache.org/). + +jetcd Notice + +CoreOS Project +Copyright 2018 CoreOS, Inc + +This product includes software developed at CoreOS, Inc. +(http://www.coreos.com/). diff --git a/shenyu-extension/shenyu-extension-etcd/README.md b/shenyu-extension/shenyu-extension-etcd/README.md new file mode 100644 index 0000000..da5aa71 --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/README.md @@ -0,0 +1,163 @@ + +# ShenYu etcd extension + +The entire etcd stack is transferred as one unit to `apache/shenyu-plugin-store`: +`shenyu-infra-etcd`, `shenyu-admin-listener-etcd`, `shenyu-sync-data-etcd`, +`shenyu-registry-etcd`, and `shenyu-spring-boot-starter-sync-data-etcd`. +Infrastructure is etcd-specific; no retained core module imports it. + +## Release gates + +The main removal PR must remain draft until the community agrees to externalization, +the receiving build and real-server/full-deployment tests pass, and the external +artifacts are published. This source change does not mean artifacts are released. +The store currently uses `2.7.1-SNAPSHOT` and compiles against ShenYu `2.7.2-SNAPSHOT`; +select the announced compatible store release when deploying. Do not publish the +same group/artifact/version from both repositories. Retain the last in-tree release +and its configuration during the rollback window; the release announcement must +specify that window before the cutover is merged. + +## Dependency selection + +Admin needs `org.apache.shenyu:shenyu-admin-listener-etcd`; gateway needs +`org.apache.shenyu:shenyu-spring-boot-starter-sync-data-etcd`. Applications using etcd +instance registration, SDK discovery, or Admin etcd discovery additionally need +`org.apache.shenyu:shenyu-registry-etcd`. Use the **store** version for all five +external modules and the **core** version for retained ShenYu APIs. +Gateway registration also requires the retained core +`shenyu-spring-boot-starter-registry`; exclude its bundled providers and add the +external `shenyu-registry-etcd`. The receiving gateway example POM shows this selection. +The infrastructure and jetcd dependencies are transitive. For binary distributions, +resolve the complete runtime dependency closure into `lib/`, remove superseded etcd +JARs, and include the extension's LICENSE/NOTICE information in the custom package. +Adding only the starter JAR without its dependencies is insufficient. + +The `etcd` SPI key is unchanged. The JAR still registers +`etcd=org.apache.shenyu.registry.etcd.EtcdInstanceRegisterRepository` in +`META-INF/shenyu/org.apache.shenyu.registry.api.ShenyuInstanceRegisterRepository`. +Admin's existing `ShenyuInstanceRegisterRepositoryFactory.loadRepository` loads the +external provider without a hard-coded class or dependency in core. + +## Configuration and storage contract + +Admin and gateway retain `shenyu.sync.etcd.url`, with comma-separated endpoint URLs: + +```yaml +shenyu: + sync: + etcd: + url: http://etcd-1:2379,http://etcd-2:2379 + sessionTimeout: 30000 + connectionTimeout: 30000 +``` + +Disable the previous sync channel when selecting etcd. Admin uses +`shenyu.sync.websocket.enabled=false`; gateway must remove/disable its WebSocket +`urls` setting. Preserve the existing timeout values: the historical binding maps +`sessionTimeout` to the client's operation timeout (milliseconds) and +`connectionTimeout` to its lease TTL (seconds). This migration does not reinterpret +those values; revisit the naming/units in a separate compatible change. + +Gateway instance registration keeps: + +```yaml +shenyu: + register: + enabled: true + registerType: etcd + serverLists: http://etcd-1:2379,http://etcd-2:2379 + props: + etcdTimeout: "3000" + etcdTTL: "5" +``` + +Configuration keys retain their leading slash and namespace: + +| Data | Key | +| --- | --- | +| Plugin | `//shenyu/plugin/` | +| Selector | `//shenyu/selector//` | +| Rule | `//shenyu/rule//-` | +| Authentication | `//shenyu/auth/` | +| Metadata | `//shenyu/metaData/` | +| Proxy selector | `//shenyu/proxySelectorData//` | +| Discovery upstream | `//shenyu/discoveryUpstream//` | +| Instance | `/shenyu/register/instance//:` | + +JSON DTOs, enum values, discovery mode, namespace IDs and existing keyspaces stay +compatible. Instance keys remain lease-backed; after client shutdown they disappear +when their lease expires. No schema conversion or data deletion is required. + +## Compatibility, licensing and verification + +The transferred client remains `io.etcd:jetcd-core:0.7.3`, using the core-managed +gRPC `1.65.1`. The real-server fixture targets etcd `3.5.21`; other etcd/client +versions need validation before claiming support. Server and client are Apache-2.0: +see [etcd LICENSE](https://github.com/etcd-io/etcd/blob/v3.5.21/LICENSE) and +[jetcd LICENSE](https://github.com/etcd-io/jetcd/blob/jetcd-0.7.3/LICENSE). +The receiving extension carries the former distribution's CoreOS notice. +Historical release notes and upgrade documentation remain in main. + +The store README documents unit, starter, real-server and full Admin/gateway E2E +commands. Real-server tests cover initial snapshots, namespace isolation, route +create/update/delete, server restart/watch recovery, external SPI registration, +instance update/delete/lease expiry and client shutdown. CI uses Testcontainers; +a native etcd binary is an explicit local alternative. Full deployment validation +uses the real Admin API and gateway HTTP traffic with externally supplied JARs. + +## Upgrade and rollback + +1. Back up Admin's database and take an etcd snapshot. Record namespace, endpoints, + timeouts, TTL, core/store versions and the deployed JAR inventory. +2. Verify compatible external artifacts and all release gates in a staging deployment. + Add the selected artifacts and their runtime dependencies before changing images. +3. Roll Admin and gateways one at a time, preserving their namespace/endpoints and + registry settings. Check initial synchronization, route updates and instance discovery. +4. Keep the previous in-tree images and configuration available for the announced window. + To roll back, restore those images/configuration and remove the external etcd JARs. + Reuse the same etcd keyspace and Admin database; do not clear either as part of rollback. + +The archived compose/Kubernetes assets in `examples/etcd/legacy` belong to the last +in-tree deployment. They require the original core images; the receiving project's +new external-artifact E2E launcher is the cutover validation path. + +## Build and test + +Prepare current core APIs (including `shenyu-infra-common` and `shenyu-registry-core`) +from the pinned main commit `5aa9517a8fb513c3afaa3606cfe8bfefb32fc5ad` using its Maven wrapper. +The receiving reactor builds all five children together: + +```bash +mvn -B -ntp -pl shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd -am clean verify +``` + +The starter's test dependencies include the listener and registry, so `-am` brings +in the whole stack. Docker is required by `EtcdRealServerTest`; it is not silently +skipped when unavailable. A developer may instead pass +`-Detcd.test.binary=/absolute/path/to/etcd` to start disposable native processes. +Use the same server version and verify the official download checksum. + +Full deployment E2E requires locally built admin/gateway images: + +```bash +SHENYU_CORE_SOURCE=/path/to/shenyu bash script/plugin-store/build-etcd-runtime.sh +mvn -B -ntp -pl shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd -am verify -Petcd-e2e +``` + +The initial receiving PR and paired main PR remain draft until container/full +application verification and the release gates above are complete. diff --git a/shenyu-extension/shenyu-extension-etcd/pom.xml b/shenyu-extension/shenyu-extension-etcd/pom.xml new file mode 100644 index 0000000..1ce97cc --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/pom.xml @@ -0,0 +1,52 @@ + + + + + org.apache.shenyu + shenyu-extension + 2.7.1-SNAPSHOT + + 4.0.0 + pom + shenyu-extension-etcd + + + 0.7.3 + 1.65.1 + + + + + + io.etcd + jetcd-core + ${jetcd-core.version} + + + + + + shenyu-infra-etcd + shenyu-admin-listener-etcd + shenyu-sync-data-etcd + shenyu-registry-etcd + shenyu-spring-boot-starter-sync-data-etcd + + diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/pom.xml b/shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/pom.xml new file mode 100644 index 0000000..b8e07a3 --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/pom.xml @@ -0,0 +1,55 @@ + + + + + 4.0.0 + + org.apache.shenyu + shenyu-extension-etcd + 2.7.1-SNAPSHOT + + + shenyu-admin-listener-etcd + + + + + org.apache.shenyu + shenyu-infra-etcd + ${project.version} + + + + org.apache.shenyu + shenyu-admin-listener-api + ${shenyu.version} + + + + org.springframework.boot + spring-boot-starter-web + provided + + + org.springframework.boot + spring-boot-test + test + + + + diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/main/java/org/apache/shenyu/admin/config/EtcdSyncConfiguration.java b/shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/main/java/org/apache/shenyu/admin/config/EtcdSyncConfiguration.java new file mode 100644 index 0000000..d01a6ae --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/main/java/org/apache/shenyu/admin/config/EtcdSyncConfiguration.java @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shenyu.admin.config; + +import org.apache.shenyu.admin.listener.DataChangedInit; +import org.apache.shenyu.admin.listener.DataChangedListener; +import org.apache.shenyu.admin.listener.etcd.EtcdDataChangedInit; +import org.apache.shenyu.admin.listener.etcd.EtcdDataDataChangedListener; +import org.apache.shenyu.infra.etcd.autoconfig.ConditionOnSyncEtcd; +import org.apache.shenyu.infra.etcd.autoconfig.EtcdConfiguration; +import org.apache.shenyu.infra.etcd.client.EtcdClient; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * The type Etcd listener. + */ + +@Configuration +@ConditionOnSyncEtcd +@AutoConfiguration(after = { EtcdConfiguration.class }) +@ImportAutoConfiguration(EtcdConfiguration.class) +public class EtcdSyncConfiguration { + + /** + * Config event listener data changed listener. + * + * @param etcdClient the etcd client + * @return the data changed listener + */ + @Bean + @ConditionalOnMissingBean(EtcdDataDataChangedListener.class) + public DataChangedListener etcdDataChangedListener(final EtcdClient etcdClient) { + + return new EtcdDataDataChangedListener(etcdClient); + } + + /** + * data init. + * + * @param etcdClient the etcd client + * @return the etcd data init + */ + @Bean + @ConditionalOnMissingBean(EtcdDataChangedInit.class) + public DataChangedInit etcdDataChangedInit(final EtcdClient etcdClient) { + + return new EtcdDataChangedInit(etcdClient); + } +} diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/main/java/org/apache/shenyu/admin/listener/etcd/EtcdDataChangedInit.java b/shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/main/java/org/apache/shenyu/admin/listener/etcd/EtcdDataChangedInit.java new file mode 100644 index 0000000..8a11e8d --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/main/java/org/apache/shenyu/admin/listener/etcd/EtcdDataChangedInit.java @@ -0,0 +1,52 @@ +/* + * 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.etcd; + +import org.apache.shenyu.admin.listener.AbstractDataChangedInit; +import org.apache.shenyu.common.constant.DefaultPathConstants; +import org.apache.shenyu.infra.etcd.client.EtcdClient; + +import java.util.stream.Stream; + +/** + * The type Etcd data changed init. + * + * @since 2.5.0 + */ +public class EtcdDataChangedInit extends AbstractDataChangedInit { + + private final EtcdClient etcdClient; + + /** + * Instantiates a new Etcd data changed init. + * + * @param etcdClient the etcdClient client + */ + public EtcdDataChangedInit(final EtcdClient etcdClient) { + this.etcdClient = etcdClient; + } + + @Override + protected boolean notExist() { + return Stream.of(DefaultPathConstants.PLUGIN_PARENT, + DefaultPathConstants.APP_AUTH_PARENT, + DefaultPathConstants.META_DATA, + DefaultPathConstants.PROXY_SELECTOR, + DefaultPathConstants.DISCOVERY_UPSTREAM).noneMatch(etcdClient::exists); + } +} diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/main/java/org/apache/shenyu/admin/listener/etcd/EtcdDataDataChangedListener.java b/shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/main/java/org/apache/shenyu/admin/listener/etcd/EtcdDataDataChangedListener.java new file mode 100644 index 0000000..8c5c78c --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/main/java/org/apache/shenyu/admin/listener/etcd/EtcdDataDataChangedListener.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shenyu.admin.listener.etcd; + +import org.apache.shenyu.admin.listener.AbstractPathDataChangedListener; +import org.apache.shenyu.common.utils.GsonUtils; +import org.apache.shenyu.infra.etcd.client.EtcdClient; + +/** + * EtcdDataDataChangedListener. + */ +public class EtcdDataDataChangedListener extends AbstractPathDataChangedListener { + + private final EtcdClient etcdClient; + + public EtcdDataDataChangedListener(final EtcdClient client) { + this.etcdClient = client; + } + + @Override + public void createOrUpdate(final String pluginPath, final Object data) { + etcdClient.put(pluginPath, GsonUtils.getInstance().toJson(data)); + } + + @Override + public void deleteNode(final String pluginPath) { + etcdClient.delete(pluginPath); + } + + @Override + public void deletePathRecursive(final String selectorParentPath) { + etcdClient.deleteEtcdPathRecursive(selectorParentPath); + } +} diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/main/resources/META-INF/spring.factories b/shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..8b477f3 --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/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.EtcdSyncConfiguration diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/main/resources/META-INF/spring.provides b/shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/main/resources/META-INF/spring.provides new file mode 100644 index 0000000..55e4b72 --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/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-nacos diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 0000000..927e1ac --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/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.EtcdSyncConfiguration diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/test/java/org/apache/shenyu/admin/config/EtcdSyncConfigurationTest.java b/shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/test/java/org/apache/shenyu/admin/config/EtcdSyncConfigurationTest.java new file mode 100644 index 0000000..ff680c5 --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/test/java/org/apache/shenyu/admin/config/EtcdSyncConfigurationTest.java @@ -0,0 +1,62 @@ +/* + * 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.listener.DataChangedInit; +import org.apache.shenyu.admin.listener.DataChangedListener; +import org.apache.shenyu.admin.service.SyncDataService; +import org.apache.shenyu.infra.etcd.client.EtcdClient; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.Mockito.mock; + +public class EtcdSyncConfigurationTest { + + @Test + public void testEtcdDataChangedListener() { + EtcdSyncConfiguration etcdListener = new EtcdSyncConfiguration(); + EtcdClient client = mock(EtcdClient.class); + assertNotNull(etcdListener.etcdDataChangedListener(client)); + } + + @Test + public void testEtcdDataInit() { + EtcdSyncConfiguration etcdListener = new EtcdSyncConfiguration(); + EtcdClient client = mock(EtcdClient.class); + assertNotNull(etcdListener.etcdDataChangedInit(client)); + } + + @Test + public void createsAdminBeansWithExternalEtcdConfiguration() { + new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(EtcdSyncConfiguration.class)) + .withBean(EtcdClient.class, () -> mock(EtcdClient.class)) + .withBean(SyncDataService.class, () -> mock(SyncDataService.class)) + .withPropertyValues("shenyu.sync.etcd.url=http://127.0.0.1:2379") + .run(context -> assertThat(context).hasNotFailed().hasSingleBean(DataChangedListener.class).hasSingleBean(DataChangedInit.class)); + } + + @Test + public void backsOffWithoutEtcdConfiguration() { + new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(EtcdSyncConfiguration.class)) + .run(context -> assertThat(context).hasNotFailed().doesNotHaveBean(DataChangedListener.class).doesNotHaveBean(DataChangedInit.class)); + } +} diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/test/java/org/apache/shenyu/admin/listener/etcd/EtcdDataChangedInitTest.java b/shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/test/java/org/apache/shenyu/admin/listener/etcd/EtcdDataChangedInitTest.java new file mode 100644 index 0000000..d017112 --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/test/java/org/apache/shenyu/admin/listener/etcd/EtcdDataChangedInitTest.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.etcd; + +import org.apache.shenyu.common.constant.DefaultPathConstants; +import org.apache.shenyu.infra.etcd.client.EtcdClient; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.when; + +/** + * Test cases for {@link EtcdDataChangedInit}. + */ +@ExtendWith(MockitoExtension.class) +public class EtcdDataChangedInitTest { + + @Mock + private EtcdClient etcdClient; + + @Test + public void testNotExist() throws Exception { + EtcdDataChangedInit etcdDataChangedInit = new EtcdDataChangedInit(etcdClient); + assertNotNull(etcdDataChangedInit); + + when(etcdClient.exists(DefaultPathConstants.PLUGIN_PARENT)).thenReturn(true); + boolean pluginExist = etcdDataChangedInit.notExist(); + assertFalse(pluginExist, "plugin exist."); + when(etcdClient.exists(DefaultPathConstants.PLUGIN_PARENT)).thenReturn(false); + + when(etcdClient.exists(DefaultPathConstants.APP_AUTH_PARENT)).thenReturn(true); + boolean appAuthExist = etcdDataChangedInit.notExist(); + assertFalse(appAuthExist, "app auth exist."); + when(etcdClient.exists(DefaultPathConstants.APP_AUTH_PARENT)).thenReturn(false); + + when(etcdClient.exists(DefaultPathConstants.META_DATA)).thenReturn(true); + boolean metaDataExist = etcdDataChangedInit.notExist(); + assertFalse(metaDataExist, "metadata exist."); + when(etcdClient.exists(DefaultPathConstants.META_DATA)).thenReturn(false); + boolean metaDataNotExist = etcdDataChangedInit.notExist(); + assertTrue(metaDataNotExist, "metadata not exist."); + } +} diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/test/java/org/apache/shenyu/admin/listener/etcd/EtcdDataDataChangedListenerTest.java b/shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/test/java/org/apache/shenyu/admin/listener/etcd/EtcdDataDataChangedListenerTest.java new file mode 100644 index 0000000..f8e0c24 --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-admin-listener-etcd/src/test/java/org/apache/shenyu/admin/listener/etcd/EtcdDataDataChangedListenerTest.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.admin.listener.etcd; + +import com.google.common.collect.ImmutableList; +import org.apache.shenyu.common.dto.AppAuthData; +import org.apache.shenyu.common.dto.MetaData; +import org.apache.shenyu.common.dto.PluginData; +import org.apache.shenyu.common.dto.RuleData; +import org.apache.shenyu.common.dto.SelectorData; +import org.apache.shenyu.common.enums.DataEventTypeEnum; +import org.apache.shenyu.infra.etcd.client.EtcdClient; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +/** + * The testCase for {@link EtcdDataDataChangedListener}. + */ +@ExtendWith(MockitoExtension.class) +public class EtcdDataDataChangedListenerTest { + + private static final String MOCK_APP_KEY = "MOCK_APP_KEY"; + + private static final String MOCK_APP_SECRET = "MOCK_APP_SECRET"; + + private static final String MOCK_ID = "MOCK_ID"; + + private static final String MOCK_PATH = "MOCK_PATH"; + + private static final String MOCK_APP_NAME = "MOCK_APP_NAME"; + + private static final String MOCK_NAME = "MOCK_NAME"; + + private static final String MOCK_CONFIG = "MOCK_CONFIG"; + + private static final String MOCK_PLUGIN_NAME = "MOCK_PLUGIN_NAME"; + + private static final String MOCK_SELECTOR_ID = "MOCK_SELECTOR_ID"; + + @InjectMocks + private EtcdDataDataChangedListener etcdDataDataChangedListener; + + @Mock + private EtcdClient etcdClient; + + /** + * test case onAppAuthChanged event. + */ + @Test + public void testOnAppAuthChanged() { + AppAuthData appAuthData = AppAuthData.builder().appKey(MOCK_APP_KEY).appSecret(MOCK_APP_SECRET).build(); + etcdDataDataChangedListener.onAppAuthChanged(ImmutableList.of(appAuthData), DataEventTypeEnum.CREATE); + etcdDataDataChangedListener.onAppAuthChanged(ImmutableList.of(appAuthData), DataEventTypeEnum.UPDATE); + etcdDataDataChangedListener.onAppAuthChanged(ImmutableList.of(appAuthData), DataEventTypeEnum.DELETE); + verify(etcdClient, times(2)).put(any(String.class), any(String.class)); + verify(etcdClient, times(1)).delete(any(String.class)); + } + + /** + * test case onMetaDataChanged event. + */ + @Test + public void testOnMetaDataChanged() { + MetaData metaData = MetaData.builder().id(MOCK_ID).path(MOCK_PATH).appName(MOCK_APP_NAME).build(); + etcdDataDataChangedListener.onMetaDataChanged(ImmutableList.of(metaData), DataEventTypeEnum.CREATE); + etcdDataDataChangedListener.onMetaDataChanged(ImmutableList.of(metaData), DataEventTypeEnum.UPDATE); + etcdDataDataChangedListener.onMetaDataChanged(ImmutableList.of(metaData), DataEventTypeEnum.DELETE); + verify(etcdClient, times(2)).put(any(String.class), any(String.class)); + verify(etcdClient, times(1)).delete(any(String.class)); + } + + /** + * test case onPluginChanged event. + */ + @Test + public void testOnPluginChanged() { + PluginData pluginData = PluginData.builder().id(MOCK_ID).name(MOCK_NAME).config(MOCK_CONFIG).build(); + etcdDataDataChangedListener.onPluginChanged(ImmutableList.of(pluginData), DataEventTypeEnum.CREATE); + etcdDataDataChangedListener.onPluginChanged(ImmutableList.of(pluginData), DataEventTypeEnum.UPDATE); + etcdDataDataChangedListener.onPluginChanged(ImmutableList.of(pluginData), DataEventTypeEnum.DELETE); + verify(etcdClient, times(2)).put(any(String.class), any(String.class)); + verify(etcdClient, times(3)).deleteEtcdPathRecursive(any(String.class)); + } + + /** + * test case onSelectorChanged event. + */ + @Test + public void testOnSelectorChanged() { + SelectorData selectorData = SelectorData.builder().id(MOCK_ID).name(MOCK_NAME).pluginName(MOCK_PLUGIN_NAME).build(); + etcdDataDataChangedListener.onSelectorChanged(ImmutableList.of(selectorData), DataEventTypeEnum.CREATE); + etcdDataDataChangedListener.onSelectorChanged(ImmutableList.of(selectorData), DataEventTypeEnum.UPDATE); + etcdDataDataChangedListener.onSelectorChanged(ImmutableList.of(selectorData), DataEventTypeEnum.REFRESH); + etcdDataDataChangedListener.onSelectorChanged(ImmutableList.of(selectorData), DataEventTypeEnum.DELETE); + verify(etcdClient, times(3)).put(any(String.class), any(String.class)); + verify(etcdClient, times(1)).delete(any(String.class)); + verify(etcdClient, times(1)).deleteEtcdPathRecursive(any(String.class)); + } + + /** + * test case onRuleChanged event. + */ + @Test + public void testOnRuleChanged() { + RuleData ruleData = RuleData.builder() + .id(MOCK_ID) + .name(MOCK_NAME) + .pluginName(MOCK_PLUGIN_NAME) + .selectorId(MOCK_SELECTOR_ID) + .build(); + etcdDataDataChangedListener.onRuleChanged(ImmutableList.of(ruleData), DataEventTypeEnum.CREATE); + etcdDataDataChangedListener.onRuleChanged(ImmutableList.of(ruleData), DataEventTypeEnum.UPDATE); + etcdDataDataChangedListener.onRuleChanged(ImmutableList.of(ruleData), DataEventTypeEnum.REFRESH); + etcdDataDataChangedListener.onRuleChanged(ImmutableList.of(ruleData), DataEventTypeEnum.DELETE); + verify(etcdClient, times(3)).put(any(String.class), any(String.class)); + verify(etcdClient, times(1)).delete(any(String.class)); + verify(etcdClient, times(1)).deleteEtcdPathRecursive(any(String.class)); + } +} diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/pom.xml b/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/pom.xml new file mode 100644 index 0000000..631c75f --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/pom.xml @@ -0,0 +1,88 @@ + + + + + + org.apache.shenyu + shenyu-extension-etcd + 2.7.1-SNAPSHOT + + 4.0.0 + shenyu-infra-etcd + + + + + org.apache.shenyu + shenyu-infra-common + ${shenyu.version} + + + + io.etcd + jetcd-core + + + grpc-netty + io.grpc + + + grpc-protobuf + io.grpc + + + grpc-stub + io.grpc + + + + + + io.grpc + grpc-core + ${grpc.version} + + + + grpc-protobuf + io.grpc + ${grpc.version} + + + + grpc-stub + io.grpc + ${grpc.version} + + + + grpc-netty + io.grpc + ${grpc.version} + + + + + org.springframework.boot + spring-boot-test + test + + + + + diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/main/java/org/apache/shenyu/infra/etcd/autoconfig/ConditionOnSyncEtcd.java b/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/main/java/org/apache/shenyu/infra/etcd/autoconfig/ConditionOnSyncEtcd.java new file mode 100644 index 0000000..45a85c5 --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/main/java/org/apache/shenyu/infra/etcd/autoconfig/ConditionOnSyncEtcd.java @@ -0,0 +1,39 @@ +/* + * 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.infra.etcd.autoconfig; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Enable etcd data sync. + */ + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +@ConditionalOnProperty(prefix = EtcdProperties.CONFIG_PREFIX, name = "url") +public @interface ConditionOnSyncEtcd { +} diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/main/java/org/apache/shenyu/infra/etcd/autoconfig/EtcdConfiguration.java b/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/main/java/org/apache/shenyu/infra/etcd/autoconfig/EtcdConfiguration.java new file mode 100644 index 0000000..d3c55a9 --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/main/java/org/apache/shenyu/infra/etcd/autoconfig/EtcdConfiguration.java @@ -0,0 +1,70 @@ +/* + * 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.infra.etcd.autoconfig; + +import io.etcd.jetcd.Client; +import org.apache.shenyu.infra.etcd.client.EtcdClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.Objects; + +@Configuration +@ConditionOnSyncEtcd +@ConditionalOnClass(Client.class) +@EnableConfigurationProperties(EtcdProperties.class) +public class EtcdConfiguration { + + private static final Logger log = LoggerFactory.getLogger(EtcdConfiguration.class); + + private static final Integer DEFAULT_SESSION_TIMEOUT = 30 * 1000; + + private static final Integer DEFAULT_CONNECT_TIMEOUT = 30 * 1000; + + /** + * Init etcd client. + * + * @param etcdProperties etcd properties + * @return Etcd Client + */ + @Bean + @ConditionalOnMissingBean + public EtcdClient etcdClient(final EtcdProperties etcdProperties) { + + log.info("Initializing Etcd Client with URL: {}", etcdProperties.getEtcd()); + + int timeout = Objects.isNull(etcdProperties.getEtcd().getSessionTimeout()) ? DEFAULT_SESSION_TIMEOUT : etcdProperties.getEtcd().getSessionTimeout(); + int ttl = Objects.isNull(etcdProperties.getEtcd().getConnectionTimeout()) ? DEFAULT_CONNECT_TIMEOUT : etcdProperties.getEtcd().getConnectionTimeout(); + + return EtcdClient.builder() + .client( + Client.builder() + .endpoints(etcdProperties.getEtcd().getUrl().split(",")) + .build() + ) + .timeout(timeout) + .ttl(ttl) + .build(); + } + +} diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/main/java/org/apache/shenyu/infra/etcd/autoconfig/EtcdProperties.java b/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/main/java/org/apache/shenyu/infra/etcd/autoconfig/EtcdProperties.java new file mode 100644 index 0000000..5697f30 --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/main/java/org/apache/shenyu/infra/etcd/autoconfig/EtcdProperties.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shenyu.infra.etcd.autoconfig; + +import org.apache.shenyu.common.constant.Constants; +import org.apache.shenyu.infra.common.InfraConstants; +import org.apache.shenyu.infra.common.InfraParentProperties; +import org.apache.shenyu.infra.etcd.config.EtcdConfig; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.NestedConfigurationProperty; + +@ConfigurationProperties(InfraParentProperties.PARENT_CONFIG_PREFIX) +public class EtcdProperties extends InfraParentProperties { + + public static final String CONFIG_PREFIX = PARENT_CONFIG_PREFIX + Constants.DOT + InfraConstants.SHENYU_ETCD; + + @NestedConfigurationProperty + private EtcdConfig etcd = EtcdConfig.builder().build(); + + public EtcdConfig getEtcd() { + return etcd; + } + + public void setEtcd(final EtcdConfig etcd) { + this.etcd = etcd; + } + +} diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/main/java/org/apache/shenyu/infra/etcd/client/EtcdClient.java b/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/main/java/org/apache/shenyu/infra/etcd/client/EtcdClient.java new file mode 100644 index 0000000..c307539 --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/main/java/org/apache/shenyu/infra/etcd/client/EtcdClient.java @@ -0,0 +1,549 @@ +/* + * 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.infra.etcd.client; + +import io.etcd.jetcd.ByteSequence; +import io.etcd.jetcd.Client; +import io.etcd.jetcd.KV; +import io.etcd.jetcd.KeyValue; +import io.etcd.jetcd.Watch; +import io.etcd.jetcd.kv.GetResponse; +import io.etcd.jetcd.lease.LeaseKeepAliveResponse; +import io.etcd.jetcd.options.DeleteOption; +import io.etcd.jetcd.options.GetOption; +import io.etcd.jetcd.options.PutOption; +import io.etcd.jetcd.options.WatchOption; +import io.etcd.jetcd.watch.WatchEvent; +import io.grpc.stub.StreamObserver; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.shenyu.common.exception.ShenyuException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.BiConsumer; +import java.util.function.Consumer; +import java.util.stream.Collectors; + +import static java.nio.charset.StandardCharsets.UTF_8; + +/** + * Etcd client of Bootstrap. + */ +public class EtcdClient { + + private static final Logger LOG = LoggerFactory.getLogger(EtcdClient.class); + + private static final int MAX_RETRY_DELAY_SECONDS = 60; + + private static final int INITIAL_RETRY_DELAY_SECONDS = 1; + + private final Client client; + + private final long ttl; + + private final long timeout; + + private long globalLeaseId; + + private final ConcurrentHashMap watchCache = new ConcurrentHashMap<>(); + + private final ConcurrentHashMap watchChildCache = new ConcurrentHashMap<>(); + + private volatile boolean keepAliveRunning = true; + + private volatile StreamObserver currentObserver; + + private final ScheduledExecutorService retryExecutor; + + private volatile ScheduledFuture retryFuture; + + private final AtomicInteger retryCount = new AtomicInteger(0); + + public EtcdClient(final Client client, final long ttl, final long timeout) { + + this.ttl = ttl; + this.timeout = timeout; + this.client = client; + this.retryExecutor = new ScheduledThreadPoolExecutor(1, r -> { + Thread thread = new Thread(r, "etcd-keepalive-retry"); + thread.setDaemon(true); + return thread; + }); + + initLease(); + } + + /** + * close client. + */ + public synchronized void close() { + if (!keepAliveRunning) { + return; + } + keepAliveRunning = false; + watchCache.values().forEach(Watch.Watcher::close); + watchCache.clear(); + watchChildCache.values().forEach(Watch.Watcher::close); + watchChildCache.clear(); + if (Objects.nonNull(retryFuture)) { + retryFuture.cancel(false); + } + retryExecutor.shutdown(); + try { + if (!retryExecutor.awaitTermination(5, TimeUnit.SECONDS)) { + retryExecutor.shutdownNow(); + } + } catch (InterruptedException e) { + retryExecutor.shutdownNow(); + Thread.currentThread().interrupt(); + } + this.client.close(); + } + + /** + * get node value. + * + * @param key node name + * @return string + */ + public String get(final String key) { + List keyValues = null; + try { + keyValues = client.getKVClient().get(bytesOf(key)).get().getKvs(); + } catch (InterruptedException | ExecutionException e) { + LOG.error("get key error, key:{}", key, e); + } + + if (CollectionUtils.isEmpty(keyValues)) { + LOG.warn("get key {} is empty", key); + return null; + } + return keyValues.iterator().next().getValue().toString(UTF_8); + } + + /** + * get keys by prefix. + * + * @param prefix key prefix. + * @return key valuesMap. + */ + public Map getKeysMapByPrefix(final String prefix) { + GetOption getOption = GetOption.newBuilder() + .isPrefix(true) + .build(); + try { + return this.client.getKVClient().get(bytesOf(prefix), getOption) + .get().getKvs().stream() + .collect(Collectors.toMap(e -> e.getKey().toString(UTF_8), e -> e.getValue().toString(UTF_8))); + } catch (ExecutionException | InterruptedException e) { + LOG.error("etcd getKeysMapByPrefix key {} error", prefix, e); + throw new ShenyuException(e); + } + + } + + /** + * bytesOf string. + * + * @param val val. + * @return bytes val. + */ + public ByteSequence bytesOf(final String val) { + return ByteSequence.from(val, UTF_8); + } + + /** + * get node sub nodes. + * + * @param prefix node prefix. + * @param separator separator char + * @return sub nodes + * @throws ExecutionException the exception + * @throws InterruptedException the exception + */ + public List getChildrenKeys(final String prefix, final String separator) + throws ExecutionException, InterruptedException { + ByteSequence prefixByteSequence = bytesOf(prefix); + GetOption getOption = GetOption.newBuilder() + .withPrefix(prefixByteSequence) + .withSortField(GetOption.SortTarget.KEY) + .withSortOrder(GetOption.SortOrder.ASCEND) + .build(); + + List keyValues = client.getKVClient() + .get(prefixByteSequence, getOption) + .get() + .getKvs(); + + return keyValues.stream() + .map(e -> getSubNodeKeyName(prefix, e.getKey().toString(UTF_8), separator)) + .distinct() + .filter(Objects::nonNull) + .collect(Collectors.toList()); + } + + /** + * get keyPrefix map. + * + * @param prefix key prefix. + * @param separator separator char + * @param map prefix map + * @return sub map + */ + public List getChildrenKeysByMap(final String prefix, final String separator, + final Map map) { + + return map.entrySet().stream() + .filter(e -> e.getKey().contains(prefix)) + .map(e -> getSubNodeKeyName(prefix, e.getKey(), separator)) + .distinct() + .filter(Objects::nonNull) + .collect(Collectors.toList()); + } + + private String getSubNodeKeyName(final String prefix, final String fullPath, final String separator) { + if (prefix.length() > fullPath.length()) { + return null; + } + String pathWithoutPrefix = fullPath.substring(prefix.length()); + return pathWithoutPrefix.contains(separator) ? pathWithoutPrefix.substring(1) : pathWithoutPrefix; + } + + /** + * subscribe data change. + * + * @param key node name + * @param updateHandler node value handler of update + * @param deleteHandler node value handler of delete + */ + public void watchDataChange(final String key, + final BiConsumer updateHandler, + final Consumer deleteHandler) { + Watch.Listener listener = watch(updateHandler, deleteHandler); + watchCache.computeIfAbsent(key, path -> client.getWatchClient().watch(ByteSequence.from(path, UTF_8), listener)); + } + + /** + * subscribe sub node change. + * + * @param key param node name. + * @param updateHandler sub node handler of update + * @param deleteHandler sub node delete of delete + */ + public void watchChildChange(final String key, + final BiConsumer updateHandler, + final Consumer deleteHandler) { + Watch.Listener listener = watch(updateHandler, deleteHandler); + WatchOption option = WatchOption.newBuilder() + .withPrefix(ByteSequence.from(key, UTF_8)) + .build(); + watchChildCache.computeIfAbsent(key, path -> client.getWatchClient().watch(ByteSequence.from(path, UTF_8), option, listener)); + } + + private Watch.Listener watch(final BiConsumer updateHandler, + final Consumer deleteHandler) { + return Watch.listener(response -> { + for (WatchEvent event : response.getEvents()) { + String path = event.getKeyValue().getKey().toString(UTF_8); + String value = event.getKeyValue().getValue().toString(UTF_8); + switch (event.getEventType()) { + case PUT: + Optional.ofNullable(updateHandler).ifPresent(handler -> handler.accept(path, value)); + continue; + case DELETE: + Optional.ofNullable(deleteHandler).ifPresent(handler -> handler.accept(path)); + continue; + default: + } + } + }, throwable -> { + // jetcd schedules watch recovery after this callback returns. + LOG.error("etcd watch error {}", throwable.getMessage(), throwable); + }); + } + + /** + * cancel subscribe. + * + * @param key node name + */ + public void watchClose(final String key) { + if (watchCache.containsKey(key)) { + watchCache.get(key).close(); + watchCache.remove(key); + } + if (watchChildCache.containsKey(key)) { + watchChildCache.get(key).close(); + watchChildCache.remove(key); + } + } + + /** + * check node exists. + * + * @param key node name + * @return bool + */ + public Boolean exists(final String key) { + try { + GetOption option = GetOption.newBuilder() + .withPrefix(ByteSequence.from(key, StandardCharsets.UTF_8)) + .build(); + List keyValues = client.getKVClient().get(ByteSequence.from(key, StandardCharsets.UTF_8), option) + .get().getKvs(); + return !keyValues.isEmpty(); + } catch (Exception e) { + LOG.error("check node exists error", e); + throw new ShenyuException(e); + } + } + + /** + * update value of node. + * + * @param key node name + * @param value node value + */ + public void put(final String key, final String value) { + try { + client.getKVClient().put(ByteSequence.from(key, StandardCharsets.UTF_8), + ByteSequence.from(value, StandardCharsets.UTF_8)).get(); + } catch (Exception e) { + LOG.error("update value of node error.", e); + throw new ShenyuException(e); + } + } + + /** + * delete node. + * + * @param key node name + */ + public void delete(final String key) { + client.getKVClient().delete(ByteSequence.from(key, StandardCharsets.UTF_8)); + } + + /** + * delete node of recursive. + * + * @param path parent node name + */ + public void deleteEtcdPathRecursive(final String path) { + DeleteOption option = DeleteOption.newBuilder() + .withPrefix(ByteSequence.from(path, StandardCharsets.UTF_8)) + .build(); + try { + client.getKVClient().delete(ByteSequence.from(path, StandardCharsets.UTF_8), option).get(10, + TimeUnit.SECONDS); + } catch (Exception e) { + LOG.error("delete node of recursive error.", e); + throw new ShenyuException(e); + } + } + + /** + * watchKeyChanges. + * + * @param key key + * @param listener listener + * @return {@link Watch.Watcher} + */ + public Watch.Watcher watchKeyChanges(final String key, final Watch.Listener listener) { + WatchOption option = WatchOption.newBuilder().isPrefix(true).build(); + + return client.getWatchClient().watch(bytesOf(key), option, listener); + } + + /** + * get keyResponse. + * + * @param key watch key. + * @param getOption get option. + * @return key response. + */ + public GetResponse getRange(final String key, final GetOption getOption) { + try { + return this.client.getKVClient().get(bytesOf(key), getOption).get(); + } catch (ExecutionException | InterruptedException e) { + LOG.error("etcd getRange key {} error", key, e); + throw new ShenyuException(e); + } + } + + /** + * put data as ephemeral. + * + * @param key key + * @param value value + */ + public void putEphemeral(final String key, final String value) { + try { + KV kvClient = client.getKVClient(); + kvClient.put(ByteSequence.from(key, UTF_8), ByteSequence.from(value, UTF_8), + PutOption.newBuilder().withLeaseId(globalLeaseId).build()) + .get(timeout, TimeUnit.MILLISECONDS); + } catch (InterruptedException | ExecutionException | TimeoutException e) { + LOG.error("putEphemeral(key:{},value:{}) error.", key, value, e); + throw new ShenyuException(e); + } + } + + private void initLease() { + try { + this.globalLeaseId = client.getLeaseClient().grant(ttl).get().getID(); + keepAlive(); + } catch (InterruptedException e) { + LOG.error("initLease error.", e); + Thread.currentThread().interrupt(); + throw new ShenyuException(e); + } catch (ExecutionException e) { + LOG.error("initLease error.", e); + throw new ShenyuException(e); + } + } + + private void keepAlive() { + if (!keepAliveRunning) { + return; + } + retryCount.set(0); + scheduleRetryInternal(0); + } + + private void scheduleRetry() { + int attempt = retryCount.incrementAndGet(); + scheduleRetryInternal(attempt); + } + + private void scheduleRetryInternal(final int attempt) { + if (!keepAliveRunning) { + return; + } + + if (Objects.nonNull(retryFuture) && !retryFuture.isDone()) { + retryFuture.cancel(false); + } + + long delaySeconds; + if (attempt <= 0) { + delaySeconds = 0; + } else { + long backoffFactor = 1L << Math.min(Math.max(attempt - 1, 0), 6); + delaySeconds = Math.min(INITIAL_RETRY_DELAY_SECONDS * backoffFactor, MAX_RETRY_DELAY_SECONDS); + } + + retryFuture = retryExecutor.schedule(() -> { + if (!keepAliveRunning) { + return; + } + + currentObserver = new StreamObserver<>() { + @Override + public void onNext(final LeaseKeepAliveResponse leaseKeepAliveResponse) { + retryCount.set(0); + } + + @Override + public void onError(final Throwable throwable) { + LOG.error("keep alive error", throwable); + currentObserver = null; + scheduleRetry(); + } + + @Override + public void onCompleted() { + LOG.warn("keep alive stream completed"); + currentObserver = null; + scheduleRetry(); + } + }; + + try { + client.getLeaseClient().keepAlive(globalLeaseId, currentObserver); + } catch (Exception e) { + LOG.error("Failed to start keep alive", e); + currentObserver = null; + scheduleRetry(); + } + }, delaySeconds, TimeUnit.SECONDS); + + LOG.debug("Scheduled keep alive retry in {} seconds (attempt: {})", delaySeconds, attempt); + } + + /** + * Create a new {@link Builder} instance for building an {@link EtcdClient}. + * + * @return a new {@link Builder} instance + */ + public static Builder builder() { + + return Builder.builder(); + } + + public static final class Builder { + + /** + * etcd client. + */ + private Client client; + + private long ttl = 60; + + private long timeout = 5000; + + private Builder() { + } + + private static Builder builder() { + return new Builder(); + } + + public Builder client(final Client client) { + this.client = client; + return this; + } + + public Builder ttl(final long ttl) { + this.ttl = ttl; + return this; + } + + public Builder timeout(final long timeout) { + this.timeout = timeout; + return this; + } + + public EtcdClient build() { + return new EtcdClient(client, ttl, timeout); + } + } + +} diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/main/java/org/apache/shenyu/infra/etcd/config/EtcdConfig.java b/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/main/java/org/apache/shenyu/infra/etcd/config/EtcdConfig.java new file mode 100644 index 0000000..5d15413 --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/main/java/org/apache/shenyu/infra/etcd/config/EtcdConfig.java @@ -0,0 +1,173 @@ +/* + * 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.infra.etcd.config; + +import java.util.StringJoiner; + +/** + * The type etcd configuration. + */ +public class EtcdConfig { + + private String url; + + private Integer sessionTimeout; + + private Integer connectionTimeout; + + private String serializer; + + public EtcdConfig(final String url, final Integer session, + final Integer connection, final String serializer) { + + this.url = url; + this.sessionTimeout = session; + this.connectionTimeout = connection; + this.serializer = serializer; + } + + /** + * Get url. + * + * @return url + */ + public String getUrl() { + return url; + } + + /** + * Set url. + * + * @param url url + */ + public void setUrl(final String url) { + this.url = url; + } + + /** + * Get sessionTimeout. + * + * @return sessionTimeout + */ + public Integer getSessionTimeout() { + return sessionTimeout; + } + + /** + * Set sessionTimeout. + * + * @param sessionTimeout sessionTimeout + */ + public void setSessionTimeout(final Integer sessionTimeout) { + this.sessionTimeout = sessionTimeout; + } + + /** + * Get connectionTimeout. + * + * @return connectionTimeout + */ + public Integer getConnectionTimeout() { + return connectionTimeout; + } + + /** + * Set connectionTimeout. + * + * @param connectionTimeout connectionTimeout + */ + public void setConnectionTimeout(final Integer connectionTimeout) { + this.connectionTimeout = connectionTimeout; + } + + /** + * Get serializer. + * + * @return serializer + */ + public String getSerializer() { + return serializer; + } + + /** + * Set serializer. + * + * @param serializer serializer + */ + public void setSerializer(final String serializer) { + this.serializer = serializer; + } + + @Override + public String toString() { + return new StringJoiner(", ", EtcdConfig.class.getSimpleName() + "[", "]") + .add("url='" + url + "'") + .add("sessionTimeout=" + sessionTimeout) + .add("connectionTimeout=" + connectionTimeout) + .add("serializer='" + serializer + "'") + .toString(); + } + + public static Builder builder() { + + return new Builder(); + } + + public static final class Builder { + + private String url; + + private Integer sessionTimeout; + + private Integer connectionTimeout; + + private String serializer; + + /** + * private constructor. not allow to create instance. + */ + private Builder() { + } + + public Builder url(final String url) { + this.url = url; + return this; + } + + public Builder sessionTimeout(final Integer sessionTimeout) { + this.sessionTimeout = sessionTimeout; + return this; + } + + public Builder connectionTimeout(final Integer connectionTimeout) { + this.connectionTimeout = connectionTimeout; + return this; + } + + public Builder serializer(final String serializer) { + this.serializer = serializer; + return this; + } + + public EtcdConfig build() { + + return new EtcdConfig(url, sessionTimeout, connectionTimeout, serializer); + } + } + +} diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/main/resources/META-INF/spring.factories b/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000..8ddda59 --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/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.infra.etcd.autoconfig.EtcdConfiguration diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 0000000..060f974 --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/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.infra.etcd.autoconfig.EtcdConfiguration diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/test/java/org/apache/shenyu/infra/etcd/client/EtcdClientTest.java b/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/test/java/org/apache/shenyu/infra/etcd/client/EtcdClientTest.java new file mode 100644 index 0000000..59666e4 --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/test/java/org/apache/shenyu/infra/etcd/client/EtcdClientTest.java @@ -0,0 +1,270 @@ +/* + * 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.infra.etcd.client; + +import io.etcd.jetcd.ByteSequence; +import io.etcd.jetcd.Client; +import io.etcd.jetcd.ClientBuilder; +import io.etcd.jetcd.KV; +import io.etcd.jetcd.Lease; +import io.etcd.jetcd.kv.PutResponse; +import io.etcd.jetcd.lease.LeaseGrantResponse; +import io.etcd.jetcd.lease.LeaseKeepAliveResponse; +import io.grpc.stub.StreamObserver; +import org.apache.shenyu.common.exception.ShenyuException; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; + +@MockitoSettings(strictness = Strictness.LENIENT) +@ExtendWith(MockitoExtension.class) +public class EtcdClientTest { + + @Test + public void etcdClientTest() { + try (MockedStatic clientMockedStatic = mockStatic(Client.class)) { + final ClientBuilder clientBuilder = mock(ClientBuilder.class); + clientMockedStatic.when(Client::builder).thenReturn(clientBuilder); + when(clientBuilder.endpoints(anyString())).thenReturn(clientBuilder); + final Client client = mock(Client.class); + when(clientBuilder.endpoints(anyString()).build()).thenReturn(client); + final Lease lease = mock(Lease.class); + when(client.getLeaseClient()).thenReturn(lease); + final CompletableFuture completableFuture = mock(CompletableFuture.class); + final LeaseGrantResponse leaseGrantResponse = mock(LeaseGrantResponse.class); + when(leaseGrantResponse.getID()).thenReturn(1L); + + when(client.getLeaseClient().grant(anyLong())).thenReturn(completableFuture); + when(completableFuture.get()).thenReturn(leaseGrantResponse); + List> observerList = new ArrayList<>(); + doAnswer(invocation -> { + observerList.add(invocation.getArgument(1)); + return lease; + }).when(lease).keepAlive(anyLong(), any()); + + final EtcdClient etcdClient = Assertions.assertDoesNotThrow(() -> EtcdClient.builder() + .client(Client.builder().endpoints("url").build()) + .ttl(60L) + .timeout(3000L) + .build()); + Assertions.assertNotNull(etcdClient); + + final LeaseKeepAliveResponse leaseKeepAliveResponse = mock(LeaseKeepAliveResponse.class); + observerList.forEach(streamObserver -> { + streamObserver.onCompleted(); + streamObserver.onError(new ShenyuException("test")); + streamObserver.onNext(leaseKeepAliveResponse); + }); + etcdClient.close(); + + doThrow(new InterruptedException("error")).when(completableFuture).get(); + Assertions.assertThrows(ShenyuException.class, () -> EtcdClient.builder() + .client(Client.builder().endpoints("url").build()) + .ttl(60L) + .timeout(3000L) + .build()); + } catch (Exception e) { + throw new ShenyuException(e.getCause()); + } + } + + @Test + public void keepAliveRetryTest() throws ExecutionException, InterruptedException { + try (MockedStatic clientMockedStatic = mockStatic(Client.class)) { + final Client client = this.mockEtcd(clientMockedStatic); + final Lease lease = client.getLeaseClient(); + + AtomicInteger keepAliveInvoked = new AtomicInteger(0); + CountDownLatch latch = new CountDownLatch(2); + CountDownLatch firstCallLatch = new CountDownLatch(1); + List> observerList = new ArrayList<>(); + + doAnswer(invocation -> { + keepAliveInvoked.incrementAndGet(); + latch.countDown(); + firstCallLatch.countDown(); + observerList.add(invocation.getArgument(1)); + return lease; + }).when(lease).keepAlive(anyLong(), any()); + + final EtcdClient etcdClient = EtcdClient.builder() + .client(Client.builder().endpoints("url").build()) + .ttl(60L) + .timeout(3000L) + .build(); + Assertions.assertNotNull(etcdClient); + + // Wait for the initial keepAlive registration to complete + Assertions.assertTrue(firstCallLatch.await(2, TimeUnit.SECONDS), "Initial keepAlive should run"); + awaitObserverRegistered(observerList); + + // Trigger error to schedule retry keep-alive + observerList.get(0).onError(new RuntimeException("test-error")); + + boolean retried = latch.await(3, TimeUnit.SECONDS); + etcdClient.close(); + + Assertions.assertTrue(retried, "keepAlive should retry after onError"); + Assertions.assertTrue(keepAliveInvoked.get() >= 2, "Expected retry keepAlive invocation"); + } + } + + @Test + public void keepAliveCompletedRetryTest() throws ExecutionException, InterruptedException { + try (MockedStatic clientMockedStatic = mockStatic(Client.class)) { + final Client client = this.mockEtcd(clientMockedStatic); + final Lease lease = client.getLeaseClient(); + + AtomicInteger keepAliveInvoked = new AtomicInteger(0); + CountDownLatch callsLatch = new CountDownLatch(2); + CountDownLatch firstCallLatch = new CountDownLatch(1); + List> observerList = new ArrayList<>(); + + doAnswer(invocation -> { + keepAliveInvoked.incrementAndGet(); + callsLatch.countDown(); + firstCallLatch.countDown(); + observerList.add(invocation.getArgument(1)); + return lease; + }).when(lease).keepAlive(anyLong(), any()); + + final EtcdClient etcdClient = EtcdClient.builder() + .client(Client.builder().endpoints("url").build()) + .ttl(60L) + .timeout(3000L) + .build(); + Assertions.assertNotNull(etcdClient); + + Assertions.assertTrue(firstCallLatch.await(2, TimeUnit.SECONDS), "Initial keepAlive should run"); + awaitObserverRegistered(observerList); + + observerList.get(0).onCompleted(); + + boolean retried = callsLatch.await(3, TimeUnit.SECONDS); + etcdClient.close(); + + Assertions.assertTrue(retried, "keepAlive should retry after onCompleted"); + Assertions.assertTrue(keepAliveInvoked.get() >= 2, "Expected keepAlive invocation after completion"); + } + } + + @Test + public void closeTest() { + try (MockedStatic clientMockedStatic = mockStatic(Client.class)) { + this.mockEtcd(clientMockedStatic); + final EtcdClient etcdClient = EtcdClient.builder().client(Client.builder().endpoints("url").build()).ttl(60L).timeout(3000L).build(); + etcdClient.close(); + } catch (Exception e) { + throw new ShenyuException(e.getCause()); + } + } + + @Test + public void putEphemeralTest() { + try (MockedStatic clientMockedStatic = mockStatic(Client.class)) { + final Client client = this.mockEtcd(clientMockedStatic); + final KV mockKV = mock(KV.class); + when(client.getKVClient()).thenReturn(mockKV); + final CompletableFuture completableFuture = mock(CompletableFuture.class); + when(mockKV.put(any(), any(), any())).thenReturn(completableFuture); + final PutResponse putResponse = mock(PutResponse.class); + when(completableFuture.get(anyLong(), any(TimeUnit.class))).thenReturn(putResponse); + final EtcdClient etcdClient = EtcdClient.builder().client(Client.builder().endpoints("url").build()).ttl(60L).timeout(3000L).build(); + etcdClient.putEphemeral("key", "value"); + + doThrow(new InterruptedException("error")).when(completableFuture).get(anyLong(), any(TimeUnit.class)); + Assertions.assertThrows(ShenyuException.class, () -> etcdClient.putEphemeral("key", "value")); + } catch (Exception e) { + throw new ShenyuException(e.getCause()); + } + } + + private Client mockEtcd(final MockedStatic clientMockedStatic) throws InterruptedException, ExecutionException { + final ClientBuilder clientBuilder = mock(ClientBuilder.class); + clientMockedStatic.when(Client::builder).thenReturn(clientBuilder); + when(clientBuilder.endpoints(anyString())).thenReturn(clientBuilder); + final Client client = mock(Client.class); + when(clientBuilder.endpoints(anyString()).build()).thenReturn(client); + final Lease lease = mock(Lease.class); + when(client.getLeaseClient()).thenReturn(lease); + final CompletableFuture completableFuture = mock(CompletableFuture.class); + final LeaseGrantResponse leaseGrantResponse = mock(LeaseGrantResponse.class); + when(leaseGrantResponse.getID()).thenReturn(1L); + when(client.getLeaseClient().grant(anyLong())).thenReturn(completableFuture); + when(completableFuture.get()).thenReturn(leaseGrantResponse); + return client; + } + + private void awaitObserverRegistered(final List> observerList) + throws InterruptedException { + for (int i = 0; i < 20 && observerList.isEmpty(); i++) { + Thread.sleep(50); + } + Assertions.assertFalse(observerList.isEmpty(), "Observer should be registered"); + } + + @Test + public void concurrentSubscriptionsCloseEveryWatcherOnce() throws Exception { + Client client = mock(Client.class); + Lease lease = mock(Lease.class); + io.etcd.jetcd.Watch watch = mock(io.etcd.jetcd.Watch.class); + io.etcd.jetcd.Watch.Watcher watcher = mock(io.etcd.jetcd.Watch.Watcher.class); + LeaseGrantResponse grant = mock(LeaseGrantResponse.class); + when(client.getLeaseClient()).thenReturn(lease); + when(lease.grant(anyLong())).thenReturn(CompletableFuture.completedFuture(grant)); + when(grant.getID()).thenReturn(1L); + when(client.getWatchClient()).thenReturn(watch); + when(watch.watch(any(ByteSequence.class), any(io.etcd.jetcd.options.WatchOption.class), any(io.etcd.jetcd.Watch.Listener.class))).thenReturn(watcher); + EtcdClient adapter = EtcdClient.builder().client(client).ttl(30).timeout(3000).build(); + try { + java.util.stream.IntStream.range(0, 50).parallel().forEach(index -> adapter.watchChildChange("/test/", (key, value) -> { }, key -> { })); + org.mockito.ArgumentCaptor listener = org.mockito.ArgumentCaptor.forClass(io.etcd.jetcd.Watch.Listener.class); + verify(watch, times(1)).watch(any(ByteSequence.class), any(io.etcd.jetcd.options.WatchOption.class), listener.capture()); + Assertions.assertDoesNotThrow(() -> listener.getValue().onError(new IllegalStateException("connection interrupted"))); + } finally { + adapter.close(); + adapter.close(); + } + verify(watcher, times(1)).close(); + verify(client, times(1)).close(); + } +} diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/test/java/org/apache/shenyu/infra/etcd/properties/AbstractConfigurationTest.java b/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/test/java/org/apache/shenyu/infra/etcd/properties/AbstractConfigurationTest.java new file mode 100644 index 0000000..f376e46 --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/test/java/org/apache/shenyu/infra/etcd/properties/AbstractConfigurationTest.java @@ -0,0 +1,69 @@ +/* + * 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.infra.etcd.properties; + +import org.junit.jupiter.api.AfterEach; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.test.context.support.TestPropertySourceUtils; + +/** + * AbstractConfigurationTest for Configuration or Properties. + */ +public abstract class AbstractConfigurationTest { + + private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + + /** + * Get the current mock context. + * + * @return AnnotationConfigApplicationContext + */ + public AnnotationConfigApplicationContext getContext() { + return context; + } + + /** + * clear context. + */ + @AfterEach + public void clear() { + context.close(); + } + + /** + * Add properties to Environment and register configuration into spring context. + * + * @param configuration the configuration class + * @param inlinedProperties the config properties + */ + public void load(final Class configuration, final String... inlinedProperties) { + load(new Class[]{configuration}, inlinedProperties); + } + + /** + * Add properties to Environment and register configuration into spring context. + * + * @param configuration the configuration class array + * @param inlinedProperties the config properties + */ + public void load(final Class[] configuration, final String... inlinedProperties) { + TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.context, inlinedProperties); + this.context.register(configuration); + this.context.refresh(); + } +} diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/test/java/org/apache/shenyu/infra/etcd/properties/EtcdPropertiesTest.java b/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/test/java/org/apache/shenyu/infra/etcd/properties/EtcdPropertiesTest.java new file mode 100644 index 0000000..96d2636 --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-infra-etcd/src/test/java/org/apache/shenyu/infra/etcd/properties/EtcdPropertiesTest.java @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shenyu.infra.etcd.properties; + +import org.apache.shenyu.infra.etcd.autoconfig.EtcdProperties; +import org.apache.shenyu.infra.etcd.config.EtcdConfig; +import org.junit.jupiter.api.Test; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +/** + * Test case for ZookeeperProperties. + */ +public final class EtcdPropertiesTest extends AbstractConfigurationTest { + + @Test + public void testLoadPropertiesBySpringContext() { + final String url = "127.0.0.1:2181"; + final Integer sessionTimeOut = 100; + final Integer connectionTimeout = 20560; + final String[] inlinedProperties = new String[]{ + "shenyu.sync.etcd.url=" + url, + "shenyu.sync.etcd.sessionTimeout=" + sessionTimeOut, + "shenyu.sync.etcd.connectionTimeout=" + connectionTimeout, + }; + load(EtcdPropertiesConfiguration.class, inlinedProperties); + EtcdProperties properties = getContext().getBean(EtcdProperties.class); + assertThat(properties.getEtcd().getUrl(), is(url)); + assertThat(properties.getEtcd().getSessionTimeout(), is(sessionTimeOut)); + assertThat(properties.getEtcd().getConnectionTimeout(), is(connectionTimeout)); + } + + @Test + public void testSetEtcdConfig() { + EtcdProperties etcdProperties = new EtcdProperties(); + EtcdConfig customConfig = EtcdConfig.builder() + .connectionTimeout(60 * 1000) + .sessionTimeout(60 * 1000) + .build(); + + etcdProperties.setEtcd(customConfig); + EtcdConfig etcdConfig = etcdProperties.getEtcd(); + + assertNotNull(etcdConfig, "EtcdConfig should not be null after setting"); + assertEquals(60 * 1000, etcdConfig.getConnectionTimeout(), "Connection timeout should be updated to 60 seconds"); + assertEquals(60 * 1000, etcdConfig.getSessionTimeout(), "Session timeout(TTL) should be updated to 60 seconds"); + } + + @Configuration + @EnableConfigurationProperties(EtcdProperties.class) + static class EtcdPropertiesConfiguration { + } + +} diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-registry-etcd/pom.xml b/shenyu-extension/shenyu-extension-etcd/shenyu-registry-etcd/pom.xml new file mode 100644 index 0000000..0c6a1f2 --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-registry-etcd/pom.xml @@ -0,0 +1,74 @@ + + + + + + org.apache.shenyu + shenyu-extension-etcd + 2.7.1-SNAPSHOT + + 4.0.0 + shenyu-registry-etcd + + + + org.apache.shenyu + shenyu-registry-api + ${shenyu.version} + + + + org.apache.shenyu + shenyu-infra-etcd + ${project.version} + + + + io.etcd + jetcd-core + + + grpc-protobuf + io.grpc + + + grpc-stub + io.grpc + + + io.grpc + grpc-core + + + grpc-netty + io.grpc + + + protobuf-java-util + com.google.protobuf + + + protobuf-java + com.google.protobuf + + + + + + + diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-registry-etcd/src/main/java/org/apache/shenyu/registry/etcd/EtcdInstanceRegisterRepository.java b/shenyu-extension/shenyu-extension-etcd/shenyu-registry-etcd/src/main/java/org/apache/shenyu/registry/etcd/EtcdInstanceRegisterRepository.java new file mode 100644 index 0000000..c7f2f4c --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-registry-etcd/src/main/java/org/apache/shenyu/registry/etcd/EtcdInstanceRegisterRepository.java @@ -0,0 +1,172 @@ +/* + * 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.etcd; + +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Multimap; +import io.etcd.jetcd.Client; +import io.etcd.jetcd.Watch; +import io.etcd.jetcd.watch.WatchEvent; +import org.apache.shenyu.common.constant.Constants; +import org.apache.shenyu.common.exception.ShenyuException; +import org.apache.shenyu.common.utils.GsonUtils; +import org.apache.shenyu.infra.etcd.client.EtcdClient; +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.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.Properties; +import java.util.stream.Collectors; + +import static java.nio.charset.StandardCharsets.UTF_8; + +/** + * The type Etcd instance register repository. + */ +@Join(isSingleton = false) +public class EtcdInstanceRegisterRepository implements ShenyuInstanceRegisterRepository { + + private static final Logger LOGGER = LoggerFactory.getLogger(EtcdInstanceRegisterRepository.class); + + private EtcdClient client; + + private final Multimap watchCache = ArrayListMultimap.create(); + + @Override + public void init(final RegisterConfig config) { + Properties props = config.getProps(); + long timeout = Long.parseLong(props.getProperty("etcdTimeout", "3000")); + long ttl = Long.parseLong(props.getProperty("etcdTTL", "5")); + client = EtcdClient.builder() + .client( + Client.builder() + .endpoints(config.getServerLists().split(",")) + .build() + ).ttl(ttl) + .timeout(timeout) + .build(); + } + + @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); + client.putEphemeral(realNode, nodeData); + LOGGER.info("etcd client register success: {}", nodeData); + } + + @Override + public List selectInstances(final String selectKey) { + final String watchKey = InstancePathConstants.buildInstanceParentPath(selectKey); + return client.getKeysMapByPrefix(watchKey).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()); + } + + @Override + public boolean serviceExists(final String key) { + try { + return !selectInstances(key).isEmpty(); + } catch (Exception e) { + throw new ShenyuException(e); + } + } + + @Override + public synchronized void watchInstances(final String watchKey, final ChangedEventListener listener) { + try { + Map serverNodes = client.getKeysMapByPrefix(watchKey); + serverNodes.forEach((k, v) -> listener.onEvent(k, v, ChangedEventListener.Event.ADDED)); + final Watch.Watcher watcher = this.client.watchKeyChanges(watchKey, Watch.listener(response -> { + for (WatchEvent event : response.getEvents()) { + String value = event.getKeyValue().getValue().toString(UTF_8); + String path = event.getKeyValue().getKey().toString(UTF_8); + switch (event.getEventType()) { + case PUT: + if (event.getKeyValue().getCreateRevision() == event.getKeyValue().getModRevision()) { + listener.onEvent(path, value, ChangedEventListener.Event.ADDED); + } else { + listener.onEvent(path, value, ChangedEventListener.Event.UPDATED); + } + LOGGER.info("watch key {} updated, value is {}", watchKey, value); + continue; + case DELETE: + listener.onEvent(path, value, ChangedEventListener.Event.DELETED); + LOGGER.info("watch key {} deleted, key is {}", watchKey, path); + continue; + default: + } + } + })); + watchCache.put(watchKey, watcher); + LOGGER.info("etcd added watcher for key: {}", watchKey); + } catch (Exception e) { + LOGGER.error("etcd client watch key: {} error", watchKey, e); + throw new ShenyuException(e); + } + } + + @Override + public synchronized void unWatchInstances(final String key) { + if (watchCache.containsKey(key)) { + watchCache.get(key).forEach(Watch.Watcher::close); + watchCache.removeAll(key); + LOGGER.info("etcd Unwatched etcd key: {}", key); + } + } + + 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 synchronized void close() { + try { + watchCache.values().forEach(Watch.Watcher::close); + watchCache.clear(); + if (Objects.nonNull(client)) { + client.close(); + } + LOGGER.info("Shutting down EtcdDiscoveryService"); + } catch (Exception e) { + LOGGER.error("etcd client shutdown error", e); + throw new ShenyuException(e); + } + } +} diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-registry-etcd/src/main/resources/META-INF/shenyu/org.apache.shenyu.registry.api.ShenyuInstanceRegisterRepository b/shenyu-extension/shenyu-extension-etcd/shenyu-registry-etcd/src/main/resources/META-INF/shenyu/org.apache.shenyu.registry.api.ShenyuInstanceRegisterRepository new file mode 100644 index 0000000..a9742e2 --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-registry-etcd/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. + +etcd=org.apache.shenyu.registry.etcd.EtcdInstanceRegisterRepository diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-registry-etcd/src/test/java/org/apache/shenyu/registry/etcd/EtcdClientTest.java b/shenyu-extension/shenyu-extension-etcd/shenyu-registry-etcd/src/test/java/org/apache/shenyu/registry/etcd/EtcdClientTest.java new file mode 100644 index 0000000..8876e2c --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-registry-etcd/src/test/java/org/apache/shenyu/registry/etcd/EtcdClientTest.java @@ -0,0 +1,148 @@ +/* + * 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.etcd; + +import io.etcd.jetcd.Client; +import io.etcd.jetcd.ClientBuilder; +import io.etcd.jetcd.KV; +import io.etcd.jetcd.Lease; +import io.etcd.jetcd.kv.PutResponse; +import io.etcd.jetcd.lease.LeaseGrantResponse; +import io.etcd.jetcd.lease.LeaseKeepAliveResponse; +import io.grpc.stub.StreamObserver; +import org.apache.shenyu.common.exception.ShenyuException; +import org.apache.shenyu.infra.etcd.client.EtcdClient; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.when; + +/** + * test for EtcdClient. + */ +public class EtcdClientTest { + + @Test + public void etcdClientTest() { + try (MockedStatic clientMockedStatic = mockStatic(Client.class)) { + final ClientBuilder clientBuilder = mock(ClientBuilder.class); + clientMockedStatic.when(Client::builder).thenReturn(clientBuilder); + when(clientBuilder.endpoints(anyString())).thenReturn(clientBuilder); + final Client client = mock(Client.class); + when(clientBuilder.endpoints(anyString()).build()).thenReturn(client); + final Lease lease = mock(Lease.class); + when(client.getLeaseClient()).thenReturn(lease); + final CompletableFuture completableFuture = mock(CompletableFuture.class); + final LeaseGrantResponse leaseGrantResponse = mock(LeaseGrantResponse.class); + when(leaseGrantResponse.getID()).thenReturn(1L); + + when(client.getLeaseClient().grant(anyLong())).thenReturn(completableFuture); + when(completableFuture.get()).thenReturn(leaseGrantResponse); + List> observerList = new ArrayList<>(); + doAnswer(invocation -> { + observerList.add(invocation.getArgument(1)); + return lease; + }).when(lease).keepAlive(anyLong(), any()); + + final EtcdClient etcdClient = Assertions.assertDoesNotThrow(() -> EtcdClient.builder() + .client(Client.builder().endpoints("url").build()) + .ttl(60L) + .timeout(3000L) + .build()); + Assertions.assertNotNull(etcdClient); + + final LeaseKeepAliveResponse leaseKeepAliveResponse = mock(LeaseKeepAliveResponse.class); + observerList.forEach(streamObserver -> { + streamObserver.onCompleted(); + streamObserver.onError(new ShenyuException("test")); + streamObserver.onNext(leaseKeepAliveResponse); + }); + etcdClient.close(); + + doThrow(new InterruptedException("error")).when(completableFuture).get(); + Assertions.assertThrows(ShenyuException.class, () -> EtcdClient.builder() + .client(Client.builder().endpoints("url").build()) + .ttl(60L) + .timeout(3000L) + .build()); + } catch (Exception e) { + throw new ShenyuException(e.getCause()); + } + } + + @Test + public void closeTest() { + try (MockedStatic clientMockedStatic = mockStatic(Client.class)) { + this.mockEtcd(clientMockedStatic); + final EtcdClient etcdClient = EtcdClient.builder().client(Client.builder().endpoints("url").build()).ttl(60L).timeout(3000L).build(); + etcdClient.close(); + } catch (Exception e) { + throw new ShenyuException(e.getCause()); + } + } + + @Test + public void putEphemeralTest() { + try (MockedStatic clientMockedStatic = mockStatic(Client.class)) { + final Client client = this.mockEtcd(clientMockedStatic); + final KV mockKV = mock(KV.class); + when(client.getKVClient()).thenReturn(mockKV); + final CompletableFuture completableFuture = mock(CompletableFuture.class); + when(mockKV.put(any(), any(), any())).thenReturn(completableFuture); + final PutResponse putResponse = mock(PutResponse.class); + when(completableFuture.get(anyLong(), any(TimeUnit.class))).thenReturn(putResponse); + final EtcdClient etcdClient = EtcdClient.builder().client(Client.builder().endpoints("url").build()).ttl(60L).timeout(3000L).build(); + etcdClient.putEphemeral("key", "value"); + + doThrow(new InterruptedException("error")).when(completableFuture).get(anyLong(), any(TimeUnit.class)); + Assertions.assertThrows(ShenyuException.class, () -> etcdClient.putEphemeral("key", "value")); + } catch (Exception e) { + throw new ShenyuException(e.getCause()); + } + } + + private Client mockEtcd(final MockedStatic clientMockedStatic) throws InterruptedException, ExecutionException { + final ClientBuilder clientBuilder = mock(ClientBuilder.class); + clientMockedStatic.when(Client::builder).thenReturn(clientBuilder); + when(clientBuilder.endpoints(anyString())).thenReturn(clientBuilder); + final Client client = mock(Client.class); + when(clientBuilder.endpoints(anyString()).build()).thenReturn(client); + final Lease lease = mock(Lease.class); + when(client.getLeaseClient()).thenReturn(lease); + final CompletableFuture completableFuture = mock(CompletableFuture.class); + final LeaseGrantResponse leaseGrantResponse = mock(LeaseGrantResponse.class); + when(leaseGrantResponse.getID()).thenReturn(1L); + when(client.getLeaseClient().grant(anyLong())).thenReturn(completableFuture); + when(completableFuture.get()).thenReturn(leaseGrantResponse); + return client; + } +} diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-registry-etcd/src/test/java/org/apache/shenyu/registry/etcd/EtcdInstanceRegisterRepositoryTest.java b/shenyu-extension/shenyu-extension-etcd/shenyu-registry-etcd/src/test/java/org/apache/shenyu/registry/etcd/EtcdInstanceRegisterRepositoryTest.java new file mode 100644 index 0000000..fecd391 --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-registry-etcd/src/test/java/org/apache/shenyu/registry/etcd/EtcdInstanceRegisterRepositoryTest.java @@ -0,0 +1,239 @@ +/* + * 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.etcd; + +import io.etcd.jetcd.ByteSequence; +import io.etcd.jetcd.Client; +import io.etcd.jetcd.ClientBuilder; +import io.etcd.jetcd.KeyValue; +import io.etcd.jetcd.Lease; +import io.etcd.jetcd.Watch; +import io.etcd.jetcd.lease.LeaseGrantResponse; +import io.etcd.jetcd.watch.WatchEvent; +import io.etcd.jetcd.watch.WatchResponse; +import org.apache.shenyu.common.exception.ShenyuException; +import org.apache.shenyu.common.utils.GsonUtils; +import org.apache.shenyu.infra.etcd.client.EtcdClient; +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.registry.api.path.InstancePathConstants; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.MockedStatic; + +import java.lang.reflect.Field; +import java.net.URI; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CompletableFuture; + +import static java.nio.charset.StandardCharsets.UTF_8; +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.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * The type Etcd instance register repository test. + */ +public final class EtcdInstanceRegisterRepositoryTest { + + private EtcdInstanceRegisterRepository repository; + + private EtcdClient etcdClient; + + private final Map etcdBroker = new HashMap<>(); + + /** + * Sets up. + * + * @throws NoSuchFieldException the no such field exception + * @throws IllegalAccessException the illegal access exception + */ + @BeforeEach + public void setUp() throws NoSuchFieldException, IllegalAccessException { + this.repository = new EtcdInstanceRegisterRepository(); + Class clazz = this.repository.getClass(); + + String fieldString = "client"; + Field field = clazz.getDeclaredField(fieldString); + field.setAccessible(true); + field.set(repository, mockEtcdClient()); + + etcdBroker.clear(); + } + + private EtcdClient mockEtcdClient() { + etcdClient = mock(EtcdClient.class); + when(etcdClient.getKeysMapByPrefix(anyString())).thenReturn(Collections.emptyMap()); + + doAnswer(invocationOnMock -> { + String key = invocationOnMock.getArgument(0); + String value = invocationOnMock.getArgument(1); + etcdBroker.put(key, value); + return null; + }).when(etcdClient).putEphemeral(anyString(), anyString()); + + doAnswer(invocationOnMock -> { + etcdBroker.clear(); + return null; + }).when(etcdClient).close(); + return etcdClient; + } + + /** + * Test persist instance. + */ + @Test + public void testPersistInstance() { + InstanceEntity data = InstanceEntity.builder() + .appName("shenyu-test") + .host("shenyu-host") + .port(9195) + .build(); + + final String realNode = "/shenyu/register/instance/shenyu-test/shenyu-host:9195"; + repository.persistInstance(data); + assertTrue(etcdBroker.containsKey(realNode)); + assertEquals(GsonUtils.getInstance().toJson(data), etcdBroker.get(realNode)); + repository.close(); + } + + /** + * Init test. + */ + @Test + public void initTest() { + try (MockedStatic clientMockedStatic = mockStatic(Client.class)) { + final ClientBuilder clientBuilder = mock(ClientBuilder.class); + clientMockedStatic.when(Client::builder).thenReturn(clientBuilder); + when(clientBuilder.endpoints(anyString())).thenReturn(clientBuilder); + final Client client = mock(Client.class); + when(clientBuilder.endpoints(anyString()).build()).thenReturn(client); + final Lease lease = mock(Lease.class); + when(client.getLeaseClient()).thenReturn(lease); + final CompletableFuture completableFuture = mock(CompletableFuture.class); + final LeaseGrantResponse leaseGrantResponse = mock(LeaseGrantResponse.class); + + when(client.getLeaseClient().grant(anyLong())).thenReturn(completableFuture); + when(completableFuture.get()).thenReturn(leaseGrantResponse); + RegisterConfig config = new RegisterConfig(); + config.setServerLists("url"); + Assertions.assertDoesNotThrow(() -> repository.init(config)); + } catch (Exception e) { + throw new ShenyuException(e); + } + } + + /** + * Test selecting instances does not create a persistent watcher. + */ + @Test + public void testSelectInstancesDoesNotCreateWatcher() { + assertTrue(repository.selectInstances("shenyu-test").isEmpty()); + assertTrue(repository.selectInstances("shenyu-test").isEmpty()); + + verify(etcdClient, times(2)).getKeysMapByPrefix(InstancePathConstants.buildInstanceParentPath("shenyu-test")); + verify(etcdClient, never()).watchKeyChanges(anyString(), any(Watch.Listener.class)); + } + + /** + * Test each selection reads current instances and fills their URIs. + */ + @Test + public void testSelectInstancesReadsCurrentInstances() { + InstanceEntity data = InstanceEntity.builder() + .appName("shenyu-test") + .host("shenyu-host") + .port(9195) + .build(); + String prefix = InstancePathConstants.buildInstanceParentPath(data.getAppName()); + String node = InstancePathConstants.buildRealNode(prefix, "shenyu-host:9195"); + Map serverNodes = new HashMap<>(); + when(etcdClient.getKeysMapByPrefix(prefix)).thenAnswer(invocation -> new HashMap<>(serverNodes)); + + assertTrue(repository.selectInstances(data.getAppName()).isEmpty()); + serverNodes.put(node, GsonUtils.getInstance().toJson(data)); + List instances = repository.selectInstances(data.getAppName()); + assertEquals(1, instances.size()); + assertEquals(data.getAppName(), instances.get(0).getAppName()); + assertEquals(data.getHost(), instances.get(0).getHost()); + assertEquals(data.getPort(), instances.get(0).getPort()); + assertEquals(URI.create("http://shenyu-host:9195"), instances.get(0).getUri()); + + data.setWeight(10); + data.setUri(URI.create("https://shenyu-host:9195")); + serverNodes.put(node, GsonUtils.getInstance().toJson(data)); + instances = repository.selectInstances(data.getAppName()); + assertEquals(1, instances.size()); + assertEquals(10, instances.get(0).getWeight()); + assertEquals(URI.create("https://shenyu-host:9195"), instances.get(0).getUri()); + + InstanceEntity another = new InstanceEntity(data.getAppName(), "another-host", 9196); + serverNodes.put(InstancePathConstants.buildRealNode(prefix, "another-host:9196"), GsonUtils.getInstance().toJson(another)); + assertEquals(2, repository.selectInstances(data.getAppName()).size()); + serverNodes.remove(node); + instances = repository.selectInstances(data.getAppName()); + assertEquals(1, instances.size()); + assertEquals(URI.create("http://another-host:9196"), instances.get(0).getUri()); + serverNodes.clear(); + assertTrue(repository.selectInstances(data.getAppName()).isEmpty()); + verify(etcdClient, times(6)).getKeysMapByPrefix(prefix); + } + + /** + * Test selecting instances preserves an explicit watcher and its notifications. + */ + @Test + public void testSelectInstancesPreservesExplicitWatcher() { + String key = InstancePathConstants.buildInstanceParentPath("shenyu-test"); + ChangedEventListener listener = mock(ChangedEventListener.class); + Watch.Watcher watcher = mock(Watch.Watcher.class); + when(etcdClient.watchKeyChanges(eq(key), any(Watch.Listener.class))).thenReturn(watcher); + + repository.watchInstances(key, listener); + repository.selectInstances("shenyu-test"); + ArgumentCaptor captor = ArgumentCaptor.forClass(Watch.Listener.class); + verify(etcdClient).watchKeyChanges(eq(key), captor.capture()); + verify(watcher, never()).close(); + String node = key + "/shenyu-host:9195"; + String value = "instance-data"; + KeyValue keyValue = mock(KeyValue.class); + when(keyValue.getKey()).thenReturn(ByteSequence.from(node, UTF_8)); + when(keyValue.getValue()).thenReturn(ByteSequence.from(value, UTF_8)); + WatchResponse response = mock(WatchResponse.class); + when(response.getEvents()).thenReturn(Collections.singletonList(new WatchEvent(keyValue, keyValue, WatchEvent.EventType.PUT))); + + captor.getValue().onNext(response); + verify(listener).onEvent(node, value, ChangedEventListener.Event.ADDED); + } +} diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-registry-etcd/src/test/java/org/apache/shenyu/registry/etcd/RegistrySpiTest.java b/shenyu-extension/shenyu-extension-etcd/shenyu-registry-etcd/src/test/java/org/apache/shenyu/registry/etcd/RegistrySpiTest.java new file mode 100644 index 0000000..b1ae3d7 --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-registry-etcd/src/test/java/org/apache/shenyu/registry/etcd/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.etcd; + +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("etcd"); + assertEquals(EtcdInstanceRegisterRepository.class, repository.getClass()); + } +} diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/pom.xml b/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/pom.xml new file mode 100644 index 0000000..da4bcb8 --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/pom.xml @@ -0,0 +1,134 @@ + + + + + + org.apache.shenyu + shenyu-extension-etcd + 2.7.1-SNAPSHOT + + 4.0.0 + + shenyu-spring-boot-starter-sync-data-etcd + + + + org.apache.shenyu + shenyu-sync-data-etcd + ${project.version} + + + org.apache.shenyu + shenyu-admin-listener-etcd + ${project.version} + test + + + org.apache.shenyu + shenyu-registry-etcd + ${project.version} + test + + + + org.apache.shenyu + shenyu-web + ${shenyu.version} + test + + + org.apache.shenyu + shenyu-plugin-global + ${shenyu.version} + test + + + org.apache.shenyu + shenyu-plugin-divide + ${shenyu.version} + test + + + org.apache.shenyu + shenyu-plugin-uri + ${shenyu.version} + test + + + org.apache.shenyu + shenyu-plugin-httpclient + ${shenyu.version} + test + + + org.apache.shenyu + shenyu-plugin-response + ${shenyu.version} + test + + + org.springframework.boot + spring-boot-autoconfigure + + + org.springframework.boot + spring-boot-test + test + + + org.springframework.boot + spring-boot-test-autoconfigure + test + + + org.testcontainers + testcontainers + ${testcontainers.version} + test + + + org.apache.shenyu + shenyu-registry-core + ${shenyu.version} + test + + + + + + etcd-e2e + + + + org.apache.maven.plugins + maven-failsafe-plugin + ${maven-surefire-plugin.version} + + + + integration-test + verify + + + + + + + + + diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/main/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdSyncDataConfiguration.java b/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/main/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdSyncDataConfiguration.java new file mode 100644 index 0000000..1c9a33f --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/main/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdSyncDataConfiguration.java @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shenyu.springboot.sync.data.etcd; + +import io.etcd.jetcd.Client; +import org.apache.shenyu.common.config.ShenyuConfig; +import org.apache.shenyu.infra.etcd.autoconfig.ConditionOnSyncEtcd; +import org.apache.shenyu.infra.etcd.autoconfig.EtcdProperties; +import org.apache.shenyu.infra.etcd.autoconfig.EtcdConfiguration; +import org.apache.shenyu.infra.etcd.client.EtcdClient; +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.etcd.EtcdSyncDataService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.Collections; +import java.util.List; + +/** + * Etcd sync data configuration for spring boot. + */ + +@Configuration +@ConditionOnSyncEtcd +@EnableConfigurationProperties(EtcdProperties.class) +@ImportAutoConfiguration(EtcdConfiguration.class) +@ConditionalOnClass({EtcdSyncDataConfiguration.class, Client.class}) +public class EtcdSyncDataConfiguration { + + private static final Logger LOGGER = LoggerFactory.getLogger(EtcdSyncDataConfiguration.class); + + /** + * Sync data service. + * + * @param shenyuConfig the shenyu config + * @param etcdClients the etcd client + * @param pluginSubscriber the plugin subscriber + * @param metaSubscribers the meta subscribers + * @param authSubscribers the auth subscribers + * @param proxySelectorDataSubscribers the proxy selector data subscribers + * @param discoveryUpstreamDataSubscribers the discovery upstream data subscribers + * @return the sync data service + */ + @Bean + @ConditionalOnMissingBean(SyncDataService.class) + public SyncDataService syncDataService(final ObjectProvider shenyuConfig, + final ObjectProvider etcdClients, + final ObjectProvider pluginSubscriber, + final ObjectProvider> metaSubscribers, + final ObjectProvider> authSubscribers, + final ObjectProvider> proxySelectorDataSubscribers, + final ObjectProvider> discoveryUpstreamDataSubscribers) { + + LOGGER.info("you use etcd sync shenyu data......."); + return new EtcdSyncDataService(shenyuConfig.getIfAvailable(), + etcdClients.getIfAvailable(), + pluginSubscriber.getIfAvailable(), + metaSubscribers.getIfAvailable(Collections::emptyList), + authSubscribers.getIfAvailable(Collections::emptyList), + proxySelectorDataSubscribers.getIfAvailable(Collections::emptyList), + discoveryUpstreamDataSubscribers.getIfAvailable(Collections::emptyList)); + } + +} diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 0000000..89b299d --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/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.etcd.EtcdSyncDataConfiguration diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/test/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdDeploymentIT.java b/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/test/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdDeploymentIT.java new file mode 100644 index 0000000..ab6faad --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/test/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdDeploymentIT.java @@ -0,0 +1,151 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shenyu.springboot.sync.data.etcd; + +import io.etcd.jetcd.Client; +import org.apache.shenyu.infra.etcd.client.EtcdClient; +import org.junit.jupiter.api.Test; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.Network; +import org.testcontainers.containers.wait.strategy.Wait; +import org.testcontainers.images.builder.Transferable; +import org.testcontainers.utility.DockerImageName; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.time.Duration; +import java.util.Objects; +import java.util.concurrent.TimeUnit; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Full Admin HTTP API to gateway E2E with store artifacts in the runtime images. + */ +class EtcdDeploymentIT { + + private static final String NAMESPACE = "649330b6-c2d7-4edc-be8e-8a54df9eb385"; + + @Test + void externalArtifactsSupportAdminApiAndGatewayRegistration() throws Exception { + try (Network network = Network.newNetwork(); + GenericContainer etcd = etcd(network); + GenericContainer admin = admin(network); + GenericContainer backend = backend(network); + GenericContainer gateway = gateway(network)) { + etcd.start(); + backend.start(); + admin.start(); + Path state = Files.createTempFile("etcd-deployment-", ".json"); + runScenario("prepare", state, admin, gateway); + gateway.start(); + EtcdClient probe = EtcdClient.builder() + .client(Client.builder().endpoints("http://" + etcd.getHost() + ":" + etcd.getMappedPort(2379)).build()) + .ttl(30).timeout(5000).build(); + try { + assertThat(probe.getKeysMapByPrefix("/" + NAMESPACE + "/shenyu/plugin/")).isNotEmpty(); + awaitInstances(probe, true); + runScenario("verify", state, admin, gateway); + gateway.close(); + awaitInstances(probe, false); + } finally { + probe.close(); + } + Files.deleteIfExists(state); + } + } + + private static GenericContainer etcd(final Network network) { + return new GenericContainer<>(DockerImageName.parse("quay.io/coreos/etcd:v3.5.21")) + .withNetwork(network).withNetworkAliases("etcd").withExposedPorts(2379) + .withCommand("etcd", "--listen-client-urls=http://0.0.0.0:2379", "--advertise-client-urls=http://etcd:2379") + .waitingFor(Wait.forHttp("/health").forPort(2379)); + } + + private static GenericContainer admin(final Network network) { + return new GenericContainer<>(DockerImageName.parse("shenyu-plugin-store-admin:etcd")) + .withNetwork(network).withExposedPorts(9095) + .withEnv("ADMIN_JVM", "-Xms128m -Xmx768m") + .withEnv("spring.profiles.active", "h2") + .withEnv("shenyu.sync.websocket.enabled", "false") + .withEnv("shenyu.sync.etcd.url", "http://etcd:2379") + .waitingFor(Wait.forHttp("/actuator/health").forPort(9095).withStartupTimeout(Duration.ofMinutes(3))); + } + + private static GenericContainer backend(final Network network) { + String config = "server { listen 9080; location / { return 200 'etcd-e2e-upstream'; } }"; + return new GenericContainer<>(DockerImageName.parse("nginx:1.27.4-alpine")) + .withNetwork(network).withNetworkAliases("plugin-store-backend") + .withCopyToContainer(Transferable.of(config), "/etc/nginx/conf.d/default.conf"); + } + + private static GenericContainer gateway(final Network network) { + return new GenericContainer<>(DockerImageName.parse("shenyu-plugin-store-bootstrap:etcd")) + .withNetwork(network).withExposedPorts(9195) + .withEnv("BOOT_JVM", "-Xms128m -Xmx768m") + .withEnv("shenyu.namespace", NAMESPACE) + .withEnv("shenyu.sync.websocket.urls", "false") + .withEnv("shenyu.sync.etcd.url", "http://etcd:2379,http://etcd:2379") + .withEnv("shenyu.register.enabled", "true") + .withEnv("shenyu.register.registerType", "etcd") + .withEnv("shenyu.register.serverLists", "http://etcd:2379") + .withEnv("shenyu.register.props.name", "etcd-e2e-gateway") + .withEnv("shenyu.register.props.etcdTTL", "2") + .waitingFor(Wait.forHttp("/actuator/health").forPort(9195).withStartupTimeout(Duration.ofMinutes(3))); + } + + private static void runScenario(final String action, final Path state, + final GenericContainer admin, final GenericContainer gateway) throws Exception { + Path root = Path.of(System.getProperty("user.dir")).toAbsolutePath(); + while (!Files.isRegularFile(root.resolve("script/plugin-store/etcd-deployment-e2e.py"))) { + root = root.getParent(); + if (Objects.isNull(root)) { + throw new IllegalStateException("Cannot find plugin-store E2E script"); + } + } + ProcessBuilder builder = new ProcessBuilder(System.getProperty("etcd.test.python", "python3"), + root.resolve("script/plugin-store/etcd-deployment-e2e.py").toString(), action); + builder.environment().put("SHENYU_ADMIN_URL", "http://" + admin.getHost() + ":" + admin.getMappedPort(9095)); + if ("verify".equals(action)) { + builder.environment().put("SHENYU_GATEWAY_URL", "http://" + gateway.getHost() + ":" + gateway.getMappedPort(9195)); + } + builder.environment().put("SHENYU_NAMESPACE_ID", NAMESPACE); + builder.environment().put("ETCD_E2E_STATE", state.toString()); + Process process = builder.inheritIO().start(); + try { + assertThat(process.waitFor(180, TimeUnit.SECONDS)).isTrue(); + assertThat(process.exitValue()).isZero(); + } finally { + if (process.isAlive()) { + process.destroyForcibly(); + } + } + } + + private static void awaitInstances(final EtcdClient client, final boolean present) throws InterruptedException { + String prefix = "/shenyu/register/instance/etcd-e2e-gateway/"; + long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(30); + while (System.nanoTime() < deadline) { + if (client.getKeysMapByPrefix(prefix).isEmpty() != present) { + return; + } + Thread.sleep(100); + } + assertThat(client.getKeysMapByPrefix(prefix).isEmpty()).isEqualTo(!present); + } +} diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/test/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdRealServerTest.java b/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/test/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdRealServerTest.java new file mode 100644 index 0000000..9c2672e --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/test/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdRealServerTest.java @@ -0,0 +1,656 @@ +/* + * 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.etcd; + +import org.apache.shenyu.admin.listener.etcd.EtcdDataChangedInit; +import org.apache.shenyu.admin.listener.etcd.EtcdDataDataChangedListener; +import org.apache.shenyu.admin.service.SyncDataService; +import org.apache.shenyu.common.config.ShenyuConfig; +import org.apache.shenyu.common.constant.Constants; +import org.apache.shenyu.common.dto.ConditionData; +import org.apache.shenyu.common.dto.DiscoverySyncData; +import org.apache.shenyu.common.dto.DiscoveryUpstreamData; +import org.apache.shenyu.common.dto.PluginData; +import org.apache.shenyu.common.dto.RuleData; +import org.apache.shenyu.common.dto.SelectorData; +import org.apache.shenyu.common.dto.convert.rule.impl.DivideRuleHandle; +import org.apache.shenyu.common.enums.DataEventTypeEnum; +import org.apache.shenyu.common.enums.MatchModeEnum; +import org.apache.shenyu.common.enums.OperatorEnum; +import org.apache.shenyu.common.enums.ParamTypeEnum; +import org.apache.shenyu.common.enums.PluginEnum; +import org.apache.shenyu.common.enums.RpcTypeEnum; +import org.apache.shenyu.common.enums.SelectorTypeEnum; +import org.apache.shenyu.common.utils.GsonUtils; +import org.apache.shenyu.loadbalancer.cache.UpstreamCacheManager; +import org.apache.shenyu.plugin.api.ShenyuPlugin; +import org.apache.shenyu.plugin.api.context.ShenyuContextDecorator; +import org.apache.shenyu.plugin.api.result.DefaultShenyuResult; +import org.apache.shenyu.plugin.api.result.ShenyuResult; +import org.apache.shenyu.plugin.api.utils.SpringBeanUtils; +import org.apache.shenyu.plugin.base.cache.BaseDataCache; +import org.apache.shenyu.plugin.base.cache.CommonDiscoveryUpstreamDataSubscriber; +import org.apache.shenyu.plugin.base.cache.CommonPluginDataSubscriber; +import org.apache.shenyu.plugin.base.cache.MatchDataCache; +import org.apache.shenyu.plugin.base.cache.PluginHandlerEvent; +import org.apache.shenyu.plugin.base.handler.DiscoveryUpstreamDataHandler; +import org.apache.shenyu.plugin.base.handler.PluginDataHandler; +import org.apache.shenyu.plugin.divide.DividePlugin; +import org.apache.shenyu.plugin.divide.context.DivideShenyuContextDecorator; +import org.apache.shenyu.plugin.divide.handler.DividePluginDataHandler; +import org.apache.shenyu.plugin.divide.handler.DivideUpstreamDataHandler; +import org.apache.shenyu.plugin.global.DefaultShenyuContextBuilder; +import org.apache.shenyu.plugin.global.GlobalPlugin; +import org.apache.shenyu.plugin.httpclient.NettyHttpClientPlugin; +import org.apache.shenyu.plugin.response.ResponsePlugin; +import org.apache.shenyu.plugin.response.strategy.MessageWriter; +import org.apache.shenyu.plugin.response.strategy.NettyClientMessageWriter; +import org.apache.shenyu.plugin.uri.URIPlugin; +import org.apache.shenyu.registry.api.config.RegisterConfig; +import org.apache.shenyu.registry.api.entity.InstanceEntity; +import org.apache.shenyu.registry.etcd.EtcdInstanceRegisterRepository; +import org.apache.shenyu.registry.core.ShenyuInstanceRegisterRepositoryFactory; +import org.apache.shenyu.registry.api.event.ChangedEventListener; +import org.apache.shenyu.infra.etcd.client.EtcdClient; +import io.etcd.jetcd.Client; +import org.apache.shenyu.sync.data.etcd.EtcdSyncDataService; +import org.apache.shenyu.web.handler.ShenyuWebHandler; +import org.apache.shenyu.web.loader.ShenyuLoaderService; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.springframework.context.ApplicationEventPublisher; +import org.springframework.context.support.GenericApplicationContext; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.server.reactive.HttpHandler; +import org.springframework.http.server.reactive.ReactorHttpHandlerAdapter; +import org.springframework.test.util.ReflectionTestUtils; +import org.springframework.web.server.adapter.HttpWebHandlerAdapter; +import reactor.core.publisher.Mono; +import reactor.netty.DisposableServer; +import reactor.netty.http.client.HttpClient; +import reactor.netty.http.server.HttpServer; + +import java.net.URI; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.time.Duration; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Properties; +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; + +/** + * Real Etcd server integration tests. + */ +public final class EtcdRealServerTest { + + private static final java.net.http.HttpClient HTTP_CLIENT = java.net.http.HttpClient.newBuilder() + .connectTimeout(Duration.ofSeconds(2)) + .build(); + + @AfterEach + void stopsWatcherAndLeaseRetryThreads() { + awaitUntilAsserted(() -> assertThat(Thread.getAllStackTraces().keySet()) + .noneMatch(thread -> thread.isAlive() && (thread.getName().startsWith("etcd-keepalive-retry") + || thread.getName().startsWith("jetcd-watch-")))); + } + + @Test + void shouldDriveGatewayRouteLifecycleThroughRealEtcd() throws Exception { + try (EtcdTestServer etcd = new EtcdTestServer()) { + etcd.start(); + final String host = etcd.getHost(); + final Integer port = etcd.getPort(); + final EtcdClient adminClient = client(host, port); + final String namespace = "etcd-gateway-it-" + System.nanoTime(); + final String selectorId = "selector" + System.nanoTime(); + final String ruleId = "rule" + System.nanoTime(); + final String basePath = "/etcd/gateway/" + selectorId; + final AtomicReference upstreamBody = new AtomicReference<>("created"); + final AtomicInteger upstreamHits = new AtomicInteger(); + final EtcdDataDataChangedListener adminListener = new EtcdDataDataChangedListener(adminClient); + final DisposableServer upstream = upstreamServer(upstreamBody, upstreamHits); + ShenyuGatewayTestServer gateway = null; + try { + clearGatewayCaches(selectorId); + gateway = ShenyuGatewayTestServer.start(namespace, host, port, adminListener, selectorId, + "127.0.0.1:" + upstream.port(), basePath); + assertGatewayRouteLifecycle(adminListener, gateway, upstreamBody, upstreamHits, namespace, + namespace + "-other", selectorId, ruleId, basePath); + } finally { + Optional.ofNullable(gateway).ifPresent(ShenyuGatewayTestServer::close); + adminClient.close(); + upstream.disposeNow(); + clearGatewayCaches(selectorId); + } + } + } + + @Test + void shouldLoadSnapshotRecoverWatchAndDiscoverExternalInstances() throws Exception { + try (EtcdTestServer etcd = new EtcdTestServer()) { + etcd.start(); + final String host = etcd.getHost(); + final Integer port = etcd.getPort(); + final EtcdClient adminClient = client(host, port); + final String namespace = "etcd-snapshot-" + System.nanoTime(); + final EtcdDataDataChangedListener adminListener = new EtcdDataDataChangedListener(adminClient); + final RecordingPluginSubscriber subscriber = new RecordingPluginSubscriber(); + adminListener.onPluginChanged(List.of(pluginData(namespace, "divide", "initial")), DataEventTypeEnum.CREATE); + adminListener.onSelectorChanged(List.of(selectorData(namespace, "divide", "selector-a", "initial")), DataEventTypeEnum.CREATE); + adminListener.onRuleChanged(List.of(ruleData(namespace, "divide", "selector-a", "rule-a", "initial")), DataEventTypeEnum.CREATE); + final EtcdSyncDataService service = new EtcdSyncDataService(shenyuConfig(namespace), client(host, port), subscriber, + Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), Collections.emptyList()); + final EtcdInstanceRegisterRepository registry = (EtcdInstanceRegisterRepository) ShenyuInstanceRegisterRepositoryFactory.loadRepository("etcd"); + try { + awaitUntilAsserted(() -> assertThat(subscriber.plugins).extracting(PluginData::getConfig).contains("initial")); + awaitUntilAsserted(() -> assertThat(subscriber.selectors).extracting(SelectorData::getId).contains("selector-a")); + awaitUntilAsserted(() -> assertThat(subscriber.rules).extracting(RuleData::getId).contains("rule-a")); + etcd.interrupt(); + adminListener.onPluginChanged(List.of(pluginData(namespace, "divide", "recovered")), DataEventTypeEnum.UPDATE); + awaitUntilAsserted(() -> assertThat(subscriber.plugins).extracting(PluginData::getConfig).contains("recovered")); + adminListener.onPluginChanged(List.of(pluginData(namespace, "divide", "deleted")), DataEventTypeEnum.DELETE); + awaitUntilAsserted(() -> assertThat(subscriber.removedPlugins).extracting(PluginData::getName).contains("divide")); + assertRegistryLifecycle(registry, adminClient, host, port); + } finally { + service.close(); + registry.close(); + adminClient.close(); + } + } + } + + private static EtcdClient client(final String host, final Integer port) { + return EtcdClient.builder().client(Client.builder().endpoints("http://" + host + ":" + port).build()) + .ttl(30).timeout(5000).build(); + } + + private static void assertGatewayRouteLifecycle(final EtcdDataDataChangedListener adminListener, + final ShenyuGatewayTestServer gateway, + final AtomicReference upstreamBody, + final AtomicInteger upstreamHits, + final String namespace, + final String otherNamespace, + final String selectorId, + final String ruleId, + final String basePath) throws Exception { + awaitUntilAsserted(() -> assertThat(BaseDataCache.getInstance().obtainPluginData(PluginEnum.DIVIDE.getName())) + .isNotNull()); + awaitUntilAsserted(() -> assertThat(UpstreamCacheManager.getInstance().findUpstreamListBySelectorId(selectorId)) + .hasSize(1)); + assertNoUpstreamHit(gateway, basePath + "/initial", upstreamHits); + adminListener.onRuleChanged(List.of(ruleData(otherNamespace, selectorId, ruleId, basePath + "/initial")), + DataEventTypeEnum.CREATE); + assertNoUpstreamHit(gateway, basePath + "/initial", upstreamHits); + assertThat(BaseDataCache.getInstance().obtainRuleData(selectorId)).isNull(); + adminListener.onRuleChanged(List.of(ruleData(namespace, selectorId, ruleId, basePath + "/initial")), + DataEventTypeEnum.CREATE); + awaitUntilAsserted(() -> assertThat(BaseDataCache.getInstance().obtainRuleData(selectorId)) + .extracting(RuleData::getId) + .contains(ruleId)); + assertGatewayBody(gateway, basePath + "/initial", "created", upstreamHits); + upstreamBody.set("updated"); + assertGatewayRuleUpdate(adminListener, gateway, upstreamHits, namespace, selectorId, ruleId, basePath); + } + + private static void assertGatewayRuleUpdate(final EtcdDataDataChangedListener adminListener, + final ShenyuGatewayTestServer gateway, + final AtomicInteger upstreamHits, + final String namespace, + final String selectorId, + final String ruleId, + final String basePath) throws Exception { + adminListener.onRuleChanged(List.of(ruleData(namespace, selectorId, ruleId, basePath + "/updated")), + DataEventTypeEnum.UPDATE); + awaitUntilAsserted(() -> assertThat(onlyRule(selectorId).getConditionDataList()) + .extracting(ConditionData::getParamValue) + .containsExactly(basePath + "/updated")); + assertNoUpstreamHit(gateway, basePath + "/initial", upstreamHits); + assertGatewayBody(gateway, basePath + "/updated", "updated", upstreamHits); + adminListener.onRuleChanged(List.of(ruleData(namespace, selectorId, ruleId, basePath + "/updated")), + DataEventTypeEnum.DELETE); + awaitUntilAsserted(() -> assertThat(BaseDataCache.getInstance().obtainRuleData(selectorId)).isNull()); + assertNoUpstreamHit(gateway, basePath + "/updated", upstreamHits); + } + + private static void assertRegistryLifecycle(final EtcdInstanceRegisterRepository registry, + final EtcdClient adminClient, + final String host, + final Integer port) { + final String appName = "shenyu-etcd-it"; + final String root = "/shenyu/register/instance/" + appName; + final Queue events = new ConcurrentLinkedQueue<>(); + registry.init(registerConfig(host, port)); + registry.watchInstances(root, (key, value, event) -> events.add(event)); + registry.persistInstance(instance(appName, "127.0.0.1", 9195)); + awaitUntilAsserted(() -> assertThat(registry.selectInstances(appName)).extracting(InstanceEntity::getPort).contains(9195)); + awaitUntilAsserted(() -> assertThat(events).contains(ChangedEventListener.Event.ADDED)); + registry.persistInstance(instance(appName, "127.0.0.1", 9195)); + awaitUntilAsserted(() -> assertThat(events).contains(ChangedEventListener.Event.UPDATED)); + registry.persistInstance(instance(appName, "127.0.0.1", 9295)); + awaitUntilAsserted(() -> assertThat(registry.selectInstances(appName)).hasSize(2)); + adminClient.delete(root + "/127.0.0.1:9195"); + awaitUntilAsserted(() -> assertThat(events).contains(ChangedEventListener.Event.DELETED)); + registry.unWatchInstances(root); + registry.close(); + awaitUntilAsserted(() -> assertThat(adminClient.getKeysMapByPrefix(root)).isEmpty()); + } + + private static DisposableServer upstreamServer(final AtomicReference upstreamBody, + final AtomicInteger upstreamHits) { + return HttpServer.create() + .host("127.0.0.1") + .port(0) + .handle((request, response) -> { + upstreamHits.incrementAndGet(); + response.header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE); + return response.sendString(Mono.just(upstreamBody.get() + ":" + request.path())); + }) + .bindNow(); + } + + private static void assertGatewayBody(final ShenyuGatewayTestServer gateway, + final String path, + final String expected, + final AtomicInteger upstreamHits) throws Exception { + final int before = upstreamHits.get(); + final HttpResponse response = gateway.get(path); + assertThat(response.body()).isEqualTo(expected + ":" + path.substring(1)); + assertThat(upstreamHits.get()).isEqualTo(before + 1); + } + + private static void assertNoUpstreamHit(final ShenyuGatewayTestServer gateway, + final String path, + final AtomicInteger upstreamHits) throws Exception { + final int before = upstreamHits.get(); + final HttpResponse response = gateway.get(path); + assertThat(response.body()).doesNotContain("created", "updated"); + assertThat(upstreamHits.get()).isEqualTo(before); + } + + private static RuleData onlyRule(final String selectorId) { + final List rules = BaseDataCache.getInstance().obtainRuleData(selectorId); + assertThat(rules).hasSize(1); + return rules.get(0); + } + + private static void clearGatewayCaches(final String selectorId) { + BaseDataCache.getInstance().cleanPluginData(); + BaseDataCache.getInstance().cleanSelectorData(); + BaseDataCache.getInstance().cleanRuleData(); + MatchDataCache.getInstance().cleanSelectorData(); + MatchDataCache.getInstance().cleanRuleDataData(); + UpstreamCacheManager.getInstance().removeByKey(selectorId); + } + + private static ShenyuConfig shenyuConfig(final String namespace) { + final ShenyuConfig shenyuConfig = new ShenyuConfig(); + shenyuConfig.setNamespace(namespace); + return shenyuConfig; + } + + private static RegisterConfig registerConfig(final String host, final Integer port) { + final Properties properties = new Properties(); + properties.setProperty("etcdTTL", "2"); + properties.setProperty("etcdTimeout", "5000"); + final RegisterConfig registerConfig = new RegisterConfig(); + registerConfig.setRegisterType("etcd"); + registerConfig.setServerLists("http://127.0.0.1:1,http://" + host + ":" + port); + registerConfig.setProps(properties); + return registerConfig; + } + + private static PluginData pluginData(final String namespace, final String pluginName, final String config) { + return PluginData.builder() + .id(pluginName) + .name(pluginName) + .enabled(true) + .config(config) + .namespaceId(namespace) + .build(); + } + + private static PluginData gatewayPluginData(final String namespace) { + return PluginData.builder() + .id(PluginEnum.DIVIDE.getName()) + .name(PluginEnum.DIVIDE.getName()) + .enabled(true) + .sort(PluginEnum.DIVIDE.getCode()) + .namespaceId(namespace) + .build(); + } + + private static SelectorData selectorData(final String namespace, + final String pluginName, + final String selectorId, + final String handle) { + return SelectorData.builder() + .id(selectorId) + .name(selectorId) + .pluginName(pluginName) + .enabled(true) + .handle(handle) + .namespaceId(namespace) + .build(); + } + + private static SelectorData gatewaySelectorData(final String namespace, + final String selectorId, + final String basePath) { + return SelectorData.builder() + .id(selectorId) + .name(selectorId) + .pluginName(PluginEnum.DIVIDE.getName()) + .enabled(true) + .logged(false) + .continued(true) + .sort(1) + .type(SelectorTypeEnum.CUSTOM_FLOW.getCode()) + .matchMode(MatchModeEnum.AND.getCode()) + .conditionList(List.of(uriCondition(OperatorEnum.STARTS_WITH.getAlias(), basePath))) + .namespaceId(namespace) + .build(); + } + + private static RuleData ruleData(final String namespace, + final String pluginName, + final String selectorId, + final String ruleId, + final String handle) { + return RuleData.builder() + .id(ruleId) + .name(ruleId) + .pluginName(pluginName) + .selectorId(selectorId) + .enabled(true) + .handle(handle) + .namespaceId(namespace) + .build(); + } + + private static RuleData ruleData(final String namespace, + final String selectorId, + final String ruleId, + final String path) { + final DivideRuleHandle handle = DivideRuleHandle.newInstance(); + handle.setRetry(0); + return RuleData.builder() + .id(ruleId) + .name(ruleId) + .pluginName(PluginEnum.DIVIDE.getName()) + .selectorId(selectorId) + .enabled(true) + .loged(false) + .sort(1) + .matchMode(MatchModeEnum.AND.getCode()) + .handle(GsonUtils.getInstance().toJson(handle)) + .conditionDataList(List.of(uriCondition(OperatorEnum.EQ.getAlias(), path))) + .namespaceId(namespace) + .build(); + } + + private static ConditionData uriCondition(final String operator, final String value) { + final ConditionData conditionData = new ConditionData(); + conditionData.setParamType(ParamTypeEnum.URI.getName()); + conditionData.setOperator(operator); + conditionData.setParamValue(value); + return conditionData; + } + + private static DiscoverySyncData discoverySyncData(final String namespace, + final String selectorId, + final String upstreamUrl) { + final DiscoveryUpstreamData upstreamData = new DiscoveryUpstreamData(); + upstreamData.setId(selectorId + "-upstream"); + upstreamData.setProtocol("http://"); + upstreamData.setUrl(upstreamUrl); + upstreamData.setStatus(0); + upstreamData.setWeight(100); + upstreamData.setNamespaceId(namespace); + final DiscoverySyncData syncData = new DiscoverySyncData(); + syncData.setSelectorId(selectorId); + syncData.setSelectorName(selectorId); + syncData.setPluginName(PluginEnum.DIVIDE.getName()); + syncData.setNamespaceId(namespace); + syncData.setUpstreamDataList(List.of(upstreamData)); + return syncData; + } + + private static InstanceEntity instance(final String appName, final String host, final int port) { + return InstanceEntity.builder() + .appName(appName) + .host(host) + .port(port) + .build(); + } + + private static void awaitUntilAsserted(final Runnable assertion) { + final long deadline = System.nanoTime() + Duration.ofSeconds(10).toNanos(); + AssertionError last = null; + while (System.nanoTime() < deadline) { + try { + assertion.run(); + return; + } catch (AssertionError ex) { + last = ex; + sleep(); + } + } + if (Objects.nonNull(last)) { + throw last; + } + } + + private static void sleep() { + try { + Thread.sleep(100L); + } catch (InterruptedException ex) { + Thread.currentThread().interrupt(); + throw new IllegalStateException(ex); + } + } + + private static final class ShenyuGatewayTestServer implements AutoCloseable { + + private final GenericApplicationContext applicationContext; + + private final EtcdSyncDataService syncService; + + private final DisposableServer server; + + private ShenyuGatewayTestServer(final GenericApplicationContext applicationContext, + final EtcdSyncDataService syncService, + final DisposableServer server) { + this.applicationContext = applicationContext; + this.syncService = syncService; + this.server = server; + } + + private static ShenyuGatewayTestServer start(final String namespace, + final String etcdHost, + final Integer etcdPort, + final EtcdDataDataChangedListener adminListener, + final String selectorId, + final String upstreamUrl, + final String basePath) throws Exception { + final ShenyuConfig shenyuConfig = shenyuConfig(namespace); + final GenericApplicationContext applicationContext = new GenericApplicationContext(); + applicationContext.registerBean(ShenyuConfig.class, () -> shenyuConfig); + applicationContext.registerBean(ShenyuResult.class, DefaultShenyuResult::new); + applicationContext.refresh(); + SpringBeanUtils.getInstance().setApplicationContext(applicationContext); + + final ShenyuWebHandler handler = new ShenyuWebHandler(plugins(), mock(ShenyuLoaderService.class), + shenyuConfig); + final ApplicationEventPublisher eventPublisher = event -> { + if (event instanceof PluginHandlerEvent) { + handler.onApplicationEvent((PluginHandlerEvent) event); + } + }; + final CommonPluginDataSubscriber pluginSubscriber = new CommonPluginDataSubscriber( + pluginDataHandlers(), eventPublisher, shenyuConfig.getSelectorMatchCache(), + shenyuConfig.getRuleMatchCache()); + final CommonDiscoveryUpstreamDataSubscriber upstreamSubscriber = new CommonDiscoveryUpstreamDataSubscriber( + discoveryUpstreamDataHandlers()); + final EtcdClient etcdClient = client(etcdHost, etcdPort); + final EtcdSyncDataService syncService = new EtcdSyncDataService(shenyuConfig, etcdClient, + pluginSubscriber, Collections.emptyList(), + Collections.emptyList(), Collections.emptyList(), List.of(upstreamSubscriber)); + final HttpHandler httpHandler = new HttpWebHandlerAdapter(handler); + final DisposableServer server = HttpServer.create() + .host("127.0.0.1") + .port(0) + .handle(new ReactorHttpHandlerAdapter(httpHandler)) + .bindNow(); + final ShenyuGatewayTestServer gateway = new ShenyuGatewayTestServer(applicationContext, syncService, + server); + final EtcdDataChangedInit initializer = new EtcdDataChangedInit(etcdClient); + ReflectionTestUtils.setField(initializer, "syncDataService", new GatewayAdminSyncDataService( + adminListener, namespace, selectorId, upstreamUrl, basePath)); + initializer.run(); + return gateway; + } + + private static List plugins() { + final Map decorators = Map.of(RpcTypeEnum.HTTP.getName(), + new DivideShenyuContextDecorator()); + final Map writers = Map.of(RpcTypeEnum.HTTP.getName(), + new NettyClientMessageWriter()); + return List.of( + new GlobalPlugin(new DefaultShenyuContextBuilder(decorators)), + new DividePlugin(), + new URIPlugin(), + new NettyHttpClientPlugin(HttpClient.create(), Constants.BYTES_PER_MB), + new ResponsePlugin(writers)); + } + + private static List pluginDataHandlers() { + return List.of(new DividePluginDataHandler()); + } + + private static List discoveryUpstreamDataHandlers() { + return List.of(new DivideUpstreamDataHandler()); + } + + private HttpResponse get(final String path) throws Exception { + final HttpRequest request = HttpRequest.newBuilder(URI.create("http://127.0.0.1:" + + server.port() + path)) + .timeout(Duration.ofSeconds(5)) + .GET() + .build(); + return HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofString()); + } + + @Override + public void close() { + syncService.close(); + server.disposeNow(); + applicationContext.close(); + } + } + + private static final class GatewayAdminSyncDataService implements SyncDataService { + + private final EtcdDataDataChangedListener adminListener; + + private final String namespace; + + private final String selectorId; + + private final String upstreamUrl; + + private final String basePath; + + private GatewayAdminSyncDataService(final EtcdDataDataChangedListener adminListener, + final String namespace, + final String selectorId, + final String upstreamUrl, + final String basePath) { + this.adminListener = adminListener; + this.namespace = namespace; + this.selectorId = selectorId; + this.upstreamUrl = upstreamUrl; + this.basePath = basePath; + } + + @Override + public boolean syncAll(final DataEventTypeEnum type) { + adminListener.onPluginChanged(List.of(gatewayPluginData(namespace)), type); + adminListener.onSelectorChanged(List.of(gatewaySelectorData(namespace, selectorId, basePath)), type); + adminListener.onDiscoveryUpstreamChanged(List.of(discoverySyncData(namespace, selectorId, upstreamUrl)), + type); + return true; + } + + @Override + public boolean syncAllByNamespaceId(final DataEventTypeEnum type, final String namespaceId) { + return syncAll(type); + } + + @Override + public boolean syncPluginData(final String id) { + return syncAll(DataEventTypeEnum.UPDATE); + } + + @Override + public boolean syncPluginData(final String namespaceId, final String pluginId) { + return syncAll(DataEventTypeEnum.UPDATE); + } + } + + private static final class RecordingPluginSubscriber implements org.apache.shenyu.sync.data.api.PluginDataSubscriber { + + private final Queue plugins = new ConcurrentLinkedQueue<>(); + + private final Queue removedPlugins = new ConcurrentLinkedQueue<>(); + + private final Queue selectors = new ConcurrentLinkedQueue<>(); + + private final Queue rules = new ConcurrentLinkedQueue<>(); + + @Override + public void onSubscribe(final PluginData pluginData) { + plugins.add(pluginData); + } + + @Override + public void unSubscribe(final PluginData pluginData) { + removedPlugins.add(pluginData); + } + + @Override + public void onSelectorSubscribe(final SelectorData selectorData) { + selectors.add(selectorData); + } + + @Override + public void onRuleSubscribe(final RuleData ruleData) { + rules.add(ruleData); + } + } +} diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/test/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdSyncDataConfigurationTest.java b/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/test/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdSyncDataConfigurationTest.java new file mode 100644 index 0000000..a71fa75 --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/test/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdSyncDataConfigurationTest.java @@ -0,0 +1,70 @@ +/* + * 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.etcd; + +import org.apache.shenyu.common.config.ShenyuConfig; +import org.apache.shenyu.infra.etcd.autoconfig.EtcdProperties; +import org.apache.shenyu.infra.etcd.client.EtcdClient; +import org.apache.shenyu.sync.data.api.PluginDataSubscriber; +import org.apache.shenyu.sync.data.api.SyncDataService; +import org.apache.shenyu.sync.data.etcd.EtcdSyncDataService; +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; +import static org.mockito.Mockito.verify; + +/** + * External etcd starter auto-configuration contracts. + */ +class EtcdSyncDataConfigurationTest { + + private final ApplicationContextRunner runner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(EtcdSyncDataConfiguration.class)) + .withBean(ShenyuConfig.class) + .withBean(PluginDataSubscriber.class, () -> mock(PluginDataSubscriber.class)); + + @Test + void createsSyncServiceAndBindsMultipleEndpoints() { + EtcdClient client = mock(EtcdClient.class); + runner.withBean(EtcdClient.class, () -> client) + .withPropertyValues("shenyu.sync.etcd.url=http://127.0.0.1:2379,http://127.0.0.1:2381") + .run(context -> { + assertThat(context).hasNotFailed().hasSingleBean(SyncDataService.class).hasSingleBean(EtcdClient.class); + assertThat(context.getBean(SyncDataService.class)).isInstanceOf(EtcdSyncDataService.class); + assertThat(context.getBean(EtcdProperties.class).getEtcd().getUrl()).contains("2379", "2381"); + }); + verify(client).close(); + } + + @Test + void backsOffWithoutEtcdUrl() { + runner.run(context -> assertThat(context).hasNotFailed().doesNotHaveBean(SyncDataService.class).doesNotHaveBean(EtcdClient.class)); + } + + @Test + void preservesUserProvidedSyncService() { + SyncDataService service = mock(SyncDataService.class); + runner.withBean(EtcdClient.class, () -> mock(EtcdClient.class)) + .withBean(SyncDataService.class, () -> service) + .withPropertyValues("shenyu.sync.etcd.url=http://127.0.0.1:2379") + .run(context -> assertThat(context.getBean(SyncDataService.class)).isSameAs(service)); + } +} diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/test/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdTestServer.java b/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/test/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdTestServer.java new file mode 100644 index 0000000..e08a33f --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/test/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdTestServer.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.springboot.sync.data.etcd; + +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.wait.strategy.Wait; +import org.testcontainers.utility.DockerImageName; + +import java.io.IOException; +import java.net.ServerSocket; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.file.Files; +import java.nio.file.Path; +import java.time.Duration; +import java.util.Objects; +import java.util.concurrent.TimeUnit; + +/** + * Disposable real etcd server; CI uses Testcontainers, developer hosts can select a native binary. + */ +final class EtcdTestServer implements AutoCloseable { + + private static final int ETCD_PORT = 2379; + + private final String binary = System.getProperty("etcd.test.binary"); + + private GenericContainer container; + + private Process process; + + private Path dataDirectory; + + private int port; + + private int peerPort; + + void start() throws Exception { + if (Objects.isNull(binary)) { + container = new GenericContainer<>(DockerImageName.parse("quay.io/coreos/etcd:v3.5.21")) + .withExposedPorts(ETCD_PORT) + .withCommand("etcd", "--listen-client-urls=http://0.0.0.0:2379", "--advertise-client-urls=http://127.0.0.1:2379") + .waitingFor(Wait.forHttp("/health").forPort(ETCD_PORT).forStatusCode(200)); + container.start(); + port = container.getMappedPort(ETCD_PORT); + } else { + port = availablePort(); + peerPort = availablePort(); + dataDirectory = Files.createTempDirectory("shenyu-etcd-test-"); + startProcess(); + } + } + + String getHost() { + return Objects.isNull(container) ? "127.0.0.1" : container.getHost(); + } + + int getPort() { + return port; + } + + void interrupt() throws Exception { + if (Objects.nonNull(container)) { + container.getDockerClient().stopContainerCmd(container.getContainerId()).withTimeout(2).exec(); + container.getDockerClient().startContainerCmd(container.getContainerId()).exec(); + waitForHealth(); + } else { + stopProcess(); + startProcess(); + } + } + + private void startProcess() throws Exception { + String clientUrl = "http://127.0.0.1:" + port; + String peerUrl = "http://127.0.0.1:" + peerPort; + process = new ProcessBuilder(binary, "--data-dir=" + dataDirectory, + "--listen-client-urls=" + clientUrl, "--advertise-client-urls=" + clientUrl, + "--listen-peer-urls=" + peerUrl, "--initial-advertise-peer-urls=" + peerUrl, + "--initial-cluster=default=" + peerUrl) + .redirectErrorStream(true).redirectOutput(dataDirectory.resolve("server.log").toFile()).start(); + waitForHealth(); + } + + private void waitForHealth() throws Exception { + HttpClient http = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(1)).build(); + HttpRequest request = HttpRequest.newBuilder(URI.create("http://" + getHost() + ":" + port + "/health")) + .timeout(Duration.ofSeconds(1)).GET().build(); + long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(30); + while (System.nanoTime() < deadline) { + try { + if (http.send(request, HttpResponse.BodyHandlers.ofString()).statusCode() == 200) { + return; + } + } catch (IOException ignored) { + // The process has not opened its listener yet. + } + Thread.sleep(100); + } + throw new IllegalStateException("etcd did not become healthy; inspect " + dataDirectory); + } + + private static int availablePort() throws IOException { + try (ServerSocket socket = new ServerSocket(0)) { + return socket.getLocalPort(); + } + } + + private void stopProcess() throws InterruptedException { + if (Objects.nonNull(process)) { + process.destroy(); + if (!process.waitFor(10, TimeUnit.SECONDS)) { + process.destroyForcibly().waitFor(10, TimeUnit.SECONDS); + } + } + } + + @Override + public void close() throws Exception { + if (Objects.nonNull(container)) { + container.close(); + } + stopProcess(); + } +} diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-sync-data-etcd/pom.xml b/shenyu-extension/shenyu-extension-etcd/shenyu-sync-data-etcd/pom.xml new file mode 100644 index 0000000..ffc1395 --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-sync-data-etcd/pom.xml @@ -0,0 +1,42 @@ + + + + + + org.apache.shenyu + shenyu-extension-etcd + 2.7.1-SNAPSHOT + + 4.0.0 + shenyu-sync-data-etcd + + + + org.apache.shenyu + shenyu-sync-data-api + ${shenyu.version} + + + + org.apache.shenyu + shenyu-infra-etcd + ${project.version} + + + + diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-sync-data-etcd/src/main/java/org/apache/shenyu/sync/data/etcd/EtcdSyncDataService.java b/shenyu-extension/shenyu-extension-etcd/shenyu-sync-data-etcd/src/main/java/org/apache/shenyu/sync/data/etcd/EtcdSyncDataService.java new file mode 100644 index 0000000..cd4cffe --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-sync-data-etcd/src/main/java/org/apache/shenyu/sync/data/etcd/EtcdSyncDataService.java @@ -0,0 +1,114 @@ +/* + * 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.etcd; + +import org.apache.commons.lang3.ObjectUtils; +import org.apache.shenyu.common.config.ShenyuConfig; +import org.apache.shenyu.common.constant.Constants; +import org.apache.shenyu.common.constant.DefaultPathConstants; +import static org.apache.shenyu.common.constant.DefaultPathConstants.handlePathData; + +import org.apache.shenyu.infra.etcd.client.EtcdClient; +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.core.AbstractPathDataSyncService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; +import java.util.Objects; + +/** + * Data synchronize of etcd. + */ +public class EtcdSyncDataService extends AbstractPathDataSyncService { + + private static final Logger LOG = LoggerFactory.getLogger(EtcdSyncDataService.class); + + private final EtcdClient etcdClient; + + private final ShenyuConfig shenyuConfig; + + /** + * Instantiates a new Zookeeper cache manager. + * + * @param shenyuConfig shenyuConfig + * @param etcdClient etcdClient + * @param pluginDataSubscriber the plugin data subscriber + * @param metaDataSubscribers the meta data subscribers + * @param authDataSubscribers the auth data subscribers + */ + public EtcdSyncDataService(final ShenyuConfig shenyuConfig, + final EtcdClient etcdClient, + final PluginDataSubscriber pluginDataSubscriber, + final List metaDataSubscribers, + final List authDataSubscribers, + final List proxySelectorDataSubscribers, + final List discoveryUpstreamDataSubscribers) { + super(pluginDataSubscriber, metaDataSubscribers, authDataSubscribers, proxySelectorDataSubscribers, discoveryUpstreamDataSubscribers); + this.shenyuConfig = shenyuConfig; + this.etcdClient = etcdClient; + watcherData(); + } + + private void watcherData() { + String configNamespace = Constants.PATH_SEPARATOR + shenyuConfig.getNamespace(); + watcherData0(handlePathData(String.join(Constants.PATH_SEPARATOR, configNamespace, DefaultPathConstants.PLUGIN_PARENT))); + watcherData0(handlePathData(String.join(Constants.PATH_SEPARATOR, configNamespace, DefaultPathConstants.SELECTOR_PARENT))); + watcherData0(handlePathData(String.join(Constants.PATH_SEPARATOR, configNamespace, DefaultPathConstants.RULE_PARENT))); + watcherData0(handlePathData(String.join(Constants.PATH_SEPARATOR, configNamespace, DefaultPathConstants.PROXY_SELECTOR))); + watcherData0(handlePathData(String.join(Constants.PATH_SEPARATOR, configNamespace, DefaultPathConstants.DISCOVERY_UPSTREAM))); + watcherData0(handlePathData(String.join(Constants.PATH_SEPARATOR, configNamespace, DefaultPathConstants.APP_AUTH_PARENT))); + watcherData0(handlePathData(String.join(Constants.PATH_SEPARATOR, configNamespace, DefaultPathConstants.META_DATA))); + } + + private void watcherData0(final String registerPath) { + String configNamespace = Constants.PATH_SEPARATOR + shenyuConfig.getNamespace(); + etcdClient.watchChildChange( + registerPath, + (updatePath, updateValue) -> super.event(configNamespace, updatePath, updateValue, registerPath, EventType.PUT), + deletePath -> super.event(configNamespace, deletePath, null, registerPath, EventType.DELETE)); + try { + // load all key + final List childrenKeys = etcdClient.getChildrenKeys(registerPath, "/"); + if (!ObjectUtils.isEmpty(childrenKeys)) { + childrenKeys.forEach(nodePath -> { + // fix the update path + String updatePath = String.join(Constants.PATH_SEPARATOR, registerPath, nodePath); + final String nodeData = etcdClient.get(updatePath); + super.event(configNamespace, updatePath, nodeData, registerPath, EventType.PUT); + }); + } else { + LOG.info("etcd watcher path {} is empty", registerPath); + } + } catch (Exception e) { + LOG.error("watcherData0 errors", e); + } + } + + @Override + public void close() { + if (Objects.nonNull(etcdClient)) { + etcdClient.close(); + } + } + +} diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-sync-data-etcd/src/test/java/org/apache/shenyu/sync/data/etcd/EtcdSyncDataServiceTest.java b/shenyu-extension/shenyu-extension-etcd/shenyu-sync-data-etcd/src/test/java/org/apache/shenyu/sync/data/etcd/EtcdSyncDataServiceTest.java new file mode 100644 index 0000000..7f0e423 --- /dev/null +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-sync-data-etcd/src/test/java/org/apache/shenyu/sync/data/etcd/EtcdSyncDataServiceTest.java @@ -0,0 +1,73 @@ +/* + * 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.etcd; + +import org.apache.shenyu.common.config.ShenyuConfig; +import org.apache.shenyu.common.constant.Constants; +import org.apache.shenyu.infra.etcd.client.EtcdClient; +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.junit.jupiter.MockitoExtension; + +import java.util.Collections; +import java.util.function.BiConsumer; +import java.util.function.Consumer; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * test case for {@link EtcdSyncDataService}. + */ +@ExtendWith(MockitoExtension.class) +public class EtcdSyncDataServiceTest { + + @Test + public void testZookeeperInstanceRegisterRepository() throws Exception { + + EtcdClient etcdClient = mock(EtcdClient.class); + PluginDataSubscriber pluginDataSubscriber = mock(PluginDataSubscriber.class); + doAnswer(invocationOnMock -> { + BiConsumer updateHandler = invocationOnMock.getArgument(1); + updateHandler.accept("updateData", "{}"); + Consumer deleteHandler = invocationOnMock.getArgument(2); + String pluginName = invocationOnMock.getArgument(0); + deleteHandler.accept(pluginName + "removeKey"); + return null; + }).when(etcdClient).watchChildChange(any(), any(), any()); + when(etcdClient.getChildrenKeys(any(), any())).thenReturn(Collections.singletonList("test")); + final AuthDataSubscriber authDataSubscriber = mock(AuthDataSubscriber.class); + final MetaDataSubscriber metaDataSubscriber = mock(MetaDataSubscriber.class); + final ProxySelectorDataSubscriber proxySelectorDataSubscriber = mock(ProxySelectorDataSubscriber.class); + final DiscoveryUpstreamDataSubscriber discoveryUpstreamDataSubscriber = mock(DiscoveryUpstreamDataSubscriber.class); + final ShenyuConfig shenyuConfig = mock(ShenyuConfig.class); + when(shenyuConfig.getNamespace()).thenReturn(Constants.SYS_DEFAULT_NAMESPACE_ID); + final EtcdSyncDataService zookeeperSyncDataService = new EtcdSyncDataService(shenyuConfig, etcdClient, + pluginDataSubscriber, Collections.singletonList(metaDataSubscriber), Collections.singletonList(authDataSubscriber), + Collections.singletonList(proxySelectorDataSubscriber), Collections.singletonList(discoveryUpstreamDataSubscriber)); + + zookeeperSyncDataService.close(); + } +} From d1a77da710d68768b8d5cd2a2701de348b7af73c Mon Sep 17 00:00:00 2001 From: yangjj-iso <3202137046@qq.com> Date: Wed, 14 Oct 2026 19:41:36 +0800 Subject: [PATCH 2/6] fix(logging): resolve deferred headers before exporting logs --- .../sls/client/AliyunSlsLogCollectClient.java | 3 +++ .../lts/client/HuaweiLtsLogCollectClient.java | 3 +++ .../client/TencentClsLogCollectClient.java | 3 +++ .../LoggingAliyunSlsGatewayContractTest.java | 17 ++++++++++++----- .../LoggingHuaweiLtsGatewayContractTest.java | 4 +++- .../LoggingTencentClsGatewayContractTest.java | 19 +++++++++++++------ 6 files changed, 37 insertions(+), 12 deletions(-) diff --git a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-aliyun-sls/src/main/java/org/apache/shenyu/plugin/aliyun/sls/client/AliyunSlsLogCollectClient.java b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-aliyun-sls/src/main/java/org/apache/shenyu/plugin/aliyun/sls/client/AliyunSlsLogCollectClient.java index ab69e00..f56c747 100644 --- a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-aliyun-sls/src/main/java/org/apache/shenyu/plugin/aliyun/sls/client/AliyunSlsLogCollectClient.java +++ b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-aliyun-sls/src/main/java/org/apache/shenyu/plugin/aliyun/sls/client/AliyunSlsLogCollectClient.java @@ -139,6 +139,9 @@ public void close0() throws Exception { * @param log log */ private void sendLog(final ShenyuRequestLog log) { + // Core defers header serialization; field-based exporters must resolve it first. + log.getRequestHeader(); + log.getResponseHeader(); final List logGroup = new ArrayList<>(); LogItem logItem = new LogItem((int) (System.currentTimeMillis() / 1000)); logItem.PushBack("level", "info"); diff --git a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-huawei-lts/src/main/java/org/apache/shenyu/plugin/huawei/lts/client/HuaweiLtsLogCollectClient.java b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-huawei-lts/src/main/java/org/apache/shenyu/plugin/huawei/lts/client/HuaweiLtsLogCollectClient.java index cec7c42..2de02e7 100644 --- a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-huawei-lts/src/main/java/org/apache/shenyu/plugin/huawei/lts/client/HuaweiLtsLogCollectClient.java +++ b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-huawei-lts/src/main/java/org/apache/shenyu/plugin/huawei/lts/client/HuaweiLtsLogCollectClient.java @@ -138,6 +138,9 @@ public void close0() throws Exception { * @param log log */ private void sendLog(final ShenyuRequestLog log) { + // Core defers header serialization; field-based exporters must resolve it first. + log.getRequestHeader(); + log.getResponseHeader(); final List logItems = new ArrayList<>(); final List logItemList = new ArrayList<>(); LogItem logItem = new LogItem(); diff --git a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-tencent-cls/src/main/java/org/apache/shenyu/plugin/tencent/cls/client/TencentClsLogCollectClient.java b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-tencent-cls/src/main/java/org/apache/shenyu/plugin/tencent/cls/client/TencentClsLogCollectClient.java index 1b02ba1..4ff18e8 100644 --- a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-tencent-cls/src/main/java/org/apache/shenyu/plugin/tencent/cls/client/TencentClsLogCollectClient.java +++ b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-tencent-cls/src/main/java/org/apache/shenyu/plugin/tencent/cls/client/TencentClsLogCollectClient.java @@ -140,6 +140,9 @@ public void close0() { * @param log log */ private void sendLog(final ShenyuRequestLog log) { + // Core defers header serialization; field-based exporters must resolve it first. + log.getRequestHeader(); + log.getResponseHeader(); final List logItems = new ArrayList<>(); LogItem logItem = new LogItem((int) (System.currentTimeMillis() / 1000)); logItem.PushBack("level", "info"); diff --git a/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-logging-aliyun-sls/src/test/java/org/apache/shenyu/springboot/starter/plugin/logging/aliyun/sls/LoggingAliyunSlsGatewayContractTest.java b/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-logging-aliyun-sls/src/test/java/org/apache/shenyu/springboot/starter/plugin/logging/aliyun/sls/LoggingAliyunSlsGatewayContractTest.java index 466df75..7c9d895 100644 --- a/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-logging-aliyun-sls/src/test/java/org/apache/shenyu/springboot/starter/plugin/logging/aliyun/sls/LoggingAliyunSlsGatewayContractTest.java +++ b/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-logging-aliyun-sls/src/test/java/org/apache/shenyu/springboot/starter/plugin/logging/aliyun/sls/LoggingAliyunSlsGatewayContractTest.java @@ -21,6 +21,8 @@ import com.aliyun.openservices.log.Client; import com.aliyun.openservices.log.common.LogItem; import com.google.common.util.concurrent.SettableFuture; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import org.apache.shenyu.common.config.ShenyuConfig; import org.apache.shenyu.common.constant.Constants; import org.apache.shenyu.common.dto.RuleData; @@ -93,12 +95,17 @@ public void testGatewayTrafficReachesAliyunProducerBoundary() throws Exception { assertGatewayResponse("/contract?source=aliyun", "aliyun-response-body"); Assertions.assertTrue(sent.await(3, TimeUnit.SECONDS)); - String payload = capturedLogs.get().get(0).ToJsonString(); + JsonObject envelope = JsonParser.parseString(capturedLogs.get().get(0).ToJsonString()).getAsJsonObject(); + JsonObject payload = JsonParser.parseString(envelope.get("message").getAsString()).getAsJsonObject(); + JsonObject requestHeaders = JsonParser.parseString(payload.get("requestHeader").getAsString()).getAsJsonObject(); + JsonObject responseHeaders = JsonParser.parseString(payload.get("responseHeader").getAsString()).getAsJsonObject(); Assertions.assertEquals("topic", capturedTopic.get()); - Assertions.assertTrue(payload.contains("\\\"status\\\":202"), payload); - Assertions.assertTrue(payload.contains("Content-Type"), payload); - Assertions.assertTrue(payload.contains("aliyun-response-body"), payload); - Assertions.assertTrue(payload.contains("/contract?source=aliyun"), payload); + Assertions.assertEquals(HttpStatus.ACCEPTED.value(), payload.get("status").getAsInt()); + Assertions.assertFalse(requestHeaders.entrySet().isEmpty()); + Assertions.assertEquals(MediaType.TEXT_PLAIN_VALUE, responseHeaders.get("Content-Type").getAsString()); + Assertions.assertEquals("aliyun-response-body", payload.get("responseBody").getAsString()); + Assertions.assertEquals("source=aliyun", payload.get("queryParams").getAsString()); + Assertions.assertTrue(payload.get("requestUri").getAsString().endsWith("/contract?source=aliyun")); } /** diff --git a/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-logging-huawei-lts/src/test/java/org/apache/shenyu/springboot/starter/plugin/logging/huawei/lts/LoggingHuaweiLtsGatewayContractTest.java b/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-logging-huawei-lts/src/test/java/org/apache/shenyu/springboot/starter/plugin/logging/huawei/lts/LoggingHuaweiLtsGatewayContractTest.java index 7ee05f7..2001489 100644 --- a/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-logging-huawei-lts/src/test/java/org/apache/shenyu/springboot/starter/plugin/logging/huawei/lts/LoggingHuaweiLtsGatewayContractTest.java +++ b/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-logging-huawei-lts/src/test/java/org/apache/shenyu/springboot/starter/plugin/logging/huawei/lts/LoggingHuaweiLtsGatewayContractTest.java @@ -96,7 +96,9 @@ public void testGatewayTrafficReachesHuaweiProducerBoundary() throws Exception { String payload = capturedLogs.get().get(0).getContents().get(0).getLog(); Assertions.assertEquals("stream", capturedStream.get()); Assertions.assertTrue(payload.contains("status=202"), payload); - Assertions.assertTrue(payload.contains("Content-Type"), payload); + Assertions.assertTrue(payload.contains("requestHeader={"), payload); + Assertions.assertTrue(payload.contains("responseHeader={"), payload); + Assertions.assertTrue(payload.contains("\"Content-Type\":\"text/plain\""), payload); Assertions.assertTrue(payload.contains("huawei-response-body"), payload); Assertions.assertTrue(payload.contains("/contract?source=huawei"), payload); } diff --git a/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-logging-tencent-cls/src/test/java/org/apache/shenyu/springboot/starter/plugin/logging/tencent/cls/LoggingTencentClsGatewayContractTest.java b/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-logging-tencent-cls/src/test/java/org/apache/shenyu/springboot/starter/plugin/logging/tencent/cls/LoggingTencentClsGatewayContractTest.java index 067a936..5984dd1 100644 --- a/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-logging-tencent-cls/src/test/java/org/apache/shenyu/springboot/starter/plugin/logging/tencent/cls/LoggingTencentClsGatewayContractTest.java +++ b/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-logging-tencent-cls/src/test/java/org/apache/shenyu/springboot/starter/plugin/logging/tencent/cls/LoggingTencentClsGatewayContractTest.java @@ -18,6 +18,8 @@ package org.apache.shenyu.springboot.starter.plugin.logging.tencent.cls; import com.google.common.util.concurrent.SettableFuture; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import com.tencentcloudapi.cls.producer.AsyncProducerClient; import com.tencentcloudapi.cls.producer.common.LogItem; import org.apache.shenyu.common.config.ShenyuConfig; @@ -92,13 +94,18 @@ public void testGatewayTrafficReachesTencentProducerBoundary() throws Exception assertGatewayResponse("/contract?source=tencent", "tencent-response-body"); Assertions.assertTrue(sent.await(3, TimeUnit.SECONDS)); - String payload = capturedLogs.get().get(0).mContents.toString(); + String message = capturedLogs.get().get(0).mContents.getContentsList().stream() + .filter(content -> "message".equals(content.getKey())).findFirst().orElseThrow().getValue(); + JsonObject payload = JsonParser.parseString(message).getAsJsonObject(); + JsonObject requestHeaders = JsonParser.parseString(payload.get("requestHeader").getAsString()).getAsJsonObject(); + JsonObject responseHeaders = JsonParser.parseString(payload.get("responseHeader").getAsString()).getAsJsonObject(); Assertions.assertEquals("topic", capturedTopic.get()); - Assertions.assertTrue(payload.contains("status"), payload); - Assertions.assertTrue(payload.contains("202"), payload); - Assertions.assertTrue(payload.contains("Content-Type"), payload); - Assertions.assertTrue(payload.contains("tencent-response-body"), payload); - Assertions.assertTrue(payload.contains("/contract?source=tencent"), payload); + Assertions.assertEquals(HttpStatus.ACCEPTED.value(), payload.get("status").getAsInt()); + Assertions.assertFalse(requestHeaders.entrySet().isEmpty()); + Assertions.assertEquals(MediaType.TEXT_PLAIN_VALUE, responseHeaders.get("Content-Type").getAsString()); + Assertions.assertEquals("tencent-response-body", payload.get("responseBody").getAsString()); + Assertions.assertEquals("source=tencent", payload.get("queryParams").getAsString()); + Assertions.assertTrue(payload.get("requestUri").getAsString().endsWith("/contract?source=tencent")); } /** From 68851b04edef47ad98f16c97ffcc452450bac5f8 Mon Sep 17 00:00:00 2001 From: yangjj-iso <3202137046@qq.com> Date: Wed, 14 Oct 2026 19:59:16 +0800 Subject: [PATCH 3/6] test(etcd): keep the Docker endpoint stable across restarts --- .../shenyu/springboot/sync/data/etcd/EtcdTestServer.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/test/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdTestServer.java b/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/test/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdTestServer.java index e08a33f..969e8f7 100644 --- a/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/test/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdTestServer.java +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/test/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdTestServer.java @@ -30,6 +30,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.time.Duration; +import java.util.List; import java.util.Objects; import java.util.concurrent.TimeUnit; @@ -54,10 +55,13 @@ final class EtcdTestServer implements AutoCloseable { void start() throws Exception { if (Objects.isNull(binary)) { + port = availablePort(); container = new GenericContainer<>(DockerImageName.parse("quay.io/coreos/etcd:v3.5.21")) .withExposedPorts(ETCD_PORT) .withCommand("etcd", "--listen-client-urls=http://0.0.0.0:2379", "--advertise-client-urls=http://127.0.0.1:2379") .waitingFor(Wait.forHttp("/health").forPort(ETCD_PORT).forStatusCode(200)); + // Reconnecting clients must keep the same endpoint when Docker restarts the server. + container.setPortBindings(List.of(port + ":" + ETCD_PORT)); container.start(); port = container.getMappedPort(ETCD_PORT); } else { From 5817a3c1dfd27e12d4ff7129c6cd2d722b5d6410 Mon Sep 17 00:00:00 2001 From: yangjj-iso <3202137046@qq.com> Date: Mon, 2 Nov 2026 07:05:03 +0800 Subject: [PATCH 4/6] test(etcd): align selector fixtures with the compatible core API --- .../shenyu/springboot/sync/data/etcd/EtcdRealServerTest.java | 2 +- .../shenyu/plugin/store/test/support/GatewayFixtures.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/test/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdRealServerTest.java b/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/test/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdRealServerTest.java index 9c2672e..96200d5 100644 --- a/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/test/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdRealServerTest.java +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/test/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdRealServerTest.java @@ -405,7 +405,7 @@ private static RuleData ruleData(final String namespace, .pluginName(PluginEnum.DIVIDE.getName()) .selectorId(selectorId) .enabled(true) - .loged(false) + .logged(false) .sort(1) .matchMode(MatchModeEnum.AND.getCode()) .handle(GsonUtils.getInstance().toJson(handle)) diff --git a/shenyu-plugin/shenyu-plugin-store-test-support/src/main/java/org/apache/shenyu/plugin/store/test/support/GatewayFixtures.java b/shenyu-plugin/shenyu-plugin-store-test-support/src/main/java/org/apache/shenyu/plugin/store/test/support/GatewayFixtures.java index a8441c2..d28c8cd 100644 --- a/shenyu-plugin/shenyu-plugin-store-test-support/src/main/java/org/apache/shenyu/plugin/store/test/support/GatewayFixtures.java +++ b/shenyu-plugin/shenyu-plugin-store-test-support/src/main/java/org/apache/shenyu/plugin/store/test/support/GatewayFixtures.java @@ -113,7 +113,7 @@ public static RuleData cacheRule(final String pluginName, final String selectorI .selectorId(selectorId) .name(pluginName + "-rule") .enabled(true) - .loged(true) + .logged(true) .sort(1) .matchMode(MatchModeEnum.AND.getCode()) .matchRestful(false) From 2a89e235db1d8fc2460c4c56bcbd8523778602f1 Mon Sep 17 00:00:00 2001 From: yangjj-iso <3202137046@qq.com> Date: Mon, 2 Nov 2026 07:17:49 +0800 Subject: [PATCH 5/6] test(etcd): support both core logging-field snapshots in the real-server fixture --- shenyu-extension/shenyu-extension-etcd/README.md | 4 ++-- .../springboot/sync/data/etcd/EtcdRealServerTest.java | 9 +++++++-- .../plugin/store/test/support/GatewayFixtures.java | 2 +- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/shenyu-extension/shenyu-extension-etcd/README.md b/shenyu-extension/shenyu-extension-etcd/README.md index da5aa71..f3bfe5e 100644 --- a/shenyu-extension/shenyu-extension-etcd/README.md +++ b/shenyu-extension/shenyu-extension-etcd/README.md @@ -23,7 +23,7 @@ Infrastructure is etcd-specific; no retained core module imports it. ## Release gates -The main removal PR must remain draft until the community agrees to externalization, +The main removal PR must not merge until the community agrees to externalization, the receiving build and real-server/full-deployment tests pass, and the external artifacts are published. This source change does not mean artifacts are released. The store currently uses `2.7.1-SNAPSHOT` and compiles against ShenYu `2.7.2-SNAPSHOT`; @@ -159,5 +159,5 @@ SHENYU_CORE_SOURCE=/path/to/shenyu bash script/plugin-store/build-etcd-runtime.s mvn -B -ntp -pl shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd -am verify -Petcd-e2e ``` -The initial receiving PR and paired main PR remain draft until container/full +The receiving PR and paired main PR require passing container/full application verification and the release gates above are complete. diff --git a/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/test/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdRealServerTest.java b/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/test/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdRealServerTest.java index 96200d5..41d2701 100644 --- a/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/test/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdRealServerTest.java +++ b/shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd/src/test/java/org/apache/shenyu/springboot/sync/data/etcd/EtcdRealServerTest.java @@ -17,6 +17,7 @@ package org.apache.shenyu.springboot.sync.data.etcd; +import com.google.gson.JsonObject; import org.apache.shenyu.admin.listener.etcd.EtcdDataChangedInit; import org.apache.shenyu.admin.listener.etcd.EtcdDataDataChangedListener; import org.apache.shenyu.admin.service.SyncDataService; @@ -399,19 +400,23 @@ private static RuleData ruleData(final String namespace, final String path) { final DivideRuleHandle handle = DivideRuleHandle.newInstance(); handle.setRetry(0); - return RuleData.builder() + final RuleData rule = RuleData.builder() .id(ruleId) .name(ruleId) .pluginName(PluginEnum.DIVIDE.getName()) .selectorId(selectorId) .enabled(true) - .logged(false) .sort(1) .matchMode(MatchModeEnum.AND.getCode()) .handle(GsonUtils.getInstance().toJson(handle)) .conditionDataList(List.of(uriCondition(OperatorEnum.EQ.getAlias(), path))) .namespaceId(namespace) .build(); + // Snapshot cores straddle the loged/logged rename; exercise their supported wire fields. + final JsonObject json = GsonUtils.getInstance().fromJson(GsonUtils.getInstance().toJson(rule), JsonObject.class); + json.addProperty("loged", false); + json.addProperty("logged", false); + return GsonUtils.getInstance().fromJson(json.toString(), RuleData.class); } private static ConditionData uriCondition(final String operator, final String value) { diff --git a/shenyu-plugin/shenyu-plugin-store-test-support/src/main/java/org/apache/shenyu/plugin/store/test/support/GatewayFixtures.java b/shenyu-plugin/shenyu-plugin-store-test-support/src/main/java/org/apache/shenyu/plugin/store/test/support/GatewayFixtures.java index d28c8cd..a8441c2 100644 --- a/shenyu-plugin/shenyu-plugin-store-test-support/src/main/java/org/apache/shenyu/plugin/store/test/support/GatewayFixtures.java +++ b/shenyu-plugin/shenyu-plugin-store-test-support/src/main/java/org/apache/shenyu/plugin/store/test/support/GatewayFixtures.java @@ -113,7 +113,7 @@ public static RuleData cacheRule(final String pluginName, final String selectorI .selectorId(selectorId) .name(pluginName + "-rule") .enabled(true) - .logged(true) + .loged(true) .sort(1) .matchMode(MatchModeEnum.AND.getCode()) .matchRestful(false) From be11348b56ac322896d0470ed38d55a31285643d Mon Sep 17 00:00:00 2001 From: yangjj-iso <3202137046@qq.com> Date: Mon, 2 Nov 2026 07:20:13 +0800 Subject: [PATCH 6/6] docs(etcd): retain deployment and release requirements for merge --- shenyu-extension/shenyu-extension-etcd/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shenyu-extension/shenyu-extension-etcd/README.md b/shenyu-extension/shenyu-extension-etcd/README.md index f3bfe5e..2ad7727 100644 --- a/shenyu-extension/shenyu-extension-etcd/README.md +++ b/shenyu-extension/shenyu-extension-etcd/README.md @@ -159,5 +159,5 @@ SHENYU_CORE_SOURCE=/path/to/shenyu bash script/plugin-store/build-etcd-runtime.s mvn -B -ntp -pl shenyu-extension/shenyu-extension-etcd/shenyu-spring-boot-starter-sync-data-etcd -am verify -Petcd-e2e ``` -The receiving PR and paired main PR require passing container/full -application verification and the release gates above are complete. +Merge still requires passing container/full-application verification and +completion of the release gates above.