最近在学习Dubbo的知识,就做一个入门小demo,但是一直报错,很懊恼。
本来是一个简单的客户端调用服务端的例子,但是一直访问不到。报错如下
严重: StandardWrapper.Throwable
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController' defined in file [G:\idea_file\DubboxDemoWeb\target\classes\com\company\dubbox\controller\UserController.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanInitializationException: Failed to init remote service reference at filed userService in class com.company.dubbox.controller.UserController; nested exception is java.lang.IllegalStateException: Failed to check the status of the service com.company.dubbox.service.UserService. No provider available for the service com.company.dubbox.service.UserService from the url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=DubboxDemoWeb&dubbo=2.8.4&interface=com.company.dubbox.service.UserService&methods=getName&pid=9280&side=consumer×tamp=1561779657577 to the consumer 192.168.0.101 use dubbo version 2.8.4
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:160)
从报错可以看出来是spring没有办法通过反射创建UserService对象,但是为什么呢?明明已经注册了
根据报错的信息去网上查询资料的时候,发现大多数网友的建议都是检查zookeeper的地址是否有错之类的,并不能解决。
然后根据自己的实际问题出发,重新梳理一遍问题。
查看zookeeper的日志,发现一个报错,报错节点重复了
- INFO [ProcessThread(sid:0 cport:-1)::PrepRequ
estProcessor@645] - Got user-level KeeperException when processing sessionid:0x1
6ba138baf40007 type:create cxid:0x10 zxid:0x63 txntype:-1 reqpath:n/a Error Path
:/dubbo/com.company.dubbox.service.UserService Error:KeeperErrorCode = NodeExist
s for /dubbo/com.company.dubbox.service.UserService
从报错信息可以看到有两个UserService,但是为什么呢?明明是写了一下
通过zookeeper的可视化工具来看,确实也是有两个,并且路径是不一样的,很奇怪
查看代码,发现了大坑
服务端
客户端
服务端UserService的路径写错了,应该是在service包下的
解决办法:修改服务端UserService的路径,然后重新启动
修改服务端UserService的路径
重启项目,访问正常
总结
遇到问题的时候,需要弄清楚问题的根本原因,才容易解决问题。
比如对于这个问题,自己并没有想到是代码的路径写错了,可能是晚上困了【哈哈哈~】,看错了idea的目录树层级结构。然后就一直查看zookeeper的配置相关的内容。但是没有解决。第二天早上,重新梳理问题,查看zookeeper日志,找到了是因为重复注册了不同路径的UserService,然后就去查看自己代码,问题得以解决。