最近在学习 Dubbo 时,调用远程服务时抛出 “Invalid token! ”错误。详细错误信息如下:
org.apache.dubbo.rpc.RpcException: Invalid token! Forbid invoke remote service interface com.huangx.dubbo.service.HelloService method sayHello() from consumer 192.168.238.1 to provider 192.168.238.1
at org.apache.dubbo.rpc.filter.TokenFilter.invoke(TokenFilter.java:49)
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:73)
at org.apache.dubbo.rpc.protocol.dubbo.filter.TraceFilter.invoke(TraceFilter.java:80)
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:73)
at org.apache.dubbo.rpc.filter.ContextFilter.invoke(ContextFilter.java:79)
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:73)
at org.apache.dubbo.rpc.filter.GenericFilter.invoke(GenericFilter.java:137)
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:73)
at org.apache.dubbo.rpc.filter.ClassLoaderFilter.invoke(ClassLoaderFilter.java:38)
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:73)
at org.apache.dubbo.rpc.filter.EchoFilter.invoke(EchoFilter.java:39)
at org.apache.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:73)
at org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol$1.reply(DubboProtocol.java:128)
at org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.handleRequest(HeaderExchangeHandler.java:103)
at org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.received(HeaderExchangeHandler.java:200)
at org.apache.dubbo.remoting.transport.DecodeHandler.received(DecodeHandler.java:51)
at org.apache.dubbo.remoting.transport.dispatcher.ChannelEventRunnable.run(ChannelEventRunnable.java:57)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
...
解决办法:
根据上面的错误信息得知,是因为我们传递的token无效或者根本没有传递token。查看 Dubbo 的源码发现是可以在直连模式下使用。但是如果你想通过配置文件,给 referrence 配置一个token 是不行的,因为它的的 schema 里面并没有这样定义。那我们怎么把 token 传给服务端呢? 采用隐式传参
RpcContext.getContext().setAttachment("token","123456");
完整实例代码如下:
服务配置代码:
<!-- 固定token令牌,相当于密码 --> <dubbo:service interface="com.huangx.dubbo.service.HelloService" ref="helloService" token="123456" />
客户端代码:
import com.huangx.dubbo.service.HelloService; import org.apache.dubbo.rpc.RpcContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Consumer { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo-consumer.xml"}); context.start(); /** * 通过隐私参数传递token */ RpcContext.getContext().setAttachment("token","123456"); HelloService helloService = (HelloService) context.getBean("helloService"); String val = helloService.sayHello("Helen"); System.out.println("#### val=" + val); } }