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

Customized handler parser in Dubbo provider filter. #1555

Merged
merged 7 commits into from
Jun 28, 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
8 changes: 7 additions & 1 deletion sentinel-adapter/sentinel-dubbo-adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,10 @@ flow control, degrade or system load protection. You can implement your own `Dub
and then register to `DubboFallbackRegistry`. If no fallback is configured, Sentinel will wrap the `BlockException`
then directly throw it out.

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.
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.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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 @@ -55,14 +56,17 @@ public SentinelDubboProviderFilter() {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
// Get origin caller.
String application = DubboUtils.getApplication(invocation, "");
String origin = DubboOriginParserRegistry.getDubboOriginParser().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, application);
ContextUtil.enter(resourceName, origin);
interfaceEntry = SphU.entry(interfaceName, ResourceTypeConstants.COMMON_RPC, EntryType.IN);
methodEntry = SphU.entry(resourceName, ResourceTypeConstants.COMMON_RPC,
EntryType.IN, invocation.getArguments());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 1999-2020 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
*
* https://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.origin;
cityiron marked this conversation as resolved.
Show resolved Hide resolved

import com.alibaba.csp.sentinel.adapter.dubbo.DubboUtils;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;

/**
* Default Dubbo origin parse.
*
* @author tiecheng
*/
public class DefaultDubboOriginParser implements DubboOriginParser {

@Override
public String parse(Invoker<?> invoker, Invocation invocation) {
return DubboUtils.getApplication(invocation, "");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 1999-2020 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
*
* https://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.origin;

import com.alibaba.csp.sentinel.context.Context;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;

/**
* Customized origin parse in Dubbo provider filter. {@link Context#getOrigin()}
*
* @author tiecheng
*/
public interface DubboOriginParser {

/**
* Handle the origin parse.
*
* @param invoker Dubbo invoker
* @param invocation Dubbo invocation
* @return parse result
*/
String parse(Invoker<?> invoker, Invocation invocation);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 1999-2020 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
*
* https://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.origin;

/**
* Global origin parser registry for Dubbo.
*
* @author tiecheng
*/
public final class DubboOriginParserRegistry {

private static volatile DubboOriginParser dubboOriginParser = new DefaultDubboOriginParser();

public static DubboOriginParser getDubboOriginParser() {
return dubboOriginParser;
}

public static void setDubboOriginParser(DubboOriginParser dubboOriginParser) {
DubboOriginParserRegistry.dubboOriginParser = dubboOriginParser;
}

private DubboOriginParserRegistry() {}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 1999-2020 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
*
* https://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.origin;

import com.alibaba.csp.sentinel.adapter.dubbo.DubboUtils;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.RpcInvocation;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;

/**
* @author tiecheng
*/
public class DubboOriginRegistryTest {

@After
public void cleanUp() {
DubboOriginParserRegistry.setDubboOriginParser(new DefaultDubboOriginParser());
}

@Test(expected = IllegalArgumentException.class)
public void testDefaultOriginParserFail() {
DubboOriginParserRegistry.getDubboOriginParser().parse(null, null);
}

@Test
public void testDefaultOriginParserSuccess() {
RpcInvocation invocation = new RpcInvocation();
String dubboName = "sentinel";
invocation.setAttachment(DubboUtils.DUBBO_APPLICATION_KEY, dubboName);
String origin = DubboOriginParserRegistry.getDubboOriginParser().parse(null, invocation);
Assert.assertEquals(dubboName, origin);
}

@Test
public void testCustomOriginParser() {
DubboOriginParserRegistry.setDubboOriginParser(new DubboOriginParser() {
@Override
public String parse(Invoker<?> invoker, Invocation invocation) {
return invocation.getAttachment(DubboUtils.DUBBO_APPLICATION_KEY, "default") + "_" + invocation
.getMethodName();
}
});

RpcInvocation invocation = new RpcInvocation();
String origin = DubboOriginParserRegistry.getDubboOriginParser().parse(null, invocation);
Assert.assertEquals("default_null", origin);

String dubboName = "sentinel";
invocation.setAttachment(DubboUtils.DUBBO_APPLICATION_KEY, dubboName);
origin = DubboOriginParserRegistry.getDubboOriginParser().parse(null, invocation);
Assert.assertEquals(dubboName + "_null", origin);

invocation.setMethodName("hello");
origin = DubboOriginParserRegistry.getDubboOriginParser().parse(null, invocation);
Assert.assertEquals(dubboName + "_hello", origin);
}

}