Support resource classification in Reactor adapter module and polish SC Gateway adapter
Signed-off-by: Eric Zhao <sczyh16@gmail.com>
This commit is contained in:
parent
6bb2de8750
commit
086a6b0370
|
|
@ -18,6 +18,7 @@ package com.alibaba.csp.sentinel.adapter.reactor;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.EntryType;
|
import com.alibaba.csp.sentinel.EntryType;
|
||||||
|
import com.alibaba.csp.sentinel.ResourceTypeConstants;
|
||||||
import com.alibaba.csp.sentinel.util.AssertUtil;
|
import com.alibaba.csp.sentinel.util.AssertUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -28,6 +29,8 @@ public class EntryConfig {
|
||||||
|
|
||||||
private final String resourceName;
|
private final String resourceName;
|
||||||
private final EntryType entryType;
|
private final EntryType entryType;
|
||||||
|
private final int resourceType;
|
||||||
|
|
||||||
private final int acquireCount;
|
private final int acquireCount;
|
||||||
private final Object[] args;
|
private final Object[] args;
|
||||||
private final ContextConfig contextConfig;
|
private final ContextConfig contextConfig;
|
||||||
|
|
@ -50,11 +53,21 @@ public class EntryConfig {
|
||||||
|
|
||||||
public EntryConfig(String resourceName, EntryType entryType, int acquireCount, Object[] args,
|
public EntryConfig(String resourceName, EntryType entryType, int acquireCount, Object[] args,
|
||||||
ContextConfig contextConfig) {
|
ContextConfig contextConfig) {
|
||||||
|
this(resourceName, ResourceTypeConstants.COMMON, entryType, acquireCount, args, contextConfig);
|
||||||
|
}
|
||||||
|
|
||||||
|
public EntryConfig(String resourceName, int resourceType, EntryType entryType, int acquireCount, Object[] args) {
|
||||||
|
this(resourceName, resourceType, entryType, acquireCount, args, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public EntryConfig(String resourceName, int resourceType, EntryType entryType, int acquireCount, Object[] args,
|
||||||
|
ContextConfig contextConfig) {
|
||||||
AssertUtil.assertNotBlank(resourceName, "resourceName cannot be blank");
|
AssertUtil.assertNotBlank(resourceName, "resourceName cannot be blank");
|
||||||
AssertUtil.notNull(entryType, "entryType cannot be null");
|
AssertUtil.notNull(entryType, "entryType cannot be null");
|
||||||
AssertUtil.isTrue(acquireCount > 0, "acquireCount should be positive");
|
AssertUtil.isTrue(acquireCount > 0, "acquireCount should be positive");
|
||||||
this.resourceName = resourceName;
|
this.resourceName = resourceName;
|
||||||
this.entryType = entryType;
|
this.entryType = entryType;
|
||||||
|
this.resourceType = resourceType;
|
||||||
this.acquireCount = acquireCount;
|
this.acquireCount = acquireCount;
|
||||||
this.args = args;
|
this.args = args;
|
||||||
// Constructed ContextConfig should be valid here. Null is allowed here.
|
// Constructed ContextConfig should be valid here. Null is allowed here.
|
||||||
|
|
@ -81,11 +94,19 @@ public class EntryConfig {
|
||||||
return contextConfig;
|
return contextConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @since 1.7.0
|
||||||
|
*/
|
||||||
|
public int getResourceType() {
|
||||||
|
return resourceType;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "EntryConfig{" +
|
return "EntryConfig{" +
|
||||||
"resourceName='" + resourceName + '\'' +
|
"resourceName='" + resourceName + '\'' +
|
||||||
", entryType=" + entryType +
|
", entryType=" + entryType +
|
||||||
|
", resourceType=" + resourceType +
|
||||||
", acquireCount=" + acquireCount +
|
", acquireCount=" + acquireCount +
|
||||||
", args=" + Arrays.toString(args) +
|
", args=" + Arrays.toString(args) +
|
||||||
", contextConfig=" + contextConfig +
|
", contextConfig=" + contextConfig +
|
||||||
|
|
|
||||||
|
|
@ -89,8 +89,8 @@ public class SentinelReactorSubscriber<T> extends InheritableBaseSubscriber<T> {
|
||||||
ContextUtil.enter(sentinelContextConfig.getContextName(), sentinelContextConfig.getOrigin());
|
ContextUtil.enter(sentinelContextConfig.getContextName(), sentinelContextConfig.getOrigin());
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
AsyncEntry entry = SphU.asyncEntry(entryConfig.getResourceName(), entryConfig.getEntryType(),
|
AsyncEntry entry = SphU.asyncEntry(entryConfig.getResourceName(), entryConfig.getResourceType(),
|
||||||
entryConfig.getAcquireCount(), entryConfig.getArgs());
|
entryConfig.getEntryType(), entryConfig.getAcquireCount(), entryConfig.getArgs());
|
||||||
this.currentEntry = entry;
|
this.currentEntry = entry;
|
||||||
actual.onSubscribe(this);
|
actual.onSubscribe(this);
|
||||||
} catch (BlockException ex) {
|
} catch (BlockException ex) {
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import com.alibaba.csp.sentinel.EntryType;
|
import com.alibaba.csp.sentinel.EntryType;
|
||||||
|
import com.alibaba.csp.sentinel.ResourceTypeConstants;
|
||||||
import com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants;
|
import com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants;
|
||||||
import com.alibaba.csp.sentinel.adapter.gateway.common.param.GatewayParamParser;
|
import com.alibaba.csp.sentinel.adapter.gateway.common.param.GatewayParamParser;
|
||||||
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
|
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
|
||||||
|
|
@ -70,8 +71,8 @@ public class SentinelGatewayFilter implements GatewayFilter, GlobalFilter, Order
|
||||||
.map(f -> f.apply(exchange))
|
.map(f -> f.apply(exchange))
|
||||||
.orElse("");
|
.orElse("");
|
||||||
asyncResult = asyncResult.transform(
|
asyncResult = asyncResult.transform(
|
||||||
new SentinelReactorTransformer<>(new EntryConfig(routeId, EntryType.IN,
|
new SentinelReactorTransformer<>(new EntryConfig(routeId, ResourceTypeConstants.COMMON_API_GATEWAY,
|
||||||
1, params, new ContextConfig(contextName(routeId), origin)))
|
EntryType.IN, 1, params, new ContextConfig(contextName(routeId), origin)))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -80,7 +81,8 @@ public class SentinelGatewayFilter implements GatewayFilter, GlobalFilter, Order
|
||||||
Object[] params = paramParser.parseParameterFor(apiName, exchange,
|
Object[] params = paramParser.parseParameterFor(apiName, exchange,
|
||||||
r -> r.getResourceMode() == SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME);
|
r -> r.getResourceMode() == SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME);
|
||||||
asyncResult = asyncResult.transform(
|
asyncResult = asyncResult.transform(
|
||||||
new SentinelReactorTransformer<>(new EntryConfig(apiName, EntryType.IN, 1, params))
|
new SentinelReactorTransformer<>(new EntryConfig(apiName, ResourceTypeConstants.COMMON_API_GATEWAY,
|
||||||
|
EntryType.IN, 1, params))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue