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

Polish Dubbo 2.6.x adapter #1572

Merged
merged 3 commits into from
Aug 19, 2020
Merged
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
26 changes: 12 additions & 14 deletions sentinel-adapter/sentinel-dubbo-adapter/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Sentinel Dubbo Adapter

> Note: 中文文档请见[此处](https://github.com/alibaba/Sentinel/wiki/%E4%B8%BB%E6%B5%81%E6%A1%86%E6%9E%B6%E7%9A%84%E9%80%82%E9%85%8D#dubbo)。
> Note: 中文文档请见[此处](https://github.com/alibaba/Sentinel/wiki/主流框架的适配#dubbo)。

Sentinel Dubbo Adapter provides service consumer filter and provider filter
for [Dubbo](https://dubbo.apache.org/en-us/) services.
Expand Down Expand Up @@ -52,23 +52,21 @@ If `limitApp` of flow rules is not configured (`default`), flow control will tak
If `limitApp` of a flow rule is configured with a caller, then the corresponding flow rule will only take effect on the specific caller.

> Note: Dubbo consumer does not provide its Dubbo application name when doing RPC,
so developers should manually put the application name into *attachment* at consumer side,
then extract it at provider side. Sentinel Dubbo Adapter has implemented a filter (`DubboAppContextFilter`)
where consumer can carry application name information to provider automatically.
If the consumer does not use Sentinel Dubbo Adapter but requires flow control based on caller, developers can manually put the application name into attachment with the key `dubboApplication`.
> so developers should manually put the application name into *attachment* at consumer side,
> then extract it at provider side. Sentinel Dubbo Adapter has implemented a filter (`DubboAppContextFilter`)
> where consumer can carry application name information to provider automatically.
> If the consumer does not use Sentinel Dubbo Adapter but requires flow control based on caller,
> developers can manually put the application name into attachment with the key `dubboApplication`.
>
> Since 1.8.0, the adapter provides support for customizing origin parsing logic. You may register your own `DubboOriginParser`
> implementation to `DubboAdapterGlobalConfig`.

## Global fallback

Sentinel Dubbo Adapter supports global fallback configuration.
The global fallback will handle exceptions and give replacement result when blocked by
flow control, degrade or system load protection. You can implement your own `DubboFallback` interface
and then register to `DubboFallbackRegistry`. If no fallback is configured, Sentinel will wrap the `BlockException`
then directly throw it out.
and then register to `DubboAdapterGlobalConfig`.
If no fallback is configured, Sentinel will wrap the `BlockException` as the fallback result.

Besides, we can also leverage [Dubbo mock mechanism](http://dubbo.apache.org/en-us/docs/user/demos/local-mock.html) to provide fallback implementation of degraded Dubbo services.

## Global dubbo provider origin parse

Sentinel Dubbo Adapter supports global origin parse for provider.
You can implement your own `DubboOriginParser` interface
and then register to `DubboOriginParserRegistry`. If no originParse is configured, Sentinel will user dubbo url property application.
Besides, we can also leverage [Dubbo mock mechanism](http://dubbo.apache.org/en-us/docs/user/demos/local-mock.html) to provide fallback implementation of degraded Dubbo services.
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
*/
abstract class AbstractDubboFilter implements Filter {

protected String getResourceName(Invoker<?> invoker, Invocation invocation) {
protected String getMethodResourceName(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 @@ -43,15 +43,24 @@ protected String getResourceName(Invoker<?> invoker, Invocation invocation) {
return buf.toString();
}

protected String getResourceName(Invoker<?> invoker, Invocation invocation, String prefix) {
protected String getMethodResourceName(Invoker<?> invoker, Invocation invocation, String prefix) {
if (StringUtil.isBlank(prefix)) {
return getResourceName(invoker, invocation);
return getMethodResourceName(invoker, invocation);
}
StringBuilder buf = new StringBuilder(64);
return buf.append(prefix)
.append(getResourceName(invoker, invocation))
.toString();
.append(getMethodResourceName(invoker, invocation))
.toString();
}

protected String getInterfaceName(Invoker<?> invoker) {
return invoker.getInterface().getName();
}

protected String getInterfaceName(Invoker<?> invoker, String prefix) {
if (StringUtil.isBlank(prefix)) {
return getInterfaceName(invoker);
}
return prefix + getInterfaceName(invoker);
Comment on lines +56 to +64
Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems that normally when overloading methods, the method which has less parameters call the method which has more. This is the opposite, but it is okay.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Besides, I found that dubbo-adapter of 2.7.x has a useGroupAndVersion setting.
String getMethodResourceName(Invoker<?> invoker, Invocation invocation, Boolean useGroupAndVersion).
DubboConfig.getDubboInterfaceGroupAndVersionEnabled(), csp.sentinel.dubbo.interface.group.version.enabled
Do we need to add this in 2.6.x?

Another setting is csp.sentinel.dubbo.trace.biz.exception.enabled in 2.7.x, and it is unused.

Copy link
Member Author

Choose a reason for hiding this comment

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

Besides, I found that dubbo-adapter of 2.7.x has a useGroupAndVersion setting.
String getMethodResourceName(Invoker<?> invoker, Invocation invocation, Boolean useGroupAndVersion).
DubboConfig.getDubboInterfaceGroupAndVersionEnabled(), csp.sentinel.dubbo.interface.group.version.enabled
Do we need to add this in 2.6.x?

Yes, we could add this in the future. We may open another issue to address this.

Another setting is csp.sentinel.dubbo.trace.biz.exception.enabled in 2.7.x, and it is unused.

Actually I don't think it's useful.

Copy link
Collaborator

Choose a reason for hiding this comment

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

OK, I agree with you. IMHO the prefix is more useful, maybe enough at present.

Copy link
Member Author

Choose a reason for hiding this comment

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

OK, I agree with you. IMHO the prefix is more useful, maybe enough at present.

The group and version info can be useful. This could be contributed by community later.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for detail description, group and version are useful indeed, got it : )

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* 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;

import com.alibaba.csp.sentinel.adapter.dubbo.fallback.DefaultDubboFallback;
import com.alibaba.csp.sentinel.adapter.dubbo.fallback.DubboFallback;
import com.alibaba.csp.sentinel.adapter.dubbo.origin.DefaultDubboOriginParser;
import com.alibaba.csp.sentinel.adapter.dubbo.origin.DubboOriginParser;
import com.alibaba.csp.sentinel.config.SentinelConfig;
import com.alibaba.csp.sentinel.util.AssertUtil;
import com.alibaba.csp.sentinel.util.StringUtil;

/**
* <p>Global config and callback registry of Dubbo legacy adapter.</p>
*
* @author lianglin
* @author Eric Zhao
* @since 1.7.0
*/
public final class DubboAdapterGlobalConfig {

private static final String TRUE_STR = "true";

public static final String DUBBO_RES_NAME_WITH_PREFIX_KEY = "csp.sentinel.dubbo.resource.use.prefix";
public static final String DUBBO_PROVIDER_RES_NAME_PREFIX_KEY = "csp.sentinel.dubbo.resource.provider.prefix";
public static final String DUBBO_CONSUMER_RES_NAME_PREFIX_KEY = "csp.sentinel.dubbo.resource.consumer.prefix";

private static final String DEFAULT_DUBBO_PROVIDER_PREFIX = "dubbo:provider:";
private static final String DEFAULT_DUBBO_CONSUMER_PREFIX = "dubbo:consumer:";

private static volatile DubboFallback consumerFallback = new DefaultDubboFallback();
private static volatile DubboFallback providerFallback = new DefaultDubboFallback();
private static volatile DubboOriginParser originParser = new DefaultDubboOriginParser();

public static boolean isUsePrefix() {
return TRUE_STR.equalsIgnoreCase(SentinelConfig.getConfig(DUBBO_RES_NAME_WITH_PREFIX_KEY));
}

public static String getDubboProviderPrefix() {
if (isUsePrefix()) {
String config = SentinelConfig.getConfig(DUBBO_PROVIDER_RES_NAME_PREFIX_KEY);
return StringUtil.isNotBlank(config) ? config : DEFAULT_DUBBO_PROVIDER_PREFIX;
}
return null;
}

public static String getDubboConsumerPrefix() {
if (isUsePrefix()) {
String config = SentinelConfig.getConfig(DUBBO_CONSUMER_RES_NAME_PREFIX_KEY);
return StringUtil.isNotBlank(config) ? config : DEFAULT_DUBBO_CONSUMER_PREFIX;
}
return null;
}

public static DubboFallback getConsumerFallback() {
return consumerFallback;
}

public static void setConsumerFallback(DubboFallback consumerFallback) {
AssertUtil.notNull(consumerFallback, "consumerFallback cannot be null");
DubboAdapterGlobalConfig.consumerFallback = consumerFallback;
}

public static DubboFallback getProviderFallback() {
return providerFallback;
}

public static void setProviderFallback(DubboFallback providerFallback) {
AssertUtil.notNull(providerFallback, "providerFallback cannot be null");
DubboAdapterGlobalConfig.providerFallback = providerFallback;
}

/**
* Get the origin parser of Dubbo adapter.
*
* @return the origin parser
* @since 1.8.0
*/
public static DubboOriginParser getOriginParser() {
return originParser;
}

/**
* Set the origin parser of Dubbo adapter.
*
* @param originParser the origin parser
* @since 1.8.0
*/
public static void setOriginParser(DubboOriginParser originParser) {
AssertUtil.notNull(originParser, "originParser cannot be null");
DubboAdapterGlobalConfig.originParser = originParser;
}

private DubboAdapterGlobalConfig() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import com.alibaba.csp.sentinel.ResourceTypeConstants;
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.dubbo.common.extension.Activate;
Expand Down Expand Up @@ -56,10 +54,12 @@ public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcExcept
Entry interfaceEntry = null;
Entry methodEntry = null;
try {
String resourceName = getResourceName(invoker, invocation, DubboConfig.getDubboConsumerPrefix());
interfaceEntry = SphU.entry(invoker.getInterface().getName(), ResourceTypeConstants.COMMON_RPC,
EntryType.OUT);
methodEntry = SphU.entry(resourceName, ResourceTypeConstants.COMMON_RPC, EntryType.OUT, invocation.getArguments());
String prefix = DubboAdapterGlobalConfig.getDubboConsumerPrefix();
String interfaceResourceName = getInterfaceName(invoker, prefix);
String methodResourceName = getMethodResourceName(invoker, invocation, prefix);
interfaceEntry = SphU.entry(interfaceResourceName, ResourceTypeConstants.COMMON_RPC, EntryType.OUT);
methodEntry = SphU.entry(methodResourceName, ResourceTypeConstants.COMMON_RPC,
EntryType.OUT, invocation.getArguments());

Result result = invoker.invoke(invocation);
if (result.hasException()) {
Expand All @@ -70,7 +70,7 @@ public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcExcept
}
return result;
} catch (BlockException e) {
return DubboFallbackRegistry.getConsumerFallback().handle(invoker, invocation, e);
return DubboAdapterGlobalConfig.getConsumerFallback().handle(invoker, invocation, e);
} catch (RpcException e) {
Tracer.traceEntry(e, interfaceEntry);
Tracer.traceEntry(e, methodEntry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
import com.alibaba.csp.sentinel.ResourceTypeConstants;
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.adapter.dubbo.origin.DubboOriginParserRegistry;
import com.alibaba.csp.sentinel.context.ContextUtil;
import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.slots.block.BlockException;
Expand Down Expand Up @@ -56,19 +53,20 @@ public SentinelDubboProviderFilter() {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
// Get origin caller.
String origin = DubboOriginParserRegistry.getDubboOriginParser().parse(invoker, invocation);
String origin = DubboAdapterGlobalConfig.getOriginParser().parse(invoker, invocation);
if (null == origin) {
origin = "";
}

Entry interfaceEntry = null;
Entry methodEntry = null;
try {
String resourceName = getResourceName(invoker, invocation, DubboConfig.getDubboProviderPrefix());
String interfaceName = invoker.getInterface().getName();
ContextUtil.enter(resourceName, origin);
String prefix = DubboAdapterGlobalConfig.getDubboProviderPrefix();
String methodResourceName = getMethodResourceName(invoker, invocation, prefix);
String interfaceName = getInterfaceName(invoker, prefix);
ContextUtil.enter(methodResourceName, origin);
interfaceEntry = SphU.entry(interfaceName, ResourceTypeConstants.COMMON_RPC, EntryType.IN);
methodEntry = SphU.entry(resourceName, ResourceTypeConstants.COMMON_RPC,
methodEntry = SphU.entry(methodResourceName, ResourceTypeConstants.COMMON_RPC,
EntryType.IN, invocation.getArguments());

Result result = invoker.invoke(invocation);
Expand All @@ -80,7 +78,7 @@ public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcExcept
}
return result;
} catch (BlockException e) {
return DubboFallbackRegistry.getProviderFallback().handle(invoker, invocation, e);
return DubboAdapterGlobalConfig.getProviderFallback().handle(invoker, invocation, e);
} catch (RpcException e) {
Tracer.traceEntry(e, interfaceEntry);
Tracer.traceEntry(e, methodEntry);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.Result;
import com.alibaba.dubbo.rpc.RpcResult;

/**
* @author Eric Zhao
Expand All @@ -28,7 +29,7 @@ public class DefaultDubboFallback implements DubboFallback {

@Override
public Result handle(Invoker<?> invoker, Invocation invocation, BlockException ex) {
// Just wrap and throw the exception.
throw new SentinelRpcException(ex);
// Just wrap the exception.
return new RpcResult(new SentinelRpcException(ex));
}
}
Loading