第2章 - Dubbo初试

Dubbo初试

在本章中,你将运行自己的第一个Dubbo程序。为此,你首先需要检查自己的计算机是否安装了Java、ZooKeeper。本文使用Java 1.8。

因为Dubbo依赖于ZooKeeper作为注册中心,我们需要先保证启动了ZooKeeper服务。

Hello程序

Dubbo是一个分布式服务框架,提供了优秀的RPC远程服务调用方案。本文使用Dubbo的2.7.9版本。

Dubbo有两个重要的角色,分别是Provider(暴露服务的服务提供方)与Consumer(调用远程服务的服务消费方)。本文我们实现一个简单的Dubbo程序,实现简单的Provider和Consumer,Consumer调用Provider的方法实现输出Hello World的功能。

本文我们使用maven来构建项目,我们可以有如下的项目结构:

这里简单解释下每个包的作用:
api包为服务生产者提供的api接口。provider包为服务生产者的具体发布和实现。consumer包为服务消费方,调用远程服务。

我们先在pom.xml里写好程序运行必备的依赖,pom.xml如下:

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

    <properties>
        <dubbo.version>2.7.9</dubbo.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-bom</artifactId>
                <version>${dubbo.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-dependencies-zookeeper</artifactId>
                <version>${dubbo.version}</version>
                <type>pom</type>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <type>pom</type>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

为了实现Hello程序的功能,Consumer可能需要调用Provider来获取一个字符串,然后将其输出。

我们可以简单地定义好api接口,定义好Consumer需要调用的方法名、方法参数、方法返回类型,HelloService只需要简单几行代码,如下:

public interface HelloService {
    String hello(String name);
}

定义好api接口后,要先实现Provider,不然Consumer必然就调用失败。我们针对HelloService可以有个实现类HelloServiceImpl,实现hello方法,如下:

import org.msl12.dubbo.api.HelloService;

public class HelloServiceImpl implements HelloService {
    @Override
    public String hello(String name) {
        return "Hello " + name;
    }
}

api实现好啦,接下来我们就发布它吧,下面是Provider的Application应用的简单实现:

import java.util.concurrent.CountDownLatch;

import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;

import org.msl12.dubbo.api.HelloService;

public class Application {
    private static final String ZOOKEEPER_HOST = "127.0.0.1"; // ZooKeeper的host
    private static final String ZOOKEEPER_PORT = "2181"; // Zookeeper的端口号

    public static CountDownLatch countDownLatch = new CountDownLatch(1); // CountDownLatch是一个同步辅助类,在其他线程中执行的操作完成之前,它允许一个线程一直等待

    public static void main(String[] args) throws Exception {
        ServiceConfig<HelloServiceImpl> service = new ServiceConfig<>(); // 服务配置类
        service.setApplication(new ApplicationConfig("provider")); // 设置当前应用配置,给该应用取名为"provider"
        service.setRegistry(new RegistryConfig("zookeeper://" + ZOOKEEPER_HOST + ":" + ZOOKEEPER_PORT)); // 设置注册中心配置,这里使用了ZooKeeper作为注册中心
        service.setInterface(HelloService.class); // 设置服务接口名
        service.setRef(new HelloServiceImpl()); // 设置服务对象实现的引用
        service.export(); // 暴露服务

        countDownLatch.await(); // 使应用保持等待状态,使得可以保持运行状态响应Consumer的调用
    }
}

把Provider的Application运行起来后。Consumer也可以编写自己的应用程序来调用Provider的hello方法啦,下面是Consumer的Application应用的简单实现:

import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;

import org.msl12.dubbo.api.HelloService;

public class Application {
    private static final String ZOOKEEPER_HOST = "127.0.0.1"; // ZooKeeper的host
    private static final String ZOOKEEPER_PORT = "2181"; // Zookeeper的端口号

    public static void main(String[] args) {
        ReferenceConfig<HelloService> reference = new ReferenceConfig<>(); // 服务消费方引用配置类
        reference.setApplication(new ApplicationConfig("consumer")); // 设置当前应用配置,给该应用取名为"consumer"
        reference.setRegistry(new RegistryConfig("zookeeper://" + ZOOKEEPER_HOST + ":" + ZOOKEEPER_PORT)); // 设置注册中心配置,要与Provider使用同一个注册中心
        reference.setInterface(HelloService.class); // 设置引用的接口名
        HelloService service = reference.get(); // 获取引用的对象
        System.out.println(service.hello("Dubbo")); // 调用服务
    }
}

我们把Consumer的Application也运行起来,会看到执行结果:

Hello Dubbo

这代表着Consumer成功地调用了Provider提供的hello方法了!

至此,在本章中,你大致了解了怎么构建一个简单的Dubbo程序。

下一章,我们将学习Dubbo的体系结构。

posted @ 2021-03-16 22:30  msl12  阅读(34)  评论(0编辑  收藏  举报