背景:
一个应用使用dubbo,自己提供的服务,自己去调。(不要问我干嘛通过dubbo去掉自己的服务)
实践一:
启动一个spring容器,加载dubbo容器,注册并使用dubbo服务。
<dubbo:application name="app" />
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<dubbo:protocol name="dubbo" port="20881" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.coder4j.job.facade.HiDubboFacade" ref="hiDubboFacade" />
<!-- 声明bean, 用于实际提供服务 -->
<bean id="hiDubboFacade" class="com.coder4j.job.facade.impl.HiDubboFacadeImpl"/>
<!-- 使用dubbo引入bean -->
<dubbo:reference id="helloDubbo" interface="com.coder4j.job.facade.HiDubboFacade" />
启动容器
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "classpath:provider.xml" } );
context.start();
HiDubboFacade o1 = ( HiDubboFacade ) context.getBean( "hiDubboFacade" );
String s1 = o1.hi( "xxt11111" );
System.out.println( s1 );
System.out.println( "reference1 hashcode:" + o1.hashCode() );
HiDubboFacade o2 = ( HiDubboFacade ) context.getBean( "helloDubbo" );
String s2 = o2.hi( "xxt" );
System.out.println( s2 );
System.out.println( "reference2 hashcode:" + o2.hashCode() );
没有问题:
i received msg:xxt11111
reference1 hashcode:352598575
[2017-07-20 16:27:59.659] [main] [INFO ] [c.a.dubbo.config.AbstractConfig] - [DUBBO] Using injvm service com.coder4j.job.facade.HiDubboFacade, dubbo version: 2.5.3, current host: 127.0.0.1
[2017-07-20 16:27:59.659] [main] [INFO ] [c.a.dubbo.config.AbstractConfig] - [DUBBO] Refer dubbo service com.coder4j.job.facade.HiDubboFacade from url injvm://127.0.0.1/com.coder4j.job.facade.HiDubboFacade?application=app&dubbo=2.5.3&interface=com.coder4j.job.facade.HiDubboFacade&methods=hi&pid=11220&side=consumer×tamp=1500539279654, dubbo version: 2.5.3, current host: 127.0.0.1
i received msg:xxt
reference hashcode:1873091796
实践二:
使用相同的配置,在web应用中启动,就拿不到 helloDubbo,报错。
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'helloDubbo': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: Failed to check the status of the service com.coder4j.job.facade.HiDubboFacade. No provider available for the service com.coder4j.job.facade.HiDubboFacade from the url registry://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=app&cluster=available&dubbo=2.5.3&pid=13008&refer=application%3Dapp%26dubbo%3D2.5.3%26interface%3Dcom.coder4j.job.facade.HiDubboFacade%26methods%3Dtell%2Chello%26pid%3D13008%26revision%3D1.0.0%26side%3Dconsumer%26timestamp%3D1500532931746®istry=zookeeper×tamp=1500532931788 to the consumer 192.168.23.144 use dubbo version 2.5.3
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:175)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:103)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1634)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:254)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081)
at com.coder4j.job.integration.MyIntegration.setApplicationContext(MyIntegration.java:61)
at org.springframework.context.support.ApplicationContextAwareProcessor.invokeAwareInterfaces(ApplicationContextAwareProcessor.java:121)
at org.springframework.context.support.ApplicationContextAwareProcessor.postProcessBeforeInitialization(ApplicationContextAwareProcessor.java:97)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1620)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
... 20 more
Caused by: java.lang.IllegalStateException: Failed to check the status of the service com.coder4j.job.facade.HiDubboFacade. No provider available for the service com.coder4j.job.facade.HiDubboFacade from the url registry://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=app&cluster=available&dubbo=2.5.3&pid=13008&refer=application%3Dapp%26dubbo%3D2.5.3%26interface%3Dcom.coder4j.job.facade.HiDubboFacade%26methods%3Dtell%2Chello%26pid%3D13008%26revision%3D1.0.0%26side%3Dconsumer%26timestamp%3D1500532931746®istry=zookeeper×tamp=1500532931788 to the consumer 192.168.23.144 use dubbo version 2.5.3
at com.alibaba.dubbo.config.ReferenceConfig.createProxy(ReferenceConfig.java:420)
at com.alibaba.dubbo.config.ReferenceConfig.init(ReferenceConfig.java:300)
at com.alibaba.dubbo.config.ReferenceConfig.get(ReferenceConfig.java:138)
at com.alibaba.dubbo.config.spring.ReferenceBean.getObject(ReferenceBean.java:65)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:168)
... 31 more
实践三:
如果我单独启动一个spring java容器,将服务注册到zookeeper, 再启动一个web应用是可以拿到java容器发布的服务的。
牛魔王的故事
相关分类