dubbo客户端初始化问题
背景:工作中遇到了调用dubbo服务的场景,项目不是spring项目,我只想简单的调用服务端的接口,不想加入那么多配置。
在服务对接中,遇到调用dubbo服务的场景。按照公司的框架给的开发文档,要加配置!要写注解!不符合我的期望。我只是想用一下Dubbo!
于是把目光转向了dubbo官网的示例。
1、添加依赖
首先是添加pom依赖,我是一步步添加这么多的
1 <spring.version>4.2.5.RELEASE</spring.version> 2 3 <dependency> 4 <groupId>com.alibaba</groupId> 5 <artifactId>dubbo</artifactId> 6 <version>2.6.2</version> 7 <exclusions> 8 <exclusion> 9 <groupId>com.101tec</groupId> 10 <artifactId>zkclient</artifactId> 11 </exclusion> 12 </exclusions> 13 </dependency> 14 <dependency> 15 <groupId>org.springframework</groupId> 16 <artifactId>spring-context</artifactId> 17 <version>${spring.version}</version> 18 </dependency> 19 <dependency> 20 <groupId>org.springframework</groupId> 21 <artifactId>spring-beans</artifactId> 22 <version>${spring.version}</version> 23 </dependency> 24 <dependency> 25 <groupId>org.springframework</groupId> 26 <artifactId>spring-core</artifactId> 27 <version>${spring.version}</version> 28 </dependency> 29 <dependency> 30 <groupId>org.springframework</groupId> 31 <artifactId>spring-tx</artifactId> 32 <version>${spring.version}</version> 33 </dependency>
2、写代码
a、有错误的代码示例(Dubbo的示例,这也是坑的开始。)
1 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-consumer.xml"}); 2 context.start(); 3 DemoService demoService = (DemoService) context.getBean("demoService"); // get remote service proxy 4 while (true) { 5 try { 6 Thread.sleep(1000); 7 String hello = demoService.sayHello("world"); // call remote method 8 System.out.println(hello); // get result 9 } catch (Throwable throwable) { 10 throwable.printStackTrace(); 11 } 12 }
我这样用了,项目启动的时候一直报错,如下:
2018-10-15 16:30:30 [org.springframework.beans.factory.xml.XmlBeanDefinitionReader]-[INFO] Loading XML bean definitions from class path resource [E:/opt/dwf/dwf-dispatch-center/conf/dubbo_consumer.xml] java.lang.reflect.InvocationTargetException 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 com.daojia.dwf.mvc.initial.app.AppInitial.initial(AppInitial.java:30) at com.daojia.dwf.mvc.DWFBootstrap.init(DWFBootstrap.java:71) at org.apache.catalina.core.ApplicationFilterConfig.initFilter(ApplicationFilterConfig.java:279) at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:260) at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:105) at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4950) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5652) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1694) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1684) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748) Caused by: java.lang.ExceptionInInitializerError at com.daojia.dispatchcenter.inits.ServerInit.initial(ServerInit.java:38) ... 18 more Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [E:/opt/dwf/dwf-dispatch-center/conf/dubbo_consumer.xml]; nested exception is java.io.FileNotFoundException: class path resource [E:/opt/dwf/dwf-dispatch-center/conf/dubbo_consumer.xml] cannot be opened because it does not exist at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188) at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:252) at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127) at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93) at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129) at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:614) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:515) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93) at com.daojia.dispatchcenter.inits.DubboInit.<clinit>(DubboInit.java:33) ... 19 more Caused by: java.io.FileNotFoundException: class path resource [E:/opt/dwf/dwf-dispatch-center/conf/dubbo_consumer.xml] cannot be opened because it does not exist at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172) at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330) ... 32 more
一直报找不到配置,这就奇怪了。尝试了许多次,依然这样。
我在代码里尝试把配置文件打印出来,内容是可以打印出来的。这说明路径是没有错的。
搜索了一番,
看了这篇文章之后,才了解到,
ClassPathXmlApplicationContext 对于文件的路径是有要求的。类似于类需要先被加载一样。我这配置文件很明显是没有被提前加载的。
因为使用框架的原因,我又不想把配置放到那个目录下,于是查看该继承的父类,找到了“FileSystemXmlApplicationContext”这个方法,改了之后,不再提示那个错误了。
b、正常的代码
1 String configFolder = Config.getConfigFolder(); 2 if(configFolder.startsWith("/")) { 3 configFolder = "/" + configFolder; 4 } 5 String dubboConfig = configFolder + ServerInit.getConfigPath(SystemConstant.DUBBO_CONFIG_NAME); 6 log.warn("DubboInit,dubboConfig:" + dubboConfig); 7 FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(new String[] {dubboConfig}); 8 context.start();