Dubbo学习(三)HelloWorld

准备

  1. 创建一个maven主工程
Dubbo学习(三)HelloWorld_第1张图片
  1. 创建三个子模块,接口模块dubboapi、生产者模块dubboprovider、消费者模块dubboconsumer


    4.0.0

    com.maple.dubbodemo
    dubbo-root
    pom
    1.0-SNAPSHOT
    
        dubboapi
        dubboprovider
        dubboconsumer
    

接口模块dubboapi

  1. 创建接口
package com.maple.dubbodemo;

/**
 * Created by maple on 2017-10-26.
 */
public interface HelloService {
    public String sayHello(String content);
}

生产者模块dubboprovider

  1. 引入接口模块,在pom.xml配置

       com.maple.dubbodemo
       dubbo-api
       ${project.parent.version}

  1. 引入dubbo以及spring的包,在pom.xml配置

        com.alibaba
        dubbo
        2.5.6

 

            org.apache.zookeeper
            zookeeper
            3.4.10
            pom



            com.101tec
            zkclient
            0.10

  1. 实现接口
package com.maple.dubbodemo.provider;

import com.alibaba.dubbo.rpc.RpcContext;
import com.maple.dubbodemo.HelloService;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * Created by maple on 2017-10-26.
 */
public class HelloServiceImpl implements HelloService {
    public String sayHello(String content) {
        System.out.println("[" + new SimpleDateFormat("HH:mm:ss").format(new Date()) + "] Hello " + content + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
        return "Hello " + content + ", response form provider: " + RpcContext.getContext().getLocalAddress();
    }
}
  1. 生产者spring配置,路径\src\main\resources\META-INF\spring下创建配置文件dubbo-demo-provider.xml;




    
    

    
    

    
    

    

    
    

    
    


  1. 创建生产者
package com.maple.dubbodemo.provider;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by maple on 2017-10-26.
 */
public class Provider {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-provider.xml"});
        context.start();
        System.in.read(); // 按任意键退出
    }
}
  1. 运行main,查看dubbo-admin;
Dubbo学习(三)HelloWorld_第2张图片
  1. 模块结构
Dubbo学习(三)HelloWorld_第3张图片

消费者模块dubboconsumer

  1. 引入接口模块,在pom.xml配置

       com.maple.dubbodemo
       dubbo-api
       ${project.parent.version}

  1. 引入dubbo以及spring的包,在pom.xml配置

        com.alibaba
        dubbo
        2.5.6

 

            org.apache.zookeeper
            zookeeper
            3.4.10
            pom



            com.101tec
            zkclient
            0.10

  1. 消费者spring配置,路径\src\main\resources\META-INF\spring下创建配置文件dubbo-demo-consumer.xml;




    
    

    
    

    

    
    

  1. 创建消费者
package com.maple.dubbodemo.consumer;

import com.maple.dubbodemo.HelloService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by maple on 2017-10-26.
 */
public class Consumer {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});
        context.start();

        HelloService helloService = (HelloService) context.getBean("helloService"); // 获取远程服务代理
        String hello = helloService.sayHello("world"); // 执行远程方法

        System.out.println(hello); // 显示调用结果
    }
}
  1. 运行main
    消费者调用到服务
Dubbo学习(三)HelloWorld_第4张图片

生产者服务被调用

Dubbo学习(三)HelloWorld_第5张图片

你可能感兴趣的:(Dubbo学习(三)HelloWorld)