Skip to content

Commit

Permalink
Polish code and document of sentinel-sofa-rpc-adapter
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Zhao <[email protected]>
  • Loading branch information
sczyh30 committed Mar 3, 2020
1 parent 18acb1d commit d07d17b
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 39 deletions.
1 change: 1 addition & 0 deletions sentinel-adapter/sentinel-sofa-rpc-adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ consumerConfig.setParameter("sofa.rpc.sentinel.enabled", "false");
```

or add setting in `rpc-config.json` file, and its priority is lower than above.

```json
{
"sofa.rpc.sentinel.enabled": true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
/*
* 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.sofa.rpc;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.Tracer;
import com.alibaba.csp.sentinel.adapter.sofa.rpc.config.SofaRpcConfig;

import com.alipay.sofa.rpc.common.RpcConfigs;
import com.alipay.sofa.rpc.common.utils.StringUtils;
import com.alipay.sofa.rpc.config.AbstractInterfaceConfig;
Expand All @@ -19,14 +34,14 @@ abstract class AbstractSofaRpcFilter extends Filter {

@Override
public boolean needToLoad(FilterInvoker invoker) {
AbstractInterfaceConfig config = invoker.getConfig();
AbstractInterfaceConfig<?, ?> config = invoker.getConfig();

String enabled = config.getParameter(SofaRpcConfig.SOFA_RPC_SENTINEL_ENABLED);
String enabled = config.getParameter(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED);
if (StringUtils.isNotBlank(enabled)) {
return Boolean.valueOf(enabled);
return Boolean.parseBoolean(enabled);
}

return RpcConfigs.getOrDefaultValue(SofaRpcConfig.SOFA_RPC_SENTINEL_ENABLED, true);
return RpcConfigs.getOrDefaultValue(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED, true);
}

protected void traceResponseException(SofaResponse response, Entry interfaceEntry, Entry methodEntry) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.sofa.rpc;

/**
* @author cdfive
* @since 1.7.2
*/
public final class SentinelConstants {

public static final String SOFA_RPC_SENTINEL_ENABLED = "sofa.rpc.sentinel.enabled";

private SentinelConstants() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.alibaba.csp.sentinel.*;
import com.alibaba.csp.sentinel.adapter.sofa.rpc.fallback.SofaRpcFallbackRegistry;
import com.alibaba.csp.sentinel.slots.block.BlockException;

import com.alipay.sofa.rpc.common.RpcConstants;
import com.alipay.sofa.rpc.core.exception.SofaRpcException;
import com.alipay.sofa.rpc.core.request.SofaRequest;
Expand Down Expand Up @@ -59,7 +60,8 @@ public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws So
Entry methodEntry = null;
try {
interfaceEntry = SphU.entry(interfaceResourceName, ResourceTypeConstants.COMMON_RPC, EntryType.OUT);
methodEntry = SphU.entry(methodResourceName, ResourceTypeConstants.COMMON_RPC, EntryType.OUT, getMethodArguments(request));
methodEntry = SphU.entry(methodResourceName, ResourceTypeConstants.COMMON_RPC,
EntryType.OUT, getMethodArguments(request));

SofaResponse response = invoker.invoke(request);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.alibaba.csp.sentinel.adapter.sofa.rpc.fallback.SofaRpcFallbackRegistry;
import com.alibaba.csp.sentinel.context.ContextUtil;
import com.alibaba.csp.sentinel.slots.block.BlockException;

import com.alipay.sofa.rpc.common.RpcConstants;
import com.alipay.sofa.rpc.core.exception.SofaRpcException;
import com.alipay.sofa.rpc.core.request.SofaRequest;
Expand Down Expand Up @@ -58,17 +59,18 @@ public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws So
return invoker.invoke(request);
}

String applicationName = getApplicationName(request);
String callerApp = getApplicationName(request);
String interfaceResourceName = getInterfaceResourceName(request);
String methodResourceName = getMethodResourceName(request);

Entry interfaceEntry = null;
Entry methodEntry = null;
try {
ContextUtil.enter(methodResourceName, applicationName);
ContextUtil.enter(methodResourceName, callerApp);

interfaceEntry = SphU.entry(interfaceResourceName, ResourceTypeConstants.COMMON_RPC, EntryType.IN);
methodEntry = SphU.entry(methodResourceName, ResourceTypeConstants.COMMON_RPC, EntryType.IN, getMethodArguments(request));
methodEntry = SphU.entry(methodResourceName, ResourceTypeConstants.COMMON_RPC,
EntryType.IN, getMethodArguments(request));

SofaResponse response = invoker.invoke(request);

Expand All @@ -82,11 +84,9 @@ public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws So
if (methodEntry != null) {
methodEntry.exit(1, getMethodArguments(request));
}

if (interfaceEntry != null) {
interfaceEntry.exit();
}

ContextUtil.exit();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,6 @@ public static String getMethodResourceName(SofaRequest request) {
public static Object[] getMethodArguments(SofaRequest request) {
return request.getMethodArgs();
}

private SofaRpcUtils() {}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.alibaba.csp.sentinel.adapter.sofa.rpc.fallback;

import com.alibaba.csp.sentinel.util.AssertUtil;

/**
* Global Sentinel fallback registry for SOFARPC services.
*
Expand All @@ -30,6 +32,7 @@ public static SofaRpcFallback getProviderFallback() {
}

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

Expand All @@ -38,6 +41,7 @@ public static SofaRpcFallback getConsumerFallback() {
}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.alibaba.csp.sentinel.adapter.sofa.rpc;

import com.alibaba.csp.sentinel.adapter.sofa.rpc.config.SofaRpcConfig;
import com.alipay.sofa.rpc.codec.Serializer;
import com.alipay.sofa.rpc.common.RpcConfigs;
import com.alipay.sofa.rpc.config.ConsumerConfig;
Expand All @@ -23,12 +22,12 @@ public class AbstractSofaRpcFilterTest {

@Before
public void setUp() {
removeRpcConfig(SofaRpcConfig.SOFA_RPC_SENTINEL_ENABLED);
removeRpcConfig(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED);
}

@After
public void cleanUp() {
removeRpcConfig(SofaRpcConfig.SOFA_RPC_SENTINEL_ENABLED);
removeRpcConfig(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED);
}

@Test
Expand All @@ -40,13 +39,13 @@ public void testNeedToLoadProvider() {
FilterInvoker invoker = new FilterInvoker(null, null, providerConfig);
assertTrue(providerFilter.needToLoad(invoker));

providerConfig.setParameter(SofaRpcConfig.SOFA_RPC_SENTINEL_ENABLED, "false");
providerConfig.setParameter(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED, "false");
assertFalse(providerFilter.needToLoad(invoker));

providerConfig.setParameter(SofaRpcConfig.SOFA_RPC_SENTINEL_ENABLED, "");
providerConfig.setParameter(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED, "");
assertTrue(providerFilter.needToLoad(invoker));

RpcConfigs.putValue(SofaRpcConfig.SOFA_RPC_SENTINEL_ENABLED, "false");
RpcConfigs.putValue(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED, "false");
assertFalse(providerFilter.needToLoad(invoker));
}

Expand All @@ -59,13 +58,13 @@ public void testNeedToLoadConsumer() {
FilterInvoker invoker = new FilterInvoker(null, null, consumerConfig);
assertTrue(consumerFilter.needToLoad(invoker));

consumerConfig.setParameter(SofaRpcConfig.SOFA_RPC_SENTINEL_ENABLED, "false");
consumerConfig.setParameter(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED, "false");
assertFalse(consumerFilter.needToLoad(invoker));

consumerConfig.setParameter(SofaRpcConfig.SOFA_RPC_SENTINEL_ENABLED, "");
consumerConfig.setParameter(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED, "");
assertTrue(consumerFilter.needToLoad(invoker));

RpcConfigs.putValue(SofaRpcConfig.SOFA_RPC_SENTINEL_ENABLED, "false");
RpcConfigs.putValue(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED, "false");
assertFalse(consumerFilter.needToLoad(invoker));
}

Expand All @@ -85,14 +84,14 @@ public void testNeedToLoadProviderAndConsumer() {
FilterInvoker consumerInvoker = new FilterInvoker(null, null, consumerConfig);
assertTrue(consumerFilter.needToLoad(consumerInvoker));

providerConfig.setParameter(SofaRpcConfig.SOFA_RPC_SENTINEL_ENABLED, "false");
providerConfig.setParameter(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED, "false");
assertFalse(providerFilter.needToLoad(providerInvoker));
assertTrue(consumerFilter.needToLoad(consumerInvoker));

providerConfig.setParameter(SofaRpcConfig.SOFA_RPC_SENTINEL_ENABLED, "");
providerConfig.setParameter(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED, "");
assertTrue(providerFilter.needToLoad(providerInvoker));

RpcConfigs.putValue(SofaRpcConfig.SOFA_RPC_SENTINEL_ENABLED, "false");
RpcConfigs.putValue(SentinelConstants.SOFA_RPC_SENTINEL_ENABLED, "false");
assertFalse(providerFilter.needToLoad(providerInvoker));
assertFalse(consumerFilter.needToLoad(consumerInvoker));
}
Expand Down
14 changes: 7 additions & 7 deletions sentinel-demo/sentinel-demo-sofa-rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Sentinel 提供了与 SOFARPC 整合的模块 - `sentinel-sofa-rpc-adapter`,

引入此依赖后,SOFARPC 的服务接口和方法(包括调用端和服务端)就会成为 Sentinel 中的资源,在配置了规则后就可以自动享受到 Sentinel 的防护能力。

> **注:若希望接入 Dashboard,请参考demo中的注释添加VM参数,只引入`sentinel-sofa-rpc-adapter`无法接入控制台**
> **注:若希望接入 Dashboard,请参考 demo 中的注释添加启动参数。只引入 `sentinel-sofa-rpc-adapter` 依赖无法接入控制台**
若不希望开启 Sentinel SOFARPC Adapter 中的某个 Filter,可以手动关闭对应的 Filter,比如:

Expand All @@ -21,22 +21,22 @@ providerConfig.setParameter("sofa.rpc.sentinel.enabled", "false");
consumerConfig.setParameter("sofa.rpc.sentinel.enabled", "false");
```

或者在`rpc-config.json`文件中设置,它的优先级要低一些。
或者在 `rpc-config.json` 文件中设置,它的优先级要低一些。

```json
{
"sofa.rpc.sentinel.enabled": true
}
```

# 运行Demo
## 运行 Demo

1. 启动控制台,运行`DashboardApplication`
1. 启动控制台,运行 `DashboardApplication`

2. 启动Provider,运行`DemoProvider`(VM参数:`-Dproject.name=DemoProvider -Dcsp.sentinel.dashboard.server=localhost:8080`
2. 启动 Provider,运行 `DemoProvider`(VM参数:`-Dproject.name=DemoProvider -Dcsp.sentinel.dashboard.server=localhost:8080`

3. 启动Consumer,运行`DemoConsumer`(VM参数:`-Dproject.name=DemoConsumer -Dcsp.sentinel.dashboard.server=localhost:8080`
3. 启动 Consumer,运行 `DemoConsumer`(VM参数:`-Dproject.name=DemoConsumer -Dcsp.sentinel.dashboard.server=localhost:8080`

通过控制台实时监控、簇点链路菜单观察接口调用、资源情况;对资源设置不同流控规则,进行观察和调试。

参考:[Sentinel控制台](https://github.com/alibaba/Sentinel/wiki/控制台).
参考:[Sentinel 控制台文档](https://github.com/alibaba/Sentinel/wiki/控制台).

0 comments on commit d07d17b

Please sign in to comment.