dubbo入门案例

      Apache Dubbo (incubating) |ˈdʌbəʊ| 是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。详细介绍请访问官网http://dubbo.apache.org/zh-cn/

1. Zookeeper的安装与启动,请参考我的上一篇文章https://blog.csdn.net/DWL0208/article/details/81807881

2.创建maven项目,其中dubbo-api 公共接口,dubbo-consumer消费者模块,dubbo-provider和dubbo-consumer都需要在pom中引入dubbo-api 。

项目结构如下:

dubbo入门案例_第1张图片

3.在dubbo-demo下的pom文件中引入公共的依赖。



    4.0.0

    com.dwl
    dubbo-demo
    pom
    1.0-SNAPSHOT
    
        dubbo-api
        dubbo-provider
        dubbo-consumer
    
    
        2.5.3
        4.3.6.RELEASE
        1.8
        1.2.17
        UTF-8
    

    
        
            com.alibaba
            dubbo
            2.5.3
            
                
                    org.springframework
                    spring
                
            
        
        
            com.github.sgroschupf
            zkclient
            0.1
        
        
        
            org.springframework
            spring-core
            ${spring.version}
        
        
            org.springframework
            spring-beans
            ${spring.version}
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
            log4j
            log4j
            ${log4j.version}
        
    


4.公共接口模块,供提供者和消费者使用。

package com.dwl;

public interface DubboService {

    String getMessage(String msg);

}

5.服务提供者模块

dubbo-provider模块下pom中引入依赖



    
        dubbo-demo
        com.dwl
        1.0-SNAPSHOT
    
    4.0.0

    dubbo-provider

    
         //y依赖dubbo-api模块
        
            com.dwl
            dubbo-api
            1.0-SNAPSHOT
        
    

实现api中的接口

package com.dwl;

/**
 * @program: dubbo-demo
 * @description: 实现服务接口
 * @author: daiwenlong
 * @create: 2018-08-18 19:31
 **/
public class DubboServiceImpl implements DubboService {
    public String getMessage(String msg) {
        return "receive your call:"+msg;
    }
}


 使用provider.xml配置zookeeper并暴露服务



    
    
    
    
    
    
    
    
    
    

启动提供者服务

package com.dwl;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

/**
 * @program: dubbo-demo
 * @description: 启动服务
 * @author: daiwenlong
 * @create: 2018-08-18 19:48
 **/
public class Provider {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:provider.xml");
        System.out.println(context.getDisplayName() + ": here");
        context.start();
        System.out.println("provider started");
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


6.消费者模块

通过consumer.xml 配置需要远程调用的服务



    
    
    
    
    
    

启动Consumer,调用远程服务

package com.dwl;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

/**
 * @program: dubbo-demo
 * @description: 消费者
 * @author: daiwenlong
 * @create: 2018-08-18 20:27
 **/
public class Consumer {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("consumer.xml");
        context.start();
        System.out.println("consumer start");
        DubboService service = context.getBean(DubboService.class);
        System.out.println("consumer started");
        System.out.println(service.getMessage("dubbo"));
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


查看调用结果

dubbo入门案例_第2张图片

 7.下载dubbo-admin(https://github.com/daiwenlong/dubbo-demo/)并启动服务可查看提供者消费者信息,将下载好的dubbo-admin.war包使用Tomcat启动,访问http://localhost:8080/dubbo-admin/进入管理页面,用户名和密码均为root。

dubbo入门案例_第3张图片

dubbo入门案例_第4张图片

 详细代码:https://github.com/daiwenlong/dubbo-demo

你可能感兴趣的:(分布式)