Skip to content

Quick start for Alibaba's Dubbo RPC framework with Spring Boot

Notifications You must be signed in to change notification settings

dcnis/dubbo-spring-boot-example

Repository files navigation

dubbo-spring-boot-autoconfigure (Example)

There are lots of different implementations (most of them are very old or deprecated) of alibaba's dubbo RPC framework for spring-boot.

Here I created a quick start for a dubbo RPC application using dubbo-spring-boot-autoconfigure Version 3.0.x from org.apache.dubbo in the year 2022.

Project structure

We are having three modules. dubbo-demo-api, dubbo-demo-consumer and dubbo-demo-provider

- root
    - dubbo-demo-api
    - dubbo-demo-consumer
    - dubbo-demo-provider

Create API

Module: dubbo-demo-api

package de.dcnis.dubbo;

public interface DemoService {
    String  sayHello(String name);
}

Create Dubbo Service Provider

1. Add Maven Dependency

In module dubbo-demo-provider's pom.xml

<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-spring-boot-autoconfigure</artifactId>
    <version>3.0.2.1</version>
</dependency>

<!-- dependency to dubbo API from module "dubbo-demo-api" -->
<dependency>
    <groupId>de.dcnis</groupId>
    <artifactId>dubbo-demo-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

2. Create Dubbo Service Provider Implementation

package de.dcnis.dubbo.provider;

import de.dcnis.dubbo.api.DemoService;
import org.apache.dubbo.config.annotation.DubboService;

@DubboService(version = "1.0.0")
public class DubboServiceImpl implements DemoService {

    @Override
    public String sayHello(String name) {
        return "Hello, " + name + " (from Spring Boot)";
    }

}

3. Create Boostrap Class

package de.dcnis.dubbo.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Provider {

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

}

4. Configure application.properties

spring.application.name=dubbo-auto-configuration-provider-demo
server.port=8085

dubbo.application.name=dubbo-provider
dubbo.registry.address=N/A
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
dubbo.scan.base-packages=de.dcnis.dubbo.provider

Create Dubbo Service Consumer

1. Add Maven Dependency

In module dubbo-demo-consumer's pom.xml add the same dependencies as in dubbo-demo-provider.

<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-spring-boot-autoconfigure</artifactId>
    <version>3.0.2.1</version>
</dependency>

<!-- dependency to dubbo API from module "dubbo-demo-api" -->
<dependency>
    <groupId>de.dcnis</groupId>
    <artifactId>dubbo-demo-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

2. Create RestController

package de.dcnis.dubbo.consumer.api;

import de.dcnis.dubbo.api.DemoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConsumerController {

    @DubboReference(version = "1.0.0", url = "dubbo://127.0.0.1:20880")
    private DemoService dubboService;

    @GetMapping("/sayHello")
    public String sayHello(@RequestParam String name) {
        return dubboService.sayHello(name);
    }

}

3. Create Bootstrap Class

package de.dcnis.dubbo.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConsumerApplication {

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

}

4. Create application.properties

There is no need for specific dubbo properties for the consumer.

spring.application.name=dubbo-auto-configure-consumer-sample
server.port=8080

Run or application

  1. Start your provider
  2. Start your consumer
  3. Go to http://localhost:8080/sayHello?name=Dennis

About

Quick start for Alibaba's Dubbo RPC framework with Spring Boot

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages