Skip to content

feat: Add Java Dubbo code generator for Apache Dubbo microservices#21918

Closed
redoom wants to merge 3 commits intoOpenAPITools:masterfrom
redoom:dubbo-feature
Closed

feat: Add Java Dubbo code generator for Apache Dubbo microservices#21918
redoom wants to merge 3 commits intoOpenAPITools:masterfrom
redoom:dubbo-feature

Conversation

@redoom
Copy link
Copy Markdown
Contributor

@redoom redoom commented Sep 7, 2025

Add Dubbo Code Generator

Feature Overview

A new Java Dubbo code generator has been added to OpenAPI Generator, enabling automatic generation of Apache Dubbo-based microservice project code from OpenAPI specifications.

Key Features

Complete Project Generation

  • Service Interfaces: Generate standard Dubbo service interfaces
  • Service Implementations: Generate service provider implementation classes
  • Consumer Controllers: Generate Spring Boot controllers for HTTP access
  • Project Configuration: Automatically generate Maven pom.xml and application.yml configurations
  • Documentation: Generate detailed README documentation with usage guides

Multi-Version Support

Supports multiple versions of Apache Dubbo with automatic dependency adaptation:

Dubbo Version Dependency Strategy
3.2 and earlier Uses aggregated POM dubbo-dependencies-zookeeper
3.3+ Uses modular dependencies dubbo-registry-* + dubbo-remoting-*

Registry Center Support

Supports mainstream registry centers with automatic client version selection:

  • Zookeeper: zookeeper://127.0.0.1:2181
  • Nacos: nacos://127.0.0.1:8848
    • Dubbo 3.2: nacos-client 2.2.4
    • Dubbo 3.3+: nacos-client 2.5.0

Rich Configuration Options

Configuration Default Value Description
title "OpenAPI Dubbo" API service title
basePackage "org.openapitools" Base package name
dubboVersion "3.2.18" Dubbo version
javaVersion "17" Java version
interfaceOnly false Generate interfaces only, no implementations
serviceInterface true Generate service interfaces
serviceImplementation true Generate service implementations
useGenericResponse false Use generic response wrapper
useTags true Use OpenAPI tags to create class names
registry-address "zookeeper://127.0.0.1:2181" Registry center address

Generated Project Structure

project-name/
├── src/main/java/org/openapitools/
│   ├── api/
│   │   ├── interfaces/          # Dubbo service interfaces
│   │   │   └── UserService.java
│   │   ├── provider/            # Dubbo service implementations
│   │   │   └── UserServiceImpl.java
│   │   └── consumer/            # Spring Boot controllers
│   │       └── UserController.java
│   ├── model/                   # Data models
│   │   └── User.java
│   └── Application.java         # Main application class
├── src/main/resources/
│   └── application.yml          # Application configuration
├── src/test/java/
│   └── ApplicationTests.java    # Test classes
├── pom.xml                      # Maven configuration
└── README.md                    # Project documentation

Usage Examples

Basic Usage

java -jar openapi-generator-cli.jar generate \
  -i petstore.yaml \
  -g dubbo \
  -o ./generated-dubbo-project

Using Nacos Registry Center

java -jar openapi-generator-cli.jar generate \
  -i petstore.yaml \
  -g dubbo \
  -o ./generated-dubbo-project \
  --additional-properties=registry-address=nacos://127.0.0.1:8848 \
  --additional-properties=dubboVersion=3.3.0 \
  --additional-properties=basePackage=com.example

Generate Interfaces Only

java -jar openapi-generator-cli.jar generate \
  -i petstore.yaml \
  -g dubbo \
  -o ./generated-dubbo-project \
  --additional-properties=interfaceOnly=true \
  --additional-properties=serviceImplementation=false

Technical Implementation

Core Class Structure

  • DubboCodegen: Main generator class, extends AbstractJavaCodegen
  • Template Files: Located in java-dubbo/ directory
    • api.mustache: Service interface template
    • apiDubbo.mustache: Service implementation template
    • apiController.mustache: Controller template
    • model.mustache: Data model template
    • pom.mustache: Maven configuration template
    • application.mustache: Application configuration template

Intelligent Dependency Management

The generator automatically selects appropriate dependencies based on Dubbo version and registry address:

private boolean isDubbo33OrHigher(String version) {
    // Version parsing logic
    return major > 3 || (major == 3 && minor >= 3);
}

Package Structure Optimization

  • interfaces: Pure interface package, can be independently published for consumer reference
  • provider: Service provider implementations
  • consumer: HTTP gateway controllers

Test Coverage

Generated projects include:

  • Spring Boot testing infrastructure
  • Application startup tests
  • Complete Maven build configuration

Documentation Support

Auto-generated README.md includes:

  • Quick start guide
  • Registry center configuration instructions
  • Project structure description
  • Detailed configuration options
  • Troubleshooting guide
  • Deployment guide

Compatibility

  • Java: Supports Java 8+ to Java 21
  • Spring Boot: Version 2.7.x
  • Maven: Version 3.6+
  • Dubbo: Supports all 3.0+ versions

- Add comprehensive Dubbo microservice code generator
- Support Zookeeper and Nacos registries with auto-dependency selection
- Implement version-aware dependency management (Dubbo 3.2 vs 3.3+)
- Generate service interfaces, implementations, and Spring Boot REST controllers
- Include complete Spring Boot application structure with configuration
- Add detailed documentation and usage examples
- Support async operations and generic response wrappers
- Provide flexible configuration options for packages, versions, and features
Adds a new sample generator configuration for dubbo.

The existing GitHub workflow for Java samples is updated to build and test this new sample automatically.
- Set registry address to N/A to avoid ZooKeeper dependency
- Change protocol from 'triple' to 'tri' to resolve extension loading error
@wing328
Copy link
Copy Markdown
Member

wing328 commented Sep 8, 2025

please follow step 3 to update the samples and doc

@wing328
Copy link
Copy Markdown
Member

wing328 commented Sep 15, 2025

i've filed #21968 based on this PR with

  • some minor fixes
  • renaming
  • add docs

tests passed. please have a look

@wing328
Copy link
Copy Markdown
Member

wing328 commented Sep 15, 2025

closed via #21968

@wing328 wing328 closed this Sep 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants