Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support adding prefix to Dubbo service resource name in Sentinel Dubbo Adapter #859

Merged
merged 10 commits into from
Aug 14, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.alibaba.csp.sentinel.adapter.dubbo;

import com.alibaba.csp.sentinel.util.StringUtil;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;

Expand All @@ -35,9 +36,9 @@ public static String getApplication(Invocation invocation, String defaultValue)
public static String getResourceName(Invoker<?> invoker, Invocation invocation) {
StringBuilder buf = new StringBuilder(64);
buf.append(invoker.getInterface().getName())
.append(":")
.append(invocation.getMethodName())
.append("(");
.append(":")
.append(invocation.getMethodName())
.append("(");
boolean isFirst = true;
for (Class<?> clazz : invocation.getParameterTypes()) {
if (!isFirst) {
Expand All @@ -50,5 +51,17 @@ public static String getResourceName(Invoker<?> invoker, Invocation invocation)
return buf.toString();
}

private DubboUtils() {}
public static String getResourceName(Invoker<?> invoker, Invocation invocation, String prefix) {
if (StringUtil.isNotBlank(prefix)) {
return new StringBuilder(64)
.append(prefix).append(":")
.append(getResourceName(invoker, invocation))
.toString();
} else {
return getResourceName(invoker, invocation);
}
}

private DubboUtils() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import com.alibaba.csp.sentinel.EntryType;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.Tracer;
import com.alibaba.csp.sentinel.adapter.dubbo.config.DubboConfig;
import com.alibaba.csp.sentinel.adapter.dubbo.fallback.DubboFallbackRegistry;
import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.slots.block.BlockException;

import com.alibaba.csp.sentinel.adapter.dubbo.fallback.DubboFallbackRegistry;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.Filter;
import org.apache.dubbo.rpc.Invocation;
Expand All @@ -32,7 +32,7 @@

/**
* <p>Dubbo service consumer filter for Sentinel. Auto activated by default.</p>
*
* <p>
* If you want to disable the consumer filter, you can configure:
* <pre>
* &lt;dubbo:consumer filter="-sentinel.dubbo.consumer.filter"/&gt;
Expand All @@ -53,7 +53,7 @@ public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcExcept
Entry interfaceEntry = null;
Entry methodEntry = null;
try {
String resourceName = DubboUtils.getResourceName(invoker, invocation);
String resourceName = DubboUtils.getResourceName(invoker, invocation, DubboConfig.getDubboConsumerPrefix());
interfaceEntry = SphU.entry(invoker.getInterface().getName(), EntryType.OUT);
methodEntry = SphU.entry(resourceName, EntryType.OUT);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
import com.alibaba.csp.sentinel.EntryType;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.Tracer;
import com.alibaba.csp.sentinel.adapter.dubbo.config.DubboConfig;
import com.alibaba.csp.sentinel.adapter.dubbo.fallback.DubboFallbackRegistry;
import com.alibaba.csp.sentinel.context.ContextUtil;
import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.slots.block.BlockException;

import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.Filter;
import org.apache.dubbo.rpc.Invocation;
Expand All @@ -34,7 +34,7 @@
/**
* <p>Apache Dubbo service provider filter that enables integration with Sentinel. Auto activated by default.</p>
* <p>Note: this only works for Apache Dubbo 2.7.x or above version.</p>
*
* <p>
* If you want to disable the provider filter, you can configure:
* <pre>
* &lt;dubbo:provider filter="-sentinel.dubbo.provider.filter"/&gt;
Expand All @@ -58,7 +58,7 @@ public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcExcept
Entry interfaceEntry = null;
Entry methodEntry = null;
try {
String resourceName = DubboUtils.getResourceName(invoker, invocation);
String resourceName = DubboUtils.getResourceName(invoker, invocation, DubboConfig.getDubboProviderPrefix());
String interfaceName = invoker.getInterface().getName();
// Only need to create entrance context at provider side, as context will take effect
// at entrance of invocation chain only (for inbound traffic).
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.csp.sentinel.adapter.dubbo.config;

import com.alibaba.csp.sentinel.config.SentinelConfig;
import com.alibaba.csp.sentinel.util.StringUtil;

/**
* <p>
* Responsible for dubbo service provider, consumer attribute configuration
* </p>
*
* @author lianglin
* @since 1.7.0
*/
public final class DubboConfig {

public static final String DUBBO_USE_PREFIX = "csp.sentinel.dubbo.use.prefix";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe csp.sentinel.dubbo.use.prefix -> csp.sentinel.dubbo.resource.use.prefix is clearer? (though too long...)

So as the following two keys.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

config keys had modified

private static boolean usePrefix;
private static final String TRUE_STR = "true";

public static final String DUBBO_PROVIDER_PREFIX = "csp.sentinel.dubbo.provider.prefix";
public static final String DUBBO_CONSUMER_PREFIX = "csp.sentinel.dubbo.consumer.prefix";

static {
String dubboUsePrefix = SentinelConfig.getConfig(DUBBO_USE_PREFIX);
if (StringUtil.isNotBlank(dubboUsePrefix) && TRUE_STR.equalsIgnoreCase(dubboUsePrefix)) {
usePrefix = true;
}else{
usePrefix = false;
}
}


public static String getDubboProviderPrefix() {
if (usePrefix) {
return SentinelConfig.getConfig(DUBBO_PROVIDER_PREFIX);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we provide a default pattern (e.g. dubbo:provider/consumer: + the origin name) if the usePrefix is enabled but no prefix is provided?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we provide a default pattern (e.g. dubbo:provider/consumer: + the origin name) if the usePrefix is enabled but no prefix is provided?

default prefix had provided

}
return null;
}

public static String getDubboConsumerPrefix() {
if (usePrefix) {
return SentinelConfig.getConfig(DUBBO_CONSUMER_PREFIX);
}
return null;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
*/
package com.alibaba.csp.sentinel.adapter.dubbo;

import com.alibaba.csp.sentinel.adapter.dubbo.config.DubboConfig;
import com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.junit.Test;

import java.lang.reflect.Method;
import java.util.HashMap;

import com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.*;
Expand Down Expand Up @@ -72,4 +72,31 @@ public void testGetResourceName() {

assertEquals("com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService:sayHello(java.lang.String,int)", resourceName);
}


/**
* When test
* Jvm args:
* -Dcsp.sentinel.dubbo.use.prefix=true
* -Dcsp.sentinel.dubbo.provider.prefix=dubbo-provider
* -Dcsp.sentinel.dubbo.consumer.prefix=dubbo-consumer
*/
//@Test
public void testGetResourceNameWithPrefix(){
Invoker invoker = mock(Invoker.class);
when(invoker.getInterface()).thenReturn(DemoService.class);

Invocation invocation = mock(Invocation.class);
Method method = DemoService.class.getMethods()[0];
when(invocation.getMethodName()).thenReturn(method.getName());
when(invocation.getParameterTypes()).thenReturn(method.getParameterTypes());

String resourceName = DubboUtils.getResourceName(invoker, invocation, DubboConfig.getDubboProviderPrefix());
assertEquals(DubboConfig.getDubboProviderPrefix()+":"+"com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService:sayHello(java.lang.String,int)", resourceName);

resourceName = DubboUtils.getResourceName(invoker, invocation, DubboConfig.getDubboConsumerPrefix());
assertEquals(DubboConfig.getDubboConsumerPrefix()+":"+"com.alibaba.csp.sentinel.adapter.dubbo.provider.DemoService:sayHello(java.lang.String,int)", resourceName);


}
}