本文目的:
本來網上此文極多,但是代碼能直接運行的不多。這裏做了一個總結
1、dubbo相關介紹(略)
2、搭建
2.1 zookeeper單機或集羣(略)
2.2 dubbo server (見參考文章)
2.2 dubbo client(見參考文章)
此處說明:
1、application.xml一個名字就可以,不必分開,給自己找麻煩。
2、pom.xml建議:spring用4.x版本,maven complier pluin配置(eclipse之後版本要install jre -> jdk的方式,最新eclipse可能還需要登錄授權更改)
3、client的pom引用server的jar,這樣不必再重新寫server中的接口代碼,不用搞兩份了,也不用手動打什麼jar包了,maven install會打。
4、3之後,運行之前,先maven install 一下server, 然後再啓動,
5、啓動過程,先啓動server的main, 再是client的main
運行中zookeeper說明
運行後,在zookeeper上ls /, 發現多了dubbo節點,然後向下下鑽查看,
在demoServer下,有了四個節點(consumers, souters, providers, configurators)看到都是空的,因爲程序停止了。重新啓動一下
重新啓動後,configurators還是空的,providers有了內容如下
即內容:
dubbo%3A%2F%2F10.128.6.35%3A20880%2Fcom.mor.server.dubbo.service.DemoServer%3Fanyhost%3Dtrue%26application%3Dhello-world-app%26dubbo%3D2.5.3%26interface%3Dcom.mor.server.dubbo.service.DemoServer%26methods%3DsayHello%26pid%3D2256%26side%3Dprovider%26timestamp%3D1524541239406
對照url 編碼值[https://zhidao.baidu.com/question/584352067.html]
字符 - URL編碼值
空格 - %20
" - %22
# - %23
% - %25
& - %26
( - %28
) - %29
+ - %2B
, - %2C
/ - %2F
: - %3A
; - %3B
< - %3C
= - %3D
> - %3E
? - %3F
@ - %40
\ - %5C
| - %7C
這個providers是
dubbo%3A%2F%2F10.128.6.35%3A20880%2Fcom.mor.server.dubbo.service.DemoServer%3Fanyhost%3Dtrue%26application%3Dhello-world-app%26dubbo%3D2.5.3%26interface%3Dcom.mor.server.dubbo.service.DemoServer%26methods%3DsayHello%26pid%3D2256%26side%3Dprovider%26timestamp%3D1524541239406
解析下來就是:
dubbo://10.128.6.35:20880/com.mor.server.dubbo.service.DemoServer?anyhost=true&application=hello-world-app&dubbo=2.5.3&interface=com.mor.server.dubbo.service.DemoServer&methods=sayHello&pid=2256&side=provider×tamp=1524541239406
/dubbo/com.mor.server.dubbo.service.DemoServer/routers 還是[]
/dubbo/com.mor.server.dubbo.service.DemoServer/consumers:
consumer%3A%2F%2F10.128.6.35%2Fcom.mor.server.dubbo.service.DemoServer%3Fapplication%3Dconsumer-of-helloworld-app%26category%3Dconsumers%26check%3Dfalse%26dubbo%3D2.5.3%26interface%3Dcom.mor.server.dubbo.service.DemoServer%26methods%3DsayHello%26pid%3D12848%26side%3Dconsumer%26timestamp%3D1524550888405
解析後:
consumer://10.128.6.35/com.mor.server.dubbo.service.DemoServer?application=consumer-of-helloworld-app&category=consumers&check=false&dubbo=2.5.3&interface=com.mor.server.dubbo.service.DemoServer&methods=sayHello&pid=12848&side=consumer×tamp=1524550888405
當再次啓動client的時候,這個值會增加,成爲[consumer://xxx, consumer://xxx, consumer://xxx, ...],會變成5個,再過會,會變成46個,那是因爲參考文章裏的代碼在消費端重複註冊了。寫在外面就不會有這麼多個了,應該是隻有兩個是正常的
參考文章
[Dubbo實戰]dubbo + zookeeper + spring 實戰 - CSDN博客
https://blog.csdn.net/ggibenben1314/article/details/47725241
git上已經上傳代碼
https://github.com/stevenlii/dubboclient
https://github.com/stevenlii/dubboserver