From 47ac5a3fe3585a04dae2c1e7ff5ecf08007d1a54 Mon Sep 17 00:00:00 2001 From: "shengwei.psw" Date: Wed, 23 Dec 2020 11:13:05 +0800 Subject: [PATCH] #1 --- .../remoting/netty4/Http2WireProtocol.java | 5 +- .../dubbo/rpc/protocol/grpc/GrpcElf.java | 148 +++++++++++ .../protocol/grpc/GrpcHttp2FrameListener.java | 108 ++++++++ .../rpc/protocol/grpc/GrpcHttp2Protocol.java | 18 ++ .../dubbo/rpc/protocol/grpc/Http2Request.java | 73 ++++++ ...he.dubbo.remoting.netty4.Http2WireProtocol | 1 + .../rpc/protocol/dubbo/DubboProtocolTest.java | 230 ++++++++++++++++++ .../dubbo/support/CustomArgument.java | 51 ++++ .../protocol/dubbo/support/DemoRequest.java | 58 +++++ .../protocol/dubbo/support/DemoService.java | 69 ++++++ .../dubbo/support/DemoServiceImpl.java | 129 ++++++++++ .../rpc/protocol/dubbo/support/EnumBak.java | 201 +++++++++++++++ .../dubbo/rpc/protocol/dubbo/support/Man.java | 46 ++++ .../protocol/dubbo/support/NonSerialized.java | 24 ++ .../rpc/protocol/dubbo/support/Person.java | 45 ++++ .../protocol/dubbo/support/ProtocolUtils.java | 65 +++++ .../protocol/dubbo/support/RemoteService.java | 26 ++ .../dubbo/support/RemoteServiceImpl.java | 32 +++ .../rpc/protocol/dubbo/support/Type.java | 21 ++ ...apache.dubbo.remoting.telnet.TelnetHandler | 1 + .../src/test/resources/log4j.xml | 38 +++ 21 files changed, 1388 insertions(+), 1 deletion(-) create mode 100644 dubbo-rpc/dubbo-rpc-grpc/src/main/java/org/apache/dubbo/rpc/protocol/grpc/GrpcElf.java create mode 100644 dubbo-rpc/dubbo-rpc-grpc/src/main/java/org/apache/dubbo/rpc/protocol/grpc/GrpcHttp2FrameListener.java create mode 100644 dubbo-rpc/dubbo-rpc-grpc/src/main/java/org/apache/dubbo/rpc/protocol/grpc/GrpcHttp2Protocol.java create mode 100644 dubbo-rpc/dubbo-rpc-grpc/src/main/java/org/apache/dubbo/rpc/protocol/grpc/Http2Request.java create mode 100644 dubbo-rpc/dubbo-rpc-grpc/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.remoting.netty4.Http2WireProtocol create mode 100644 dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocolTest.java create mode 100644 dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/CustomArgument.java create mode 100644 dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoRequest.java create mode 100644 dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoService.java create mode 100644 dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoServiceImpl.java create mode 100644 dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/EnumBak.java create mode 100644 dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/Man.java create mode 100644 dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/NonSerialized.java create mode 100644 dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/Person.java create mode 100644 dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/ProtocolUtils.java create mode 100644 dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/RemoteService.java create mode 100644 dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/RemoteServiceImpl.java create mode 100644 dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/Type.java create mode 100644 dubbo-rpc/dubbo-rpc-grpc/src/test/resources/META-INF/dubbo/internal/org.apache.dubbo.remoting.telnet.TelnetHandler create mode 100644 dubbo-rpc/dubbo-rpc-grpc/src/test/resources/log4j.xml diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/netty4/Http2WireProtocol.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/netty4/Http2WireProtocol.java index c96e8618655..95296522e50 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/netty4/Http2WireProtocol.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/netty4/Http2WireProtocol.java @@ -30,9 +30,12 @@ public void configServerPipeline(ChannelHandlerContext ctx) { .frameListener(frameListener()) .build(); p.addLast(handler); + configServerPipeline0(ctx); } - abstract Http2FrameListener frameListener(); + protected abstract Http2FrameListener frameListener(); + + protected abstract void configServerPipeline0(ChannelHandlerContext ctx); @Override public void configClientPipeline(ChannelHandlerContext ctx) { diff --git a/dubbo-rpc/dubbo-rpc-grpc/src/main/java/org/apache/dubbo/rpc/protocol/grpc/GrpcElf.java b/dubbo-rpc/dubbo-rpc-grpc/src/main/java/org/apache/dubbo/rpc/protocol/grpc/GrpcElf.java new file mode 100644 index 00000000000..99d6edf7332 --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-grpc/src/main/java/org/apache/dubbo/rpc/protocol/grpc/GrpcElf.java @@ -0,0 +1,148 @@ +package org.apache.dubbo.rpc.protocol.grpc; + +import io.netty.util.AsciiString; + +public class GrpcElf { + public static final AsciiString GRPC_STATUS = AsciiString.cached("grpc-status"); + public static final AsciiString GRPC_MESSAGE = AsciiString.cached("grpc-message"); + public static final AsciiString GRPC_ENCODING = AsciiString.cached("grpc-encoding"); + public static final AsciiString GRPC_TIMEOUT = AsciiString.cached("grpc-timeout"); + public static final AsciiString GRPC_ACCEPT_ENCODING = AsciiString.cached("grpc-accept-encoding"); + + public static final AsciiString APPLICATION_GRPC = AsciiString.cached("application/grpc"); + public static final AsciiString TEXT_MIME = AsciiString.cached("text/plain; encoding=utf-8"); + public static final AsciiString GRPC_JSON = AsciiString.cached("application/grpc+json"); + public static final AsciiString GRPC_PROTO = AsciiString.cached("application/grpc+proto"); + + + /** + * Indicates whether or not the given value is a valid gRPC content-type. + */ + public static boolean isGrpcContentType(CharSequence contentType) { + if (contentType == null) { + return false; + } + + if (APPLICATION_GRPC.length() > contentType.length()) { + return false; + } + + if (APPLICATION_GRPC.contentEquals(contentType)) { + return true; + } + if (!AsciiString.of(contentType).startsWith(APPLICATION_GRPC)) { + // Not a gRPC content-type. + return false; + } + + if (contentType.length() == APPLICATION_GRPC.length()) { + // The strings match exactly. + return true; + } + + // The contentType matches, but is longer than the expected string. + // We need to support variations on the content-type (e.g. +proto, +json) as defined by the + // gRPC wire spec. + char nextChar = contentType.charAt(APPLICATION_GRPC.length()); + return nextChar == '+' || nextChar == ';'; + } + + /** + * All error codes identified by the HTTP/2 spec. Used in GOAWAY and RST_STREAM frames. + */ + //public enum Http2Error { + // /** + // * Servers implementing a graceful shutdown of the connection will send {@code GOAWAY} with + // * {@code NO_ERROR}. In this case it is important to indicate to the application that the + // * request should be retried (i.e. {@link Status#UNAVAILABLE}). + // */ + // NO_ERROR(0x0, Status.UNAVAILABLE), + // PROTOCOL_ERROR(0x1, Status.INTERNAL), + // INTERNAL_ERROR(0x2, Status.INTERNAL), + // FLOW_CONTROL_ERROR(0x3, Status.INTERNAL), + // SETTINGS_TIMEOUT(0x4, Status.INTERNAL), + // STREAM_CLOSED(0x5, Status.INTERNAL), + // FRAME_SIZE_ERROR(0x6, Status.INTERNAL), + // REFUSED_STREAM(0x7, Status.UNAVAILABLE), + // CANCEL(0x8, Status.CANCELLED), + // COMPRESSION_ERROR(0x9, Status.INTERNAL), + // CONNECT_ERROR(0xA, Status.INTERNAL), + // ENHANCE_YOUR_CALM(0xB, Status.RESOURCE_EXHAUSTED.withDescription("Bandwidth exhausted")), + // INADEQUATE_SECURITY(0xC, Status.PERMISSION_DENIED.withDescription("Permission denied as " + // + "protocol is not secure enough to call")), + // HTTP_1_1_REQUIRED(0xD, Status.UNKNOWN); + // + // // Populate a mapping of code to enum value for quick look-up. + // private static final Http2Error[] codeMap = buildHttp2CodeMap(); + // private final int code; + // // Status is not guaranteed to be deeply immutable. Don't care though, since that's only true + // // when there are exceptions in the Status, which is not true here. + // @SuppressWarnings("ImmutableEnumChecker") + // private final Status status; + // + // Http2Error(int code, Status status) { + // this.code = code; + // this.status = status.augmentDescription("HTTP/2 error code: " + this.name()); + // } + // + // private static Http2Error[] buildHttp2CodeMap() { + // Http2Error[] errors = Http2Error.values(); + // int size = (int) errors[errors.length - 1].code() + 1; + // Http2Error[] http2CodeMap = new Http2Error[size]; + // for (Http2Error error : errors) { + // int index = (int) error.code(); + // http2CodeMap[index] = error; + // } + // return http2CodeMap; + // } + // + // /** + // * Looks up the HTTP/2 error code enum value for the specified code. + // * + // * @param code an HTTP/2 error code value. + // * @return the HTTP/2 error code enum or {@code null} if not found. + // */ + // public static Http2Error forCode(long code) { + // if (code >= codeMap.length || code < 0) { + // return null; + // } + // return codeMap[(int) code]; + // } + // + // /** + // * Looks up the {@link Status} from the given HTTP/2 error code. This is preferred over {@code + // * forCode(code).status()}, to more easily conform to HTTP/2: + // * + // *
Unknown or unsupported error codes MUST NOT trigger any special behavior. + // * These MAY be treated by an implementation as being equivalent to INTERNAL_ERROR.
+ // * + // * @param code the HTTP/2 error code. + // * @return a {@link Status} representing the given error. + // */ + // public static Status statusForCode(long code) { + // Http2Error error = forCode(code); + // if (error == null) { + // // This "forgets" the message of INTERNAL_ERROR while keeping the same status code. + // Status.Code statusCode = INTERNAL_ERROR.status().getCode(); + // return Status.fromCodeValue(statusCode.value()) + // .withDescription("Unrecognized HTTP/2 error code: " + code); + // } + // + // return error.status(); + // } + // + // /** + // * Gets the code for this error used on the wire. + // */ + // public long code() { + // return code; + // } + // + // /** + // * Gets the {@link Status} associated with this HTTP/2 code. + // */ + // public Status status() { + // return status; + // } + //} +} diff --git a/dubbo-rpc/dubbo-rpc-grpc/src/main/java/org/apache/dubbo/rpc/protocol/grpc/GrpcHttp2FrameListener.java b/dubbo-rpc/dubbo-rpc-grpc/src/main/java/org/apache/dubbo/rpc/protocol/grpc/GrpcHttp2FrameListener.java new file mode 100644 index 00000000000..ea94e9977f0 --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-grpc/src/main/java/org/apache/dubbo/rpc/protocol/grpc/GrpcHttp2FrameListener.java @@ -0,0 +1,108 @@ +package org.apache.dubbo.rpc.protocol.grpc; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufUtil; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.http.HttpHeaderNames; +import io.netty.handler.codec.http.HttpMethod; +import io.netty.handler.codec.http.HttpUtil; +import io.netty.handler.codec.http2.Http2Connection; +import io.netty.handler.codec.http2.Http2Exception; +import io.netty.handler.codec.http2.Http2FrameAdapter; +import io.netty.handler.codec.http2.Http2Headers; +import io.netty.handler.codec.http2.Http2Stream; +import io.netty.util.AsciiString; +import io.netty.util.ReferenceCountUtil; +import org.apache.dubbo.remoting.netty4.DubboHttp2ConnectionHandler; +import org.apache.dubbo.rpc.Invocation; +import org.apache.dubbo.rpc.RpcInvocation; + +public class GrpcHttp2FrameListener extends Http2FrameAdapter { + + protected Http2Connection.PropertyKey streamKey = null; + + @Override + public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) + throws Http2Exception { + System.out.println("onDataRead:" + streamId); + final DubboHttp2ConnectionHandler connectionHandler = ctx.pipeline().get(DubboHttp2ConnectionHandler.class); + Http2Connection connection = connectionHandler.encoder().connection(); + Http2Stream stream = connection.stream(streamId); + Http2Request request = stream == null ? null : (Http2Request) stream.getProperty(streamKey); + + if (request == null || request.getStreamId() != streamId) { + System.out.println("received remote data from streamId:" + streamId + ", but not found payload."); + int processed = data.readableBytes() + padding; + return processed; + } + + request.cumulate(data, ctx.alloc()); + int processed = data.readableBytes() + padding; + if (endOfStream) { + Invocation invocation = buildInvocation(request.getHeaders(), request.getData()); + ctx.pipeline().fireChannelRead(invocation); + } + return processed; + } + + private Invocation buildInvocation(Http2Headers http2Headers, ByteBuf data) { + return new RpcInvocation(); + } + + @Override + public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int padding, + boolean endStream) throws Http2Exception { + System.out.println("onHeadersRead" + streamId); + + final DubboHttp2ConnectionHandler connectionHandler = ctx.pipeline().get(DubboHttp2ConnectionHandler.class); + if (headers.path() == null) { + System.out.println("Expected path but is missing"); + return; + } + + final String path = headers.path().toString(); + if (path.charAt(0) != '/') { + System.out.println("Expected path but is missing1"); + return; + } + + final CharSequence contentType = HttpUtil.getMimeType(headers.get(HttpHeaderNames.CONTENT_TYPE)); + if (contentType == null) { + System.out.println("Expected path but is missing2"); + return; + } + + if (!GrpcElf.isGrpcContentType(contentType)) { + System.out.println("Expected path but is missing3"); + return; + } + + if (!HttpMethod.POST.asciiName().equals(headers.method())) { + System.out.println("Expected path but is missing4"); + return; + } + + String marshaller; + if (AsciiString.contentEquals(contentType, GrpcElf.APPLICATION_GRPC) || AsciiString.contentEquals(contentType, GrpcElf.GRPC_PROTO)) { + marshaller = "protobuf"; + } else if (AsciiString.contentEquals(contentType, GrpcElf.GRPC_JSON)) { + marshaller = "protobuf-json"; + } else { + System.out.println("Expected path but is missing5"); + return; + } + + Http2Connection connection = connectionHandler.encoder().connection(); + Http2Stream http2Stream = connection.stream(streamId); + if (streamKey == null) { + streamKey = connection.newKey(); + } + Http2Request request = new Http2Request(streamId, http2Stream, headers, streamKey, marshaller); + http2Stream.setProperty(streamKey, request); + + if (endStream) { + + } + } + +} diff --git a/dubbo-rpc/dubbo-rpc-grpc/src/main/java/org/apache/dubbo/rpc/protocol/grpc/GrpcHttp2Protocol.java b/dubbo-rpc/dubbo-rpc-grpc/src/main/java/org/apache/dubbo/rpc/protocol/grpc/GrpcHttp2Protocol.java new file mode 100644 index 00000000000..946be958743 --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-grpc/src/main/java/org/apache/dubbo/rpc/protocol/grpc/GrpcHttp2Protocol.java @@ -0,0 +1,18 @@ +package org.apache.dubbo.rpc.protocol.grpc; + +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.http2.Http2FrameListener; +import org.apache.dubbo.remoting.netty4.Http2WireProtocol; + +public class GrpcHttp2Protocol extends Http2WireProtocol { + + @Override + protected Http2FrameListener frameListener() { + return new GrpcHttp2FrameListener(); + } + + @Override + protected void configServerPipeline0(ChannelHandlerContext ctx) { + // response -> data header/data + } +} diff --git a/dubbo-rpc/dubbo-rpc-grpc/src/main/java/org/apache/dubbo/rpc/protocol/grpc/Http2Request.java b/dubbo-rpc/dubbo-rpc-grpc/src/main/java/org/apache/dubbo/rpc/protocol/grpc/Http2Request.java new file mode 100644 index 00000000000..9fe1007226e --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-grpc/src/main/java/org/apache/dubbo/rpc/protocol/grpc/Http2Request.java @@ -0,0 +1,73 @@ +package org.apache.dubbo.rpc.protocol.grpc; + +import java.util.HashMap; +import java.util.Map; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; +import io.netty.handler.codec.http2.Http2Connection; +import io.netty.handler.codec.http2.Http2Headers; +import io.netty.handler.codec.http2.Http2Stream; + +public class Http2Request { + private int streamId; + private ByteBuf commulation; + private volatile Http2Headers headers; + private Http2Stream http2Stream; + private Http2Connection.PropertyKey streamKey; + private String marshaller; + + public Http2Request(int streamId, Http2Stream http2Stream + , Http2Headers headers + //, AbstractHttp2CodecHandler http2CodecHandler + , Http2Connection.PropertyKey streamKey, String marshaller) { + this.http2Stream = http2Stream; + this.headers = headers; + //this.http2CodecHandler = http2CodecHandler; + this.streamKey = streamKey; + this.marshaller = marshaller; + } + + public Http2Headers getHeaders() { + return headers; + } + + public ByteBuf getData() { + return commulation; + } + + public int getStreamId() { + return streamId; + } + + public void cumulate(ByteBuf byteBuf, ByteBufAllocator allocator) { + commulation = cumulate(allocator, commulation, byteBuf); + } + + public ByteBuf cumulate(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf in) { + final ByteBuf buffer; + if (cumulation.writerIndex() > cumulation.maxCapacity() - in.readableBytes() + || cumulation.refCnt() > 1 || cumulation.isReadOnly()) { + // Expand cumulation (by replace it) when either there is not more room in the buffer + // or if the refCnt is greater then 1 which may happen when the user use slice().retain() or + // duplicate().retain() or if its read-only. + // + // See: + // - https://github.com/netty/netty/issues/2327 + // - https://github.com/netty/netty/issues/1764 + buffer = expandCumulation(alloc, cumulation, in.readableBytes()); + } else { + buffer = cumulation; + } + buffer.writeBytes(in); + return buffer; + } + + private ByteBuf expandCumulation(ByteBufAllocator alloc, ByteBuf cumulation, int readable) { + ByteBuf oldCumulation = cumulation; + cumulation = alloc.buffer(oldCumulation.readableBytes() + readable); + cumulation.writeBytes(oldCumulation); + oldCumulation.release(); + return cumulation; + } +} diff --git a/dubbo-rpc/dubbo-rpc-grpc/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.remoting.netty4.Http2WireProtocol b/dubbo-rpc/dubbo-rpc-grpc/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.remoting.netty4.Http2WireProtocol new file mode 100644 index 00000000000..339b069423e --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-grpc/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.remoting.netty4.Http2WireProtocol @@ -0,0 +1 @@ +grpc=org.apache.dubbo.rpc.protocol.grpc.GrpcHttp2Protocol \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocolTest.java b/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocolTest.java new file mode 100644 index 00000000000..0a9c2c8249b --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocolTest.java @@ -0,0 +1,230 @@ +/* + * 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.rpc.protocol.dubbo; + + +import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.extension.ExtensionLoader; +import org.apache.dubbo.common.utils.NetUtils; +import org.apache.dubbo.rpc.Protocol; +import org.apache.dubbo.rpc.ProxyFactory; +import org.apache.dubbo.rpc.RpcException; +import org.apache.dubbo.rpc.model.ApplicationModel; +import org.apache.dubbo.rpc.protocol.dubbo.support.DemoService; +import org.apache.dubbo.rpc.protocol.dubbo.support.DemoServiceImpl; +import org.apache.dubbo.rpc.protocol.dubbo.support.NonSerialized; +import org.apache.dubbo.rpc.protocol.dubbo.support.ProtocolUtils; +import org.apache.dubbo.rpc.protocol.dubbo.support.RemoteService; +import org.apache.dubbo.rpc.protocol.dubbo.support.RemoteServiceImpl; +import org.apache.dubbo.rpc.protocol.dubbo.support.Type; +import org.apache.dubbo.rpc.service.EchoService; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * ProxiesTest + */ + +public class DubboProtocolTest { + private Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); + private ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); + + @AfterAll + public static void after() { + //ProtocolUtils.closeAll(); + ApplicationModel.getServiceRepository().unregisterService(DemoService.class); + } + + @BeforeAll + public static void setup() { + ApplicationModel.getServiceRepository().registerService(DemoService.class); + } + + @Test + public void testDemoProtocol() throws Exception { + DemoService service = new DemoServiceImpl(); + int port = NetUtils.getAvailablePort(); + protocol.export(proxy.getInvoker(service, DemoService.class, URL.valueOf("grpc://127.0.0.1:" + port + "/" + DemoService.class.getName() + "?codec=exchange"))); + service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("grpc://127.0.0.1:" + port + "/" + DemoService.class.getName() + "?codec=exchange").addParameter("timeout", + 3000L))); + assertEquals(service.getSize(new String[]{"", "", ""}), 3); + } + + @Test + public void testDubboProtocol() throws Exception { + DemoService service = new DemoServiceImpl(); + int port = NetUtils.getAvailablePort(); + protocol.export(proxy.getInvoker(service, DemoService.class, URL.valueOf("dubbo://127.0.0.1:" + port + "/" + DemoService.class.getName()))); + service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("dubbo://127.0.0.1:" + port + "/" + DemoService.class.getName()).addParameter("timeout", + 3000L))); + assertEquals(service.enumlength(new Type[]{}), Type.Lower); + assertEquals(service.getSize(null), -1); + assertEquals(service.getSize(new String[]{"", "", ""}), 3); + Map map = new HashMap(); + map.put("aa", "bb"); + Set set = service.keys(map); + assertEquals(set.size(), 1); + assertEquals(set.iterator().next(), "aa"); + service.invoke("dubbo://127.0.0.1:" + port + "/" + DemoService.class.getName() + "", "invoke"); + + service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("dubbo://127.0.0.1:" + port + "/" + DemoService.class.getName() + "?client=netty").addParameter("timeout", + 3000L))); + // test netty client + StringBuffer buf = new StringBuffer(); + for (int i = 0; i < 1024 * 32 + 32; i++) + buf.append('A'); + System.out.println(service.stringLength(buf.toString())); + + // cast to EchoService + EchoService echo = proxy.getProxy(protocol.refer(EchoService.class, URL.valueOf("dubbo://127.0.0.1:" + port + "/" + DemoService.class.getName() + "?client=netty").addParameter("timeout", + 3000L))); + assertEquals(echo.$echo(buf.toString()), buf.toString()); + assertEquals(echo.$echo("test"), "test"); + assertEquals(echo.$echo("abcdefg"), "abcdefg"); + assertEquals(echo.$echo(1234), 1234); + } + +// @Test +// public void testDubboProtocolWithMina() throws Exception { +// DemoService service = new DemoServiceImpl(); +// int port = NetUtils.getAvailablePort(); +// protocol.export(proxy.getInvoker(service, DemoService.class, URL.valueOf("dubbo://127.0.0.1:" + port + "/" + DemoService.class.getName()).addParameter(Constants.SERVER_KEY, "mina"))); +// service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("dubbo://127.0.0.1:" + port + "/" + DemoService.class.getName()).addParameter(Constants.CLIENT_KEY, "mina").addParameter("timeout", +// 3000L))); +// for (int i = 0; i < 10; i++) { +// assertEquals(service.enumlength(new Type[]{}), Type.Lower); +// assertEquals(service.getSize(null), -1); +// assertEquals(service.getSize(new String[]{"", "", ""}), 3); +// } +// Map map = new HashMap(); +// map.put("aa", "bb"); +// for (int i = 0; i < 10; i++) { +// Set set = service.keys(map); +// assertEquals(set.size(), 1); +// assertEquals(set.iterator().next(), "aa"); +// service.invoke("dubbo://127.0.0.1:" + port + "/" + DemoService.class.getName() + "", "invoke"); +// } +// +// service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("dubbo://127.0.0.1:" + port + "/" + DemoService.class.getName() + "?client=mina").addParameter("timeout", +// 3000L))); +// // test netty client +// StringBuffer buf = new StringBuffer(); +// for (int i = 0; i < 1024 * 32 + 32; i++) +// buf.append('A'); +// System.out.println(service.stringLength(buf.toString())); +// +// // cast to EchoService +// EchoService echo = proxy.getProxy(protocol.refer(EchoService.class, URL.valueOf("dubbo://127.0.0.1:" + port + "/" + DemoService.class.getName() + "?client=mina").addParameter("timeout", +// 3000L))); +// for (int i = 0; i < 10; i++) { +// assertEquals(echo.$echo(buf.toString()), buf.toString()); +// assertEquals(echo.$echo("test"), "test"); +// assertEquals(echo.$echo("abcdefg"), "abcdefg"); +// assertEquals(echo.$echo(1234), 1234); +// } +// } + + @Test + public void testDubboProtocolMultiService() throws Exception { +// DemoService service = new DemoServiceImpl(); +// protocol.export(proxy.getInvoker(service, DemoService.class, URL.valueOf("dubbo://127.0.0.1:9010/" + DemoService.class.getName()))); +// service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("dubbo://127.0.0.1:9010/" + DemoService.class.getName()).addParameter("timeout", +// 3000L))); + + RemoteService remote = new RemoteServiceImpl(); + + ApplicationModel.getServiceRepository().registerService(RemoteService.class); + + int port = NetUtils.getAvailablePort(); + protocol.export(proxy.getInvoker(remote, RemoteService.class, URL.valueOf("dubbo://127.0.0.1:" + port + "/" + RemoteService.class.getName()))); + remote = proxy.getProxy(protocol.refer(RemoteService.class, URL.valueOf("dubbo://127.0.0.1:" + port + "/" + RemoteService.class.getName()).addParameter("timeout", + 3000L))); + +// service.sayHello("world"); + + // test netty client +// assertEquals("world", service.echo("world")); + assertEquals("hello world@" + RemoteServiceImpl.class.getName(), remote.sayHello("world")); + +// can't find target service addresses + EchoService remoteEecho = (EchoService) remote; + assertEquals(remoteEecho.$echo("ok"), "ok"); + } + + @Test + public void testPerm() throws Exception { + DemoService service = new DemoServiceImpl(); + int port = NetUtils.getAvailablePort(); + protocol.export(proxy.getInvoker(service, DemoService.class, URL.valueOf("dubbo://127.0.0.1:" + port + "/" + DemoService.class.getName() + "?codec=exchange"))); + service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("dubbo://127.0.0.1:" + port + "/" + DemoService.class.getName() + "?codec=exchange").addParameter("timeout", + 3000L))); + long start = System.currentTimeMillis(); + for (int i = 0; i < 1000; i++) + service.getSize(new String[]{"", "", ""}); + System.out.println("take:" + (System.currentTimeMillis() - start)); + } + + @Test + public void testNonSerializedParameter() throws Exception { + DemoService service = new DemoServiceImpl(); + int port = NetUtils.getAvailablePort(); + protocol.export(proxy.getInvoker(service, DemoService.class, URL.valueOf("dubbo://127.0.0.1:" + port + "/" + DemoService.class.getName() + "?codec=exchange"))); + service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("dubbo://127.0.0.1:" + port + "/" + DemoService.class.getName() + "?codec=exchange").addParameter("timeout", + 3000L))); + try { + service.nonSerializedParameter(new NonSerialized()); + Assertions.fail(); + } catch (RpcException e) { + Assertions.assertTrue(e.getMessage().contains("org.apache.dubbo.rpc.protocol.dubbo.support.NonSerialized must implement java.io.Serializable")); + } + } + + @Test + public void testReturnNonSerialized() throws Exception { + DemoService service = new DemoServiceImpl(); + int port = NetUtils.getAvailablePort(); + protocol.export(proxy.getInvoker(service, DemoService.class, URL.valueOf("dubbo://127.0.0.1:" + port + "/" + DemoService.class.getName() + "?codec=exchange"))); + service = proxy.getProxy(protocol.refer(DemoService.class, URL.valueOf("dubbo://127.0.0.1:" + port + "/" + DemoService.class.getName() + "?codec=exchange").addParameter("timeout", + 3000L))); + try { + service.returnNonSerialized(); + Assertions.fail(); + } catch (RpcException e) { + Assertions.assertTrue(e.getMessage().contains("org.apache.dubbo.rpc.protocol.dubbo.support.NonSerialized must implement java.io.Serializable")); + } + } + + @Test + public void testRemoteApplicationName() throws Exception { + DemoService service = new DemoServiceImpl(); + int port = NetUtils.getAvailablePort(); + URL url = URL.valueOf("dubbo://127.0.0.1:" + port + "/" + DemoService.class.getName() + "?codec=exchange").addParameter("timeout", + 3000L).addParameter("application", "consumer"); + protocol.export(proxy.getInvoker(service, DemoService.class, url)); + service = proxy.getProxy(protocol.refer(DemoService.class, url)); + assertEquals(service.getRemoteApplicationName(), "consumer"); + } +} diff --git a/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/CustomArgument.java b/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/CustomArgument.java new file mode 100644 index 00000000000..4391988aaf3 --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/CustomArgument.java @@ -0,0 +1,51 @@ +/* + * 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.rpc.protocol.dubbo.support; + +import java.io.Serializable; + + +@SuppressWarnings("serial") +public class CustomArgument implements Serializable { + + Type type; + String name; + + public CustomArgument() { + } + public CustomArgument(Type type, String name) { + super(); + this.type = type; + this.name = name; + } + + public Type getType() { + return type; + } + + public void setType(Type type) { + this.type = type; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoRequest.java b/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoRequest.java new file mode 100644 index 00000000000..d7de5ecf11d --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoRequest.java @@ -0,0 +1,58 @@ +/* + * 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.rpc.protocol.dubbo.support; + +import java.io.Serializable; + +/** + * TestRequest. + */ + +class DemoRequest implements Serializable { + private static final long serialVersionUID = -2579095288792344869L; + + private String mServiceName; + + private String mMethodName; + + private Class[] mParameterTypes; + + private Object[] mArguments; + + public DemoRequest(String serviceName, String methodName, Class[] parameterTypes, Object[] args) { + mServiceName = serviceName; + mMethodName = methodName; + mParameterTypes = parameterTypes; + mArguments = args; + } + + public String getServiceName() { + return mServiceName; + } + + public String getMethodName() { + return mMethodName; + } + + public Class[] getParameterTypes() { + return mParameterTypes; + } + + public Object[] getArguments() { + return mArguments; + } +} \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoService.java b/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoService.java new file mode 100644 index 00000000000..40ca4fbce71 --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoService.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.dubbo.rpc.protocol.dubbo.support; + +import java.util.Map; +import java.util.Set; + + +/** + * TestService + */ + +public interface DemoService { + void sayHello(String name); + + Set keys(Map map); + + String echo(String text); + + Map echo(Map map); + + long timestamp(); + + String getThreadName(); + + int getSize(String[] strs); + + int getSize(Object[] os); + + Object invoke(String service, String method) throws Exception; + + int stringLength(String str); + + Type enumlength(Type... types); + + Type getType(Type type); + + String get(CustomArgument arg1); + + int getInt(int arg); + + void nonSerializedParameter(NonSerialized ns); + + NonSerialized returnNonSerialized(); + + long add(int a, long b); + + int getPerson(Person person); + + int getPerson(Person person1, Person perso2); + + String getPerson(Man man); + + String getRemoteApplicationName(); +} \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoServiceImpl.java b/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoServiceImpl.java new file mode 100644 index 00000000000..0d56e3e75b0 --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/DemoServiceImpl.java @@ -0,0 +1,129 @@ +/* + * 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.rpc.protocol.dubbo.support; + +import org.apache.dubbo.rpc.RpcContext; + +import java.util.Map; +import java.util.Set; + +/** + * DemoServiceImpl + */ +public class DemoServiceImpl implements DemoService { + public DemoServiceImpl() { + super(); + } + + public void sayHello(String name) { + System.out.println("hello " + name); + } + + public String echo(String text) { + return text; + } + + public Map echo(Map map) { + return map; + } + + public long timestamp() { + return System.currentTimeMillis(); + } + + public String getThreadName() { + return Thread.currentThread().getName(); + } + + public int getSize(String[] strs) { + if (strs == null) + return -1; + return strs.length; + } + + public int getSize(Object[] os) { + if (os == null) + return -1; + return os.length; + } + + public Object invoke(String service, String method) throws Exception { + System.out.println("RpcContext.getContext().getRemoteHost()=" + RpcContext.getContext().getRemoteHost()); + return service + ":" + method; + } + + public Type enumlength(Type... types) { + if (types.length == 0) + return Type.Lower; + return types[0]; + } + + public Type getType(Type type) { + return type; + } + + public int stringLength(String str) { + return str.length(); + } + + public String get(CustomArgument arg1) { + return arg1.toString(); + } + + public int getInt(int arg) { + return arg; + } + + public Person gerPerson(Person person) { + return person; + } + + public Set keys(Map map) { + return map == null ? null : map.keySet(); + } + + public void nonSerializedParameter(NonSerialized ns) { + } + + public NonSerialized returnNonSerialized() { + return new NonSerialized(); + } + + public long add(int a, long b) { + return a + b; + } + + @Override + public int getPerson(Person person) { + return person.getAge(); + } + + @Override + public int getPerson(Person person1, Person perso2) { + return person1.getAge() + perso2.getAge(); + } + + @Override + public String getPerson(Man man) { + return man.getName(); + } + + @Override + public String getRemoteApplicationName() { + return RpcContext.getContext().getRemoteApplicationName(); + } +} \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/EnumBak.java b/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/EnumBak.java new file mode 100644 index 00000000000..0b74b1033c6 --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/EnumBak.java @@ -0,0 +1,201 @@ +/* + * 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.rpc.protocol.dubbo.support; + + +import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.extension.ExtensionLoader; +import org.apache.dubbo.common.utils.NetUtils; +import org.apache.dubbo.rpc.Invoker; +import org.apache.dubbo.rpc.Protocol; +import org.apache.dubbo.rpc.ProxyFactory; +import org.apache.dubbo.rpc.model.ApplicationModel; +import org.apache.dubbo.rpc.service.GenericService; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.Map; + +public class EnumBak { + + private Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); + private ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); + + @Test + public void testNormal() { + int port = NetUtils.getAvailablePort(); + URL serviceurl = URL.valueOf("dubbo://127.0.0.1:" + port + "/test?proxy=jdk" + + "&interface=" + DemoService.class.getName() + + "&timeout=" + Integer.MAX_VALUE + ); + DemoService demo = new DemoServiceImpl(); + + ApplicationModel.getServiceRepository().registerService("test", DemoService.class); + + Invoker invoker = proxy.getInvoker(demo, DemoService.class, serviceurl); + protocol.export(invoker); + + URL consumerurl = serviceurl; + Invoker reference = protocol.refer(DemoService.class, consumerurl); + DemoService demoProxy = (DemoService) proxy.getProxy(reference); +// System.out.println(demoProxy.getThreadName()); + System.out.println(demoProxy.getInt(Integer.MIN_VALUE)); + Assertions.assertEquals(Integer.MIN_VALUE, demoProxy.getInt(Integer.MIN_VALUE)); + +// invoker.destroy(); + reference.destroy(); + } + + @Disabled + @Test + public void testExportService() throws InterruptedException { + int port = NetUtils.getAvailablePort(); + URL serviceurl = URL.valueOf("dubbo://127.0.0.1:" + port + "/test?proxy=jdk&timeout=" + Integer.MAX_VALUE + ); + DemoService demo = new DemoServiceImpl(); + Invoker invoker = proxy.getInvoker(demo, DemoService.class, serviceurl); + protocol.export(invoker); + synchronized (EnumBak.class) { + EnumBak.class.wait(); + } + +// URL consumerurl = serviceurl; +// Invoker reference = protocol.refer(DemoService.class, consumerurl); +// DemoService demoProxy = (DemoService)proxyFactory.createProxy(reference); +//// System.out.println(demoProxy.getThreadName()); +// System.out.println("byte:"+demoProxy.getbyte((byte)-128)); +// +// invoker.destroy(); +// reference.destroy(); + } + + @Test + public void testNormalEnum() { + int port = NetUtils.getAvailablePort(); + URL serviceurl = URL.valueOf("dubbo://127.0.0.1:" + port + "/test?timeout=" + Integer.MAX_VALUE + ); + DemoService demo = new DemoServiceImpl(); + + ApplicationModel.getServiceRepository().registerService("test", DemoService.class); + + Invoker invoker = proxy.getInvoker(demo, DemoService.class, serviceurl); + protocol.export(invoker); + + URL consumerurl = serviceurl; + Invoker reference = protocol.refer(DemoService.class, consumerurl); + DemoService demoProxy = (DemoService) proxy.getProxy(reference); + Type type = demoProxy.enumlength(Type.High); + System.out.println(type); + Assertions.assertEquals(Type.High, type); + + invoker.destroy(); + reference.destroy(); + } + + // verify compatibility when 2.0.5 invokes 2.0.3 + @Disabled + @Test + public void testEnumCompat() { + int port = NetUtils.getAvailablePort(); + URL consumerurl = URL.valueOf("dubbo://127.0.0.1:" + port + "/test?timeout=" + Integer.MAX_VALUE + ); + + ApplicationModel.getServiceRepository().registerService(DemoService.class); + + Invoker reference = protocol.refer(DemoService.class, consumerurl); + DemoService demoProxy = (DemoService) proxy.getProxy(reference); + Type type = demoProxy.enumlength(Type.High); + System.out.println(type); + Assertions.assertEquals(Type.High, type); + reference.destroy(); + } + + // verify compatibility when 2.0.5 invokes 2.0.3 + @Disabled + @Test + public void testGenricEnumCompat() { + int port = NetUtils.getAvailablePort(); + URL consumerurl = URL.valueOf("dubbo://127.0.0.1:" + port + "/test?timeout=" + Integer.MAX_VALUE + ); + Invoker reference = protocol.refer(GenericService.class, consumerurl); + + GenericService demoProxy = (GenericService) proxy.getProxy(reference); + Object obj = demoProxy.$invoke("enumlength", new String[]{Type[].class.getName()}, new Object[]{new Type[]{Type.High, Type.High}}); + System.out.println("obj---------->" + obj); + reference.destroy(); + } + + // verify compatibility when 2.0.5 invokes 2.0.3, enum in custom parameter + @Disabled + @Test + public void testGenricCustomArg() { + + int port = NetUtils.getAvailablePort(); + URL consumerurl = URL.valueOf("dubbo://127.0.0.1:" + port + "/test?timeout=2000000" + ); + Invoker reference = protocol.refer(GenericService.class, consumerurl); + + GenericService demoProxy = (GenericService) proxy.getProxy(reference); + Map arg = new HashMap(); + arg.put("type", "High"); + arg.put("name", "hi"); + + Object obj = demoProxy.$invoke("get", new String[]{"org.apache.dubbo.rpc.CustomArgument"}, new Object[]{arg}); + System.out.println("obj---------->" + obj); + reference.destroy(); + } + + @Disabled + @Test + public void testGenericExport() throws InterruptedException { + int port = NetUtils.getAvailablePort(); + //port = 20880; + URL serviceurl = URL.valueOf("dubbo://127.0.0.1:" + port + "/test?timeout=" + Integer.MAX_VALUE + ); + DemoService demo = new DemoServiceImpl(); + Invoker invoker = proxy.getInvoker(demo, DemoService.class, serviceurl); + protocol.export(invoker); + + + //SERVER + Thread.sleep(Integer.MAX_VALUE); + } + + @Test + public void testGenericEnum() throws InterruptedException { + int port = NetUtils.getAvailablePort(); + URL serviceurl = URL.valueOf("dubbo://127.0.0.1:" + port + "/test?timeout=" + Integer.MAX_VALUE + ); + DemoService demo = new DemoServiceImpl(); + Invoker invoker = proxy.getInvoker(demo, DemoService.class, serviceurl); + protocol.export(invoker); + + URL consumerurl = serviceurl; + + Invoker reference = protocol.refer(GenericService.class, consumerurl); + + GenericService demoProxy = (GenericService) proxy.getProxy(reference); + Object obj = demoProxy.$invoke("enumlength", new String[]{Type[].class.getName()}, new Object[]{new Type[]{Type.High, Type.High}}); + System.out.println("obj---------->" + obj); + + invoker.destroy(); + reference.destroy(); + } +} diff --git a/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/Man.java b/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/Man.java new file mode 100644 index 00000000000..440630ada1b --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/Man.java @@ -0,0 +1,46 @@ +/* + * 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.rpc.protocol.dubbo.support; + + +import java.io.Serializable; + +/** + * Man.java + */ +public class Man implements Serializable { + + private static final long serialVersionUID = 1L; + private String name; + private int age; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } +} \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/NonSerialized.java b/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/NonSerialized.java new file mode 100644 index 00000000000..1ec80e5c0cd --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/NonSerialized.java @@ -0,0 +1,24 @@ +/* + * 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.rpc.protocol.dubbo.support; + +/** + * NonSerialized + */ +public class NonSerialized { + +} diff --git a/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/Person.java b/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/Person.java new file mode 100644 index 00000000000..58cb712e3ad --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/Person.java @@ -0,0 +1,45 @@ +/* + * 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.rpc.protocol.dubbo.support; + +import java.io.Serializable; + +/** + * Person.java + */ +public class Person implements Serializable { + + private static final long serialVersionUID = 1L; + private String name; + private int age; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } +} \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/ProtocolUtils.java b/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/ProtocolUtils.java new file mode 100644 index 00000000000..493ae09b84a --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/ProtocolUtils.java @@ -0,0 +1,65 @@ +/* + * 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.rpc.protocol.dubbo.support; + +import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.extension.ExtensionLoader; +import org.apache.dubbo.rpc.Exporter; +import org.apache.dubbo.rpc.Invoker; +import org.apache.dubbo.rpc.Protocol; +import org.apache.dubbo.rpc.ProtocolServer; +import org.apache.dubbo.rpc.ProxyFactory; + + +import java.util.List; + +/** + * TODO Comment of ProtocolUtils + */ +public class ProtocolUtils { + + public static ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension(); + private static Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getAdaptiveExtension(); + + public static T refer(Class type, String url) { + return refer(type, URL.valueOf(url)); + } + + public static T refer(Class type, URL url) { + return proxy.getProxy(protocol.refer(type, url)); + } + + public static Invoker referInvoker(Class type, URL url) { + return (Invoker) protocol.refer(type, url); + } + + public static Exporter export(T instance, Class type, String url) { + return export(instance, type, URL.valueOf(url)); + } + + public static Exporter export(T instance, Class type, URL url) { + return protocol.export(proxy.getInvoker(instance, type, url)); + } + + //public static void closeAll() { + // DubboProtocol.getDubboProtocol().destroy(); + // List servers = DubboProtocol.getDubboProtocol().getServers(); + // for (ProtocolServer server : servers) { + // server.close(); + // } + //} +} \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/RemoteService.java b/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/RemoteService.java new file mode 100644 index 00000000000..0a84fa4bdc8 --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/RemoteService.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.rpc.protocol.dubbo.support; + +import java.rmi.Remote; +import java.rmi.RemoteException; + +public interface RemoteService extends Remote { + String sayHello(String name) throws RemoteException; + + String getThreadName() throws RemoteException; +} \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/RemoteServiceImpl.java b/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/RemoteServiceImpl.java new file mode 100644 index 00000000000..967bc4c7e15 --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/RemoteServiceImpl.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.rpc.protocol.dubbo.support; + +import org.apache.dubbo.rpc.RpcContext; + +import java.rmi.RemoteException; + +public class RemoteServiceImpl implements RemoteService { + public String getThreadName() throws RemoteException { + System.out.println("RpcContext.getContext().getRemoteHost()=" + RpcContext.getContext().getRemoteHost()); + return Thread.currentThread().getName(); + } + + public String sayHello(String name) throws RemoteException { + return "hello " + name + "@" + RemoteServiceImpl.class.getName(); + } +} \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/Type.java b/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/Type.java new file mode 100644 index 00000000000..893907e19a8 --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-grpc/src/test/java/org/apache/dubbo/rpc/protocol/dubbo/support/Type.java @@ -0,0 +1,21 @@ +/* + * 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.rpc.protocol.dubbo.support; + +public enum Type { + High, Normal, Lower +} \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-grpc/src/test/resources/META-INF/dubbo/internal/org.apache.dubbo.remoting.telnet.TelnetHandler b/dubbo-rpc/dubbo-rpc-grpc/src/test/resources/META-INF/dubbo/internal/org.apache.dubbo.remoting.telnet.TelnetHandler new file mode 100644 index 00000000000..60d4bee8172 --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-grpc/src/test/resources/META-INF/dubbo/internal/org.apache.dubbo.remoting.telnet.TelnetHandler @@ -0,0 +1 @@ +test=org.apache.dubbo.rpc.protocol.dubbo.decode.telnet.TestTelnetHandler \ No newline at end of file diff --git a/dubbo-rpc/dubbo-rpc-grpc/src/test/resources/log4j.xml b/dubbo-rpc/dubbo-rpc-grpc/src/test/resources/log4j.xml new file mode 100644 index 00000000000..09dba05e6cc --- /dev/null +++ b/dubbo-rpc/dubbo-rpc-grpc/src/test/resources/log4j.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + +