Quick start

The most common way to use Dubbo is to run it in Spring framework. The following content will guide you to develop a Dubbo application with Spring framework's XML configuration.

最普遍的方式去使用dubbo就在spring框架中运行。以下内容会指引你去开发一个使用spring框架xml配置的dubbo程序。

If you don't want to rely on Spring, you can try using API configuration.

如果你不想依赖spring, 你可以尝试使用api配置。

First let's create a root directory called dubbo-demo:

首先让我们创建一个叫buddo-demo的主目录

mkdir dubbo-demo
cd dubbo-demo

Next, we are going to create 3 sub-directories under root directory:

下一步,我们要在主目录下创建三个子目录:

  • dubbo-demo-api: the common service api
  • 通用服务API
  • dubbo-demo-provider: the demo provider codes
  • 提供者的代码用例
  • dubbo-demo-consumer: the demo consumer codes
  • 消费者的代码用例

Service provider

服务提供者

Defining service interfaces

定义服务接口

DemoService.java 1

package org.apache.dubbo.demo;

public interface DemoService {
    String sayHello(String name);

}

The project structure should look like this:

项目结构应该看起来是这样子的:

.
├── dubbo-demo-api
│   ├── pom.xml
│   └── src
│       └── main
│           └── java
│               └── org
│                   └── apache
│                       └── dubbo
│                           └── demo
│                               └── DemoService.java

Implement interface in service provider

在服务提供者里实现接口:

DemoServiceImpl.java 2

package org.apache.dubbo.demo.provider;
import org.apache.dubbo.demo.DemoService;

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

Exposing service with Spring configuration

使用spring配置暴漏服务

provider.xml:

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- provider's application name, used for tracing dependency relationship -->
    <dubbo:application name="demo-provider"/>
    <!-- use multicast registry center to export service -->
    <dubbo:registry address="multicast://224.5.6.7:1234"/>
    <!-- use dubbo protocol to export service on port 20880 -->
    <dubbo:protocol name="dubbo" port="20880"/>
    <!-- service implementation, as same as regular local bean -->
    <bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl"/>
    <!-- declare the service interface to be exported -->
    <dubbo:service interface="org.apache.dubbo.demo.DemoService" ref="demoService"/>
</beans>

The demo uses multicast as the registry since it is simple and does not require to extra installation. If you prefer a registry like zookeeper, please check out examples here.

案例使用多播作为注册中心,由于简单并且不需要额外的安装。如果你倾向于一个类似zookeeper这种注册中心,请查阅例子。

Configure the logging system

配置日志系统

Dubbo use log4j as logging system by default, it also support slf4j, Apache Commons Logging, and JUL logging.

Dubbo默认使用log4k作为日志系统,它也支持slf4j, Apache通用Logging和JUL日志 Logging。

Following is a sample configuration:

一下是配置的样例:

log4j.properties

###set log levels###
log4j.rootLogger=info, stdout
###output to the console###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n

Bootstrap the service provider

Provider.java

package org.apache.dubbo.demo.provider;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Provider {

    public static void main(String[] args) throws Exception {
        System.setProperty("java.net.preferIPv4Stack", "true");
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/dubbo-demo-provider.xml"});
        context.start();
        System.out.println("Provider started.");
        System.in.read(); // press any key to exit
    }
}

Finally, the project structure should look like this:

最终,项目结构看起来应该是这样子:

├── dubbo-demo-provider
│   ├── pom.xml
│   └── src
│       └── main
│           ├── java
│           │   └── org
│           │       └── apache
│           │           └── dubbo
│           │               └── demo
│           │                   └── provider
│           │                       ├── DemoServiceImpl.java
│           │                       └── Provider.java
│           └── resources
│               ├── META-INF
│               │   └── spring
│               │       └── dubbo-demo-provider.xml
│               └── log4j.properties

Service consumer

Complete installation steps, see:Consumer demo installation

完成安装步骤: 参考 Consumer demo installation

Using the Spring configuration to reference a remote service

使用spring配置去引用一个远程服务。

consumer.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <!-- consumer's application name, used for tracing dependency relationship (not a matching criterion),
    don't set it same as provider -->
    <dubbo:application name="demo-consumer"/>
    <!-- use multicast registry center to discover service -->
    <dubbo:registry address="multicast://224.5.6.7:1234"/>
    <!-- generate proxy for the remote service, then demoService can be used in the same way as the
    local regular interface -->
    <dubbo:reference id="demoService" check="false" interface="org.apache.dubbo.demo.DemoService"/>
</beans>

Bootstrap the consumer

Consumer.java 3

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.apache.dubbo.demo.DemoService;

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();
        // Obtaining a remote service proxy
        DemoService demoService = (DemoService)context.getBean("demoService");
        // Executing remote methods
        String hello = demoService.sayHello("world");
        // Display the call result
        System.out.println(hello);
    }
}

Config the logging system

配置日志系统

This is the same as how to config it on provider side.

跟提供端配置方式一样。

Finally, the project structure should be look like this:

最总,项目结构看起来应该是这个样子:

├── dubbo-demo-consumer
│   ├── pom.xml
│   └── src
│       └── main
│           ├── java
│           │   └── org
│           │       └── apache
│           │           └── dubbo
│           │               └── demo
│           │                   └── consumer
│           │                       └── Consumer.java
│           └── resources
│               ├── META-INF
│               │   └── spring
│               │       └── dubbo-demo-consumer.xml
│               └── log4j.properties

Start the demo

启动demo

Start service provider

启动服务提供者

Run the org.apache.dubbo.demo.provider.Provider class to start the provider.

运行org.apache.dubbo.demo.provider.Provider类去启动提供者

Start service consumer

启动服务消费者

Run the org.apache.dubbo.demo.provider.Consumer class to start the consumer, and you should be able to see the following result:

运行 org.apache.dubbo.demo.provider.Consumer class去启动消费者,并且你应该可以看到以下结果:

Hello world

Complete example

完整例子

You can find the complete example code in the Github repository.

你可以在Github仓库找到完整的例子。


  1. The interface needs to be packaged separately, shared by the service provider and the consumer  接口需要分开打包,服务提供者和消费者共享接口。

  2. Hidden realization of service consumer  对服务消费端隐藏实现。

  3. IoC injection can also be used  可以使用Ioc注入

posted @ 2020-11-04 20:52  ZhiJi&Ethan  阅读(243)  评论(0编辑  收藏  举报