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.
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
Module: dubbo-demo-api
package de.dcnis.dubbo;
public interface DemoService {
String sayHello(String name);
}
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>
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)";
}
}
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);
}
}
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
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>
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);
}
}
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);
}
}
There is no need for specific dubbo properties for the consumer.
spring.application.name=dubbo-auto-configure-consumer-sample
server.port=8080
- Start your provider
- Start your consumer
- Go to http://localhost:8080/sayHello?name=Dennis