1. 程式人生 > >springboot下的dubbo、zookeeper 結合使用

springboot下的dubbo、zookeeper 結合使用

exception center bat cep ren ice admin onf face

近期在研究dubbo框架

相信看到這篇博客的,dubbo的基礎應該都有了

zookeeper的搭建走了點彎路,配置起來各種麻煩,媽的搞的好煩。

正好一直想用一下docker,但對docker只是有個簡單的概念。

用了一晚上去研究docker,之後發現真的好用

搭建個zookeeper就跟玩似的。

這裏記錄一下遇到的一些坑!

1、springboot引入dubbo的配置文件

網上搜索了一下,大概的兩種方式

1、 這種方式是通過 ClassPathXmlApplicationContext 加載xml來獲取上下文Context啟動

 1 @SpringBootApplication
 2  3 public
class WreserveApplication { 4 5 public static void main(String[] args) throws IOException, InterruptedException { 6 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo-provider.xml"}); 7 context.start(); 8 System.in.read(); //
press any key to exit 9 } 10 11 }

2、通過 @ImportResource({ "classpath:dubbo-provider.xml" }) 加載。

 1 @SpringBootApplication
 2 @ImportResource({ "classpath:dubbo-provider.xml" })
 3 public class WreserveApplication {
 4     @Bean
 5     public CountDownLatch closeLatch() {
 6         return new
CountDownLatch(1); 7 } 8 9 public static void main(String[] args) throws IOException, InterruptedException { 10 ConfigurableApplicationContext context = SpringApplication.run(WreserveApplication.class, args); 11 CountDownLatch closeLatch = context.getBean(CountDownLatch.class); 12 closeLatch.await(); 13 14 } 15 16 }

2、dubbo-provider.xml和dubbo-consumer.xml中需要註意的一些問題

dubbo-provider.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    
    <dubbo:application name="demo-provider"/>

    <!-- use multicast registry center to export service -->
    <dubbo:registry address="zookeeper://192.168.99.100: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="helloService" class="com.zhyonk.service.Impl.HelloServiceImpl"/>

    <!-- declare the service interface to be exported -->
    <dubbo:service interface="com.zhyonk.service.HelloService" ref="helloService"/>
    
</beans>

dubbo-consumer.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">


    <dubbo:application name="wreserve-wechat"></dubbo:application>
    <!--zookeeper註冊中心 -->
    <dubbo:registry
        address="zookeeper://192.168.99.100:2181"></dubbo:registry>

    <dubbo:consumer check="false" timeout="60000" />

    <!-- 生成遠程服務代理,可以和本地bean一樣使用demoService -->
    <dubbo:reference id="helloService"
        interface="com.zhyonk.wreserve.service.HelloService" protocol="dubbo"
        timeout="15000" />


</beans>

1、消費者要訪問提供者,

<dubbo:reference id="helloService"
        interface="com.zhyonk.wreserve.service.HelloService" protocol="dubbo"
    <!-- service implementation, as same as regular local bean -->
    <bean id="helloService" class="com.zhyonk.service.Impl.HelloServiceImpl"/>

    <!-- declare the service interface to be exported -->
    <dubbo:service interface="com.zhyonk.service.HelloService" ref="helloService"/>

其中的interface必須要求同一個路徑之下,不然對應不上,會出現下面的情況

技術分享圖片

正常情況下dubbo-admin中會顯示

技術分享圖片

2、如果嫌自己搭建麻煩的話可以直接用用網站自動生成 http://start.dubbo.io/

技術分享圖片

就先到這,睡了

springboot下的dubbo、zookeeper 結合使用