-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Add apache dubbo3 support implementation #2789
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # Sentinel Apache Dubbo Adapter (for 3.0.5+) | ||
|
|
||
| > Note: 中文文档请见[此处](https://github.com/alibaba/Sentinel/wiki/主流框架的适配#dubbo)。 | ||
|
|
||
| Sentinel Dubbo Adapter provides service consumer filter and provider filter | ||
| for [Apache Dubbo](https://dubbo.apache.org/en-us/) services. | ||
|
|
||
| **Note: This adapter only supports Apache Dubbo 3.0.5 and above.** | ||
|
|
||
| For legacy `com.alibaba:dubbo` 2.6.x, please use `sentinel-dubbo-adapter` module instead. | ||
|
|
||
| For `org.apache:dubbo` 2.7.x, please use `sentinel-apache-dubbo-adapter` module instead. | ||
|
|
||
| To use Sentinel Dubbo Adapter, you can simply add the following dependency to your `pom.xml`: | ||
|
|
||
| ```xml | ||
| <dependency> | ||
| <groupId>com.alibaba.csp</groupId> | ||
| <artifactId>sentinel-apache-dubbo3-adapter</artifactId> | ||
| <version>x.y.z</version> | ||
| </dependency> | ||
| ``` | ||
|
|
||
| The Sentinel filters are **enabled by default**. Once you add the dependency, | ||
| the Dubbo services and methods will become protected resources in Sentinel, | ||
| which can leverage Sentinel's flow control and guard ability when rules are configured. | ||
| Demos can be found in [sentinel-demo-apache-dubbo](https://github.com/alibaba/Sentinel/tree/master/sentinel-demo/sentinel-demo-apache-dubbo). | ||
|
|
||
| If you don't want the filters enabled, you can manually disable them. For example: | ||
|
|
||
| ```xml | ||
| <dubbo:consumer filter="-sentinel.dubbo.consumer.filter"/> | ||
|
|
||
| <dubbo:provider filter="-sentinel.dubbo.provider.filter"/> | ||
| ``` | ||
|
|
||
| For more details of Dubbo filter, see [here](http://dubbo.apache.org/en-us/docs/dev/impls/filter.html). | ||
|
|
||
| ## Dubbo resources | ||
|
|
||
| The resource for Dubbo services has two granularities: service interface and service method. | ||
|
|
||
| - Service interface: resourceName format is `interfaceName`, e.g. `com.alibaba.csp.sentinel.demo.dubbo.FooService` | ||
| - Service method: resourceName format is `interfaceName:methodSignature`, e.g. `com.alibaba.csp.sentinel.demo.dubbo.FooService:sayHello(java.lang.String)` | ||
|
|
||
| ## Flow control based on caller | ||
|
|
||
| In many circumstances, it's also significant to control traffic flow based on the **caller**. | ||
| For example, assuming that there are two services A and B, both of them initiate remote call requests to the service provider. | ||
| If we want to limit the calls from service B only, we can set the `limitApp` of flow rule as the identifier of service B (e.g. service name). | ||
|
|
||
| Sentinel Dubbo Adapter will automatically resolve the Dubbo consumer's *application name* as the caller's name (`origin`), | ||
| and will bring the caller's name when doing resource protection. | ||
| If `limitApp` of flow rules is not configured (`default`), flow control will take effects on all callers. | ||
| 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`. | ||
| > | ||
| > 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 `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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <parent> | ||
| <artifactId>sentinel-adapter</artifactId> | ||
| <groupId>com.alibaba.csp</groupId> | ||
| <version>1.8.4</version> | ||
| </parent> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <artifactId>sentinel-apache-dubbo3-adapter</artifactId> | ||
| <packaging>jar</packaging> | ||
|
|
||
| <properties> | ||
| <java.source.version>1.8</java.source.version> | ||
| <java.target.version>1.8</java.target.version> | ||
| <apache.dubbo.version>3.0.9</apache.dubbo.version> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.alibaba.csp</groupId> | ||
| <artifactId>sentinel-core</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.dubbo</groupId> | ||
| <artifactId>dubbo</artifactId> | ||
| <version>${apache.dubbo.version}</version> | ||
| <scope>provided</scope> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.mockito</groupId> | ||
| <artifactId>mockito-core</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.alibaba</groupId> | ||
| <artifactId>fastjson</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| </project> |
48 changes: 48 additions & 0 deletions
48
...dapter/src/main/java/com/alibaba/csp/sentinel/adapter/dubbo3/BaseSentinelDubboFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * 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.dubbo3; | ||
|
|
||
|
|
||
| import org.apache.dubbo.rpc.Invocation; | ||
| import org.apache.dubbo.rpc.Invoker; | ||
|
|
||
| /** | ||
| * Base class of the {@link SentinelDubboProviderFilter} and {@link SentinelDubboConsumerFilter}. | ||
| * | ||
| * @author Zechao Zheng | ||
| */ | ||
| public abstract class BaseSentinelDubboFilter { | ||
|
|
||
|
|
||
| /** | ||
| * Get method name of dubbo rpc | ||
| * | ||
| * @param invoker | ||
| * @param invocation | ||
| * @return | ||
| */ | ||
| abstract String getMethodName(Invoker invoker, Invocation invocation, String prefix); | ||
|
|
||
| /** | ||
| * Get interface name of dubbo rpc | ||
| * | ||
| * @param invoker | ||
| * @return | ||
| */ | ||
| abstract String getInterfaceName(Invoker invoker, String prefix); | ||
|
|
||
|
|
||
| } |
49 changes: 49 additions & 0 deletions
49
...-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/dubbo3/DubboAppContextFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| * 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.dubbo3; | ||
|
|
||
| import org.apache.dubbo.common.extension.Activate; | ||
| import org.apache.dubbo.rpc.Invocation; | ||
| import org.apache.dubbo.rpc.Invoker; | ||
| import org.apache.dubbo.rpc.Result; | ||
| import org.apache.dubbo.rpc.RpcException; | ||
| import org.apache.dubbo.rpc.cluster.filter.ClusterFilter; | ||
| import org.apache.dubbo.rpc.model.ApplicationModel; | ||
|
|
||
| import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER; | ||
|
|
||
| /** | ||
| * Puts current consumer's application name in the attachment of each invocation. | ||
| * | ||
| * @author Eric Zhao | ||
| */ | ||
| @Activate(group = CONSUMER) | ||
| public class DubboAppContextFilter implements ClusterFilter { | ||
|
|
||
| private final String applicationName; | ||
|
|
||
| public DubboAppContextFilter(ApplicationModel applicationModel) { | ||
| this.applicationName = applicationModel.tryGetApplicationName(); | ||
| } | ||
|
|
||
| @Override | ||
| public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException { | ||
| if (applicationName != null && !applicationName.isEmpty()) { | ||
| invocation.setAttachment(DubboUtils.SENTINEL_DUBBO_APPLICATION_KEY, applicationName); | ||
| } | ||
| return invoker.invoke(invocation); | ||
| } | ||
| } | ||
109 changes: 109 additions & 0 deletions
109
...ache-dubbo3-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/dubbo3/DubboUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /* | ||
| * 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.dubbo3; | ||
|
|
||
| import com.alibaba.csp.sentinel.adapter.dubbo3.config.DubboAdapterGlobalConfig; | ||
| import com.alibaba.csp.sentinel.util.StringUtil; | ||
|
|
||
| import org.apache.dubbo.common.utils.StringUtils; | ||
| import org.apache.dubbo.rpc.Invocation; | ||
| import org.apache.dubbo.rpc.Invoker; | ||
| import org.apache.dubbo.rpc.RpcContext; | ||
|
|
||
| import static org.apache.dubbo.common.constants.CommonConstants.REMOTE_APPLICATION_KEY; | ||
|
|
||
| /** | ||
| * @author Eric Zhao | ||
| */ | ||
| public final class DubboUtils { | ||
|
|
||
| public static final String SENTINEL_DUBBO_APPLICATION_KEY = "dubboApplication"; | ||
|
|
||
| public static String getApplication(Invocation invocation, String defaultValue) { | ||
| if (invocation == null || invocation.getAttachments() == null) { | ||
| throw new IllegalArgumentException("Bad invocation instance"); | ||
| } | ||
| // 1. try to get application from dubbo context | ||
| String remoteApplication = invocation.getAttachment(REMOTE_APPLICATION_KEY); | ||
| if (StringUtils.isEmpty(remoteApplication)) { | ||
| remoteApplication = RpcContext.getServerAttachment().getAttachment(REMOTE_APPLICATION_KEY); | ||
| } | ||
| if (StringUtils.isNotEmpty(remoteApplication)) { | ||
| return remoteApplication; | ||
| } | ||
| // 2. fallback to sentinel application | ||
| return invocation.getAttachment(SENTINEL_DUBBO_APPLICATION_KEY, defaultValue); | ||
AlbumenJ marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public static String getMethodResourceName(Invoker<?> invoker, Invocation invocation){ | ||
| return getMethodResourceName(invoker, invocation, false); | ||
| } | ||
|
|
||
| public static String getMethodResourceName(Invoker<?> invoker, Invocation invocation, Boolean useGroupAndVersion) { | ||
| StringBuilder buf = new StringBuilder(64); | ||
| String interfaceResource = useGroupAndVersion ? invoker.getUrl().getColonSeparatedKey() : invoker.getInterface().getName(); | ||
| buf.append(interfaceResource) | ||
| .append(":") | ||
| .append(invocation.getMethodName()) | ||
| .append("("); | ||
| boolean isFirst = true; | ||
| for (Class<?> clazz : invocation.getParameterTypes()) { | ||
| if (!isFirst) { | ||
| buf.append(","); | ||
| } | ||
| buf.append(clazz.getName()); | ||
| isFirst = false; | ||
| } | ||
| buf.append(")"); | ||
| return buf.toString(); | ||
| } | ||
|
|
||
| public static String getMethodResourceName(Invoker<?> invoker, Invocation invocation, String prefix) { | ||
| if (StringUtil.isNotBlank(prefix)) { | ||
| return new StringBuilder(64) | ||
| .append(prefix) | ||
| .append(getMethodResourceName(invoker, invocation, DubboAdapterGlobalConfig.getDubboInterfaceGroupAndVersionEnabled())) | ||
| .toString(); | ||
| } else { | ||
| return getMethodResourceName(invoker, invocation, DubboAdapterGlobalConfig.getDubboInterfaceGroupAndVersionEnabled()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
if () {
return foo;
}
return bar; |
||
| } | ||
| } | ||
|
|
||
|
|
||
| public static String getInterfaceName(Invoker invoker) { | ||
| return getInterfaceName(invoker, false); | ||
| } | ||
|
|
||
| public static String getInterfaceName(Invoker<?> invoker, Boolean useGroupAndVersion) { | ||
| StringBuilder buf = new StringBuilder(64); | ||
| return useGroupAndVersion ? invoker.getUrl().getColonSeparatedKey() : invoker.getInterface().getName(); | ||
| } | ||
|
|
||
| public static String getInterfaceName(Invoker<?> invoker, String prefix) { | ||
| if (StringUtil.isNotBlank(prefix)) { | ||
| return new StringBuilder(64) | ||
| .append(prefix) | ||
| .append(getInterfaceName(invoker, DubboAdapterGlobalConfig.getDubboInterfaceGroupAndVersionEnabled())) | ||
| .toString(); | ||
| } else { | ||
| return getInterfaceName(invoker, DubboAdapterGlobalConfig.getDubboInterfaceGroupAndVersionEnabled()); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| private DubboUtils() { | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.