Dubbo AOT — How to Implement Static Dubbo Applications using GraalVM Native Image

Detailed explanation of Dubbo AOT technology and how to implement static Dubbo applications using GraalVM Native Image.

In the Dubbo 3.3.0 version, we officially released the Dubbo AOT static solution. This document will introduce how to connect applications to GraalVM Native Image through Dubbo AOT technology, the process of compiling applications into native binary packages, and the components currently supported.

Dubbo GraalVM adaptation document may be out of date

Due to the rapid development of Dubbo AOT technology, this document may not always be up to date. Please refer to the following content for the latest information and usage:

For more information about GraalVm, you can read the document at https://www.graalvm.org/docs/getting-started/container-images/.

Usage Scenarios

  • Native Image Compilation: Pre-compile applications into native images to reduce startup time and memory usage.

  • Language Interoperability: GraalVM allows writing code in multiple languages to interoperate within the same application.

  • Optimization: GraalVM provides optimizations for applications written in Java, JavaScript, and other languages, enhancing the performance of Dubbo applications.

  • Polyglot Debugging: GraalVM can debug code written in multiple languages within the same session, which is very useful for troubleshooting issues in complex Dubbo applications.

  • Java Runtime: It can run on GraalVM, providing a faster, more efficient Java runtime environment.

  • Developing Microservices: It can be combined with GraalVM to create high-performance, low-resource-utilization microservices.

How to Use

Before compiling our Dubbo project, ensure that we are based on a GraalVM environment.

Step 1: Install GraalVM

  1. Select the corresponding GraalVM version for your system from the official GraalVM website: https://www.graalvm.org/downloads/
  2. Install native-image according to the official documentation: https://www.graalvm.org/latest/reference-manual/native-image/#install-native-image

Step 2: Configure Profiles

This includes maven-compiler-plugin, spring-boot-maven-plugin, native-maven-plugin, and dubbo-maven-plugin. Modify the mainClass in dubbo-maven-plugin to the full path of the required startup class. (The API usage does not require adding the spring-boot-maven-plugin dependency.)

  1. <profiles>
  2. <profile>
  3. <id>native</id>
  4. <build>
  5. <plugins>
  6. <plugin>
  7. <artifactId>maven-compiler-plugin</artifactId>
  8. <configuration>
  9. <release>17</release>
  10. <fork>true</fork>
  11. <verbose>true</verbose>
  12. </configuration>
  13. </plugin>
  14. <plugin>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-maven-plugin</artifactId>
  17. <executions>
  18. <execution>
  19. <id>process-aot</id>
  20. <goals>
  21. <goal>process-aot</goal>
  22. </goals>
  23. </execution>
  24. </executions>
  25. </plugin>
  26. <plugin>
  27. <groupId>org.graalvm.buildtools</groupId>
  28. <artifactId>native-maven-plugin</artifactId>
  29. <version>0.9.25</version>
  30. <configuration>
  31. <classesDirectory>${project.build.outputDirectory}</classesDirectory>
  32. <metadataRepository>
  33. <enabled>true</enabled>
  34. </metadataRepository>
  35. <requiredVersion>22.3</requiredVersion>
  36. </configuration>
  37. <executions>
  38. <execution>
  39. <id>add-reachability-metadata</id>
  40. <goals>
  41. <goal>add-reachability-metadata</goal>
  42. </goals>
  43. </execution>
  44. </executions>
  45. </plugin>
  46. <plugin>
  47. <groupId>org.apache.dubbo</groupId>
  48. <artifactId>dubbo-maven-plugin</artifactId>
  49. <version>${dubbo.version}</version>
  50. <configuration>
  51. <mainClass>com.example.nativedemo.NativeDemoApplication</mainClass>
  52. </configuration>
  53. <executions>
  54. <execution>
  55. <phase>process-sources</phase>
  56. <goals>
  57. <goal>dubbo-process-aot</goal>
  58. </goals>
  59. </execution>
  60. </executions>
  61. </plugin>
  62. </plugins>
  63. </build>
  64. </profile>
  65. </profiles>

The API usage does not require adding the dubbo-config-spring6 dependency.

  1. <dependency>
  2. <groupId>org.apache.dubbo</groupId>
  3. <artifactId>dubbo-config-spring6</artifactId>
  4. <version>${dubbo.version}</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.apache.dubbo</groupId>
  8. <artifactId>dubbo-native</artifactId>
  9. <version>${dubbo.version}</version>
  10. </dependency>

Step 4: Configure the Application

Example configuration:

  1. dubbo:
  2. application:
  3. name: ${spring.application.name}
  4. logger: slf4j
  5. protocol:
  6. name: dubbo
  7. port: -1
  8. serialization: fastjson2
  9. registry:
  10. id: zk-registry
  11. address: zookeeper://127.0.0.1:2181
  12. config-center:
  13. address: zookeeper://127.0.0.1:2181
  14. metadata-report:
  15. address: zookeeper://127.0.0.1:2181
  16. provider:
  17. serialization: fastjson2
  18. consumer:
  19. serialization: fastjson2

Step 5: Compile

Run the following compile command in the project’s root path:

  • Execute directly for API usage
  1. mvn clean install -P native -Dmaven.test.skip=true
  • For annotation and XML methods (Springboot3 integration)
  1. mvn clean install -P native native:compile -Dmaven.test.skip=true

Step 6: Execute the Binary File

The binary file is located in the target/ directory, usually named after the project, for example, target/native-demo.

Supported Components and Corresponding Versions

Logging Components

Component NameRequired PluginPlugin VersionNotes
Apache Commons Loggingnative-maven-plugin0.9.24 and above
JDK Loggernative-maven-plugin0.9.24 and above
slf4jspring-boot-maven-plugin3.x.x (latest version)
Log4jnative-maven-plugin0.10.0 and above
Log4j2

Serialization Components

Component NameRequired PluginPlugin VersionNotes
FastJson2dubbo-maven-plugin3.3.0 and above
JDKnative-maven-plugin0.9.24 and above
Hessian-LiteNot friendly with JDK 17, unsupported for now

Registry Center Components

Component NameRequired PluginPlugin VersionNotes
Zookeeperdubbo-maven-plugin3.3.0Only supports Zookeeper Curator5

Metadata Center Components

Component NameRequired PluginPlugin VersionNotes
Zookeeperdubbo-maven-plugin3.3.0Only supports Zookeeper Curator5

Configuration Center Components

Component NameRequired PluginPlugin VersionNotes
Zookeeperdubbo-maven-plugin3.3.0Only supports Zookeeper Curator5

Observability Components

Component NameRequired PluginPlugin VersionNotes
Micrometer

Feedback

Was this page helpful?

Yes No

Last modified September 30, 2024: Update & Translate Overview Docs (#3040) (d37ebceaea7)