在使用dubbo时远程访问服务提供者的方法时出现空指针异常java.lang.NullPointerException
错误日志如下:
java.lang.NullPointerException
at com.atguigu.service.UserService.hello(UserService.java:20)
at com.atguigu.ConsumerUserApplicationTests.contextLoads(ConsumerUserApplicationTests.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
分析:
-
想了很久,它报空指针说明方法没找到,没找到的原因是什么,我仔细检查了一遍,方法的提供者正常运行,
-
这边方法的使用着的基本配置也都是对的
#配置当前应用的名字
dubbo.application.name=consumer-user
#配置zookeeper地址,这里的zookeeper是在虚拟机中docker装的
dubbo.registry.address=zookeeper://192.168.88.132:2181
- 包括接口目录也是正确的,所以错误最终被定位在调用者上,也就是:
import jdk.nashorn.internal.ir.annotations.Reference;
import org.springframework.stereotype.Service;
/**
* Created by CHEN on 2019/7/18.
*/
@Service
public class UserService {
// @Reference远程调用,通过全类名匹配的方式
@Reference
TicketService ticketService;
public String hello(){
String ticket = ticketService.getTicket();
System.out.println(ticket);
return ticket;
}
}
- 仔细观察,发现流程没大问题,再看注解,@Service调用的时Spring包,这没问题,@Reference这里,调用的是JDK的包,瞬间就发现自己有点蠢,用Dubbo的注解竟然调JDK的包。。。
解决
- 方案很明确,把包换成Dubbo的包:
import com.alibaba.dubbo.config.annotation.Reference;
-
运行结果是:
- Dubbo+zookeeper的入门使用流程:
- https://www.jianshu.com/p/944284c80479
版权声明:本文为symuamua原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。