入门技术的第一件事就是官网的quick start,把Hello World跑起来。
根据 http://dubbo.apache.org/en-us/docs/user/quick-start.html 中的步骤,构建相关工程结构和文件。
POM中dubbo的依赖使用的com.alibaba:dubbo:jar:2.5.9
第一个坑
启动之后发现报错:
Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://dubbo.apache.org/schema/dubbo]
Offending resource: class path resource [META-INF/spring/dubbo-demo-provider.xml]
这个错误一看应该是XML schema的问题 :
推断由于引入的依赖包group还是com.alibaba,所以这个schema明显不应该是apache.org,因此查阅后改成下面这种:
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
</beans>
第二个坑
provider启动起来之后,再去启动consumer, 又报错了:
Failed to invoke the method sayHello in the service com.zlrui.learn.dubbo.demo.DemoService.
No provider available for the service com.zlrui.learn.dubbo.demo.DemoService:1.0.0 from registry 224.5.6.7:1234 on the consumer 192.168.245.1 using the dubbo version 2.0.1. Please check if the providers have been started and registered.
然后就是检查配置问题,包括广播地址加"?unicast=false"、服务版本加"version"、在<dubbo:protocol>中指定host等, 发现都不能解决问题, 最后在csdn的发现一个小伙伴博客, 并尝试通过禁用虚拟网卡解决了这个问题。
最后正常启动,完成了hello world的输出。
不过日志中又发现了一个问题, 就是依赖的dubbo版本是2.5.9 但是日志里却说用的2.0.1, 比较费解:
21:20:06.185 [DubboClientHandler-192.168.1.2:20880-thread-1] DEBUG com.alibaba.dubbo.remoting.transport.DecodeHandler - [DUBBO] Decode decodeable message com.alibaba.dubbo.rpc.protocol.dubbo.DecodeableRpcResult, dubbo version: 2.0.1, current host: 192.168.245.1
然后把依赖换成2.5.3之后,发现日志引用的就是2.5.3,卧槽!无情!
21:35:58.003 [DubboClientHandler-192.168.1.2:20880-thread-1] DEBUG com.alibaba.dubbo.remoting.transport.DecodeHandler - [DUBBO] Decode decodeable message com.alibaba.dubbo.rpc.protocol.dubbo.DecodeableRpcResult, dubbo version: 2.5.3, current host: 192.168.245.1
第三个坑
鉴于上面的问题, 我就想知道目前企业应用到生产上的主要是什么版本, 找了半天没找到, 一怒之下切换了中文版的去看,
惊喜!! 中文页面比英文页面竟然多了一个 "Dubbo 版本发布及新特性速览". 发现都用到2.7版本了
于是修改依赖用最贴近生产的去运行试试, 发现没有依赖包, 于时想到应该是捐给apache之后改了group了,果然应该用这个:
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.3</version>
</dependency>
然后启动provider,正常 使用的dubbo版本也是2.7.3
再启动consumer,报错!
21:49:05.943 [main] ERROR org.apache.dubbo.qos.server.Server - [DUBBO] qos-server can not bind localhost:22222, dubbo version: 2.7.3, current host: 192.168.1.2
java.net.BindException: Address already in use: bind
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:433)
at sun.nio.ch.Net.bind(Net.java:425)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
at io.netty.channel.socket.nio.NioServerSocketChannel.doBind(NioServerSocketChannel.java:134)
at io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:550)
at io.netty.channel.DefaultChannelPipeline$HeadContext.bind(DefaultChannelPipeline.java:1334)
at io.netty.channel.AbstractChannelHandlerContext.invokeBind(AbstractChannelHandlerContext.java:506)
at io.netty.channel.AbstractChannelHandlerContext.bind(AbstractChannelHandlerContext.java:491)
at io.netty.channel.DefaultChannelPipeline.bind(DefaultChannelPipeline.java:973)
at io.netty.channel.AbstractChannel.bind(AbstractChannel.java:248)
at io.netty.bootstrap.AbstractBootstrap$2.run(AbstractBootstrap.java:356)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute $|$ capture(AbstractEventExecutor.java:164)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:748)
21:49:05.943 [main] WARN org.apache.dubbo.qos.protocol.QosProtocolWrapper - [DUBBO] Fail to start qos server: , dubbo version: 2.7.3, current host: 192.168.1.2
java.net.BindException: Address already in use: bind
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:433)
at sun.nio.ch.Net.bind(Net.java:425)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
at io.netty.channel.socket.nio.NioServerSocketChannel.doBind(NioServerSocketChannel.java:134)
at io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:550)
at io.netty.channel.DefaultChannelPipeline$HeadContext.bind(DefaultChannelPipeline.java:1334)
at io.netty.channel.AbstractChannelHandlerContext.invokeBind(AbstractChannelHandlerContext.java:506)
at io.netty.channel.AbstractChannelHandlerContext.bind(AbstractChannelHandlerContext.java:491)
at io.netty.channel.DefaultChannelPipeline.bind(DefaultChannelPipeline.java:973)
at io.netty.channel.AbstractChannel.bind(AbstractChannel.java:248)
at io.netty.bootstrap.AbstractBootstrap$2.run(AbstractBootstrap.java:356)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute$|$capture(AbstractEventExecutor.java:164)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:748)
根据异常可以看出来应该是端口被占用了, 快速跑起来还没有到探究原理的地步, 搜索后发现默认情况下provider和consumer都需要绑定到22222端口上, 因此一台机器上的话只能指定不同的端口了. 分别修改2个XML增加QoS参数设置不同的端口:
<dubbo:application name="demo-provider">
<dubbo:parameter key="qos.enable" value="true"/>
<dubbo:parameter key="qos.accept.foreign.ip" value="false"/>
<dubbo:parameter key="qos.port" value="22222"/>
</dubbo:application>
<dubbo:application name="demo-consumer">
<dubbo:parameter key="qos.enable" value="true"/>
<dubbo:parameter key="qos.accept.foreign.ip" value="false"/>
<dubbo:parameter key="qos.port" value="33333"/>
</dubbo:application>
重新尝试, 顺利运行!
参考文章:
https://blog.csdn.net/liuchunlin2008/article/details/82757092
https://blog.csdn.net/axing2015/article/details/81433678
https://blog.csdn.net/u012988901/article/details/84503672