慢慢习惯着各种XML,Dubbo是依赖Spring的,看来spring也要好好研究一下
下面是一个测试demo,来自于dubbo.io里提供
github地址:git clone https://github.com/apache/incubator-dubbo.git
注意不要只下载dubbo-demo这一个模块,是不够的,这个demo是依赖于其他module的,所以将整个主分支拉下来
1:导入工程
和上面一样,不要只import dubbo-demo这部分,将整个分支都import进去,可以只测试dubbo-demo这个模块,同时选择maven工程,设置JDK等等
2:配置maven
选择自己的maven路径,可以单独配置一个setting.xml和repository,可以不要和公司内部的冲突
依赖都搞定之后,下面dubbo-demo这个模块才是要关心的
其中api就是暴露的接口,另两个生产者和消费者,以及一个maven的xml文件
3:spring
由于dubbo框架是依赖spring的,它作为dubbo的容器,因为这里是import的project,而不是创建的spring项目,因此看下有没有maven直接装好了
其实此时,dubbo架构里的一个部分注册中心Registry还没有配置,可以先执行一下demo试试,先启动生产者provider
[10/06/18 09:44:46:046 CST] main INFO support.ClassPathXmlApplicationContext: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@782830e: startup date [Sun Jun 10 21:44:46 CST 2018]; root of context hierarchy
[10/06/18 09:44:46:046 CST] main INFO xml.XmlBeanDefinitionReader: Loading XML bean definitions from class path resource [META-INF/spring/dubbo-demo-provider.xml]
[10/06/18 09:44:46:046 CST] main INFO logger.LoggerFactory: using logger: com.alibaba.dubbo.common.logger.log4j.Log4jLoggerAdapter
[10/06/18 09:44:46:046 CST] main WARN extension.SpringExtensionFactory: [DUBBO] No spring extension(bean) named:defaultCompiler, try to find an extension(bean) of type java.lang.String, dubbo version: , current host: 192.168.0.13
[10/06/18 09:44:46:046 CST] main WARN extension.SpringExtensionFactory: [DUBBO] No spring extension(bean) named:defaultCompiler, type:java.lang.String found, stop get bean., dubbo version: , current host: 192.168.0.13
[10/06/18 09:44:47:047 CST] main INFO config.AbstractConfig: [DUBBO] The service ready on spring started. service: com.alibaba.dubbo.demo.DemoService, dubbo version: , current host: 192.168.0.13
[10/06/18 09:44:47:047 CST] main WARN extension.SpringExtensionFactory: [DUBBO] No spring extension(bean) named:monitorFactory, try to find an extension(bean) of type com.alibaba.dubbo.monitor.MonitorFactory, dubbo version: , current host: 192.168.0.13
[10/06/18 09:44:47:047 CST] main WARN extension.SpringExtensionFactory: [DUBBO] No spring extension(bean) named:monitorFactory, type:com.alibaba.dubbo.monitor.MonitorFactory found, stop get bean., dubbo version: , current host: 192.168.0.13
[10/06/18 09:44:47:047 CST] main INFO config.AbstractConfig: [DUBBO] Export dubbo service com.alibaba.dubbo.demo.DemoService to local registry, dubbo version: , current host: 192.168.0.13
[10/06/18 09:44:47:047 CST] main INFO config.AbstractConfig: [DUBBO] Export dubbo service com.alibaba.dubbo.demo.DemoService to url dubbo://192.168.0.13:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&bind.ip=192.168.0.13&bind.port=20880&dubbo=2.0.2&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=10211&qos.port=22222&side=provider×tamp=1528638287262, dubbo version: , current host: 192.168.0.13
[10/06/18 09:44:47:047 CST] main INFO config.AbstractConfig: [DUBBO] Register dubbo service com.alibaba.dubbo.demo.DemoService url dubbo://192.168.0.13:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&bind.ip=192.168.0.13&bind.port=20880&dubbo=2.0.2&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=10211&qos.port=22222&side=provider×tamp=1528638287262 to registry registry://224.5.6.7:1234/com.alibaba.dubbo.registry.RegistryService?application=demo-provider&dubbo=2.0.2&pid=10211&qos.port=22222®istry=multicast×tamp=1528638287251, dubbo version: , current host: 192.168.0.13
[10/06/18 09:44:47:047 CST] main INFO transport.AbstractServer: [DUBBO] Start NettyServer bind /0.0.0.0:20880, export /192.168.0.13:20880, dubbo version: , current host: 192.168.0.13
Exception in thread "main" java.lang.IllegalStateException: Can't assign requested address
at com.alibaba.dubbo.registry.multicast.MulticastRegistry.<init>(MulticastRegistry.java:117)
at com.alibaba.dubbo.registry.multicast.MulticastRegistryFactory.createRegistry(MulticastRegistryFactory.java:31)
at com.alibaba.dubbo.registry.support.AbstractRegistryFactory.getRegistry(AbstractRegistryFactory.java:96)
at com.alibaba.dubbo.registry.RegistryFactory$Adaptive.getRegistry(RegistryFactory$Adaptive.java)
at com.alibaba.dubbo.registry.integration.RegistryProtocol.getRegistry(RegistryProtocol.java:203)
at com.alibaba.dubbo.registry.integration.RegistryProtocol.export(RegistryProtocol.java:137)
at com.alibaba.dubbo.rpc.protocol.ProtocolListenerWrapper.export(ProtocolListenerWrapper.java:55)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper.export(ProtocolFilterWrapper.java:98)
at com.alibaba.dubbo.rpc.Protocol$Adaptive.export(Protocol$Adaptive.java)
at com.alibaba.dubbo.config.ServiceConfig.doExportUrlsFor1Protocol(ServiceConfig.java:513)
at com.alibaba.dubbo.config.ServiceConfig.doExportUrls(ServiceConfig.java:358)
at com.alibaba.dubbo.config.ServiceConfig.doExport(ServiceConfig.java:317)
at com.alibaba.dubbo.config.ServiceConfig.export(ServiceConfig.java:216)
at com.alibaba.dubbo.config.spring.ServiceBean.onApplicationEvent(ServiceBean.java:123)
at com.alibaba.dubbo.config.spring.ServiceBean.onApplicationEvent(ServiceBean.java:49)
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:393)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:347)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:883)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
at com.alibaba.dubbo.demo.provider.Provider.main(Provider.java:27)
Caused by: java.net.SocketException: Can't assign requested address
at java.net.PlainDatagramSocketImpl.join(Native Method)
at java.net.AbstractPlainDatagramSocketImpl.join(AbstractPlainDatagramSocketImpl.java:178)
at java.net.MulticastSocket.joinGroup(MulticastSocket.java:323)
at com.alibaba.dubbo.registry.multicast.MulticastRegistry.<init>(MulticastRegistry.java:90)
... 24 more
[10/06/18 09:44:47:047 CST] DubboShutdownHook INFO config.DubboShutdownHook: [DUBBO] Run shutdown hook now., dubbo version: , current host: 192.168.0.13
[10/06/18 09:44:47:047 CST] DubboShutdownHook INFO support.AbstractRegistryFactory: [DUBBO] Close all registries [], dubbo version: , current host: 192.168.0.13
[10/06/18 09:44:47:047 CST] DubboShutdownHook INFO dubbo.DubboProtocol: [DUBBO] Close dubbo server: /192.168.0.13:20880, dubbo version: , current host: 192.168.0.13
[10/06/18 09:44:47:047 CST] DubboShutdownHook INFO transport.AbstractServer: [DUBBO] Close NettyServer bind /0.0.0.0:20880, export /192.168.0.13:20880, dubbo version: , current host: 192.168.0.13
[10/06/18 09:44:47:047 CST] DubboShutdownHook INFO dubbo.DubboProtocol: [DUBBO] Unexport service: dubbo://192.168.0.13:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&bind.ip=192.168.0.13&bind.port=20880&dubbo=2.0.2&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=10211&qos.port=22222&side=provider×tamp=1528638287262, dubbo version: , current host: 192.168.0.13
[10/06/18 09:44:47:047 CST] DubboShutdownHook INFO injvm.InjvmProtocol: [DUBBO] Unexport service: injvm://127.0.0.1/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&bind.ip=192.168.0.13&bind.port=20880&dubbo=2.0.2&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=10211&qos.port=22222&side=provider×tamp=1528638287262, dubbo version: , current host: 192.168.0.13
Process finished with exit code 1
可以看到报错信息:
at com.alibaba.dubbo.registry.multicast.MulticastRegistry.(MulticastRegistry.java:90)
这么看来默认配置的注册中心是multicast,而且注册中心列表为空:[DUBBO] Close all registries []
这里就需要启动注册中心
我就本地起一个注册中心,这里用zookeeper,直接brew安装一波
==> Downloading https://homebrew.bintray.com/bottles/zookeeper-3.4.12.high_sierra.bottle.tar.gz ######################################################################## 100.0% ==> Pouring zookeeper-3.4.12.high_sierra.bottle.tar.gz ==> Caveats To have launchd start zookeeper now and restart at login: brew services start zookeeper Or, if you don't want/need a background service you can just run: zkServer start ==> Summary 🍺 /usr/local/Cellar/zookeeper/3.4.12: 242 files, 32.9MB lihui@2018 ~ brew list zookeeper /usr/local/Cellar/zookeeper/3.4.12/.bottle/etc/ (4 files) /usr/local/Cellar/zookeeper/3.4.12/bin/cli_mt /usr/local/Cellar/zookeeper/3.4.12/bin/cli_st /usr/local/Cellar/zookeeper/3.4.12/bin/load_gen /usr/local/Cellar/zookeeper/3.4.12/bin/zkCleanup /usr/local/Cellar/zookeeper/3.4.12/bin/zkCli /usr/local/Cellar/zookeeper/3.4.12/bin/zkServer /usr/local/Cellar/zookeeper/3.4.12/homebrew.mxcl.zookeeper.plist /usr/local/Cellar/zookeeper/3.4.12/include/zookeeper/ (6 files) /usr/local/Cellar/zookeeper/3.4.12/lib/libzookeeper_mt.2.dylib /usr/local/Cellar/zookeeper/3.4.12/lib/libzookeeper_st.2.dylib /usr/local/Cellar/zookeeper/3.4.12/lib/ (4 other files) /usr/local/Cellar/zookeeper/3.4.12/libexec/bin/ (5 files) /usr/local/Cellar/zookeeper/3.4.12/libexec/contrib/ (196 files) /usr/local/Cellar/zookeeper/3.4.12/libexec/lib/ (12 files) /usr/local/Cellar/zookeeper/3.4.12/libexec/zookeeper-3.4.12.jar
启动zookeeper,查看一下进程和端口
lihui@2018 ~ zkServer start ZooKeeper JMX enabled by default Using config: /usr/local/etc/zookeeper/zoo.cfg Starting zookeeper ... STARTED lihui@2018 ~ cat /usr/local/etc/zookeeper/zoo.cfg | grep -i port # the port at which the clients will connect clientPort=2181 lihui@2018 ~ lsof -i:2181 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME java 10681 lihui 30u IPv6 0x9a76cec30ac2be17 0t0 TCP *:eforward (LISTEN)
除了启动进程,还需要修改xml配置,zookeeper本地IP和端口
生产者:dubbo-demo-provider/src/main/resources/META-INF.spring/dubbo-demo-provider.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/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://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- provider's application name, used for tracing dependency relationship -->
<dubbo:application name="demo-provider"/>
<!-- use multicast registry center to export service -->
<!-- dubbo:registry address="multicast://224.5.6.7:1234"/ -->
<dubbo:registry address="zookeeper://192.168.0.13:2181"/>
<!-- use dubbo protocol to export service on port 20880 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- service implementation, as same as regular local bean -->
<bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>
<!-- declare the service interface to be exported -->
<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/>
</beans>
消费者:dubbo-demo-consumer/src/main/resources/META-INF.spring/dubbo-demo-consumer.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/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://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
don't set it same as provider -->
<dubbo:application name="demo-consumer"/>
<!-- use multicast registry center to discover service -->
<!-- dubbo:registry address="multicast://224.5.6.7:1234"/ -->
<dubbo:registry address="zookeeper://192.168.0.13:2181"/>
<!-- generate proxy for the remote service, then demoService can be used in the same way as the
local regular interface -->
<dubbo:reference id="demoService" check="false" interface="com.alibaba.dubbo.demo.DemoService"/>
</beans>
执行dubbo-demo程序
首先是Provider的main
[10/06/18 10:06:46:046 CST] main INFO zookeeper.ZooKeeper: Client environment:os.name=Mac OS X
[10/06/18 10:06:46:046 CST] main INFO zookeeper.ZooKeeper: Client environment:os.arch=x86_64
[10/06/18 10:06:46:046 CST] main INFO zookeeper.ZooKeeper: Client environment:os.version=10.13.5
[10/06/18 10:06:46:046 CST] main INFO zookeeper.ZooKeeper: Client environment:user.name=lihui
[10/06/18 10:06:46:046 CST] main INFO zookeeper.ZooKeeper: Client environment:user.home=/Users/lihui
[10/06/18 10:06:46:046 CST] main INFO zookeeper.ZooKeeper: Client environment:user.dir=/Users/lihui/caocao/dubbo/incubator-dubbo
[10/06/18 10:06:46:046 CST] main INFO zookeeper.ZooKeeper: Initiating client connection, connectString=192.168.0.13:2181 sessionTimeout=60000 watcher=org.apache.curator.ConnectionState@1ff4931d
[10/06/18 10:06:46:046 CST] main-SendThread(192.168.0.13:2181) INFO zookeeper.ClientCnxn: Opening socket connection to server 192.168.0.13/192.168.0.13:2181. Will not attempt to authenticate using SASL (unknown error)
[10/06/18 10:06:46:046 CST] main INFO zookeeper.ZookeeperRegistry: [DUBBO] Register: dubbo://192.168.0.13:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.2&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=10908&side=provider×tamp=1528639606395, dubbo version: , current host: 192.168.0.13
[10/06/18 10:06:46:046 CST] main-SendThread(192.168.0.13:2181) INFO zookeeper.ClientCnxn: Socket connection established to 192.168.0.13/192.168.0.13:2181, initiating session
[10/06/18 10:06:46:046 CST] main-SendThread(192.168.0.13:2181) INFO zookeeper.ClientCnxn: Session establishment complete on server 192.168.0.13/192.168.0.13:2181, sessionid = 0x10001d113d60000, negotiated timeout = 40000
[10/06/18 10:06:46:046 CST] main-EventThread INFO state.ConnectionStateManager: State change: CONNECTED
[10/06/18 10:06:46:046 CST] main INFO zookeeper.ZookeeperRegistry: [DUBBO] Subscribe: provider://192.168.0.13:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.0.2&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=10908&side=provider×tamp=1528639606395, dubbo version: , current host: 192.168.0.13
[10/06/18 10:06:46:046 CST] main INFO zookeeper.ZookeeperRegistry: [DUBBO] Notify urls for subscribe url provider://192.168.0.13:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.0.2&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=10908&side=provider×tamp=1528639606395, urls: [empty://192.168.0.13:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&category=configurators&check=false&dubbo=2.0.2&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=10908&side=provider×tamp=1528639606395], dubbo version: , current host: 192.168.0.13
这时候服务hold住了
再来启动消费端Consumer
[10/06/18 10:11:37:037 CST] main INFO zookeeper.ZooKeeper: Client environment:os.name=Mac OS X
[10/06/18 10:11:37:037 CST] main INFO zookeeper.ZooKeeper: Client environment:os.arch=x86_64
[10/06/18 10:11:37:037 CST] main INFO zookeeper.ZooKeeper: Client environment:os.version=10.13.5
[10/06/18 10:11:37:037 CST] main INFO zookeeper.ZooKeeper: Client environment:user.name=lihui
[10/06/18 10:11:37:037 CST] main INFO zookeeper.ZooKeeper: Client environment:user.home=/Users/lihui
[10/06/18 10:11:37:037 CST] main INFO zookeeper.ZooKeeper: Client environment:user.dir=/Users/lihui/caocao/dubbo/incubator-dubbo
[10/06/18 10:11:37:037 CST] main INFO zookeeper.ZooKeeper: Initiating client connection, connectString=192.168.0.13:2181 sessionTimeout=60000 watcher=org.apache.curator.ConnectionState@6eda5c9
[10/06/18 10:11:37:037 CST] main-SendThread(192.168.0.13:2181) INFO zookeeper.ClientCnxn: Opening socket connection to server 192.168.0.13/192.168.0.13:2181. Will not attempt to authenticate using SASL (unknown error)
[10/06/18 10:11:38:038 CST] main INFO zookeeper.ZookeeperRegistry: [DUBBO] Register: consumer://192.168.0.13/com.alibaba.dubbo.demo.DemoService?application=demo-consumer&category=consumers&check=false&dubbo=2.0.2&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=10954&qos.port=33333&side=consumer×tamp=1528639897801, dubbo version: , current host: 192.168.0.13
[10/06/18 10:11:38:038 CST] main-SendThread(192.168.0.13:2181) INFO zookeeper.ClientCnxn: Socket connection established to 192.168.0.13/192.168.0.13:2181, initiating session
[10/06/18 10:11:38:038 CST] main-SendThread(192.168.0.13:2181) INFO zookeeper.ClientCnxn: Session establishment complete on server 192.168.0.13/192.168.0.13:2181, sessionid = 0x10001d113d60001, negotiated timeout = 40000
[10/06/18 10:11:38:038 CST] main-EventThread INFO state.ConnectionStateManager: State change: CONNECTED
[10/06/18 10:11:38:038 CST] main INFO zookeeper.ZookeeperRegistry: [DUBBO] Subscribe: consumer://192.168.0.13/com.alibaba.dubbo.demo.DemoService?application=demo-consumer&category=providers,configurators,routers&check=false&dubbo=2.0.2&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=10954&qos.port=33333&side=consumer×tamp=1528639897801, dubbo version: , current host: 192.168.0.13
[10/06/18 10:11:38:038 CST] main INFO zookeeper.ZookeeperRegistry: [DUBBO] Notify urls for subscribe url consumer://192.168.0.13/com.alibaba.dubbo.demo.DemoService?application=demo-consumer&category=providers,configurators,routers&check=false&dubbo=2.0.2&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=10954&qos.port=33333&side=consumer×tamp=1528639897801, urls: [dubbo://192.168.0.13:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=demo-provider&dubbo=2.0.2&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=10908&side=provider×tamp=1528639606395, empty://192.168.0.13/com.alibaba.dubbo.demo.DemoService?application=demo-consumer&category=configurators&check=false&dubbo=2.0.2&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=10954&qos.port=33333&side=consumer×tamp=1528639897801, empty://192.168.0.13/com.alibaba.dubbo.demo.DemoService?application=demo-consumer&category=routers&check=false&dubbo=2.0.2&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=10954&qos.port=33333&side=consumer×tamp=1528639897801], dubbo version: , current host: 192.168.0.13
[10/06/18 10:11:38:038 CST] main INFO transport.AbstractClient: [DUBBO] Successed connect to server /192.168.0.13:20880 from NettyClient 192.168.0.13 using dubbo version , channel is NettyChannel [channel=[id: 0x81ee2fee, /192.168.0.13:56823 => /192.168.0.13:20880]], dubbo version: , current host: 192.168.0.13
[10/06/18 10:11:38:038 CST] main INFO transport.AbstractClient: [DUBBO] Start NettyClient 2018.local/192.168.0.13 connect to the server /192.168.0.13:20880, dubbo version: , current host: 192.168.0.13
[10/06/18 10:11:38:038 CST] main WARN extension.SpringExtensionFactory: [DUBBO] No spring extension(bean) named:monitorFactory, try to find an extension(bean) of type com.alibaba.dubbo.monitor.MonitorFactory, dubbo version: , current host: 192.168.0.13
[10/06/18 10:11:38:038 CST] main WARN extension.SpringExtensionFactory: [DUBBO] No spring extension(bean) named:monitorFactory, type:com.alibaba.dubbo.monitor.MonitorFactory found, stop get bean., dubbo version: , current host: 192.168.0.13
[10/06/18 10:11:38:038 CST] main INFO config.AbstractConfig: [DUBBO] Refer dubbo service com.alibaba.dubbo.demo.DemoService from url zookeeper://192.168.0.13:2181/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=demo-consumer&check=false&dubbo=2.0.2&generic=false&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=10954&qos.port=33333®ister.ip=192.168.0.13&remote.timestamp=1528639606395&side=consumer×tamp=1528639897801, dubbo version: , current host: 192.168.0.13
Hello world, response from provider: 192.168.0.13:20880
Hello world, response from provider: 192.168.0.13:20880
Hello world, response from provider: 192.168.0.13:20880
Hello world, response from provider: 192.168.0.13:20880
Hello world, response from provider: 192.168.0.13:20880
Hello world, response from provider: 192.168.0.13:20880
Hello world, response from provider: 192.168.0.13:20880
Hello world, response from provider: 192.168.0.13:20880
Hello world, response from provider: 192.168.0.13:20880
Hello world, response from provider: 192.168.0.13:20880
Hello world, response from provider: 192.168.0.13:20880
Hello world, response from provider: 192.168.0.13:20880
Hello world, response from provider: 192.168.0.13:20880
Hello world, response from provider: 192.168.0.13:20880
最后输出的结果说明demo运行成功,Consumer通过暴露的Provider接口获取返回结果
Consumer里循环里,调用服务端接口,传一个参数world
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.demo.consumer;
import com.alibaba.dubbo.demo.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Consumer {
public static void main(String[] args) {
//Prevent to get IPV6 address,this way only work in debug mode
//But you can pass use -Djava.net.preferIPv4Stack=true,then it work well whether in debug mode or not
System.setProperty("java.net.preferIPv4Stack", "true");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});
context.start();
DemoService demoService = (DemoService) context.getBean("demoService"); // get remote service proxy
while (true) {
try {
Thread.sleep(1000);
String hello = demoService.sayHello("world"); // call remote method
System.out.println(hello); // get result
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
}
Provider里隐藏的实现代码
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.demo.provider;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.alibaba.dubbo.demo.DemoService;
import com.alibaba.dubbo.rpc.RpcContext;
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
}
}
最终返回整个字符串,Consumer打印输出结果