Skip to content

apache/dubbo-samples

Dubbo Samples

Samples for Apache Dubbo.

Build Status Build Status license

This repository contains a number of projects to illustrate various usages of Dubbo from basic to advanced, pls. check README in each individual sub projects. It is also helpful to cross reference toDubbo User Manualto understand the features demoed in this project.

What's more,dubbo-gosamples are moved todubbo-go-samples.

Build and Run Samples

To compile all samples, run the following command in the top directory of this project, or step into the sub directories to compile one single sample:

It is highly not recommend to build the entire project from the root directory, as building the entire samples can take a long time. Each module in Samples is designed independently so you can first go to the demo directory you care about, then execute the build and run the demo.

For example,

$cd1-basic/dubbo-samples-spring-boot
$ mvn clean package

You may need to read each individual README under the sub directories if you have to understand how to build and run.

Integration Test

This project is also used for integration tests for dubbo. If you are just learning how to use Dubbo you don't have to care about this part.

How to build and run a integration test

Dubbo integration test base on docker container, and relies on an image used to run the provider application and test cases.

Integration test leveragesdockerto setup test environment, more accurately, to start dubbo provider instance, and any other supporting systems like registry center if necessary, in docker.

Please installdockeranddocker-composefirst, then build the test imagedubbo/sample-test.

cddubbo-samples
./test/build-test-image.sh

Use a debian mirror through envDEBIAN_MIRRORif apt download files slowly, the following example uses aliyun mirror serverhttp://mirrors.aliyun /ubuntu/:

cddubbo-samples
DEBIAN_MIRROR=http://mirrors.aliyun./test/build-test-image.sh

Then we use therun-tests.shscript to run the test cases.

  • Run single test case

    cddubbo-samples
    ./test/run-tests.sh<project.basedir>

    For example, run thedubbo-samples-annotationtest case:

    ./test/run-tests.sh 2-advanced/dubbo-samples-annotation
    
  • Run all test cases

    cddubbo-samples
    ./test/run-tests.sh

If docker container fails to startup successfully in any case, you can check log files in directory${project.basedir}/target/logsto understand what happens.

Pls. note integration tests rely on a Docker environment, make sure the docker environment is available before running them.

How to add more integration test

If you are interested in contributing more integration test for dubbo, pls. read further to understand how to enable integration test for one particular sample from the scratch.

Please follow the steps below:

  1. Add a file namedcase-configuration.ymlto test project.

    This file is used to configure the test modules and environment, including dubbo provider / test services, dependent third-party services.

  2. Add a file namedcase-versions.confto test project.

    This file is used to configure the supported component version rules to support multi-version testing.

Details ofcase-configuration.yml:

Take the casedubbo-samples-annotationas an example:

services:
dubbo-samples-annotation:
type:app
basedir:.
mainClass:org.apache.dubbo.samples.annotation.AnnotationProviderBootstrap

dubbo-samples-annotation-test:
type:test
basedir:.
tests:
-"**/*IT.class"
systemProps:
-zookeeper.address=dubbo-samples-annotation
-zookeeper.port=2181
-dubbo.address=dubbo-samples-annotation
-dubbo.port=20880
waitPortsBeforeRun:
-dubbo-samples-annotation:2181
-dubbo-samples-annotation:20880
depends_on:
-dubbo-samples-annotation

The project contains a dubbo providerAnnotationProviderBootstrapand an embedded zookeeper server, as well as a test classAnnotationServicesIT.

Therefore, we have to define two services, one service runsAnnotationProviderBootstrap, and the other service runs test classes.

The servicetypeof running dubbo provider isapp,and the servicetypeof running test istest.

The project directory is the same as the case configuration directory, sobasediris..

Use hostname to access between containers, the defaulthostnameof the container is the same as serviceName.

So throughdubbo-samples-annotation:2181,the embedded zookeeper server can be accessed from the test container.

There are many test cases similar to this example, only need to modify themainClassand hostname. Extract the changed as variables, and the unchanged content as templates. When using the template, you only need to modify the variable value, which makes the case configuration easier.

The above example can use a templateapp-builtin-zookeeper.yml,usefromto reference it and override the variable value inprops:

from:app-builtin-zookeeper.yml

props:
project_name:dubbo-samples-annotation
main_class:org.apache.dubbo.samples.annotation.AnnotationProviderBootstrap
zookeeper_port:2181
dubbo_port:20880

Another template isapp-external-zookeeper.yml,which supports an external zookeeper service. you can find all the templates in the directorytest/dubbo-scenario-builder/src/main/resources/configs.

Details ofcase-versions.conf:

Version rules for spring app:

# Spring app
dubbo.version=2.7.*, 3.*
spring.version=4.*, 5.*

Version rules for spring-boot 1.x app:

# SpringBoot app
dubbo.version=2.7.*, 3.*
spring-boot.version=1.*

Version rules for spring-boot 2.x app:

# SpringBoot app
dubbo.version=2.7.*, 3.*
spring-boot.version=2.*

For more details, please refer to the following case configurations:

That's it, then feel free to add more integration test for the Dubbo project, have fun.