1. 程式人生 > >Dubbo、Spring、Zookeeper整合基礎案例

Dubbo、Spring、Zookeeper整合基礎案例

摘要:最近抽時間系統的學習了Dubbo的一些內容,趁有時間,整理下,順便記錄下,以防以後回顧。

一:執行環境

1>:JDK 1.8

2>:IDEA 2018.1

3>:Zookeeper 3.x

二:專案結構

三:建立服務提供者工程

package com.micai.dubbo.provider;

/**
 * @Auther: zhaoxinguo
 * @Date: 2018/9/10 13:33
 * @Description: 定義服務介面
 */
public interface DemoService {

    String sayHello(String name);
}
package com.micai.dubbo.provider;

/**
 * @Auther: zhaoxinguo
 * @Date: 2018/9/10 13:34
 * @Description: 在服務提供方實現介面
 */
public class DemoServiceImpl implements DemoService {

    @Override
    public String sayHello(String name) {
        return "Hello" + name;
    }
}
package com.micai.dubbo;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

/**
 * @Auther: zhaoxinguo
 * @Date: 2018/9/10 14:13
 * @Description:
 */
public class Provider {

    public static void main(String [] args) {
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        classPathXmlApplicationContext.start();
        try {
            System.in.read();//按任意鍵退出
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
<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-4.2.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!--提供方應用資訊,用於計算依賴關係-->
    <dubbo:application name="hello-world-app"/>

    <!--使⽤zookeeper⼴播註冊中⼼暴露服務地址-->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>

    <!--用dubbo協議在20880埠暴露服務-->
    <dubbo:protocol name="dubbo" port="20880"/>

    <!--宣告需要暴露的服務介面-->
    <dubbo:service interface="com.micai.dubbo.provider.DemoService" ref="demoService"/>

    <!--和本地bean一樣實現服務-->
    <bean id="demoService" class="com.micai.dubbo.provider.DemoServiceImpl"/>

</beans>

四:建立服務消費者工程

package com.micai.dubbo;

import com.micai.dubbo.provider.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Auther: zhaoxinguo
 * @Date: 2018/9/11 14:36
 * @Description:
 */
public class Consumer {

    public static void main(String [] args) {
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        classPathXmlApplicationContext.start();
        DemoService demoService = (DemoService) classPathXmlApplicationContext.getBean("demoService");
        String str = demoService.sayHello("趙新國");
        System.out.println("呼叫結果" + str);
    }
}
<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-4.2.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!--消費方應用名、用於計算依賴關係,不是匹配條件,不要與提供方一樣-->
    <dubbo:application name="consumer-of-helloworld-app"/>

    <!--使用zookeeper廣播註冊中心暴露發現服務地址-->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>

    <!--生成遠端服務代理,可以和本地bean一樣使用demoService-->
    <dubbo:reference id="demoService" interface="com.micai.dubbo.provider.DemoService"/>

</beans>

五:執行結果