Dubbo入门之HelloWorld篇

一、前言
Dubbo是阿里巴巴推出的分布式服务器,通过Dubbo可以为Web应用提供WebService的功能。把单服务器应用实现分布式,万事开头难,在Dubbo的使用前,必须要部署Dubbo管理控制台,搭建zookeeper消息中心(可选),而Dubbo管理控制台需要使用dubbo-admin-2.8.4.war就可以完美解决搭建Dubbo管理控制台时与linux操作系统的jdk1.8解析异常的问题。推荐使用https://my.oschina.net/wangt10/blog/522799来使用搭建Dubbo管理控制台。
二、Provider提供者的创建过程(在Maven项目上使用Spring来使用dubbo服务)
1、CustomerService接口

package com.wjw.service.customer;
public interface CustomerService {
  public String getName();
}

2、CustomerServiceImpl实现

package com.wjw.service.customer;


public class CustomerServiceImpl implements CustomerService{
  @Override
  public String getName() {
    return "HelloWorld";
  }

3、DubboProvider测试类

package com.wjw.service.customer;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DubboProvider {
  public static void main(String[] args) {
      ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( "classpath:applicationProvider.xml");  
        context.start();  
        System.out.println("Press any key to exit.");  
        try {
      System.in.read();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }  
  }
}

4、applicationProvider.xml


<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">
    
  <bean id="demoService" class="com.wjw.service.customer.CustomerServiceImpl" />  
    
  <dubbo:application name="xixi_provider"  />   
     
    
  <dubbo:registry address="zookeeper://118.89.22.169:2181" />    
    
  <dubbo:protocol name="dubbo" port="20880" />  
    
  <dubbo:service interface="com.wjw.service.customer.CustomerService" ref="demoService" />  
beans>

三、Customer消费者的创建过程
1、CustomerService 消费服务接口

package com.wjw.service.customer;
public interface CustomerService {
  public String getName();
}

2、 DubboCustomer测试类

package com.wjw.service.customer;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DubboCustomer {
  public static void main(String[] args) {
      ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationProvider.xml");  

      CustomerService demoService = (CustomerService) context.getBean("demoService"); // get  
      System.out.println(demoService.getName());
        try {
      System.in.read();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }  
  }
}

3、applicationProvider测试类


<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="consumer-of-helloworld-app" />    
      
  <dubbo:registry  protocol="zookeeper" address="zookeeper://118.89.22.169:2181" />    
      
  <dubbo:reference id="demoService" interface="com.wjw.service.customer.CustomerService" />  
beans>

四、入门成功的标识
1、先运行DubboProvider测试类,再运行DubboCustomer测试类,在Dubbo控制平台上的成功运行的效果图
这里写图片描述
2、下面是Myeclispe下的控制台的效果图
这里写图片描述

源码下载

你可能感兴趣的:(dubbo教程)