dubbo3 provider可以配置token
dubbo:
application:
name: merge-provider-1
qos-enable: false
protocol:
name: dubbo
port: 20880
registry:
address: zookeeper://${zookeeper.address:192.168.157.151}:2181
provider:
token: “true”
当token 配置为true的时候,通过代码public class ServiceConfig extends ServiceConfigBase {
的
private Map<String, String> buildAttributes(ProtocolConfig protocolConfig) 里的
/**
* Here the token value configured by the provider is used to assign the value to ServiceConfig#token
*/
if (ConfigUtils.isEmpty(token) && provider != null) {
token = provider.getToken();
}
if (!ConfigUtils.isEmpty(token)) {
if (ConfigUtils.isDefault(token)) {
map.put(TOKEN_KEY, UUID.randomUUID().toString());
} else {
map.put(TOKEN_KEY, token);
}
}
来生成token,可以看到这个是一个UUID的,多个provider实例会有不同token。
而consumer 是在类public class RpcInvocation implements Invocation, Serializable {
public RpcInvocation(Invocation invocation, Invoker<?> invoker) {
this(invocation.getTargetServiceUniqueName(), invocation.getServiceModel(), invocation.getMethodName(), invocation.getServiceName(),
invocation.getProtocolServiceKey(), invocation.getParameterTypes(), invocation.getArguments(),
invocation.copyObjectAttachments(), invocation.getInvoker(), invocation.getAttributes(),
invocation instanceof RpcInvocation ? ((RpcInvocation) invocation).getInvokeMode() : null);
if (invoker != null) {
URL url = invoker.getUrl();
setAttachment(PATH_KEY, url.getPath());
if (url.hasParameter(INTERFACE_KEY)) {
setAttachment(INTERFACE_KEY, url.getParameter(INTERFACE_KEY));
}
if (url.hasParameter(GROUP_KEY)) {
setAttachment(GROUP_KEY, url.getGroup());
}
if (url.hasParameter(VERSION_KEY)) {
setAttachment(VERSION_KEY, url.getVersion("0.0.0"));
}
if (url.hasParameter(TIMEOUT_KEY)) {
setAttachment(TIMEOUT_KEY, url.getParameter(TIMEOUT_KEY));
}
if (url.hasParameter(TOKEN_KEY)) {
setAttachment(TOKEN_KEY, url.getParameter(TOKEN_KEY));
}
if (url.hasParameter(APPLICATION_KEY)) {
setAttachment(APPLICATION_KEY, url.getApplication());
}
}
}
里面通过consumer 的url来设置token,如下图
因此在多provider的情况下会导致token 不一致,会有如下错误
org.apache.dubbo.rpc.RpcException: Invalid token! Forbid invoke remote service interface org.apache.dubbo.samples.merge.api.MergeService method mergeResult() from consumer 192.168.157.1 to provider 192.168.157.1, consumer incorrect token is 8d0fb50e-3714-4d74-9ed9-e623842c3b6a
这个错误会导致provider 下线,因此慎用,如果一定要用那么就指定token的值,不用使用UUID产生。
github也有一个issue https://github.com/apache/dubbo/issues/8383。