dubbo使用指南

2016-08-11
使用指南
多个系统间存在相互调用,同步调用可用的方式有tcp层的dubbo、thrift,http层的rest、grpc,使用zookeeper进行服务的注册与发布管理。异步调用可使用消息队列。通过tcp层通信的dubbo、thrift比普通的rest通信耗时快2-3个数量级,所以系统间调用尽量使用dubbo或thrift通信。

可运行demo


编写服务端

pom添加如下maven依赖
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.5.3</version>
    <exclusions>
        <exclusion><!-- 这个排除低版本的spring-->
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.6</version>
    <exclusions>
        <exclusion><!-- 冲突排除-->
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
        <exclusion><!-- 冲突排除-->
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>com.github.sgroschupf</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.1</version>
</dependency>
编写service接口及实现类
package net.mamian.dubboDemoProvider.rpc;

public interface DemoService {

    String sayHello(String name);

}


package net.mamian.dubboDemoProvider.rpc;

import org.springframework.stereotype.Service;

@Service("demoService")
public class DemoServiceImpl implements DemoService {

    public String sayHello(String name) {
        return "Hello " + name;
    }
}
src/main/resources下增加配置文件dubbo-provide.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:jee="http://www.springframework.org/schema/jee"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"
       default-lazy-init="false" >

    <!-- 提供方应用名称信息,这个相当于起一个名字,我们dubbo管理页面比较清晰是哪个应用暴露出来的 -->
    <dubbo:application name="dubbo_service_provider"></dubbo:application>

    <!-- 具体的实现bean,方式1:通过注入的方式 -->
    <!--<dubbo:annotation package="net.mamian.dubboDemoProvider.rpc.*" />-->
    <!-- 具体的实现bean,方式2 -->
    <bean id="demoService" class="net.mamian.dubboDemoProvider.rpc.DemoServiceImpl" /> 

    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" subscribe="false" register=""></dubbo:registry>

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="net.mamian.dubboDemoProvider.rpc.DemoService" ref="demoService" />  

</beans>
application注入dubbo配置文件
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package net.mamian.dubboDemoProvider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

/**
 * 程序入口
 *
 * @author mamian
 * @mail mamianskyma@aliyun.com
 * @date 2016-07-05 11:31
 * @copyright ©2016 马面 All Rights Reserved
 */
@SpringBootApplication
@ImportResource("classpath:dubbo-provide.xml")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

编写客户端

pom添加如下maven依赖(与服务端一样)
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.5.3</version>
    <exclusions>
        <exclusion><!-- 这个排除低版本的spring-->
            <groupId>org.springframework</groupId>
            <artifactId>spring</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.6</version>
    <exclusions>
        <exclusion><!-- 冲突排除-->
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
        </exclusion>
        <exclusion><!-- 冲突排除-->
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>com.github.sgroschupf</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.1</version>
</dependency>
复制服务端的接口类放在相同Package下(接口可以比服务端的少)
package net.mamian.dubboDemoProvider.rpc;

public interface DemoService {

    String sayHello(String name);

}
src/main/resources下增加配置文件dubbo-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://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="dubbo_service_consumer" />

    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->
    <dubbo:reference id="demoService" interface="net.mamian.dubboDemoProvider.rpc.DemoService" />

</beans>
controller层直接使用DemoService
@RequestMapping(value="/dubbo", method = RequestMethod.GET)
@ResponseBody
public String test() throws IOException{
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            new String[]{"dubbo-consumer.xml"});
    context.start();

    DemoService demoService = (DemoService) context.getBean("demoService");
    return demoService.sayHello("马面");
}

测试rpc通信

  • zookeeper
    • sudo ./zookeeper/bin/zkServer.sh start
  • provider
    • mvn package
    • java -jar XX.jar
  • consumer

Kommentare: