From 70245a2805cdbe6614ebf8012124e8472e8a40e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?lg=E6=9E=97=E5=9B=BD?= Date: Mon, 8 Apr 2019 12:02:07 +0800 Subject: [PATCH] add google pb generic service implement --- dubbo-common/pom.xml | 4 + .../org/apache/dubbo/common/Constants.java | 2 + .../dubbo/common/utils/ProtobufUtils.java | 76 ++ .../dubbo-demo-protobuf-api/pom.xml | 28 + .../dubbo/demo/protobuf/api/GooglePb.java | 1132 +++++++++++++++++ .../protobuf/api/ProtobufDemoService.java | 26 + .../main/resources/protobuf/GooglePb.proto | 31 + .../dubbo-demo-protobuf-consumer/pom.xml | 78 ++ .../demo/protobuf/consumer/Application.java | 42 + .../src/main/resources/dubbo.properties | 0 .../src/main/resources/log4j.properties | 0 .../main/resources/spring/dubbo-consumer.xml | 33 + .../dubbo-demo-protobuf-provider/pom.xml | 82 ++ .../demo/protobuf/provider/Application.java | 31 + .../provider/ProtobufDemoServiceImpl.java | 32 + .../src/main/resources/dubbo.properties | 1 + .../src/main/resources/log4j.properties | 7 + .../main/resources/spring/dubbo-provider.xml | 38 + dubbo-demo/dubbo-demo-protobuf/pom.xml | 44 + dubbo-demo/pom.xml | 1 + dubbo-dependencies-bom/pom.xml | 12 + .../ServiceDefinitionBuildderTest.java | 6 +- .../definition/service/DemoService.java | 1 - .../dubbo/rpc/filter/GenericFilter.java | 15 +- .../dubbo/rpc/support/ProtocolUtils.java | 7 +- 25 files changed, 1723 insertions(+), 6 deletions(-) create mode 100644 dubbo-common/src/main/java/org/apache/dubbo/common/utils/ProtobufUtils.java create mode 100644 dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-api/pom.xml create mode 100644 dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-api/src/main/java/org/apache/dubbo/demo/protobuf/api/GooglePb.java create mode 100644 dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-api/src/main/java/org/apache/dubbo/demo/protobuf/api/ProtobufDemoService.java create mode 100644 dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-api/src/main/resources/protobuf/GooglePb.proto create mode 100644 dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-consumer/pom.xml create mode 100644 dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-consumer/src/main/java/org/apache/dubbo/demo/protobuf/consumer/Application.java rename dubbo-demo/{dubbo-demo-xml/dubbo-demo-xml-consumer => dubbo-demo-protobuf/dubbo-demo-protobuf-consumer}/src/main/resources/dubbo.properties (100%) rename dubbo-demo/{dubbo-demo-xml/dubbo-demo-xml-consumer => dubbo-demo-protobuf/dubbo-demo-protobuf-consumer}/src/main/resources/log4j.properties (100%) create mode 100644 dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-consumer/src/main/resources/spring/dubbo-consumer.xml create mode 100644 dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-provider/pom.xml create mode 100644 dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-provider/src/main/java/org/apache/dubbo/demo/protobuf/provider/Application.java create mode 100644 dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-provider/src/main/java/org/apache/dubbo/demo/protobuf/provider/ProtobufDemoServiceImpl.java create mode 100644 dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-provider/src/main/resources/dubbo.properties create mode 100644 dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-provider/src/main/resources/log4j.properties create mode 100644 dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-provider/src/main/resources/spring/dubbo-provider.xml create mode 100644 dubbo-demo/dubbo-demo-protobuf/pom.xml diff --git a/dubbo-common/pom.xml b/dubbo-common/pom.xml index 3477f9b859a..bc146d53e69 100644 --- a/dubbo-common/pom.xml +++ b/dubbo-common/pom.xml @@ -77,5 +77,9 @@ de.ruedigermoeller fst + + com.google.protobuf + protobuf-java-util + \ No newline at end of file diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/Constants.java b/dubbo-common/src/main/java/org/apache/dubbo/common/Constants.java index b43c5321e0d..9c861ee3702 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/Constants.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/Constants.java @@ -727,6 +727,8 @@ public class Constants { public static final String GENERIC_SERIALIZATION_BEAN = "bean"; + public static final String GENERIC_SERIALIZATION_PROTO = "proto"; + public static final String DUBBO_IP_TO_REGISTRY = "DUBBO_IP_TO_REGISTRY"; public static final String DUBBO_PORT_TO_REGISTRY = "DUBBO_PORT_TO_REGISTRY"; diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ProtobufUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ProtobufUtils.java new file mode 100644 index 00000000000..532cfdeef02 --- /dev/null +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ProtobufUtils.java @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.dubbo.common.utils; + +import java.lang.reflect.Method; + +import com.google.protobuf.GeneratedMessageV3; +import com.google.protobuf.GeneratedMessageV3.Builder; +import com.google.protobuf.MessageOrBuilder; +import com.google.protobuf.util.JsonFormat; +import com.google.protobuf.util.JsonFormat.Parser; +import com.google.protobuf.util.JsonFormat.Printer; + +/** + * 2019/4/4 + */ +public class ProtobufUtils { + + public static boolean isSupported(Class clazz) { + if (clazz == null) { + return false; + } + + if (GeneratedMessageV3.class.isAssignableFrom(clazz)) { + return true; + } + return false; + } + + public static T deserialize(String json, Class requestClass) { + if (!isSupported(requestClass)) { + throw new UnsupportedOperationException(requestClass.getName() + "can not be deserialize"); + } + Builder builder; + try{ + builder = getMessageBuilder(requestClass); + Parser parser = JsonFormat.parser(); + parser.merge(json, builder); + }catch (Exception e){ + throw new RuntimeException("Failed to deserialize "+requestClass,e ); + } + + return (T) builder.build(); + } + + public static String serialize(Object value) { + String result; + try { + Printer printer = JsonFormat.printer(); + result = printer.print((MessageOrBuilder) value); + } catch (Exception e) { + result = e.getMessage(); + } + return result; + } + + private static Builder getMessageBuilder(Class requestType) throws Exception { + Method method = requestType.getMethod("newBuilder"); + + return (Builder) method.invoke(null, null); + } +} diff --git a/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-api/pom.xml b/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-api/pom.xml new file mode 100644 index 00000000000..df752e33cc6 --- /dev/null +++ b/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-api/pom.xml @@ -0,0 +1,28 @@ + + + + dubbo-demo-protobuf + org.apache.dubbo + 2.7.2-SNAPSHOT + + 4.0.0 + + dubbo-demo-protobuf-api + \ No newline at end of file diff --git a/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-api/src/main/java/org/apache/dubbo/demo/protobuf/api/GooglePb.java b/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-api/src/main/java/org/apache/dubbo/demo/protobuf/api/GooglePb.java new file mode 100644 index 00000000000..6f475d515b1 --- /dev/null +++ b/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-api/src/main/java/org/apache/dubbo/demo/protobuf/api/GooglePb.java @@ -0,0 +1,1132 @@ +/* + * 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. + */ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: GooglePb.proto + +package org.apache.dubbo.demo.protobuf.api; + +public final class GooglePb { + private GooglePb() {} + public static void registerAllExtensions( + com.google.protobuf.ExtensionRegistryLite registry) { + } + + public static void registerAllExtensions( + com.google.protobuf.ExtensionRegistry registry) { + registerAllExtensions( + (com.google.protobuf.ExtensionRegistryLite) registry); + } + public interface GooglePBRequestTypeOrBuilder extends + // @@protoc_insertion_point(interface_extends:org.apache.dubbo.demo.protobuf.api.GooglePBRequestType) + com.google.protobuf.MessageOrBuilder { + + /** + * string req = 1; + */ + String getReq(); + /** + * string req = 1; + */ + com.google.protobuf.ByteString + getReqBytes(); + } + /** + * Protobuf type {@code org.apache.dubbo.demo.protobuf.api.GooglePBRequestType} + */ + public static final class GooglePBRequestType extends + com.google.protobuf.GeneratedMessageV3 implements + // @@protoc_insertion_point(message_implements:org.apache.dubbo.demo.protobuf.api.GooglePBRequestType) + GooglePBRequestTypeOrBuilder { + private static final long serialVersionUID = 0L; + // Use GooglePBRequestType.newBuilder() to construct. + private GooglePBRequestType(com.google.protobuf.GeneratedMessageV3.Builder builder) { + super(builder); + } + private GooglePBRequestType() { + req_ = ""; + } + + @Override + public final com.google.protobuf.UnknownFieldSet + getUnknownFields() { + return this.unknownFields; + } + private GooglePBRequestType( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + this(); + int mutable_bitField0_ = 0; + com.google.protobuf.UnknownFieldSet.Builder unknownFields = + com.google.protobuf.UnknownFieldSet.newBuilder(); + try { + boolean done = false; + while (!done) { + int tag = input.readTag(); + switch (tag) { + case 0: + done = true; + break; + default: { + if (!parseUnknownFieldProto3( + input, unknownFields, extensionRegistry, tag)) { + done = true; + } + break; + } + case 10: { + String s = input.readStringRequireUtf8(); + + req_ = s; + break; + } + } + } + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + throw e.setUnfinishedMessage(this); + } catch (java.io.IOException e) { + throw new com.google.protobuf.InvalidProtocolBufferException( + e).setUnfinishedMessage(this); + } finally { + this.unknownFields = unknownFields.build(); + makeExtensionsImmutable(); + } + } + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return internal_static_org_apache_dubbo_demo_protobuf_api_GooglePBRequestType_descriptor; + } + + protected FieldAccessorTable + internalGetFieldAccessorTable() { + return internal_static_org_apache_dubbo_demo_protobuf_api_GooglePBRequestType_fieldAccessorTable + .ensureFieldAccessorsInitialized( + org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType.class, org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType.Builder.class); + } + + public static final int REQ_FIELD_NUMBER = 1; + private volatile Object req_; + /** + * string req = 1; + */ + public String getReq() { + Object ref = req_; + if (ref instanceof String) { + return (String) ref; + } else { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + String s = bs.toStringUtf8(); + req_ = s; + return s; + } + } + /** + * string req = 1; + */ + public com.google.protobuf.ByteString + getReqBytes() { + Object ref = req_; + if (ref instanceof String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (String) ref); + req_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + + private byte memoizedIsInitialized = -1; + public final boolean isInitialized() { + byte isInitialized = memoizedIsInitialized; + if (isInitialized == 1) return true; + if (isInitialized == 0) return false; + + memoizedIsInitialized = 1; + return true; + } + + public void writeTo(com.google.protobuf.CodedOutputStream output) + throws java.io.IOException { + if (!getReqBytes().isEmpty()) { + com.google.protobuf.GeneratedMessageV3.writeString(output, 1, req_); + } + unknownFields.writeTo(output); + } + + public int getSerializedSize() { + int size = memoizedSize; + if (size != -1) return size; + + size = 0; + if (!getReqBytes().isEmpty()) { + size += com.google.protobuf.GeneratedMessageV3.computeStringSize(1, req_); + } + size += unknownFields.getSerializedSize(); + memoizedSize = size; + return size; + } + + @Override + public boolean equals(final Object obj) { + if (obj == this) { + return true; + } + if (!(obj instanceof org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType)) { + return super.equals(obj); + } + org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType other = (org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType) obj; + + boolean result = true; + result = result && getReq() + .equals(other.getReq()); + result = result && unknownFields.equals(other.unknownFields); + return result; + } + + @Override + public int hashCode() { + if (memoizedHashCode != 0) { + return memoizedHashCode; + } + int hash = 41; + hash = (19 * hash) + getDescriptor().hashCode(); + hash = (37 * hash) + REQ_FIELD_NUMBER; + hash = (53 * hash) + getReq().hashCode(); + hash = (29 * hash) + unknownFields.hashCode(); + memoizedHashCode = hash; + return hash; + } + + public static org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType parseFrom( + java.nio.ByteBuffer data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType parseFrom( + java.nio.ByteBuffer data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType parseFrom( + com.google.protobuf.ByteString data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType parseFrom( + com.google.protobuf.ByteString data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType parseFrom(byte[] data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType parseFrom( + byte[] data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType parseFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input); + } + public static org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType parseFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input, extensionRegistry); + } + public static org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType parseDelimitedFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseDelimitedWithIOException(PARSER, input); + } + public static org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType parseDelimitedFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseDelimitedWithIOException(PARSER, input, extensionRegistry); + } + public static org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType parseFrom( + com.google.protobuf.CodedInputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input); + } + public static org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType parseFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input, extensionRegistry); + } + + public Builder newBuilderForType() { return newBuilder(); } + public static Builder newBuilder() { + return DEFAULT_INSTANCE.toBuilder(); + } + public static Builder newBuilder(org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType prototype) { + return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); + } + public Builder toBuilder() { + return this == DEFAULT_INSTANCE + ? new Builder() : new Builder().mergeFrom(this); + } + + @Override + protected Builder newBuilderForType( + BuilderParent parent) { + Builder builder = new Builder(parent); + return builder; + } + /** + * Protobuf type {@code org.apache.dubbo.demo.protobuf.api.GooglePBRequestType} + */ + public static final class Builder extends + com.google.protobuf.GeneratedMessageV3.Builder implements + // @@protoc_insertion_point(builder_implements:org.apache.dubbo.demo.protobuf.api.GooglePBRequestType) + org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestTypeOrBuilder { + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return internal_static_org_apache_dubbo_demo_protobuf_api_GooglePBRequestType_descriptor; + } + + protected FieldAccessorTable + internalGetFieldAccessorTable() { + return internal_static_org_apache_dubbo_demo_protobuf_api_GooglePBRequestType_fieldAccessorTable + .ensureFieldAccessorsInitialized( + org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType.class, org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType.Builder.class); + } + + // Construct using org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType.newBuilder() + private Builder() { + maybeForceBuilderInitialization(); + } + + private Builder( + BuilderParent parent) { + super(parent); + maybeForceBuilderInitialization(); + } + private void maybeForceBuilderInitialization() { + if (com.google.protobuf.GeneratedMessageV3 + .alwaysUseFieldBuilders) { + } + } + public Builder clear() { + super.clear(); + req_ = ""; + + return this; + } + + public com.google.protobuf.Descriptors.Descriptor + getDescriptorForType() { + return internal_static_org_apache_dubbo_demo_protobuf_api_GooglePBRequestType_descriptor; + } + + public org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType getDefaultInstanceForType() { + return getDefaultInstance(); + } + + public org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType build() { + org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType result = buildPartial(); + if (!result.isInitialized()) { + throw newUninitializedMessageException(result); + } + return result; + } + + public org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType buildPartial() { + org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType result = new org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType(this); + result.req_ = req_; + onBuilt(); + return result; + } + + public Builder clone() { + return (Builder) super.clone(); + } + public Builder setField( + com.google.protobuf.Descriptors.FieldDescriptor field, + Object value) { + return (Builder) super.setField(field, value); + } + public Builder clearField( + com.google.protobuf.Descriptors.FieldDescriptor field) { + return (Builder) super.clearField(field); + } + public Builder clearOneof( + com.google.protobuf.Descriptors.OneofDescriptor oneof) { + return (Builder) super.clearOneof(oneof); + } + public Builder setRepeatedField( + com.google.protobuf.Descriptors.FieldDescriptor field, + int index, Object value) { + return (Builder) super.setRepeatedField(field, index, value); + } + public Builder addRepeatedField( + com.google.protobuf.Descriptors.FieldDescriptor field, + Object value) { + return (Builder) super.addRepeatedField(field, value); + } + public Builder mergeFrom(com.google.protobuf.Message other) { + if (other instanceof org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType) { + return mergeFrom((org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType)other); + } else { + super.mergeFrom(other); + return this; + } + } + + public Builder mergeFrom(org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType other) { + if (other == getDefaultInstance()) return this; + if (!other.getReq().isEmpty()) { + req_ = other.req_; + onChanged(); + } + this.mergeUnknownFields(other.unknownFields); + onChanged(); + return this; + } + + public final boolean isInitialized() { + return true; + } + + public Builder mergeFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType parsedMessage = null; + try { + parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + parsedMessage = (org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType) e.getUnfinishedMessage(); + throw e.unwrapIOException(); + } finally { + if (parsedMessage != null) { + mergeFrom(parsedMessage); + } + } + return this; + } + + private Object req_ = ""; + /** + * string req = 1; + */ + public String getReq() { + Object ref = req_; + if (!(ref instanceof String)) { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + String s = bs.toStringUtf8(); + req_ = s; + return s; + } else { + return (String) ref; + } + } + /** + * string req = 1; + */ + public com.google.protobuf.ByteString + getReqBytes() { + Object ref = req_; + if (ref instanceof String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (String) ref); + req_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + /** + * string req = 1; + */ + public Builder setReq( + String value) { + if (value == null) { + throw new NullPointerException(); + } + + req_ = value; + onChanged(); + return this; + } + /** + * string req = 1; + */ + public Builder clearReq() { + + req_ = getDefaultInstance().getReq(); + onChanged(); + return this; + } + /** + * string req = 1; + */ + public Builder setReqBytes( + com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + checkByteStringIsUtf8(value); + + req_ = value; + onChanged(); + return this; + } + public final Builder setUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return super.setUnknownFieldsProto3(unknownFields); + } + + public final Builder mergeUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return super.mergeUnknownFields(unknownFields); + } + + + // @@protoc_insertion_point(builder_scope:org.apache.dubbo.demo.protobuf.api.GooglePBRequestType) + } + + // @@protoc_insertion_point(class_scope:org.apache.dubbo.demo.protobuf.api.GooglePBRequestType) + private static final org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType DEFAULT_INSTANCE; + static { + DEFAULT_INSTANCE = new org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType(); + } + + public static org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType getDefaultInstance() { + return DEFAULT_INSTANCE; + } + + private static final com.google.protobuf.Parser + PARSER = new com.google.protobuf.AbstractParser() { + public GooglePBRequestType parsePartialFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return new GooglePBRequestType(input, extensionRegistry); + } + }; + + public static com.google.protobuf.Parser parser() { + return PARSER; + } + + @Override + public com.google.protobuf.Parser getParserForType() { + return PARSER; + } + + public org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType getDefaultInstanceForType() { + return DEFAULT_INSTANCE; + } + + } + + public interface GooglePBResponseTypeOrBuilder extends + // @@protoc_insertion_point(interface_extends:org.apache.dubbo.demo.protobuf.api.GooglePBResponseType) + com.google.protobuf.MessageOrBuilder { + + /** + * string response = 1; + */ + String getResponse(); + /** + * string response = 1; + */ + com.google.protobuf.ByteString + getResponseBytes(); + } + /** + * Protobuf type {@code org.apache.dubbo.demo.protobuf.api.GooglePBResponseType} + */ + public static final class GooglePBResponseType extends + com.google.protobuf.GeneratedMessageV3 implements + // @@protoc_insertion_point(message_implements:org.apache.dubbo.demo.protobuf.api.GooglePBResponseType) + GooglePBResponseTypeOrBuilder { + private static final long serialVersionUID = 0L; + // Use GooglePBResponseType.newBuilder() to construct. + private GooglePBResponseType(com.google.protobuf.GeneratedMessageV3.Builder builder) { + super(builder); + } + private GooglePBResponseType() { + response_ = ""; + } + + @Override + public final com.google.protobuf.UnknownFieldSet + getUnknownFields() { + return this.unknownFields; + } + private GooglePBResponseType( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + this(); + int mutable_bitField0_ = 0; + com.google.protobuf.UnknownFieldSet.Builder unknownFields = + com.google.protobuf.UnknownFieldSet.newBuilder(); + try { + boolean done = false; + while (!done) { + int tag = input.readTag(); + switch (tag) { + case 0: + done = true; + break; + default: { + if (!parseUnknownFieldProto3( + input, unknownFields, extensionRegistry, tag)) { + done = true; + } + break; + } + case 10: { + String s = input.readStringRequireUtf8(); + + response_ = s; + break; + } + } + } + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + throw e.setUnfinishedMessage(this); + } catch (java.io.IOException e) { + throw new com.google.protobuf.InvalidProtocolBufferException( + e).setUnfinishedMessage(this); + } finally { + this.unknownFields = unknownFields.build(); + makeExtensionsImmutable(); + } + } + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return internal_static_org_apache_dubbo_demo_protobuf_api_GooglePBResponseType_descriptor; + } + + protected FieldAccessorTable + internalGetFieldAccessorTable() { + return internal_static_org_apache_dubbo_demo_protobuf_api_GooglePBResponseType_fieldAccessorTable + .ensureFieldAccessorsInitialized( + org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType.class, org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType.Builder.class); + } + + public static final int RESPONSE_FIELD_NUMBER = 1; + private volatile Object response_; + /** + * string response = 1; + */ + public String getResponse() { + Object ref = response_; + if (ref instanceof String) { + return (String) ref; + } else { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + String s = bs.toStringUtf8(); + response_ = s; + return s; + } + } + /** + * string response = 1; + */ + public com.google.protobuf.ByteString + getResponseBytes() { + Object ref = response_; + if (ref instanceof String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (String) ref); + response_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + + private byte memoizedIsInitialized = -1; + public final boolean isInitialized() { + byte isInitialized = memoizedIsInitialized; + if (isInitialized == 1) return true; + if (isInitialized == 0) return false; + + memoizedIsInitialized = 1; + return true; + } + + public void writeTo(com.google.protobuf.CodedOutputStream output) + throws java.io.IOException { + if (!getResponseBytes().isEmpty()) { + com.google.protobuf.GeneratedMessageV3.writeString(output, 1, response_); + } + unknownFields.writeTo(output); + } + + public int getSerializedSize() { + int size = memoizedSize; + if (size != -1) return size; + + size = 0; + if (!getResponseBytes().isEmpty()) { + size += com.google.protobuf.GeneratedMessageV3.computeStringSize(1, response_); + } + size += unknownFields.getSerializedSize(); + memoizedSize = size; + return size; + } + + @Override + public boolean equals(final Object obj) { + if (obj == this) { + return true; + } + if (!(obj instanceof org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType)) { + return super.equals(obj); + } + org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType other = (org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType) obj; + + boolean result = true; + result = result && getResponse() + .equals(other.getResponse()); + result = result && unknownFields.equals(other.unknownFields); + return result; + } + + @Override + public int hashCode() { + if (memoizedHashCode != 0) { + return memoizedHashCode; + } + int hash = 41; + hash = (19 * hash) + getDescriptor().hashCode(); + hash = (37 * hash) + RESPONSE_FIELD_NUMBER; + hash = (53 * hash) + getResponse().hashCode(); + hash = (29 * hash) + unknownFields.hashCode(); + memoizedHashCode = hash; + return hash; + } + + public static org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType parseFrom( + java.nio.ByteBuffer data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType parseFrom( + java.nio.ByteBuffer data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType parseFrom( + com.google.protobuf.ByteString data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType parseFrom( + com.google.protobuf.ByteString data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType parseFrom(byte[] data) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + public static org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType parseFrom( + byte[] data, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + public static org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType parseFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input); + } + public static org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType parseFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input, extensionRegistry); + } + public static org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType parseDelimitedFrom(java.io.InputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseDelimitedWithIOException(PARSER, input); + } + public static org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType parseDelimitedFrom( + java.io.InputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseDelimitedWithIOException(PARSER, input, extensionRegistry); + } + public static org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType parseFrom( + com.google.protobuf.CodedInputStream input) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input); + } + public static org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType parseFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + return com.google.protobuf.GeneratedMessageV3 + .parseWithIOException(PARSER, input, extensionRegistry); + } + + public Builder newBuilderForType() { return newBuilder(); } + public static Builder newBuilder() { + return DEFAULT_INSTANCE.toBuilder(); + } + public static Builder newBuilder(org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType prototype) { + return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype); + } + public Builder toBuilder() { + return this == DEFAULT_INSTANCE + ? new Builder() : new Builder().mergeFrom(this); + } + + @Override + protected Builder newBuilderForType( + BuilderParent parent) { + Builder builder = new Builder(parent); + return builder; + } + /** + * Protobuf type {@code org.apache.dubbo.demo.protobuf.api.GooglePBResponseType} + */ + public static final class Builder extends + com.google.protobuf.GeneratedMessageV3.Builder implements + // @@protoc_insertion_point(builder_implements:org.apache.dubbo.demo.protobuf.api.GooglePBResponseType) + org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseTypeOrBuilder { + public static final com.google.protobuf.Descriptors.Descriptor + getDescriptor() { + return internal_static_org_apache_dubbo_demo_protobuf_api_GooglePBResponseType_descriptor; + } + + protected FieldAccessorTable + internalGetFieldAccessorTable() { + return internal_static_org_apache_dubbo_demo_protobuf_api_GooglePBResponseType_fieldAccessorTable + .ensureFieldAccessorsInitialized( + org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType.class, org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType.Builder.class); + } + + // Construct using org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType.newBuilder() + private Builder() { + maybeForceBuilderInitialization(); + } + + private Builder( + BuilderParent parent) { + super(parent); + maybeForceBuilderInitialization(); + } + private void maybeForceBuilderInitialization() { + if (com.google.protobuf.GeneratedMessageV3 + .alwaysUseFieldBuilders) { + } + } + public Builder clear() { + super.clear(); + response_ = ""; + + return this; + } + + public com.google.protobuf.Descriptors.Descriptor + getDescriptorForType() { + return internal_static_org_apache_dubbo_demo_protobuf_api_GooglePBResponseType_descriptor; + } + + public org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType getDefaultInstanceForType() { + return getDefaultInstance(); + } + + public org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType build() { + org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType result = buildPartial(); + if (!result.isInitialized()) { + throw newUninitializedMessageException(result); + } + return result; + } + + public org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType buildPartial() { + org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType result = new org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType(this); + result.response_ = response_; + onBuilt(); + return result; + } + + public Builder clone() { + return (Builder) super.clone(); + } + public Builder setField( + com.google.protobuf.Descriptors.FieldDescriptor field, + Object value) { + return (Builder) super.setField(field, value); + } + public Builder clearField( + com.google.protobuf.Descriptors.FieldDescriptor field) { + return (Builder) super.clearField(field); + } + public Builder clearOneof( + com.google.protobuf.Descriptors.OneofDescriptor oneof) { + return (Builder) super.clearOneof(oneof); + } + public Builder setRepeatedField( + com.google.protobuf.Descriptors.FieldDescriptor field, + int index, Object value) { + return (Builder) super.setRepeatedField(field, index, value); + } + public Builder addRepeatedField( + com.google.protobuf.Descriptors.FieldDescriptor field, + Object value) { + return (Builder) super.addRepeatedField(field, value); + } + public Builder mergeFrom(com.google.protobuf.Message other) { + if (other instanceof org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType) { + return mergeFrom((org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType)other); + } else { + super.mergeFrom(other); + return this; + } + } + + public Builder mergeFrom(org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType other) { + if (other == getDefaultInstance()) return this; + if (!other.getResponse().isEmpty()) { + response_ = other.response_; + onChanged(); + } + this.mergeUnknownFields(other.unknownFields); + onChanged(); + return this; + } + + public final boolean isInitialized() { + return true; + } + + public Builder mergeFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws java.io.IOException { + org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType parsedMessage = null; + try { + parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + parsedMessage = (org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType) e.getUnfinishedMessage(); + throw e.unwrapIOException(); + } finally { + if (parsedMessage != null) { + mergeFrom(parsedMessage); + } + } + return this; + } + + private Object response_ = ""; + /** + * string response = 1; + */ + public String getResponse() { + Object ref = response_; + if (!(ref instanceof String)) { + com.google.protobuf.ByteString bs = + (com.google.protobuf.ByteString) ref; + String s = bs.toStringUtf8(); + response_ = s; + return s; + } else { + return (String) ref; + } + } + /** + * string response = 1; + */ + public com.google.protobuf.ByteString + getResponseBytes() { + Object ref = response_; + if (ref instanceof String) { + com.google.protobuf.ByteString b = + com.google.protobuf.ByteString.copyFromUtf8( + (String) ref); + response_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + /** + * string response = 1; + */ + public Builder setResponse( + String value) { + if (value == null) { + throw new NullPointerException(); + } + + response_ = value; + onChanged(); + return this; + } + /** + * string response = 1; + */ + public Builder clearResponse() { + + response_ = getDefaultInstance().getResponse(); + onChanged(); + return this; + } + /** + * string response = 1; + */ + public Builder setResponseBytes( + com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + checkByteStringIsUtf8(value); + + response_ = value; + onChanged(); + return this; + } + public final Builder setUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return super.setUnknownFieldsProto3(unknownFields); + } + + public final Builder mergeUnknownFields( + final com.google.protobuf.UnknownFieldSet unknownFields) { + return super.mergeUnknownFields(unknownFields); + } + + + // @@protoc_insertion_point(builder_scope:org.apache.dubbo.demo.protobuf.api.GooglePBResponseType) + } + + // @@protoc_insertion_point(class_scope:org.apache.dubbo.demo.protobuf.api.GooglePBResponseType) + private static final org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType DEFAULT_INSTANCE; + static { + DEFAULT_INSTANCE = new org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType(); + } + + public static org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType getDefaultInstance() { + return DEFAULT_INSTANCE; + } + + private static final com.google.protobuf.Parser + PARSER = new com.google.protobuf.AbstractParser() { + public GooglePBResponseType parsePartialFrom( + com.google.protobuf.CodedInputStream input, + com.google.protobuf.ExtensionRegistryLite extensionRegistry) + throws com.google.protobuf.InvalidProtocolBufferException { + return new GooglePBResponseType(input, extensionRegistry); + } + }; + + public static com.google.protobuf.Parser parser() { + return PARSER; + } + + @Override + public com.google.protobuf.Parser getParserForType() { + return PARSER; + } + + public org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType getDefaultInstanceForType() { + return DEFAULT_INSTANCE; + } + + } + + private static final com.google.protobuf.Descriptors.Descriptor + internal_static_org_apache_dubbo_demo_protobuf_api_GooglePBRequestType_descriptor; + private static final + com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internal_static_org_apache_dubbo_demo_protobuf_api_GooglePBRequestType_fieldAccessorTable; + private static final com.google.protobuf.Descriptors.Descriptor + internal_static_org_apache_dubbo_demo_protobuf_api_GooglePBResponseType_descriptor; + private static final + com.google.protobuf.GeneratedMessageV3.FieldAccessorTable + internal_static_org_apache_dubbo_demo_protobuf_api_GooglePBResponseType_fieldAccessorTable; + + public static com.google.protobuf.Descriptors.FileDescriptor + getDescriptor() { + return descriptor; + } + private static com.google.protobuf.Descriptors.FileDescriptor + descriptor; + static { + String[] descriptorData = { + "\n\016GooglePb.proto\022\"org.apache.dubbo.demo." + + "protobuf.api\"\"\n\023GooglePBRequestType\022\013\n\003r" + + "eq\030\001 \001(\t\"(\n\024GooglePBResponseType\022\020\n\010resp" + + "onse\030\001 \001(\t2\226\001\n\025CDubboGooglePBService\022}\n\010" + + "sayHello\0227.org.apache.dubbo.demo.protobu" + + "f.api.GooglePBRequestType\0328.org.apache.d" + + "ubbo.demo.protobuf.api.GooglePBResponseT" + + "ypeb\006proto3" + }; + com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner = + new com.google.protobuf.Descriptors.FileDescriptor. InternalDescriptorAssigner() { + public com.google.protobuf.ExtensionRegistry assignDescriptors( + com.google.protobuf.Descriptors.FileDescriptor root) { + descriptor = root; + return null; + } + }; + com.google.protobuf.Descriptors.FileDescriptor + .internalBuildGeneratedFileFrom(descriptorData, + new com.google.protobuf.Descriptors.FileDescriptor[] { + }, assigner); + internal_static_org_apache_dubbo_demo_protobuf_api_GooglePBRequestType_descriptor = + getDescriptor().getMessageTypes().get(0); + internal_static_org_apache_dubbo_demo_protobuf_api_GooglePBRequestType_fieldAccessorTable = new + com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( + internal_static_org_apache_dubbo_demo_protobuf_api_GooglePBRequestType_descriptor, + new String[] { "Req", }); + internal_static_org_apache_dubbo_demo_protobuf_api_GooglePBResponseType_descriptor = + getDescriptor().getMessageTypes().get(1); + internal_static_org_apache_dubbo_demo_protobuf_api_GooglePBResponseType_fieldAccessorTable = new + com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( + internal_static_org_apache_dubbo_demo_protobuf_api_GooglePBResponseType_descriptor, + new String[] { "Response", }); + } + + // @@protoc_insertion_point(outer_class_scope) +} diff --git a/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-api/src/main/java/org/apache/dubbo/demo/protobuf/api/ProtobufDemoService.java b/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-api/src/main/java/org/apache/dubbo/demo/protobuf/api/ProtobufDemoService.java new file mode 100644 index 00000000000..65786356ae7 --- /dev/null +++ b/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-api/src/main/java/org/apache/dubbo/demo/protobuf/api/ProtobufDemoService.java @@ -0,0 +1,26 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.dubbo.demo.protobuf.api; + +import org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType; +import org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType; + +public interface ProtobufDemoService { + + GooglePBResponseType sayHello(GooglePBRequestType request); + +} \ No newline at end of file diff --git a/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-api/src/main/resources/protobuf/GooglePb.proto b/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-api/src/main/resources/protobuf/GooglePb.proto new file mode 100644 index 00000000000..a899f950b5b --- /dev/null +++ b/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-api/src/main/resources/protobuf/GooglePb.proto @@ -0,0 +1,31 @@ +/* + * 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"; +package org.apache.dubbo.demo.protobuf.api; + +message GooglePBRequestType { + string req = 1; +} + + +message GooglePBResponseType { + string response =1; +} + +service CDubboGooglePBService { + rpc sayHello (GooglePBRequestType) returns (GooglePBResponseType); +} diff --git a/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-consumer/pom.xml b/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-consumer/pom.xml new file mode 100644 index 00000000000..d6cce10cc63 --- /dev/null +++ b/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-consumer/pom.xml @@ -0,0 +1,78 @@ + + + + dubbo-demo-protobuf + org.apache.dubbo + 2.7.2-SNAPSHOT + + 4.0.0 + + dubbo-demo-protobuf-consumer + + + + org.apache.dubbo + dubbo-demo-protobuf-api + ${project.version} + + + org.apache.dubbo + dubbo-demo-interface + ${project.parent.version} + + + org.apache.dubbo + dubbo-config-spring + + + org.apache.dubbo + dubbo-registry-zookeeper + + + org.apache.dubbo + dubbo-registry-multicast + + + org.apache.dubbo + dubbo-rpc-dubbo + + + org.apache.dubbo + dubbo-remoting-netty4 + + + org.apache.dubbo + dubbo-serialization-hessian2 + + + org.apache.dubbo + dubbo-configcenter-zookeeper + + + org.apache.dubbo + dubbo-metadata-report-redis + + + org.apache.dubbo + dubbo-metadata-report-zookeeper + + + + \ No newline at end of file diff --git a/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-consumer/src/main/java/org/apache/dubbo/demo/protobuf/consumer/Application.java b/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-consumer/src/main/java/org/apache/dubbo/demo/protobuf/consumer/Application.java new file mode 100644 index 00000000000..a1d5a8a83b9 --- /dev/null +++ b/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-consumer/src/main/java/org/apache/dubbo/demo/protobuf/consumer/Application.java @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.dubbo.demo.protobuf.consumer; + + +import org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType; +import org.apache.dubbo.rpc.service.GenericService; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class Application { + /** + * In order to make sure multicast registry works, need to specify '-Djava.net.preferIPv4Stack=true' before + * launch the application + */ + public static void main(String[] args) { + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-consumer.xml"); + context.start(); + GenericService genericService = context.getBean("demoService", GenericService.class); + String methodName = "sayHello"; + String[] requestType = new String[]{GooglePBRequestType.class.getName()}; + + // TODO this requestStr could generate from serviceMetaData. a new TypeDefinition is create will push next + String requestStr = "{ \"req\": \"some Message\" }"; + Object[] request = new Object[]{requestStr}; + String hello = (String) genericService.$invoke(methodName,requestType,request); + System.out.println("result: " + hello); + } +} diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/src/main/resources/dubbo.properties b/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-consumer/src/main/resources/dubbo.properties similarity index 100% rename from dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/src/main/resources/dubbo.properties rename to dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-consumer/src/main/resources/dubbo.properties diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/src/main/resources/log4j.properties b/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-consumer/src/main/resources/log4j.properties similarity index 100% rename from dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/src/main/resources/log4j.properties rename to dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-consumer/src/main/resources/log4j.properties diff --git a/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-consumer/src/main/resources/spring/dubbo-consumer.xml b/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-consumer/src/main/resources/spring/dubbo-consumer.xml new file mode 100644 index 00000000000..4f07a39f13e --- /dev/null +++ b/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-consumer/src/main/resources/spring/dubbo-consumer.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + diff --git a/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-provider/pom.xml b/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-provider/pom.xml new file mode 100644 index 00000000000..8cc856f8b87 --- /dev/null +++ b/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-provider/pom.xml @@ -0,0 +1,82 @@ + + + + dubbo-demo-protobuf + org.apache.dubbo + 2.7.2-SNAPSHOT + + 4.0.0 + + dubbo-demo-protobuf-provider + + + + org.apache.dubbo + dubbo-demo-protobuf-api + ${project.version} + + + org.apache.dubbo + dubbo-demo-interface + ${project.parent.version} + + + org.apache.dubbo + dubbo-config-spring + + + org.apache.dubbo + dubbo-registry-zookeeper + + + org.apache.dubbo + dubbo-registry-multicast + + + org.apache.dubbo + dubbo-rpc-dubbo + + + org.apache.dubbo + dubbo-remoting-netty4 + + + org.apache.dubbo + dubbo-serialization-hessian2 + + + org.apache.dubbo + dubbo-configcenter-zookeeper + + + org.apache.dubbo + dubbo-metadata-report-zookeeper + + + org.apache.dubbo + dubbo-metadata-report-redis + + + org.apache.dubbo + dubbo-qos + + + + \ No newline at end of file diff --git a/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-provider/src/main/java/org/apache/dubbo/demo/protobuf/provider/Application.java b/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-provider/src/main/java/org/apache/dubbo/demo/protobuf/provider/Application.java new file mode 100644 index 00000000000..b6b10d2dd97 --- /dev/null +++ b/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-provider/src/main/java/org/apache/dubbo/demo/protobuf/provider/Application.java @@ -0,0 +1,31 @@ +/* + * 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.dubbo.demo.protobuf.provider; + +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class Application { + /** + * In order to make sure multicast registry works, need to specify '-Djava.net.preferIPv4Stack=true' before + * launch the application + */ + public static void main(String[] args) throws Exception { + ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-provider.xml"); + context.start(); + System.in.read(); + } +} \ No newline at end of file diff --git a/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-provider/src/main/java/org/apache/dubbo/demo/protobuf/provider/ProtobufDemoServiceImpl.java b/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-provider/src/main/java/org/apache/dubbo/demo/protobuf/provider/ProtobufDemoServiceImpl.java new file mode 100644 index 00000000000..eff8db1f3b6 --- /dev/null +++ b/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-provider/src/main/java/org/apache/dubbo/demo/protobuf/provider/ProtobufDemoServiceImpl.java @@ -0,0 +1,32 @@ +/* + * 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.dubbo.demo.protobuf.provider; + +import org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBRequestType; +import org.apache.dubbo.demo.protobuf.api.GooglePb.GooglePBResponseType; +import org.apache.dubbo.demo.protobuf.api.ProtobufDemoService; + +/** + * 2019/4/8 + */ +public class ProtobufDemoServiceImpl implements ProtobufDemoService { + @Override + public GooglePBResponseType sayHello(GooglePBRequestType request) { + GooglePBResponseType response = GooglePBResponseType.newBuilder().setResponse("message from server :"+request.getReq()).build(); + return response; + } +} diff --git a/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-provider/src/main/resources/dubbo.properties b/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-provider/src/main/resources/dubbo.properties new file mode 100644 index 00000000000..ad602baa943 --- /dev/null +++ b/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-provider/src/main/resources/dubbo.properties @@ -0,0 +1 @@ +dubbo.application.qos.port=22222 diff --git a/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-provider/src/main/resources/log4j.properties b/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-provider/src/main/resources/log4j.properties new file mode 100644 index 00000000000..15a0900f0d2 --- /dev/null +++ b/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-provider/src/main/resources/log4j.properties @@ -0,0 +1,7 @@ +###set log levels### +log4j.rootLogger=info, stdout +###output to the console### +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.Target=System.out +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy HH:mm:ss:SSS z}] %t %5p %c{2}: %m%n \ No newline at end of file diff --git a/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-provider/src/main/resources/spring/dubbo-provider.xml b/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-provider/src/main/resources/spring/dubbo-provider.xml new file mode 100644 index 00000000000..c0c20a7ef1a --- /dev/null +++ b/dubbo-demo/dubbo-demo-protobuf/dubbo-demo-protobuf-provider/src/main/resources/spring/dubbo-provider.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + diff --git a/dubbo-demo/dubbo-demo-protobuf/pom.xml b/dubbo-demo/dubbo-demo-protobuf/pom.xml new file mode 100644 index 00000000000..651c651fea1 --- /dev/null +++ b/dubbo-demo/dubbo-demo-protobuf/pom.xml @@ -0,0 +1,44 @@ + + + + + dubbo-demo + org.apache.dubbo + 2.7.2-SNAPSHOT + + 4.0.0 + + dubbo-demo-protobuf + pom + + dubbo-demo-protobuf-api + dubbo-demo-protobuf-consumer + dubbo-demo-protobuf-provider + + + + com.google.protobuf + protobuf-java + + + + \ No newline at end of file diff --git a/dubbo-demo/pom.xml b/dubbo-demo/pom.xml index 9894cd95ab8..34f9f53b8b3 100644 --- a/dubbo-demo/pom.xml +++ b/dubbo-demo/pom.xml @@ -34,6 +34,7 @@ dubbo-demo-xml dubbo-demo-annotation dubbo-demo-api + dubbo-demo-protobuf diff --git a/dubbo-dependencies-bom/pom.xml b/dubbo-dependencies-bom/pom.xml index f8880494b24..427ee611ef3 100644 --- a/dubbo-dependencies-bom/pom.xml +++ b/dubbo-dependencies-bom/pom.xml @@ -108,6 +108,8 @@ 3.1.15 0.8.0 4.0.38 + 3.6.0 + 3.6.0 3.1.0 9.4.11.v20180605 1.1.0.Final @@ -257,6 +259,16 @@ hessian-lite ${hessian_lite_version} + + com.google.protobuf + protobuf-java + ${protobuf-java} + + + com.google.protobuf + protobuf-java-util + ${protobuf.java.util} + javax.servlet javax.servlet-api diff --git a/dubbo-metadata-report/dubbo-metadata-definition/src/test/java/org/apache/dubbo/metadata/definition/ServiceDefinitionBuildderTest.java b/dubbo-metadata-report/dubbo-metadata-definition/src/test/java/org/apache/dubbo/metadata/definition/ServiceDefinitionBuildderTest.java index 89c81a5ae53..700c63db48c 100644 --- a/dubbo-metadata-report/dubbo-metadata-definition/src/test/java/org/apache/dubbo/metadata/definition/ServiceDefinitionBuildderTest.java +++ b/dubbo-metadata-report/dubbo-metadata-definition/src/test/java/org/apache/dubbo/metadata/definition/ServiceDefinitionBuildderTest.java @@ -34,13 +34,13 @@ public class ServiceDefinitionBuildderTest { @Test - public void testBuilderComplextObject() { + public void testBuilderComplexObject() { FullServiceDefinition fullServiceDefinition = ServiceDefinitionBuilder.buildFullDefinition(DemoService.class); - checkComplextObjectAsParam(fullServiceDefinition); + checkComplexObjectAsParam(fullServiceDefinition); } - void checkComplextObjectAsParam(FullServiceDefinition fullServiceDefinition) { + void checkComplexObjectAsParam(FullServiceDefinition fullServiceDefinition) { List methodDefinitions = fullServiceDefinition.getMethods(); MethodDefinition complexCompute = null; MethodDefinition findComplexObject = null; diff --git a/dubbo-metadata-report/dubbo-metadata-definition/src/test/java/org/apache/dubbo/metadata/definition/service/DemoService.java b/dubbo-metadata-report/dubbo-metadata-definition/src/test/java/org/apache/dubbo/metadata/definition/service/DemoService.java index c839c6645d7..4dce2f6748e 100644 --- a/dubbo-metadata-report/dubbo-metadata-definition/src/test/java/org/apache/dubbo/metadata/definition/service/DemoService.java +++ b/dubbo-metadata-report/dubbo-metadata-definition/src/test/java/org/apache/dubbo/metadata/definition/service/DemoService.java @@ -26,5 +26,4 @@ public interface DemoService { String complexCompute(String input, ComplexObject co); ComplexObject findComplexObject(String var1, int var2, long l, String[] var3, List var4, ComplexObject.TestEnum testEnum); - } diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericFilter.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericFilter.java index ae7b92c40f1..69b9c97fd53 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericFilter.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/filter/GenericFilter.java @@ -26,6 +26,7 @@ import org.apache.dubbo.common.io.UnsafeByteArrayOutputStream; import org.apache.dubbo.common.serialize.Serialization; import org.apache.dubbo.common.utils.PojoUtils; +import org.apache.dubbo.common.utils.ProtobufUtils; import org.apache.dubbo.common.utils.ReflectUtils; import org.apache.dubbo.common.utils.StringUtils; import org.apache.dubbo.rpc.Filter; @@ -107,6 +108,16 @@ public Result invoke(Invoker invoker, Invocation inv) throws RpcException { args[i].getClass().getName()); } } + }else if (ProtocolUtils.isProtobufGenericSerialization(generic)) { + //as proto3 only accept one parameter + if (args.length == 1 && args[0] instanceof String) { + args[0] = ProtobufUtils.deserialize((String) args[0], method.getParameterTypes()[0]); + } else { + throw new RpcException( + new StringBuilder("Generic serialization [").append(Constants.GENERIC_SERIALIZATION_PROTO) + .append("] only support one").append(String.class.getName()).append(" argument ") + .append(" and your message size is ").append(args.length).append(" and type is").append(args[0].getClass().getName()).toString()); + } } Result result = invoker.invoke(new RpcInvocation(method, args, inv.getAttachments())); if (result.hasException() @@ -125,7 +136,9 @@ public Result invoke(Invoker invoker, Invocation inv) throws RpcException { } } else if (ProtocolUtils.isBeanGenericSerialization(generic)) { return new RpcResult(JavaBeanSerializeUtil.serialize(result.getValue(), JavaBeanAccessor.METHOD)); - } else { + } else if(ProtocolUtils.isProtobufGenericSerialization(generic)) { + return new RpcResult(ProtobufUtils.serialize(result.getValue())); + }else { return new RpcResult(PojoUtils.generalize(result.getValue())); } } catch (NoSuchMethodException e) { diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/ProtocolUtils.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/ProtocolUtils.java index ce5da6b51be..f71dc67c06b 100644 --- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/ProtocolUtils.java +++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/ProtocolUtils.java @@ -51,7 +51,8 @@ public static boolean isGeneric(String generic) { && !"".equals(generic) && (Constants.GENERIC_SERIALIZATION_DEFAULT.equalsIgnoreCase(generic) /* Normal generalization cal */ || Constants.GENERIC_SERIALIZATION_NATIVE_JAVA.equalsIgnoreCase(generic) /* Streaming generalization call supporting jdk serialization */ - || Constants.GENERIC_SERIALIZATION_BEAN.equalsIgnoreCase(generic)); + || Constants.GENERIC_SERIALIZATION_BEAN.equalsIgnoreCase(generic) + || Constants.GENERIC_SERIALIZATION_PROTO.equalsIgnoreCase(generic)); } public static boolean isDefaultGenericSerialization(String generic) { @@ -67,4 +68,8 @@ public static boolean isJavaGenericSerialization(String generic) { public static boolean isBeanGenericSerialization(String generic) { return isGeneric(generic) && Constants.GENERIC_SERIALIZATION_BEAN.equals(generic); } + + public static boolean isProtobufGenericSerialization(String generic) { + return isGeneric(generic) && Constants.GENERIC_SERIALIZATION_PROTO.equals(generic); + } }