Fix obtaining remote application name on the server side (#13811)

This commit is contained in:
TomlongTK 2024-03-08 10:01:18 +08:00 committed by GitHub
parent b14e5e3a12
commit cb92fcc19e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 16 additions and 9 deletions

View File

@ -31,6 +31,7 @@ import java.net.InetSocketAddress;
import static org.apache.dubbo.common.constants.CommonConstants.REMOTE_APPLICATION_KEY;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.PROTOCOL_TIMEOUT_SERVER;
import static org.apache.dubbo.rpc.protocol.tri.TripleConstant.REMOTE_ADDRESS_KEY;
public abstract class AbstractServerCallListener implements ServerCallListener {
@ -56,7 +57,7 @@ public abstract class AbstractServerCallListener implements ServerCallListener {
((Http2CancelableStreamObserver<Object>) responseObserver).getCancellationContext());
}
InetSocketAddress remoteAddress =
(InetSocketAddress) invocation.getAttributes().remove("tri.remote.address");
(InetSocketAddress) invocation.getAttributes().remove(REMOTE_ADDRESS_KEY);
RpcContext.getServiceContext().setRemoteAddress(remoteAddress);
String remoteApp = (String) invocation.getAttributes().remove(TripleHeaderEnum.CONSUMER_APP_NAME_KEY);
if (null != remoteApp) {

View File

@ -34,7 +34,6 @@ import org.apache.dubbo.rpc.model.FrameworkModel;
import org.apache.dubbo.rpc.model.MethodDescriptor;
import org.apache.dubbo.rpc.protocol.tri.DescriptorUtils;
import org.apache.dubbo.rpc.protocol.tri.RpcInvocationBuildContext;
import org.apache.dubbo.rpc.protocol.tri.TripleConstant;
import org.apache.dubbo.rpc.protocol.tri.TripleHeaderEnum;
import org.apache.dubbo.rpc.protocol.tri.route.DefaultRequestRouter;
import org.apache.dubbo.rpc.protocol.tri.route.RequestRouter;
@ -47,6 +46,7 @@ import java.util.concurrent.Executor;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.COMMON_ERROR_USE_THREAD_POOL;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.INTERNAL_ERROR;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.PROTOCOL_FAILED_PARSE;
import static org.apache.dubbo.rpc.protocol.tri.TripleConstant.REMOTE_ADDRESS_KEY;
public abstract class AbstractServerTransportListener<HEADER extends RequestMetadata, MESSAGE extends HttpInputMessage>
implements HttpTransportListener<HEADER, MESSAGE> {
@ -209,7 +209,7 @@ public abstract class AbstractServerTransportListener<HEADER extends RequestMeta
inv.setTargetServiceUniqueName(url.getServiceKey());
inv.setReturnTypes(methodDescriptor.getReturnTypes());
inv.setObjectAttachments(StreamUtils.toAttachments(httpMetadata.headers()));
inv.put(TripleConstant.REMOTE_ADDRESS_KEY, httpChannel.remoteAddress());
inv.put(REMOTE_ADDRESS_KEY, httpChannel.remoteAddress());
inv.getAttributes().putAll(context.getAttributes());
// customizer RpcInvocation

View File

@ -34,6 +34,7 @@ import org.apache.dubbo.rpc.TriRpcStatus;
import org.apache.dubbo.rpc.model.FrameworkModel;
import org.apache.dubbo.rpc.protocol.tri.DescriptorUtils;
import org.apache.dubbo.rpc.protocol.tri.RpcInvocationBuildContext;
import org.apache.dubbo.rpc.protocol.tri.TripleHeaderEnum;
import org.apache.dubbo.rpc.protocol.tri.compressor.DeCompressor;
import org.apache.dubbo.rpc.protocol.tri.compressor.Identity;
import org.apache.dubbo.rpc.protocol.tri.h12.HttpMessageListener;
@ -119,6 +120,11 @@ public class GrpcHttp2ServerTransportListener extends GenericHttp2ServerTranspor
getContext().getServiceDescriptor().getInterfaceName(),
getContext().getMethodName()));
}
String consumerAppKey =
getHttpMetadata().headers().getFirst(TripleHeaderEnum.CONSUMER_APP_NAME_KEY.getHeader());
if (null != consumerAppKey) {
invocation.put(TripleHeaderEnum.CONSUMER_APP_NAME_KEY, consumerAppKey);
}
return invocation;
}

View File

@ -21,11 +21,10 @@ import org.apache.dubbo.remoting.http12.h2.H2StreamChannel;
import org.apache.dubbo.remoting.http12.h2.Http2ServerTransportListenerFactory;
import org.apache.dubbo.remoting.http12.h2.Http2TransportListener;
import org.apache.dubbo.rpc.model.FrameworkModel;
import org.apache.dubbo.rpc.protocol.tri.TripleHeaderEnum;
public class GrpcHttp2ServerTransportListenerFactory implements Http2ServerTransportListenerFactory {
public static final String CONTENT_TYPE = "application/grpc";
@Override
public Http2TransportListener newInstance(H2StreamChannel streamChannel, URL url, FrameworkModel frameworkModel) {
return new GrpcHttp2ServerTransportListener(streamChannel, url, frameworkModel);
@ -33,6 +32,6 @@ public class GrpcHttp2ServerTransportListenerFactory implements Http2ServerTrans
@Override
public boolean supportContentType(String contentType) {
return contentType != null && contentType.startsWith(CONTENT_TYPE);
return contentType != null && contentType.startsWith(TripleHeaderEnum.APPLICATION_GRPC.getHeader());
}
}

View File

@ -88,7 +88,7 @@ public class DefaultHttp11ServerTransportListener
return new AutoCompleteUnaryServerCallListener(invocation, invoker, serverChannelObserver);
case SERVER_STREAM:
serverChannelObserver = new Http1ServerStreamChannelObserver(httpChannel);
serverChannelObserver.setHeadersCustomizer((headers) ->
serverChannelObserver.setHeadersCustomizer(headers ->
headers.set(HttpHeaderNames.CONTENT_TYPE.getName(), MediaType.TEXT_EVENT_STREAM.getName()));
return new AutoCompleteServerStreamServerCallListener(invocation, invoker, serverChannelObserver);
default:

View File

@ -18,6 +18,7 @@ package org.apache.dubbo.rpc.protocol.tri.h12.http2;
import org.apache.dubbo.remoting.http12.h2.H2StreamChannel;
import org.apache.dubbo.rpc.model.FrameworkModel;
import org.apache.dubbo.rpc.protocol.tri.TripleHeaderEnum;
import org.apache.dubbo.rpc.protocol.tri.h12.ServerCallToObserverAdapter;
public class Http2ServerCallToObserverAdapter extends Http2ServerStreamObserver
@ -27,9 +28,9 @@ public class Http2ServerCallToObserverAdapter extends Http2ServerStreamObserver
public Http2ServerCallToObserverAdapter(FrameworkModel frameworkModel, H2StreamChannel h2StreamChannel) {
super(frameworkModel, h2StreamChannel);
setHeadersCustomizer((headers) -> {
setHeadersCustomizer(headers -> {
if (exceptionCode != 0) {
headers.set("tri-exception-code", String.valueOf(exceptionCode));
headers.set(TripleHeaderEnum.TRI_EXCEPTION_CODE.getHeader(), String.valueOf(exceptionCode));
}
});
}