Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ public interface CommonConstants {

String REMOTE_APPLICATION_KEY = "remote.application";

/**
* This constant is used when the consumer calls the provider, and the application name of the consumer is passed to the consumer.
* The provider can be used to analyze which consumer applications are called by the consumer.
*
* Note that the "_" is added here to avoid the same as the user-defined parameter name.
*/
String CONSUMER_APPLICATION_KEY = "_consumerApplication";

String ENABLED_KEY = "enabled";

String DISABLED_KEY = "disabled";
Expand All @@ -44,9 +52,9 @@ public interface CommonConstants {

Pattern COMMA_SPLIT_PATTERN = Pattern.compile("\\s*[,]+\\s*");

public final static String PATH_SEPARATOR = "/";
String PATH_SEPARATOR = "/";

public final static String PROTOCOL_SEPARATOR = "://";
String PROTOCOL_SEPARATOR = "://";

String REGISTRY_SEPARATOR = "|";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ protected RpcContext initialValue() {

private InetSocketAddress remoteAddress;

private String remoteApplicationName;
/**
* This field is used to pass the applicationName of the consumer to the Provider,
*/
private String consumerApplicationName;

@Deprecated
private List<Invoker<?>> invokers;
Expand Down Expand Up @@ -389,12 +392,17 @@ public RpcContext setRemoteAddress(InetSocketAddress address) {
return this;
}

public String getRemoteApplicationName() {
return remoteApplicationName;
/**
* Since the field is taken from the application name of the consumer,
* the get method is generally only used on the provider side.
* @return
*/
public String getConsumerApplicationName() {
return consumerApplicationName;
}

public RpcContext setRemoteApplicationName(String remoteApplicationName) {
this.remoteApplicationName = remoteApplicationName;
public RpcContext setConsumerApplicationName(String consumerApplicationName) {
this.consumerApplicationName = consumerApplicationName;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.dubbo.rpc.filter;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.rpc.Invocation;
Expand All @@ -26,7 +27,9 @@
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.RpcInvocation;

import static org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER;
import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER_APPLICATION_KEY;

/**
* ConsumerContextFilter set current RpcContext with invoker,invocation, local host, remote host and port
Expand All @@ -44,12 +47,23 @@ public ConsumerContextFilter() {

@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {

URL url = invoker.getUrl();

RpcContext.getContext()
.setInvoker(invoker)
.setInvocation(invocation)
.setLocalAddress(NetUtils.getLocalHost(), 0)
.setRemoteAddress(invoker.getUrl().getHost(),
invoker.getUrl().getPort());
.setRemoteAddress(url.getHost(), url.getPort());

/**
* Pass the consumer's application name to the provider, so the provider can be used to analyze the caller (ie consumer)
*/
String application = url.getParameter(APPLICATION_KEY);
if (application != null) {
RpcContext.getContext().setAttachment(CONSUMER_APPLICATION_KEY, application);
}

if (invocation instanceof RpcInvocation) {
((RpcInvocation) invocation).setInvoker(invoker);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@
import java.util.HashMap;
import java.util.Map;

import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER_APPLICATION_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER;
import static org.apache.dubbo.common.constants.CommonConstants.REMOTE_APPLICATION_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.TIMEOUT_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
import static org.apache.dubbo.rpc.Constants.ASYNC_KEY;
import static org.apache.dubbo.common.constants.RpcConstants.DUBBO_VERSION_KEY;
import static org.apache.dubbo.rpc.Constants.ASYNC_KEY;
import static org.apache.dubbo.rpc.Constants.FORCE_USE_TAG;
import static org.apache.dubbo.rpc.Constants.TOKEN_KEY;

Expand Down Expand Up @@ -77,7 +77,7 @@ public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcExcept
.setInvocation(invocation)
// .setAttachments(attachments) // merged from dubbox
.setLocalAddress(invoker.getUrl().getHost(), invoker.getUrl().getPort())
.setRemoteApplicationName(invoker.getUrl().getParameter(REMOTE_APPLICATION_KEY));
.setConsumerApplicationName(invoker.getUrl().getParameter(CONSUMER_APPLICATION_KEY));

// merged from dubbox
// we may already added some attachments into RpcContext before this filter (e.g. in rest protocol)
Expand Down