Spring Boot

Dubbo provides full support for the Spring framework, and we recommend using the officially provided rich dubbo-spring-boot-starter for efficient development of Dubbo microservice applications.

Dubbo provides full support for the Spring framework, and we recommend using the official dubbo-spring-boot-starter for efficient development of Dubbo microservice applications.

Creating a Project

The fastest way to create a Dubbo application is to use the official project scaffold tool - start.dubbo.apache.org online service. It helps developers create Spring Boot structured applications and automatically manage dependencies like dubbo-spring-boot-starter along with necessary configurations.

Additionally, Jetbrain also provides an Apache Dubbo project plugin for quickly creating Dubbo Spring Boot projects, which is on par with start.dubbo.apache.org. For specific installation and usage.

dubbo-spring-boot-starter

In the Quick Start, we have detailed typical Dubbo Spring Boot project source code and its project structure. Developers who are unfamiliar can refer there.

dubbo-spring-boot-starter can introduce Dubbo core dependencies into the project and automatically scan Dubbo-related configurations and annotations.

Maven Dependencies

To use the Dubbo Spring Boot Starter, first include the following Maven dependency:

  1. <dependencyManagement>
  2. <dependencies>
  3. <dependency>
  4. <groupId>org.apache.dubbo</groupId>
  5. <artifactId>dubbo-bom</artifactId>
  6. <version>3.3.0</version>
  7. <type>pom</type>
  8. <scope>import</scope>
  9. </dependency>
  10. </dependencies>
  11. </dependencyManagement>

Then, add the necessary starter dependencies in the respective module’s pom:

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.apache.dubbo</groupId>
  4. <artifactId>dubbo-spring-boot-starter</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.dubbo</groupId>
  8. <artifactId>dubbo-zookeeper-spring-boot-starter</artifactId>
  9. </dependency>
  10. </dependencies>

dubbo-spring-boot-starter and dubbo-zookeeper-spring-boot-starter are official starters that provide integration adaptations for Spring Boot, and their version numbers are fully consistent with the Dubbo main framework version.

application.yml Configuration File

Here is an example configuration file:

  1. dubbo:
  2. application:
  3. name: dubbo-springboot-demo-provider
  4. logger: slf4j
  5. protocol:
  6. name: tri
  7. port: -1
  8. registry:
  9. address: zookeeper://127.0.0.1:2181

Other components besides service and reference can be set in the application.yml file, and for specifics, please refer to the configuration list.

Service and reference components can also be associated with global components in the application by using id, as in the following configuration. To extend the annotation configuration of service or reference, you need to add the dubbo.properties configuration file or use other non-annotation methods like Java Config; please see the section on Extending Annotation Configuration for details.

  1. dubbo:
  2. application:
  3. name: dubbo-springboot-demo-provider
  4. protocol:
  5. name: tri
  6. port: -1
  7. registry:
  8. id: zk-registry
  9. address: zookeeper://127.0.0.1:2181

Associate the service with the specific registry defined above (linked by id) using annotations:

  1. @DubboService(registry="zk-registry")
  2. public class DemoServiceImpl implements DemoService {}

Using Java Config for association works similarly:

  1. @Configuration
  2. public class ProviderConfiguration {
  3. @Bean
  4. public ServiceConfig demoService() {
  5. ServiceConfig service = new ServiceConfig();
  6. service.setRegistry("zk-registry");
  7. return service;
  8. }
  9. }

Dubbo Annotations

  • application.properties or application.yml configuration files.
  • @DubboService, @DubboReference, and EnableDubbo annotations. Among them, @DubboService and @DubboReference are used to mark Dubbo services, while EnableDubbo starts Dubbo-related configurations and specifies the package path for Spring Boot scanning.

@DubboService Annotation

The @Service annotation has been deprecated since version 3.0, replaced by @DubboService to differentiate it from the Spring @Service annotation.

Once the Dubbo service interface is defined, implement the service interface logic and mark it with the @DubboService annotation to expose the Dubbo service:

  1. @DubboService
  2. public class DemoServiceImpl implements DemoService {}

If you need to set service parameters, @DubboService also provides a way to set common parameters. For more complex parameter settings, consider using other configuration methods:

  1. @DubboService(version = "1.0.0", group = "dev", timeout = 5000)
  2. public class DemoServiceImpl implements DemoService {}

@DubboReference Annotation

The @Reference annotation has been deprecated since version 3.0, replaced by @DubboReference to differentiate it from the Spring @Reference annotation.

  1. @Component
  2. public class DemoClient {
  3. @DubboReference
  4. private DemoService demoService;
  5. }

The @DubboReference annotation will automatically inject a Dubbo service proxy instance, allowing remote service calls via demoService.

@EnableDubbo Annotation

The @EnableDubbo annotation must be configured; otherwise, services defined by Dubbo annotations will not load. @EnableDubbo can be defined on the main class:

  1. @SpringBootApplication
  2. @EnableDubbo
  3. public class ProviderApplication {
  4. public static void main(String[] args) throws Exception {
  5. SpringApplication.run(ProviderApplication.class, args);
  6. }
  7. }

Spring Boot annotations will only scan the package where the main class is located by default. If services are defined in other packages, you need to add the configuration EnableDubbo(scanBasePackages = {"org.apache.dubbo.springboot.demo.provider"}).

Extending Annotation Configuration

While it is possible to adjust configuration parameters through @DubboService and DubboReference (as shown in the code snippet below), overall, annotations are designed for ease of use and provide only 80% of commonly used configuration items for scenarios. In such cases, if there are more complex parameter setting needs, you can use either Java Config or dubbo.properties.

  1. @DubboService(version = "1.0.0", group = "dev", timeout = 5000)
  2. @DubboReference(version = "1.0.0", group = "dev", timeout = 5000)

Using Java Config in Place of Annotations

Note that Java Config is an alternative to DubboService or DubboReference. For services with complex configuration needs, this approach is recommended.

  1. @Configuration
  2. public class ProviderConfiguration {
  3. @Bean
  4. public ServiceBean demoService() {
  5. ServiceBean service = new ServiceBean();
  6. service.setInterface(DemoService.class);
  7. service.setRef(new DemoServiceImpl());
  8. service.setGroup("dev");
  9. service.setVersion("1.0.0");
  10. Map<String, String> parameters = new HashMap<>();
  11. service.setParameters(parameters);
  12. return service;
  13. }
  14. }

Supplementing Configuration Through dubbo.properties

For scenarios using DubboService or DubboReference, additional configuration can be provided by adding a dubbo.properties file under the project’s resources directory. Specific format has more detailed explanations here.

  1. dubbo.service.org.apache.dubbo.springboot.demo.DemoService.timeout=5000
  2. dubbo.service.org.apache.dubbo.springboot.demo.DemoService.parameters=[{myKey:myValue},{anotherKey:anotherValue}]
  3. dubbo.reference.org.apache.dubbo.springboot.demo.DemoService.timeout=6000

The properties format currently has limited structural strength, such as excessive redundancy in key fields, and support for YAML format will be considered in the future.

More Microservice Development Patterns

Relationship Between Dubbo and Spring Cloud

Dubbo and Spring Boot have a complementary relationship, where Dubbo provides complete microservice development and governance capabilities atop the Spring Boot framework. More detailed explanations on this can be found in another article: Dubbo, Spring Cloud, and Istio.

Feedback

Was this page helpful?

Yes No

Last modified October 11, 2024: fixed protocol.name wrong spelling (#3046) (35f5e799156)