From 062deb4bd17b5b97bcb323e97ba49d64d9263da8 Mon Sep 17 00:00:00 2001
From: eye-gu <734164350@qq.com>
Date: Tue, 21 Jan 2025 19:27:34 +0800
Subject: [PATCH] [type:feature] support dubbo protobuf serialization
---
.../dubbo/ApacheDubboServiceBeanListener.java | 1 +
.../client/dubbo/common/dto/DubboRpcExt.java | 39 +++++++++-
shenyu-examples/shenyu-examples-dubbo/pom.xml | 1 +
.../impl/DubboProtobufServiceImpl.java | 47 +++++++++++
.../xml/impl/DubboProtobufServiceImpl.java | 47 +++++++++++
.../src/main/resources/spring-dubbo.xml | 1 +
.../impl/DubboProtobufServiceImpl.java | 47 +++++++++++
.../src/main/resources/spring-dubbo.xml | 1 +
.../shenyu-examples-dubbo-api/pom.xml | 77 +++++++++++++++++++
.../src/main/proto/DubboTestProto.proto | 38 +++++++++
.../dubbo/cache/ApacheDubboConfigCache.java | 4 +
.../dubbo/proxy/ApacheDubboProxyService.java | 4 +
.../plugin/dubbo/common/cache/DubboParam.java | 23 ++++++
13 files changed, 329 insertions(+), 1 deletion(-)
create mode 100644 shenyu-examples/shenyu-examples-dubbo/shenyu-examples-apache-dubbo-service-annotation/src/main/java/org/apache/shenyu/examples/apache/dubbo/service/annotation/impl/DubboProtobufServiceImpl.java
create mode 100644 shenyu-examples/shenyu-examples-dubbo/shenyu-examples-apache-dubbo-service-xml/src/main/java/org/apache/shenyu/examples/apache/dubbo/service/xml/impl/DubboProtobufServiceImpl.java
create mode 100644 shenyu-examples/shenyu-examples-dubbo/shenyu-examples-apache-dubbo-service/src/main/java/org/apache/shenyu/examples/apache/dubbo/service/impl/DubboProtobufServiceImpl.java
create mode 100644 shenyu-examples/shenyu-examples-dubbo/shenyu-examples-dubbo-api/src/main/proto/DubboTestProto.proto
diff --git a/shenyu-client/shenyu-client-dubbo/shenyu-client-apache-dubbo/src/main/java/org/apache/shenyu/client/apache/dubbo/ApacheDubboServiceBeanListener.java b/shenyu-client/shenyu-client-dubbo/shenyu-client-apache-dubbo/src/main/java/org/apache/shenyu/client/apache/dubbo/ApacheDubboServiceBeanListener.java
index 8c2378a40913..1f20a01a127e 100644
--- a/shenyu-client/shenyu-client-dubbo/shenyu-client-apache-dubbo/src/main/java/org/apache/shenyu/client/apache/dubbo/ApacheDubboServiceBeanListener.java
+++ b/shenyu-client/shenyu-client-dubbo/shenyu-client-apache-dubbo/src/main/java/org/apache/shenyu/client/apache/dubbo/ApacheDubboServiceBeanListener.java
@@ -231,6 +231,7 @@ private String buildRpcExt(final ServiceBean> serviceBean, final String method
.sent(Optional.ofNullable(serviceBean.getSent()).orElse(Boolean.FALSE))
.cluster(StringUtils.isNotEmpty(serviceBean.getCluster()) ? serviceBean.getCluster() : Constants.DEFAULT_CLUSTER)
.url("")
+ .serialization(serviceBean.getSerialization())
.build();
// set method config: loadbalance,retries,timeout,sent
Optional.ofNullable(serviceBean.getMethods()).orElse(Collections.emptyList()).stream()
diff --git a/shenyu-client/shenyu-client-dubbo/shenyu-client-dubbo-common/src/main/java/org/apache/shenyu/client/dubbo/common/dto/DubboRpcExt.java b/shenyu-client/shenyu-client-dubbo/shenyu-client-dubbo-common/src/main/java/org/apache/shenyu/client/dubbo/common/dto/DubboRpcExt.java
index 76774fbdd4c9..3cc1b840bad5 100644
--- a/shenyu-client/shenyu-client-dubbo/shenyu-client-dubbo-common/src/main/java/org/apache/shenyu/client/dubbo/common/dto/DubboRpcExt.java
+++ b/shenyu-client/shenyu-client-dubbo/shenyu-client-dubbo-common/src/main/java/org/apache/shenyu/client/dubbo/common/dto/DubboRpcExt.java
@@ -44,6 +44,8 @@ public class DubboRpcExt implements Serializable {
private String protocol;
+ private String serialization;
+
/**
* constructor without parameter.
*/
@@ -71,7 +73,8 @@ public DubboRpcExt(final String group,
final String url,
final Boolean sent,
final String cluster,
- final String protocol) {
+ final String protocol,
+ final String serialization) {
this.group = group;
this.version = version;
this.loadbalance = loadbalance;
@@ -81,6 +84,7 @@ public DubboRpcExt(final String group,
this.sent = sent;
this.cluster = cluster;
this.protocol = protocol;
+ this.serialization = serialization;
}
/**
@@ -245,6 +249,24 @@ public void setProtocol(final String protocol) {
this.protocol = protocol;
}
+ /**
+ * get serialization.
+ *
+ * @return serialization
+ */
+ public String getSerialization() {
+ return serialization;
+ }
+
+ /**
+ * set serialization.
+ *
+ * @param serialization serialization
+ */
+ public void setSerialization(final String serialization) {
+ this.serialization = serialization;
+ }
+
@Override
public String toString() {
return "DubboRpcExt{"
@@ -257,6 +279,7 @@ public String toString() {
+ ", sent=" + sent
+ ", cluster='" + cluster + '\''
+ ", protocol='" + protocol + '\''
+ + ", serialization='" + serialization + '\''
+ '}';
}
@@ -292,6 +315,8 @@ public static final class Builder {
private String protocol;
+ private String serialization;
+
/**
* constructor without parameter.
*/
@@ -397,6 +422,17 @@ public Builder protocol(final String protocol) {
return this;
}
+ /**
+ * set serialization.
+ *
+ * @param serialization serialization
+ * @return Builder
+ */
+ public Builder serialization(final String serialization) {
+ this.serialization = serialization;
+ return this;
+ }
+
/**
* build DubboRpcExt.
*
@@ -413,6 +449,7 @@ public DubboRpcExt build() {
dubboRpcExt.setSent(sent);
dubboRpcExt.setCluster(cluster);
dubboRpcExt.setProtocol(protocol);
+ dubboRpcExt.setSerialization(serialization);
return dubboRpcExt;
}
}
diff --git a/shenyu-examples/shenyu-examples-dubbo/pom.xml b/shenyu-examples/shenyu-examples-dubbo/pom.xml
index e4e3cec71675..b97dee08561f 100644
--- a/shenyu-examples/shenyu-examples-dubbo/pom.xml
+++ b/shenyu-examples/shenyu-examples-dubbo/pom.xml
@@ -41,6 +41,7 @@
2.2.4
3.2.14
3.9.2
+ 3.25.5
diff --git a/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-apache-dubbo-service-annotation/src/main/java/org/apache/shenyu/examples/apache/dubbo/service/annotation/impl/DubboProtobufServiceImpl.java b/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-apache-dubbo-service-annotation/src/main/java/org/apache/shenyu/examples/apache/dubbo/service/annotation/impl/DubboProtobufServiceImpl.java
new file mode 100644
index 000000000000..e589bd0c5331
--- /dev/null
+++ b/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-apache-dubbo-service-annotation/src/main/java/org/apache/shenyu/examples/apache/dubbo/service/annotation/impl/DubboProtobufServiceImpl.java
@@ -0,0 +1,47 @@
+/*
+ * 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.apache.dubbo.service.annotation.impl;
+
+import com.google.protobuf.Empty;
+import org.apache.dubbo.config.annotation.DubboService;
+import org.apache.shenyu.client.dubbo.common.annotation.ShenyuDubboClient;
+import org.apache.shenyu.examples.dubbo.api.service.DubboProtobufService;
+import org.apache.shenyu.examples.dubbo.api.service.DubboTestProtobuf;
+
+@DubboService(serialization = "protobuf")
+@ShenyuDubboClient(value = "/protobufSerialization")
+public class DubboProtobufServiceImpl implements DubboProtobufService {
+
+ @ShenyuDubboClient("/insert")
+ @Override
+ public DubboTestProtobuf insert(final DubboTestProtobuf request) {
+ return request;
+ }
+
+ @ShenyuDubboClient("/update")
+ @Override
+ public Empty update(final DubboTestProtobuf request) {
+ return Empty.getDefaultInstance();
+ }
+
+ @ShenyuDubboClient("/findOne")
+ @Override
+ public DubboTestProtobuf findOne(final Empty request) {
+ return DubboTestProtobuf.newBuilder().setId("1").setName("test1").build();
+ }
+}
diff --git a/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-apache-dubbo-service-xml/src/main/java/org/apache/shenyu/examples/apache/dubbo/service/xml/impl/DubboProtobufServiceImpl.java b/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-apache-dubbo-service-xml/src/main/java/org/apache/shenyu/examples/apache/dubbo/service/xml/impl/DubboProtobufServiceImpl.java
new file mode 100644
index 000000000000..6084bc2470f0
--- /dev/null
+++ b/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-apache-dubbo-service-xml/src/main/java/org/apache/shenyu/examples/apache/dubbo/service/xml/impl/DubboProtobufServiceImpl.java
@@ -0,0 +1,47 @@
+/*
+ * 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.apache.dubbo.service.xml.impl;
+
+import com.google.protobuf.Empty;
+import org.apache.shenyu.client.dubbo.common.annotation.ShenyuDubboClient;
+import org.apache.shenyu.examples.dubbo.api.service.DubboProtobufService;
+import org.apache.shenyu.examples.dubbo.api.service.DubboTestProtobuf;
+import org.springframework.stereotype.Service;
+
+@Service("dubboProtobufService")
+@ShenyuDubboClient(value = "/protobufSerialization")
+public class DubboProtobufServiceImpl implements DubboProtobufService {
+
+ @ShenyuDubboClient("/insert")
+ @Override
+ public DubboTestProtobuf insert(final DubboTestProtobuf request) {
+ return request;
+ }
+
+ @ShenyuDubboClient("/update")
+ @Override
+ public Empty update(final DubboTestProtobuf request) {
+ return Empty.getDefaultInstance();
+ }
+
+ @ShenyuDubboClient("/findOne")
+ @Override
+ public DubboTestProtobuf findOne(final Empty request) {
+ return DubboTestProtobuf.newBuilder().setId("1").setName("test1").build();
+ }
+}
diff --git a/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-apache-dubbo-service-xml/src/main/resources/spring-dubbo.xml b/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-apache-dubbo-service-xml/src/main/resources/spring-dubbo.xml
index 8ffcde0a9f99..3b734eda36fe 100644
--- a/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-apache-dubbo-service-xml/src/main/resources/spring-dubbo.xml
+++ b/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-apache-dubbo-service-xml/src/main/resources/spring-dubbo.xml
@@ -32,5 +32,6 @@
+
diff --git a/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-apache-dubbo-service/src/main/java/org/apache/shenyu/examples/apache/dubbo/service/impl/DubboProtobufServiceImpl.java b/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-apache-dubbo-service/src/main/java/org/apache/shenyu/examples/apache/dubbo/service/impl/DubboProtobufServiceImpl.java
new file mode 100644
index 000000000000..da96e87072c1
--- /dev/null
+++ b/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-apache-dubbo-service/src/main/java/org/apache/shenyu/examples/apache/dubbo/service/impl/DubboProtobufServiceImpl.java
@@ -0,0 +1,47 @@
+/*
+ * 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.apache.dubbo.service.impl;
+
+import com.google.protobuf.Empty;
+import org.apache.shenyu.client.dubbo.common.annotation.ShenyuDubboClient;
+import org.apache.shenyu.examples.dubbo.api.service.DubboProtobufService;
+import org.apache.shenyu.examples.dubbo.api.service.DubboTestProtobuf;
+import org.springframework.stereotype.Service;
+
+@ShenyuDubboClient(value = "/protobufSerialization")
+@Service("dubboProtobufService")
+public class DubboProtobufServiceImpl implements DubboProtobufService {
+
+ @ShenyuDubboClient("/insert")
+ @Override
+ public DubboTestProtobuf insert(final DubboTestProtobuf request) {
+ return request;
+ }
+
+ @ShenyuDubboClient("/update")
+ @Override
+ public Empty update(final DubboTestProtobuf request) {
+ return Empty.getDefaultInstance();
+ }
+
+ @ShenyuDubboClient("/findOne")
+ @Override
+ public DubboTestProtobuf findOne(final Empty request) {
+ return DubboTestProtobuf.newBuilder().setId("1").setName("test1").build();
+ }
+}
diff --git a/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-apache-dubbo-service/src/main/resources/spring-dubbo.xml b/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-apache-dubbo-service/src/main/resources/spring-dubbo.xml
index df396b7b989f..cc4487d6bb7f 100644
--- a/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-apache-dubbo-service/src/main/resources/spring-dubbo.xml
+++ b/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-apache-dubbo-service/src/main/resources/spring-dubbo.xml
@@ -30,4 +30,5 @@
+
diff --git a/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-dubbo-api/pom.xml b/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-dubbo-api/pom.xml
index 63e4c5bb9faa..247485578058 100644
--- a/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-dubbo-api/pom.xml
+++ b/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-dubbo-api/pom.xml
@@ -27,7 +27,84 @@
4.0.0
shenyu-examples-dubbo-api
+
+
+ org.apache.dubbo.extensions
+ dubbo-serialization-protobuf
+ 3.2.0
+
+
+ org.apache.dubbo
+ dubbo
+ ${apache.dubbo.version}
+ provided
+
+
+ com.google.protobuf
+ protobuf-java-util
+ ${protobuf.version}
+
+
+ com.google.protobuf
+ protobuf-java
+ ${protobuf.version}
+
+
+
shenyu-examples-dubbo-api
+
+
+
+ kr.motd.maven
+ os-maven-plugin
+ 1.7.1
+
+
+
+
+ org.xolstice.maven.plugins
+ protobuf-maven-plugin
+ 0.6.1
+
+ com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}
+ ${project.build.directory}/generated-sources/protobuf/java
+
+
+ dubbo
+ org.apache.dubbo
+ dubbo-compiler
+ ${apache.dubbo.version}
+ org.apache.dubbo.gen.dubbo.Dubbo3Generator
+
+
+
+
+
+
+ compile
+ test-compile
+
+
+
+
+
+ org.codehaus.mojo
+ build-helper-maven-plugin
+
+
+ generate-sources
+
+ add-source
+
+
+
+
+
+
+
+
+
+
diff --git a/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-dubbo-api/src/main/proto/DubboTestProto.proto b/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-dubbo-api/src/main/proto/DubboTestProto.proto
new file mode 100644
index 000000000000..4357eeb37192
--- /dev/null
+++ b/shenyu-examples/shenyu-examples-dubbo/shenyu-examples-dubbo-api/src/main/proto/DubboTestProto.proto
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+syntax = "proto3";
+
+option java_multiple_files = true;
+option java_package = "org.apache.shenyu.examples.dubbo.api.service";
+package DubboTestProtobuf;
+
+import "google/protobuf/empty.proto";
+
+message DubboTestProtobuf {
+ string id = 1;
+ string name = 2;
+}
+
+service DubboProtobufService {
+ rpc insert(DubboTestProtobuf) returns (DubboTestProtobuf) {}
+
+ rpc findOne(google.protobuf.Empty) returns (DubboTestProtobuf) {}
+
+ rpc update(DubboTestProtobuf) returns (google.protobuf.Empty) {}
+}
diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/cache/ApacheDubboConfigCache.java b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/cache/ApacheDubboConfigCache.java
index 1aaf772eefc6..20d12d7aff62 100644
--- a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/cache/ApacheDubboConfigCache.java
+++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/cache/ApacheDubboConfigCache.java
@@ -28,6 +28,7 @@
import java.util.concurrent.ExecutionException;
import jakarta.annotation.Nonnull;
import org.apache.commons.lang3.StringUtils;
+import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ConsumerConfig;
import org.apache.dubbo.config.ReferenceConfig;
@@ -226,6 +227,9 @@ private ReferenceConfig buildReference(final MetaData metaData,
if (StringUtils.isNoneBlank(dubboParam.getLoadbalance())) {
reference.getParameters().put(Constants.DUBBO_LOAD_BALANCE, dubboParam.getLoadbalance());
}
+ if ("protobuf".equals(dubboParam.getSerialization())) {
+ reference.setGeneric(CommonConstants.GENERIC_SERIALIZATION_PROTOBUF);
+ }
// set dubbo sub protocol
Optional.ofNullable(dubboParam.getProtocol()).ifPresent(reference::setProtocol);
Optional.ofNullable(dubboParam.getTimeout()).ifPresent(reference::setTimeout);
diff --git a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/proxy/ApacheDubboProxyService.java b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/proxy/ApacheDubboProxyService.java
index e8d20be8721f..f52ffa2cbfa0 100644
--- a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/proxy/ApacheDubboProxyService.java
+++ b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/proxy/ApacheDubboProxyService.java
@@ -23,6 +23,7 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
+import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.service.GenericException;
@@ -31,6 +32,7 @@
import org.apache.shenyu.common.dto.MetaData;
import org.apache.shenyu.common.enums.ResultEnum;
import org.apache.shenyu.common.exception.ShenyuException;
+import org.apache.shenyu.common.utils.JsonUtils;
import org.apache.shenyu.common.utils.ParamCheckUtils;
import org.apache.shenyu.plugin.apache.dubbo.cache.ApacheDubboConfigCache;
import org.apache.shenyu.plugin.dubbo.common.param.DubboParamResolveService;
@@ -78,6 +80,8 @@ public Mono