阅读 188

Dubbo 官方快速入门 Demo

概述

初次接触 Dubbo, 先跑个 Hello World, 看看 dubbo 微服务究竟是如何运作的。下边是快速启动的完整项目。

服务接口

首先创建一个 dubbo-api 服务,用来暴露服务接口

服务接口 maven

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>dubbo-demo</artifactId>
        <groupId>cn.idea360</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-api</artifactId>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
复制代码

服务接口定义

DemoService.java

public interface DemoService {

    String sayHello(String name);
}
复制代码

服务提供者

服务提供方 maven

创建 dubbo-provider 服务

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>dubbo-demo</artifactId>
        <groupId>cn.idea360</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-provider</artifactId>

    <!-- 打包方式 -->
    <!--<packaging>jar</packaging>-->

    <properties>
        <!-- 源文件编码格式 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- jdk编译源码版本 -->
        <jdk.compile.source.version>1.8</jdk.compile.source.version>
        <!-- jdk编译目标版本 -->
        <jdk.compile.target.version>1.8</jdk.compile.target.version>
        <!-- springframework版本 -->
        <springframe.version>5.1.5.RELEASE</springframe.version>
        <!-- junit版本 -->
        <junit.version>4.12</junit.version>
        <!-- log4j版本 -->
        <log4j.version>1.2.17</log4j.version>
        <!-- dubbo版本 -->
        <dubbo.version>2.5.3</dubbo.version>
        <!-- zookeeper版本 -->
        <zookeeper.version>3.4.6</zookeeper.version>
        <!-- zkclient版本 -->
        <zkclient.version>0.1</zkclient.version>
    </properties>

    <!-- 依赖 -->
    <dependencies>
        <dependency>
            <groupId>cn.idea360</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0</version>
        </dependency>
        <!-- spring context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springframe.version}</version>
        </dependency>
        <!-- junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
        </dependency>
        <!-- log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <!-- dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
        <!-- zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>${zookeeper.version}</version>
        </dependency>
        <!-- zkclient -->
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>${zkclient.version}</version>
        </dependency>
    </dependencies>

    <!-- 构建配置 -->
    <build>
        <finalName>Provider</finalName>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>utf-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>cn.idea360.provider.Provider</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
复制代码

在服务提供方实现接口

DemoServiceImpl.java

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

用 Spring 配置声明暴露服务

<?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-4.3.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="dubbo-provider"  />

    <!-- 使用zookeeper广播注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" client="zkclient" />

    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="cn.idea360.api.DemoService" ref="demoService" />

    <!-- 和本地bean一样实现服务 -->
    <bean id="demoService" class="cn.idea360.provider.DemoServiceImpl" />
</beans>
复制代码

加载 Spring 配置

Provider.java:

package cn.idea360.provider;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"dubbo/provider.xml"});
        context.start();

        System.out.println("服务已经启动...");
        System.in.read(); // 按任意键退出
    }
}
复制代码

服务消费者

服务消费者 maven

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>dubbo-demo</artifactId>
        <groupId>cn.idea360</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-consumer</artifactId>

    <!-- 打包方式 -->
    <packaging>jar</packaging>

    <properties>
        <!-- 源文件编码格式 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- jdk编译源码版本 -->
        <jdk.compile.source.version>1.8</jdk.compile.source.version>
        <!-- jdk编译目标版本 -->
        <jdk.compile.target.version>1.8</jdk.compile.target.version>
        <!-- springframework版本 -->
        <springframe.version>5.1.5.RELEASE</springframe.version>
        <!-- junit版本 -->
        <junit.version>4.12</junit.version>
        <!-- log4j版本 -->
        <log4j.version>1.2.17</log4j.version>
        <!-- dubbo版本 -->
        <dubbo.version>2.5.3</dubbo.version>
        <!-- zookeeper版本 -->
        <zookeeper.version>3.4.6</zookeeper.version>
        <!-- zkclient版本 -->
        <zkclient.version>0.1</zkclient.version>
    </properties>

    <!-- 依赖 -->
    <dependencies>
        <dependency>
            <groupId>cn.idea360</groupId>
            <artifactId>dubbo-api</artifactId>
            <version>1.0</version>
        </dependency>
        <!-- spring context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springframe.version}</version>
        </dependency>
        <!-- junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
        </dependency>
        <!-- log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <!-- dubbo -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>${dubbo.version}</version>
        </dependency>
        <!-- zookeeper -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>${zookeeper.version}</version>
        </dependency>
        <!-- zkclient -->
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>${zkclient.version}</version>
        </dependency>
    </dependencies>

    <!-- 构建配置 -->
    <build>
        <finalName>Consumer</finalName>
    </build>

</project>
复制代码

通过 Spring 配置引用远程服务

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-4.3.xsd
       http://code.alibabatech.com/schema/dubbo
       http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    <dubbo:application name="dubbo-consumer"  />

    <!-- 使用zookeeper广播注册中心暴露发现服务地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" client="zkclient" />

    <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    <dubbo:reference id="demoService" interface="cn.idea360.api.DemoService" />
</beans>
复制代码

加载 Spring 配置,并调用远程服务

Consumer.java

package cn.idea360.consumer;

import cn.idea360.api.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Consumer {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"dubbo/consumer.xml"});
        context.start();
        DemoService demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理
        String hello = demoService.sayHello("world"); // 执行远程方法
        System.out.println( hello ); // 显示调用结果
    }
}
复制代码
  • 该接口需单独打包,在服务提供方和消费方共享

  • 对服务消费方隐藏实现

  • 也可以使用 IoC 注入

最后

本篇纯属入门笔记。参照 官方说明 的入门 demo。大家绕道即可。