Dubbo的初步认识和基本使用

一、Dubbo的说明

    根据Dubbo官方网站的说明,了解到Dubbo是一个基于JAVA的阿里巴巴开放式RPC框架,所谓RPC是指Remote procedure call(远程过程调用),可以理解为是为了调用远程系统的服务。在服务器端,由服务器继承接口提供一个dubbo服务供客户端调用,客户端有一个存根,提供与服务器相同的方法。

二、Dubbo的结构组成

    这是duboo官方的结构图,从图中可以看出完整的dubbo服务由五部分组成,需要有容器Container启动一个提供者

Provider,然后需要有一个注册中心Registry,用于服务的注册和通知,服务注册后,消费者Consumer订阅Registry,接

收到通知,然后请求Provider的方法获取服务。Provider提供服务和Consumer消费服务统一由Monitor监控中心进行监控。

三、快速运用Dubbo

    可以根据官方网站提供的目录下载github上的dubbo-demo例子进行学习,这里也列出了Dubbo的使用入门。

    1、要使用Dubbo的先决条件有两个:(1)、JDK1.6以上;(2)、Maven3.0以上版本。

    2、需要引入的maven依赖:

        <dependency>
        
<groupId>com.alibaba</groupId>
        
<artifactId>dubbo</artifactId>
        
<version>${dubbo.version}</version>
        </dependency>

    3、定义服务的接口

        package com.alibaba.dubbo.demo;

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

  4、服务提供者创建

package com.alibaba.dubbo.demo.provider;
import com.alibaba.dubbo.demo.DemoService;

public class DemoServiceImpl implements DemoService {
    public String sayHello(String name) {
        return "Hello " + name;
    }
}

5、配置服务提供者

    官方建议使用spring框架进行配置,也可以使用API配置。

    使用建议的spring进行配置,配置dubbo前,需要在spring的beans头中引入dubbo的xsd配置:

  <?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://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="demo-provider"/>        
        <!-- 注册中心的地址,使用zookeeper,address是zookeeper的地址 -->
        <dubbo:registry protocol="zookeeper" address="192.168.1.8:2181"/>        
        <!-- 用dubbo协议在20880端口暴露服务 -->
        <dubbo:protocol name="dubbo" port="20880"/>        
        <!-- 声明需要暴露的服务接口 -->
        <dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/>        
        <!-- 具体实现服务的bean -->
        <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>
    </beans>

6、开启服务的提供者

   

import org.springframework.context.support.ClassPathXmlApplicationContext;

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();
         //press any key to exit
         System.in.read();
     }
}

        真实项目中启动项目即可,测试时用main方法启动快捷。

    7、配置服务的消费者

    

<?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://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="demo-consumer"/>
        <!-- 注册中心的地址,使用zookeeper,address是zookeeper的地址 -->
        dubbo:registry protocol="zookeeper" address="192.168.1.8:2181"/>
        <dubbo:reference id="demoService" interface="com.alibaba.dubbo.demo.DemoService"/>
    </beans>

    8、运行消费者调用服务

   

import com.alibaba.dubbo.demo.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Consumer {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            new String[]{"META-INF/spring/dubbo-demo-consumer.xml"});
        context.start();
        // obtain proxy object for remote invocation
        DemoService demoService = (DemoService) context.getBean("demoService");
        // execute remote invocation
        String hello = demoService.sayHello("world");
        // show the result
        ystem.out.println(hello);
}

    这样就完成了一次简单的dubbo服务调用。

猜你喜欢

转载自blog.csdn.net/xiaoying0531/article/details/80829285