You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by yu...@apache.org on 2023/01/03 01:50:09 UTC
[shenyu] branch master updated: shenyu-sdk module add fallback (#4285)
This is an automated email from the ASF dual-hosted git repository.
yunlong pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 617f86f1a shenyu-sdk module add fallback (#4285)
617f86f1a is described below
commit 617f86f1aaaf1048c167ef078331af11768a680f
Author: huanccwang <Wa...@outlook.com>
AuthorDate: Tue Jan 3 09:50:00 2023 +0800
shenyu-sdk module add fallback (#4285)
* fallback以及fallback测试
* format
* format
* .
* .
* 修改zk地址为localhost
* fallback integrated test
* 修改抛出rte
* 增加spring cloud fallback以及fallback factory集成测试
* .
* spring cloud集成测试
* .
* 修改springcloud集成测试
* .
* 修改集成测试
Co-authored-by: yunlongn <yu...@outlook.com>
Co-authored-by: xiaoyu <xi...@apache.org>
---
.../consumer/api/ShenyuAlibabaDubboClientApi.java | 13 +++-
.../impl/ShenyuAlibabaDubboClientApiFallBack.java | 86 ++++++++++++++++++++++
.../consumer/api/ShenyuApacheDubboClientApi.java | 13 +++-
.../dubbo/consumer/dto/DubboRequestBody.java | 5 ++
.../dubbo/consumer/dto/DubboTestSaveRequest.java | 9 ++-
.../impl/ShenyuApacheDubboClientAplFallBack.java | 86 ++++++++++++++++++++++
.../consumer/api/ShenyuSpringCloudClientApi.java | 5 +-
.../impl/ShenyuSpringCloudClientApiFallback.java | 50 +++++++++++++
.../ShenyuSpringCloudClientApiFallbackFactory.java | 57 ++++++++++++++
.../pom.xml | 2 +-
.../dubbo/SdkAlibabaDubboIntegratedBootstrap.java | 2 +-
.../test/alibaba/dubbo/ApacheDubboPluginTest.java | 7 ++
.../dubbo/ApacheDubboPluginTest.java | 12 ++-
.../springcloud/SpringCloudPluginTest.java | 4 +-
.../sdk/spring/NoFallbackAvailableException.java | 19 +----
.../proxy/ShenyuClientInvocationHandler.java | 36 ++++++++-
16 files changed, 380 insertions(+), 26 deletions(-)
diff --git a/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-dubbo/shenyu-examples-sdk-alibaba-dubbo-consumer/src/main/java/org/apache/shenyu/examples/sdk/alibaba/dubbo/consumer/api/ShenyuAlibabaDubboClientApi.java b/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-dubbo/shenyu-examples-sdk-alibaba-dubbo-consumer/src/main/java/org/apache/shenyu/examples/sdk/alibaba/dubbo/consumer/api/ShenyuAlibabaDubboClientApi.java
index c35822feb..0a64783ef 100644
--- a/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-dubbo/shenyu-examples-sdk-alibaba-dubbo-consumer/src/main/java/org/apache/shenyu/examples/sdk/alibaba/dubbo/consumer/api/ShenyuAlibabaDubboClientApi.java
+++ b/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-dubbo/shenyu-examples-sdk-alibaba-dubbo-consumer/src/main/java/org/apache/shenyu/examples/sdk/alibaba/dubbo/consumer/api/ShenyuAlibabaDubboClientApi.java
@@ -22,6 +22,7 @@ import org.apache.shenyu.examples.dubbo.api.entity.DubboTest;
import org.apache.shenyu.examples.dubbo.api.entity.ListResp;
import org.apache.shenyu.examples.sdk.alibaba.dubbo.consumer.dto.DubboRequestBody;
import org.apache.shenyu.examples.sdk.alibaba.dubbo.consumer.dto.DubboTestSaveRequest;
+import org.apache.shenyu.examples.sdk.alibaba.dubbo.consumer.impl.ShenyuAlibabaDubboClientApiFallBack;
import org.apache.shenyu.sdk.spring.ShenyuClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@@ -33,12 +34,13 @@ import java.util.List;
/**
* ShenyuApacheDubboClientApi.
*/
-@ShenyuClient("shenyu-gateway")
+@ShenyuClient(contextId = "shenyu-gateway", name = "ShenyuSdkApiName", fallback = ShenyuAlibabaDubboClientApiFallBack.class)
public interface ShenyuAlibabaDubboClientApi {
/**
* findAll.
* test Get.
+ *
* @return SdkTestDto
*/
@GetMapping("/dubbo/findAll")
@@ -46,6 +48,7 @@ public interface ShenyuAlibabaDubboClientApi {
/**
* findList.
+ *
* @return ListResp
*/
@GetMapping("/dubbo/findList")
@@ -53,6 +56,7 @@ public interface ShenyuAlibabaDubboClientApi {
/**
* findById.
+ *
* @param id id
* @return DubboTest
*/
@@ -61,6 +65,7 @@ public interface ShenyuAlibabaDubboClientApi {
/**
* insert.
+ *
* @param dubboTest dubboTest
* @return DubboTest
*/
@@ -69,6 +74,7 @@ public interface ShenyuAlibabaDubboClientApi {
/**
* findByListId.
+ *
* @param ids ids
* @return DubboTest
*/
@@ -77,6 +83,7 @@ public interface ShenyuAlibabaDubboClientApi {
/**
* findByIdsAndName.
+ *
* @param dubboRequestBody dubboRequestBody
* @return DubboTest
*/
@@ -85,6 +92,7 @@ public interface ShenyuAlibabaDubboClientApi {
/**
* findByArrayIdsAndName.
+ *
* @param dubboRequestBody dubboRequestBody
* @return DubboTest
*/
@@ -93,6 +101,7 @@ public interface ShenyuAlibabaDubboClientApi {
/**
* saveComplexBeanTest.
+ *
* @param complexBeanTest complexBeanTest
* @return DubboTest
*/
@@ -101,6 +110,7 @@ public interface ShenyuAlibabaDubboClientApi {
/**
* batchSave.
+ *
* @param dubboTestSaveRequest dubboTestSaveRequest
* @return DubboTest
*/
@@ -109,6 +119,7 @@ public interface ShenyuAlibabaDubboClientApi {
/**
* batchSaveAndNameAndId.
+ *
* @param dubboTestSaveRequest dubboTestSaveRequest
* @return DubboTest
*/
diff --git a/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-dubbo/shenyu-examples-sdk-alibaba-dubbo-consumer/src/main/java/org/apache/shenyu/examples/sdk/alibaba/dubbo/consumer/impl/ShenyuAlibabaDubboClientApiFallBack.java b/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-dubbo/shenyu-examples-sdk-alibaba-dubbo-consumer/src/main/java/org/apache/shenyu/examples/sdk/alibaba/dubbo/consumer/impl/ShenyuAlibabaDubboClientApiFallBack.java
new file mode 100644
index 000000000..ff7452bd3
--- /dev/null
+++ b/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-dubbo/shenyu-examples-sdk-alibaba-dubbo-consumer/src/main/java/org/apache/shenyu/examples/sdk/alibaba/dubbo/consumer/impl/ShenyuAlibabaDubboClientApiFallBack.java
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.examples.sdk.alibaba.dubbo.consumer.impl;
+
+import org.apache.shenyu.examples.dubbo.api.entity.ComplexBeanTest;
+import org.apache.shenyu.examples.dubbo.api.entity.DubboTest;
+import org.apache.shenyu.examples.dubbo.api.entity.ListResp;
+import org.apache.shenyu.examples.sdk.alibaba.dubbo.consumer.api.ShenyuAlibabaDubboClientApi;
+import org.apache.shenyu.examples.sdk.alibaba.dubbo.consumer.dto.DubboRequestBody;
+import org.apache.shenyu.examples.sdk.alibaba.dubbo.consumer.dto.DubboTestSaveRequest;
+import org.springframework.stereotype.Component;
+
+import java.util.List;
+
+@Component
+public class ShenyuAlibabaDubboClientApiFallBack implements ShenyuAlibabaDubboClientApi {
+
+ @Override
+ public DubboTest findAll() {
+
+ DubboTest dubboTest = new DubboTest();
+ dubboTest.setId("a");
+ dubboTest.setName("fallback");
+ return dubboTest;
+ }
+
+ @Override
+ public ListResp findList() {
+ return null;
+ }
+
+ @Override
+ public DubboTest findById(final String id) {
+ return null;
+ }
+
+ @Override
+ public DubboTest insert(final DubboTest dubboTest) {
+ return null;
+ }
+
+ @Override
+ public DubboTest findByListId(final List<String> ids) {
+ return null;
+ }
+
+ @Override
+ public DubboTest findByIdsAndName(final DubboRequestBody dubboRequestBody) {
+ return null;
+ }
+
+ @Override
+ public DubboTest findByArrayIdsAndName(final DubboRequestBody dubboRequestBody) {
+ return null;
+ }
+
+ @Override
+ public DubboTest saveComplexBeanTest(final ComplexBeanTest complexBeanTest) {
+ return null;
+ }
+
+ @Override
+ public DubboTest batchSave(final DubboTestSaveRequest dubboTestSaveRequest) {
+ return null;
+ }
+
+ @Override
+ public DubboTest batchSaveAndNameAndId(final DubboTestSaveRequest dubboTestSaveRequest) {
+ return null;
+ }
+}
diff --git a/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-dubbo/shenyu-examples-sdk-apache-dubbo-consumer/src/main/java/org/apache/shenyu/examples/sdk/apache/dubbo/consumer/api/ShenyuApacheDubboClientApi.java b/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-dubbo/shenyu-examples-sdk-apache-dubbo-consumer/src/main/java/org/apache/shenyu/examples/sdk/apache/dubbo/consumer/api/ShenyuApacheDubboClientApi.java
index b183c3d93..1ade101d5 100644
--- a/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-dubbo/shenyu-examples-sdk-apache-dubbo-consumer/src/main/java/org/apache/shenyu/examples/sdk/apache/dubbo/consumer/api/ShenyuApacheDubboClientApi.java
+++ b/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-dubbo/shenyu-examples-sdk-apache-dubbo-consumer/src/main/java/org/apache/shenyu/examples/sdk/apache/dubbo/consumer/api/ShenyuApacheDubboClientApi.java
@@ -22,6 +22,7 @@ import org.apache.shenyu.examples.dubbo.api.entity.DubboTest;
import org.apache.shenyu.examples.dubbo.api.entity.ListResp;
import org.apache.shenyu.examples.sdk.apache.dubbo.consumer.dto.DubboRequestBody;
import org.apache.shenyu.examples.sdk.apache.dubbo.consumer.dto.DubboTestSaveRequest;
+import org.apache.shenyu.examples.sdk.apache.dubbo.consumer.impl.ShenyuApacheDubboClientAplFallBack;
import org.apache.shenyu.sdk.spring.ShenyuClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@@ -33,12 +34,13 @@ import java.util.List;
/**
* ShenyuApacheDubboClientApi.
*/
-@ShenyuClient("shenyu-gateway")
+@ShenyuClient(contextId = "shenyu-gateway", name = "ShenyuSdkApiName", fallback = ShenyuApacheDubboClientAplFallBack.class)
public interface ShenyuApacheDubboClientApi {
/**
* findAll.
* test Get.
+ *
* @return DubboTest
*/
@GetMapping("/dubbo/findAll")
@@ -46,6 +48,7 @@ public interface ShenyuApacheDubboClientApi {
/**
* findList.
+ *
* @return ListResp
*/
@GetMapping("/dubbo/findList")
@@ -53,6 +56,7 @@ public interface ShenyuApacheDubboClientApi {
/**
* findById.
+ *
* @param id id
* @return DubboTest
*/
@@ -61,6 +65,7 @@ public interface ShenyuApacheDubboClientApi {
/**
* insert.
+ *
* @param dubboTest dubboTest
* @return DubboTest
*/
@@ -69,6 +74,7 @@ public interface ShenyuApacheDubboClientApi {
/**
* findByListId.
+ *
* @param ids ids
* @return DubboTest
*/
@@ -77,6 +83,7 @@ public interface ShenyuApacheDubboClientApi {
/**
* findByIdsAndName.
+ *
* @param dubboRequestBody dubboRequestBody
* @return DubboTest
*/
@@ -85,6 +92,7 @@ public interface ShenyuApacheDubboClientApi {
/**
* findByArrayIdsAndName.
+ *
* @param dubboRequestBody dubboRequestBody
* @return DubboTest
*/
@@ -93,6 +101,7 @@ public interface ShenyuApacheDubboClientApi {
/**
* saveComplexBeanTest.
+ *
* @param complexBeanTest complexBeanTest
* @return DubboTest
*/
@@ -101,6 +110,7 @@ public interface ShenyuApacheDubboClientApi {
/**
* batchSave.
+ *
* @param dubboTestSaveRequest dubboTestSaveRequest
* @return DubboTest
*/
@@ -109,6 +119,7 @@ public interface ShenyuApacheDubboClientApi {
/**
* batchSaveAndNameAndId.
+ *
* @param dubboTestSaveRequest dubboTestSaveRequest
* @return DubboTest
*/
diff --git a/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-dubbo/shenyu-examples-sdk-apache-dubbo-consumer/src/main/java/org/apache/shenyu/examples/sdk/apache/dubbo/consumer/dto/DubboRequestBody.java b/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-dubbo/shenyu-examples-sdk-apache-dubbo-consumer/src/main/java/org/apache/shenyu/examples/sdk/apache/dubbo/consumer/dto/DubboRequestBody.java
index 0f2081e5d..858433020 100644
--- a/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-dubbo/shenyu-examples-sdk-apache-dubbo-consumer/src/main/java/org/apache/shenyu/examples/sdk/apache/dubbo/consumer/dto/DubboRequestBody.java
+++ b/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-dubbo/shenyu-examples-sdk-apache-dubbo-consumer/src/main/java/org/apache/shenyu/examples/sdk/apache/dubbo/consumer/dto/DubboRequestBody.java
@@ -21,6 +21,7 @@ import java.util.List;
/**
* DubboRequestBody.
+ *
* @param <T> type
*/
public class DubboRequestBody<T> {
@@ -38,6 +39,7 @@ public class DubboRequestBody<T> {
/**
* getIds.
+ *
* @return list
*/
public List<T> getIds() {
@@ -46,6 +48,7 @@ public class DubboRequestBody<T> {
/**
* setIds.
+ *
* @param ids ids
*/
public void setIds(final List<T> ids) {
@@ -54,6 +57,7 @@ public class DubboRequestBody<T> {
/**
* getName.
+ *
* @return name
*/
public String getName() {
@@ -62,6 +66,7 @@ public class DubboRequestBody<T> {
/**
* setName.
+ *
* @param name name
*/
public void setName(final String name) {
diff --git a/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-dubbo/shenyu-examples-sdk-apache-dubbo-consumer/src/main/java/org/apache/shenyu/examples/sdk/apache/dubbo/consumer/dto/DubboTestSaveRequest.java b/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-dubbo/shenyu-examples-sdk-apache-dubbo-consumer/src/main/java/org/apache/shenyu/examples/sdk/apache/dubbo/consumer/dto/DubboTestSaveRequest.java
index ab1f7c2cb..aab410de2 100644
--- a/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-dubbo/shenyu-examples-sdk-apache-dubbo-consumer/src/main/java/org/apache/shenyu/examples/sdk/apache/dubbo/consumer/dto/DubboTestSaveRequest.java
+++ b/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-dubbo/shenyu-examples-sdk-apache-dubbo-consumer/src/main/java/org/apache/shenyu/examples/sdk/apache/dubbo/consumer/dto/DubboTestSaveRequest.java
@@ -17,9 +17,10 @@
package org.apache.shenyu.examples.sdk.apache.dubbo.consumer.dto;
-import java.util.List;
import org.apache.shenyu.examples.dubbo.api.entity.DubboTest;
+import java.util.List;
+
/**
* DubboTestSaveRequest.
*/
@@ -42,6 +43,7 @@ public class DubboTestSaveRequest {
/**
* getId.
+ *
* @return id
*/
public String getId() {
@@ -50,6 +52,7 @@ public class DubboTestSaveRequest {
/**
* setId.
+ *
* @param id id
*/
public void setId(final String id) {
@@ -58,6 +61,7 @@ public class DubboTestSaveRequest {
/**
* getName.
+ *
* @return name
*/
public String getName() {
@@ -66,6 +70,7 @@ public class DubboTestSaveRequest {
/**
* setName.
+ *
* @param name name
*/
public void setName(final String name) {
@@ -74,6 +79,7 @@ public class DubboTestSaveRequest {
/**
* getDubboTestList.
+ *
* @return list
*/
public List<DubboTest> getDubboTestList() {
@@ -82,6 +88,7 @@ public class DubboTestSaveRequest {
/**
* setDubboTestList.
+ *
* @param dubboTestList dubboTestList
*/
public void setDubboTestList(final List<DubboTest> dubboTestList) {
diff --git a/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-dubbo/shenyu-examples-sdk-apache-dubbo-consumer/src/main/java/org/apache/shenyu/examples/sdk/apache/dubbo/consumer/impl/ShenyuApacheDubboClientAplFallBack.java b/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-dubbo/shenyu-examples-sdk-apache-dubbo-consumer/src/main/java/org/apache/shenyu/examples/sdk/apache/dubbo/consumer/impl/ShenyuApacheDubboClientAplFallBack.java
new file mode 100644
index 000000000..14d69bd5e
--- /dev/null
+++ b/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-dubbo/shenyu-examples-sdk-apache-dubbo-consumer/src/main/java/org/apache/shenyu/examples/sdk/apache/dubbo/consumer/impl/ShenyuApacheDubboClientAplFallBack.java
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.examples.sdk.apache.dubbo.consumer.impl;
+
+import org.apache.shenyu.examples.dubbo.api.entity.ComplexBeanTest;
+import org.apache.shenyu.examples.dubbo.api.entity.DubboTest;
+import org.apache.shenyu.examples.dubbo.api.entity.ListResp;
+import org.apache.shenyu.examples.sdk.apache.dubbo.consumer.api.ShenyuApacheDubboClientApi;
+import org.apache.shenyu.examples.sdk.apache.dubbo.consumer.dto.DubboRequestBody;
+import org.apache.shenyu.examples.sdk.apache.dubbo.consumer.dto.DubboTestSaveRequest;
+import org.springframework.stereotype.Component;
+
+import java.util.List;
+
+@Component
+public class ShenyuApacheDubboClientAplFallBack implements ShenyuApacheDubboClientApi {
+
+ @Override
+ public DubboTest findAll() {
+
+ DubboTest dubboTest = new DubboTest();
+ dubboTest.setId("a");
+ dubboTest.setName("fallback");
+ return dubboTest;
+ }
+
+ @Override
+ public ListResp findList() {
+ return null;
+ }
+
+ @Override
+ public DubboTest findById(final String id) {
+ return null;
+ }
+
+ @Override
+ public DubboTest insert(final DubboTest dubboTest) {
+ return null;
+ }
+
+ @Override
+ public DubboTest findByListId(final List<String> ids) {
+ return null;
+ }
+
+ @Override
+ public DubboTest findByIdsAndName(final DubboRequestBody dubboRequestBody) {
+ return null;
+ }
+
+ @Override
+ public DubboTest findByArrayIdsAndName(final DubboRequestBody dubboRequestBody) {
+ return null;
+ }
+
+ @Override
+ public DubboTest saveComplexBeanTest(final ComplexBeanTest complexBeanTest) {
+ return null;
+ }
+
+ @Override
+ public DubboTest batchSave(final DubboTestSaveRequest dubboTestSaveRequest) {
+ return null;
+ }
+
+ @Override
+ public DubboTest batchSaveAndNameAndId(final DubboTestSaveRequest dubboTestSaveRequest) {
+ return null;
+ }
+}
diff --git a/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-springcloud/shenyu-examples-sdk-springcloud-consumer/src/main/java/org/apache/shenyu/examples/sdk/springcloud/consumer/api/ShenyuSpringCloudClientApi.java b/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-springcloud/shenyu-examples-sdk-springcloud-consumer/src/main/java/org/apache/shenyu/examples/sdk/springcloud/consumer/api/ShenyuSpringCloudClientApi.java
index 3d65f3fd8..3f6fe580e 100644
--- a/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-springcloud/shenyu-examples-sdk-springcloud-consumer/src/main/java/org/apache/shenyu/examples/sdk/springcloud/consumer/api/ShenyuSpringCloudClientApi.java
+++ b/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-springcloud/shenyu-examples-sdk-springcloud-consumer/src/main/java/org/apache/shenyu/examples/sdk/springcloud/consumer/api/ShenyuSpringCloudClientApi.java
@@ -18,6 +18,7 @@
package org.apache.shenyu.examples.sdk.springcloud.consumer.api;
import org.apache.shenyu.examples.sdk.springcloud.consumer.dto.OrderDTO;
+import org.apache.shenyu.examples.sdk.springcloud.consumer.impl.ShenyuSpringCloudClientApiFallbackFactory;
import org.apache.shenyu.sdk.spring.ShenyuClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@@ -25,7 +26,9 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
-@ShenyuClient(contextId = "SHENYU-BOOTSTRAP", name = "ShenyuSpringCloudClientApi")
+@ShenyuClient(contextId = "SHENYU-BOOTSTRAP",
+ name = "ShenyuSpringCloudClientApi",
+ fallbackFactory = ShenyuSpringCloudClientApiFallbackFactory.class)
public interface ShenyuSpringCloudClientApi {
/**
diff --git a/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-springcloud/shenyu-examples-sdk-springcloud-consumer/src/main/java/org/apache/shenyu/examples/sdk/springcloud/consumer/impl/ShenyuSpringCloudClientApiFallback.java b/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-springcloud/shenyu-examples-sdk-springcloud-consumer/src/main/java/org/apache/shenyu/examples/sdk/springcloud/consumer/impl/ShenyuSpringCloudClientApiFallback.java
new file mode 100644
index 000000000..5aa1143f0
--- /dev/null
+++ b/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-springcloud/shenyu-examples-sdk-springcloud-consumer/src/main/java/org/apache/shenyu/examples/sdk/springcloud/consumer/impl/ShenyuSpringCloudClientApiFallback.java
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.examples.sdk.springcloud.consumer.impl;
+
+import org.apache.shenyu.examples.sdk.springcloud.consumer.api.ShenyuSpringCloudClientApi;
+import org.apache.shenyu.examples.sdk.springcloud.consumer.dto.OrderDTO;
+import org.springframework.stereotype.Component;
+
+@Component
+public class ShenyuSpringCloudClientApiFallback implements ShenyuSpringCloudClientApi {
+
+ @Override
+ public OrderDTO save(final OrderDTO orderDTO) {
+ return null;
+ }
+
+ @Override
+ public OrderDTO findById(final String id) {
+
+ OrderDTO orderDTO = new OrderDTO();
+ orderDTO.setId("1");
+ orderDTO.setName("fallback");
+ return orderDTO;
+ }
+
+ @Override
+ public OrderDTO getPathVariable(final String id, final String name) {
+ return null;
+ }
+
+ @Override
+ public OrderDTO testRestFul(final String id) {
+ return null;
+ }
+}
diff --git a/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-springcloud/shenyu-examples-sdk-springcloud-consumer/src/main/java/org/apache/shenyu/examples/sdk/springcloud/consumer/impl/ShenyuSpringCloudClientApiFallbackFactory.java b/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-springcloud/shenyu-examples-sdk-springcloud-consumer/src/main/java/org/apache/shenyu/examples/sdk/springcloud/consumer/impl/ShenyuSpringCloudClientApiFallbackFactory.java
new file mode 100644
index 000000000..e7d5b4d1b
--- /dev/null
+++ b/shenyu-examples/shenyu-examples-sdk/shenyu-examples-sdk-springcloud/shenyu-examples-sdk-springcloud-consumer/src/main/java/org/apache/shenyu/examples/sdk/springcloud/consumer/impl/ShenyuSpringCloudClientApiFallbackFactory.java
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.examples.sdk.springcloud.consumer.impl;
+
+import org.apache.shenyu.examples.sdk.springcloud.consumer.api.ShenyuSpringCloudClientApi;
+import org.apache.shenyu.examples.sdk.springcloud.consumer.dto.OrderDTO;
+import org.apache.shenyu.sdk.spring.FallbackFactory;
+import org.springframework.stereotype.Component;
+
+@Component
+public class ShenyuSpringCloudClientApiFallbackFactory implements FallbackFactory<ShenyuSpringCloudClientApi> {
+
+ @Override
+ public ShenyuSpringCloudClientApi create(final Throwable cause) {
+
+ return new ShenyuSpringCloudClientApi() {
+ @Override
+ public OrderDTO save(final OrderDTO orderDTO) {
+ return null;
+ }
+
+ @Override
+ public OrderDTO findById(final String id) {
+
+ OrderDTO orderDTO = new OrderDTO();
+ orderDTO.setId("1");
+ orderDTO.setName("fallback factory");
+ return orderDTO;
+ }
+
+ @Override
+ public OrderDTO getPathVariable(final String id, final String name) {
+ return null;
+ }
+
+ @Override
+ public OrderDTO testRestFul(final String id) {
+ return null;
+ }
+ };
+ }
+}
diff --git a/shenyu-integrated-test/shenyu-integrated-test-sdk-alibaba-dubbo/pom.xml b/shenyu-integrated-test/shenyu-integrated-test-sdk-alibaba-dubbo/pom.xml
index 98cb4c678..8da854dce 100644
--- a/shenyu-integrated-test/shenyu-integrated-test-sdk-alibaba-dubbo/pom.xml
+++ b/shenyu-integrated-test/shenyu-integrated-test-sdk-alibaba-dubbo/pom.xml
@@ -111,7 +111,7 @@
</execution>
</executions>
<configuration>
- <mainClass>org.apache.shenyu.integrated.test.apache.dubbo.SdkAlibabaDubboIntegratedBootstrap
+ <mainClass>org.apache.shenyu.integrated.test.alibaba.dubbo.SdkAlibabaDubboIntegratedBootstrap
</mainClass>
<executable>true</executable>
</configuration>
diff --git a/shenyu-integrated-test/shenyu-integrated-test-sdk-alibaba-dubbo/src/main/java/org/apache/shenyu/integrated/test/apache/dubbo/SdkAlibabaDubboIntegratedBootstrap.java b/shenyu-integrated-test/shenyu-integrated-test-sdk-alibaba-dubbo/src/main/java/org/apache/shenyu/integrated/test/alibaba/dubbo/SdkAlibabaDubboIntegratedBootstrap.java
similarity index 95%
copy from shenyu-integrated-test/shenyu-integrated-test-sdk-alibaba-dubbo/src/main/java/org/apache/shenyu/integrated/test/apache/dubbo/SdkAlibabaDubboIntegratedBootstrap.java
copy to shenyu-integrated-test/shenyu-integrated-test-sdk-alibaba-dubbo/src/main/java/org/apache/shenyu/integrated/test/alibaba/dubbo/SdkAlibabaDubboIntegratedBootstrap.java
index 1a21a611a..1ca6aea67 100644
--- a/shenyu-integrated-test/shenyu-integrated-test-sdk-alibaba-dubbo/src/main/java/org/apache/shenyu/integrated/test/apache/dubbo/SdkAlibabaDubboIntegratedBootstrap.java
+++ b/shenyu-integrated-test/shenyu-integrated-test-sdk-alibaba-dubbo/src/main/java/org/apache/shenyu/integrated/test/alibaba/dubbo/SdkAlibabaDubboIntegratedBootstrap.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shenyu.integrated.test.apache.dubbo;
+package org.apache.shenyu.integrated.test.alibaba.dubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
diff --git a/shenyu-integrated-test/shenyu-integrated-test-sdk-alibaba-dubbo/src/test/java/org/apache/shenyu/integrated/test/alibaba/dubbo/ApacheDubboPluginTest.java b/shenyu-integrated-test/shenyu-integrated-test-sdk-alibaba-dubbo/src/test/java/org/apache/shenyu/integrated/test/alibaba/dubbo/ApacheDubboPluginTest.java
index b9408c2ec..a7224b309 100644
--- a/shenyu-integrated-test/shenyu-integrated-test-sdk-alibaba-dubbo/src/test/java/org/apache/shenyu/integrated/test/alibaba/dubbo/ApacheDubboPluginTest.java
+++ b/shenyu-integrated-test/shenyu-integrated-test-sdk-alibaba-dubbo/src/test/java/org/apache/shenyu/integrated/test/alibaba/dubbo/ApacheDubboPluginTest.java
@@ -43,4 +43,11 @@ public class ApacheDubboPluginTest extends AbstractPluginDataInit {
assertEquals("hello world shenyu Apache, findAll", dubboTest.getName());
}
+ @Test
+ public void testFindAllFallBck() throws IOException {
+
+ DubboTest dubboTest = HttpHelper.INSTANCE.getHttpService("http://localhost:8899/sdk/dubbo/findAll", null, DubboTest.class);
+ assertEquals("fallback", dubboTest.getName());
+ }
+
}
diff --git a/shenyu-integrated-test/shenyu-integrated-test-sdk-apache-dubbo/src/test/java/org/apache/shenyu/integrated/test/alibaba/dubbo/ApacheDubboPluginTest.java b/shenyu-integrated-test/shenyu-integrated-test-sdk-apache-dubbo/src/test/java/org/apache/shenyu/integrated/test/apache/dubbo/ApacheDubboPluginTest.java
similarity index 85%
rename from shenyu-integrated-test/shenyu-integrated-test-sdk-apache-dubbo/src/test/java/org/apache/shenyu/integrated/test/alibaba/dubbo/ApacheDubboPluginTest.java
rename to shenyu-integrated-test/shenyu-integrated-test-sdk-apache-dubbo/src/test/java/org/apache/shenyu/integrated/test/apache/dubbo/ApacheDubboPluginTest.java
index b9408c2ec..920975c42 100644
--- a/shenyu-integrated-test/shenyu-integrated-test-sdk-apache-dubbo/src/test/java/org/apache/shenyu/integrated/test/alibaba/dubbo/ApacheDubboPluginTest.java
+++ b/shenyu-integrated-test/shenyu-integrated-test-sdk-apache-dubbo/src/test/java/org/apache/shenyu/integrated/test/apache/dubbo/ApacheDubboPluginTest.java
@@ -15,9 +15,8 @@
* limitations under the License.
*/
-package org.apache.shenyu.integrated.test.alibaba.dubbo;
+package org.apache.shenyu.integrated.test.apache.dubbo;
-import java.io.IOException;
import org.apache.shenyu.common.enums.PluginEnum;
import org.apache.shenyu.integratedtest.common.AbstractPluginDataInit;
import org.apache.shenyu.integratedtest.common.dto.DubboTest;
@@ -25,6 +24,8 @@ import org.apache.shenyu.integratedtest.common.helper.HttpHelper;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
+import java.io.IOException;
+
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -43,4 +44,11 @@ public class ApacheDubboPluginTest extends AbstractPluginDataInit {
assertEquals("hello world shenyu Apache, findAll", dubboTest.getName());
}
+ @Test
+ public void testFindAllFallBck() throws IOException {
+
+ DubboTest dubboTest = HttpHelper.INSTANCE.getHttpService("http://localhost:8899/sdk/dubbo/findAll", null, DubboTest.class);
+ assertEquals("fallback", dubboTest.getName());
+ }
+
}
diff --git a/shenyu-integrated-test/shenyu-integrated-test-spring-cloud/src/test/java/org/apache/shenyu/integratedtest/springcloud/SpringCloudPluginTest.java b/shenyu-integrated-test/shenyu-integrated-test-spring-cloud/src/test/java/org/apache/shenyu/integratedtest/springcloud/SpringCloudPluginTest.java
index 2d61bed60..17cb61bf5 100644
--- a/shenyu-integrated-test/shenyu-integrated-test-spring-cloud/src/test/java/org/apache/shenyu/integratedtest/springcloud/SpringCloudPluginTest.java
+++ b/shenyu-integrated-test/shenyu-integrated-test-spring-cloud/src/test/java/org/apache/shenyu/integratedtest/springcloud/SpringCloudPluginTest.java
@@ -26,12 +26,12 @@ import org.junit.jupiter.api.Test;
import java.io.IOException;
+import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
-import static org.hamcrest.MatcherAssert.assertThat;
public class SpringCloudPluginTest extends AbstractPluginDataInit {
-
+
@BeforeAll
public static void setup() throws IOException {
String pluginResult = initPlugin(PluginEnum.SPRING_CLOUD.getName(), "");
diff --git a/shenyu-integrated-test/shenyu-integrated-test-sdk-alibaba-dubbo/src/main/java/org/apache/shenyu/integrated/test/apache/dubbo/SdkAlibabaDubboIntegratedBootstrap.java b/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/NoFallbackAvailableException.java
similarity index 60%
rename from shenyu-integrated-test/shenyu-integrated-test-sdk-alibaba-dubbo/src/main/java/org/apache/shenyu/integrated/test/apache/dubbo/SdkAlibabaDubboIntegratedBootstrap.java
rename to shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/NoFallbackAvailableException.java
index 1a21a611a..60e6b48e4 100644
--- a/shenyu-integrated-test/shenyu-integrated-test-sdk-alibaba-dubbo/src/main/java/org/apache/shenyu/integrated/test/apache/dubbo/SdkAlibabaDubboIntegratedBootstrap.java
+++ b/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/NoFallbackAvailableException.java
@@ -15,23 +15,12 @@
* limitations under the License.
*/
-package org.apache.shenyu.integrated.test.apache.dubbo;
+package org.apache.shenyu.sdk.spring;
-import org.springframework.boot.SpringApplication;
-import org.springframework.boot.autoconfigure.SpringBootApplication;
+public class NoFallbackAvailableException extends RuntimeException {
-/**
- * The type sdk Alibaba dubbo integrated bootstrap.
- */
-@SpringBootApplication
-public class SdkAlibabaDubboIntegratedBootstrap {
+ public NoFallbackAvailableException(final String message, final Throwable cause) {
- /**
- * The entry point of application.
- *
- * @param args the input arguments
- */
- public static void main(final String[] args) {
- SpringApplication.run(SdkAlibabaDubboIntegratedBootstrap.class);
+ super(message, cause);
}
}
diff --git a/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/proxy/ShenyuClientInvocationHandler.java b/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/proxy/ShenyuClientInvocationHandler.java
index 277bcc161..c7db70e83 100644
--- a/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/proxy/ShenyuClientInvocationHandler.java
+++ b/shenyu-sdk/shenyu-sdk-spring/src/main/java/org/apache/shenyu/sdk/spring/proxy/ShenyuClientInvocationHandler.java
@@ -20,6 +20,8 @@ package org.apache.shenyu.sdk.spring.proxy;
import org.apache.shenyu.common.exception.ShenyuException;
import org.apache.shenyu.sdk.core.client.ShenyuSdkClient;
import org.apache.shenyu.sdk.core.common.RequestTemplate;
+import org.apache.shenyu.sdk.spring.FallbackFactory;
+import org.apache.shenyu.sdk.spring.NoFallbackAvailableException;
import org.apache.shenyu.sdk.spring.ShenyuClient;
import org.apache.shenyu.sdk.spring.ShenyuClientFactoryBean;
import org.apache.shenyu.sdk.spring.factory.AnnotatedParameterProcessor;
@@ -64,11 +66,24 @@ public class ShenyuClientInvocationHandler implements InvocationHandler {
@Override
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
+
ShenyuClientMethodHandler handler = methodHandlerMap.get(method);
if (ObjectUtils.isEmpty(handler)) {
throw new ShenyuException(String.format("the method cannot be called, please check the annotation and configuration, method %s", method.getName()));
}
- return handler.invoke(args);
+ FallbackFactory<?> fallbackFactory = getFallbackFactory(shenyuClientFactoryBean.getFallback(),
+ shenyuClientFactoryBean.getFallbackFactory());
+ Object result;
+ try {
+ result = handler.invoke(args);
+ } catch (Throwable throwable) {
+ if (fallbackFactory == null) {
+ throw new NoFallbackAvailableException("No fallback available.", throwable);
+ }
+ Object fallback = fallbackFactory.create(throwable);
+ result = method.invoke(fallback, args);
+ }
+ return result;
}
private void buildMethodHandlerMap(final Class<?> apiClass, final ShenyuClient shenyuClient) {
@@ -100,4 +115,23 @@ public class ShenyuClientInvocationHandler implements InvocationHandler {
}
return result;
}
+
+ private FallbackFactory<?> getFallbackFactory(final Class<?> fallback, final Class<?> fallbackFactory) {
+
+ if (fallback != void.class) {
+ Object fallbackInstance = applicationContext.getBean(fallback);
+ if (fallbackInstance == null) {
+ throw new IllegalStateException("No fallback instance found for shenyu client");
+ }
+ return new FallbackFactory.Default<>(fallbackInstance);
+ } else if (fallbackFactory != void.class) {
+ FallbackFactory<?> fallbackFactoryInstance = (FallbackFactory<?>) applicationContext.getBean(fallbackFactory);
+ if (fallbackFactoryInstance == null) {
+ throw new IllegalStateException("No fallbackFactory instance found for shenyu client");
+ }
+ return fallbackFactoryInstance;
+ } else {
+ return null;
+ }
+ }
}