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端,也能够看到客户端发过来的消息

 

至此,开发完成。

 

加群获取源码。

相关文章
相关标签/搜索