Skip to content

Commit 13de916

Browse files
authored
Support customized origin parser in legacy Dubbo 2.6.x adapter (alibaba#1555)
1 parent 19718ac commit 13de916

File tree

6 files changed

+196
-3
lines changed

6 files changed

+196
-3
lines changed

sentinel-adapter/sentinel-dubbo-adapter/README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,10 @@ flow control, degrade or system load protection. You can implement your own `Dub
6565
and then register to `DubboFallbackRegistry`. If no fallback is configured, Sentinel will wrap the `BlockException`
6666
then directly throw it out.
6767

68-
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.
68+
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.
69+
70+
## Global dubbo provider origin parse
71+
72+
Sentinel Dubbo Adapter supports global origin parse for provider.
73+
You can implement your own `DubboOriginParser` interface
74+
and then register to `DubboOriginParserRegistry`. If no originParse is configured, Sentinel will user dubbo url property application.

sentinel-adapter/sentinel-dubbo-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/dubbo/SentinelDubboProviderFilter.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.alibaba.csp.sentinel.Tracer;
2323
import com.alibaba.csp.sentinel.adapter.dubbo.config.DubboConfig;
2424
import com.alibaba.csp.sentinel.adapter.dubbo.fallback.DubboFallbackRegistry;
25+
import com.alibaba.csp.sentinel.adapter.dubbo.origin.DubboOriginParserRegistry;
2526
import com.alibaba.csp.sentinel.context.ContextUtil;
2627
import com.alibaba.csp.sentinel.log.RecordLog;
2728
import com.alibaba.csp.sentinel.slots.block.BlockException;
@@ -55,14 +56,17 @@ public SentinelDubboProviderFilter() {
5556
@Override
5657
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
5758
// Get origin caller.
58-
String application = DubboUtils.getApplication(invocation, "");
59+
String origin = DubboOriginParserRegistry.getDubboOriginParser().parse(invoker, invocation);
60+
if (null == origin) {
61+
origin = "";
62+
}
5963

6064
Entry interfaceEntry = null;
6165
Entry methodEntry = null;
6266
try {
6367
String resourceName = getResourceName(invoker, invocation, DubboConfig.getDubboProviderPrefix());
6468
String interfaceName = invoker.getInterface().getName();
65-
ContextUtil.enter(resourceName, application);
69+
ContextUtil.enter(resourceName, origin);
6670
interfaceEntry = SphU.entry(interfaceName, ResourceTypeConstants.COMMON_RPC, EntryType.IN);
6771
methodEntry = SphU.entry(resourceName, ResourceTypeConstants.COMMON_RPC,
6872
EntryType.IN, invocation.getArguments());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 1999-2020 Alibaba Group Holding Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.alibaba.csp.sentinel.adapter.dubbo.origin;
17+
18+
import com.alibaba.csp.sentinel.adapter.dubbo.DubboUtils;
19+
import com.alibaba.dubbo.rpc.Invocation;
20+
import com.alibaba.dubbo.rpc.Invoker;
21+
22+
/**
23+
* Default Dubbo origin parse.
24+
*
25+
* @author tiecheng
26+
*/
27+
public class DefaultDubboOriginParser implements DubboOriginParser {
28+
29+
@Override
30+
public String parse(Invoker<?> invoker, Invocation invocation) {
31+
return DubboUtils.getApplication(invocation, "");
32+
}
33+
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 1999-2020 Alibaba Group Holding Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.alibaba.csp.sentinel.adapter.dubbo.origin;
17+
18+
import com.alibaba.csp.sentinel.context.Context;
19+
import com.alibaba.dubbo.rpc.Invocation;
20+
import com.alibaba.dubbo.rpc.Invoker;
21+
22+
/**
23+
* Customized origin parse in Dubbo provider filter. {@link Context#getOrigin()}
24+
*
25+
* @author tiecheng
26+
*/
27+
public interface DubboOriginParser {
28+
29+
/**
30+
* Handle the origin parse.
31+
*
32+
* @param invoker Dubbo invoker
33+
* @param invocation Dubbo invocation
34+
* @return parse result
35+
*/
36+
String parse(Invoker<?> invoker, Invocation invocation);
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 1999-2020 Alibaba Group Holding Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.alibaba.csp.sentinel.adapter.dubbo.origin;
17+
18+
/**
19+
* Global origin parser registry for Dubbo.
20+
*
21+
* @author tiecheng
22+
*/
23+
public final class DubboOriginParserRegistry {
24+
25+
private static volatile DubboOriginParser dubboOriginParser = new DefaultDubboOriginParser();
26+
27+
public static DubboOriginParser getDubboOriginParser() {
28+
return dubboOriginParser;
29+
}
30+
31+
public static void setDubboOriginParser(DubboOriginParser dubboOriginParser) {
32+
DubboOriginParserRegistry.dubboOriginParser = dubboOriginParser;
33+
}
34+
35+
private DubboOriginParserRegistry() {}
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 1999-2020 Alibaba Group Holding Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.alibaba.csp.sentinel.adapter.dubbo.origin;
17+
18+
import com.alibaba.csp.sentinel.adapter.dubbo.DubboUtils;
19+
import com.alibaba.dubbo.rpc.Invocation;
20+
import com.alibaba.dubbo.rpc.Invoker;
21+
import com.alibaba.dubbo.rpc.RpcInvocation;
22+
import org.junit.After;
23+
import org.junit.Assert;
24+
import org.junit.Test;
25+
26+
/**
27+
* @author tiecheng
28+
*/
29+
public class DubboOriginRegistryTest {
30+
31+
@After
32+
public void cleanUp() {
33+
DubboOriginParserRegistry.setDubboOriginParser(new DefaultDubboOriginParser());
34+
}
35+
36+
@Test(expected = IllegalArgumentException.class)
37+
public void testDefaultOriginParserFail() {
38+
DubboOriginParserRegistry.getDubboOriginParser().parse(null, null);
39+
}
40+
41+
@Test
42+
public void testDefaultOriginParserSuccess() {
43+
RpcInvocation invocation = new RpcInvocation();
44+
String dubboName = "sentinel";
45+
invocation.setAttachment(DubboUtils.DUBBO_APPLICATION_KEY, dubboName);
46+
String origin = DubboOriginParserRegistry.getDubboOriginParser().parse(null, invocation);
47+
Assert.assertEquals(dubboName, origin);
48+
}
49+
50+
@Test
51+
public void testCustomOriginParser() {
52+
DubboOriginParserRegistry.setDubboOriginParser(new DubboOriginParser() {
53+
@Override
54+
public String parse(Invoker<?> invoker, Invocation invocation) {
55+
return invocation.getAttachment(DubboUtils.DUBBO_APPLICATION_KEY, "default") + "_" + invocation
56+
.getMethodName();
57+
}
58+
});
59+
60+
RpcInvocation invocation = new RpcInvocation();
61+
String origin = DubboOriginParserRegistry.getDubboOriginParser().parse(null, invocation);
62+
Assert.assertEquals("default_null", origin);
63+
64+
String dubboName = "sentinel";
65+
invocation.setAttachment(DubboUtils.DUBBO_APPLICATION_KEY, dubboName);
66+
origin = DubboOriginParserRegistry.getDubboOriginParser().parse(null, invocation);
67+
Assert.assertEquals(dubboName + "_null", origin);
68+
69+
invocation.setMethodName("hello");
70+
origin = DubboOriginParserRegistry.getDubboOriginParser().parse(null, invocation);
71+
Assert.assertEquals(dubboName + "_hello", origin);
72+
}
73+
74+
}

0 commit comments

Comments
 (0)