dubbo接口demo開發

接口需求

客戶端輸入uncleyong(固然,也能夠輸入其它字符串),服務端返回hello uncleyonghtml

開發環境

jdk + idea + maven + zookeeperjava

jdk安裝spring

idea安裝apache

maven安裝app

zookeeper安裝maven

common開發

idea中建立模塊dubbo-commonide

存放公共的實體類、接口idea

package com.uncleyong.dubbotest.service;


public interface SayHelloToClient {
    public String sayHello(String name);
}

而後mvn install打包,供provider及consumer在pom文件中引包 spa

provider開發

idea中建立模塊dubbo_provider代理

建立實現類

package com.uncleyong.dubbotest.service.impl;

import com.uncleyong.dubbotest.service.SayHelloToClient;


public class SayHelloToClientImpl implements SayHelloToClient {

    public String sayHello(String name){
        System.out.println("from client :" + name);
        return "hello, " + name;
    }
}

配置文件,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://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <dubbo:application name="dubbo-provider"/>

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

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

    <!-- 聲明須要暴露的服務接口 -->
    <dubbo:service interface="com.uncleyong.dubbotest.service.SayHelloToClient" ref="sayhellotoclient"/>


    <!-- 和本地bean同樣實現服務 -->
    <bean id="sayhellotoclient" class="com.uncleyong.dubbotest.service.impl.SayHelloToClientImpl"/>

</beans>

建立主運行文件,ProviderMain

package com.uncleyong.dubbotest.main;

import org.springframework.context.support.ClassPathXmlApplicationContext;


public class ProviderMain {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] {"provider.xml"});
        context.start();
        System.out.println("註冊成功,如想退出,按任意鍵退出");
        System.in.read(); // 按任意鍵退出
    }
}

consumer開發

idea中建立模塊dubbo_consumer

主運行文件

package com.uncleyong.dubbotest.main;

import com.uncleyong.dubbotest.service.SayHelloToClient;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Scanner;


public class ConsumerMain {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] {"consumer.xml"});
        context.start();
        // 獲取遠程服務代理
        SayHelloToClient say = (SayHelloToClient) context.getBean("sayhellotoclient");
        // 執行遠程方法
        String res = say.sayHello("UncleYong");
        // 顯示調用結果
        System.out.println(res);
        new Scanner(System.in).next();
    }
}

配置文件,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://dubbo.apache.org/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">


    <dubbo:application name="dubbo-consumer"/>

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

    <!-- 生成遠程服務代理,能夠和本地bean同樣使用demoService -->
    <dubbo:reference id="sayhellotoclient" interface="com.uncleyong.dubbotest.service.SayHelloToClient"/>

</beans>

運行結果及項目源碼

先啓動zookeeper,進入zookeeper的bin目錄,點擊【zkServer.cmd】

啓動provider

服務註冊成功

啓動consumer,能夠看到輸出告終果

在provider端,也能夠看到客戶端發過來的消息

 

至此,開發完成。

 

加羣獲取源碼。

相關文章
相關標籤/搜索