SoFunction
Updated on 2025-04-14

Steps to package Dubbo service into Jar package

background

Dubbo is a popular Java RPC framework that provides a high-performance, transparent RPC remote service call solution. When developing Dubbo-based services, we usually need to package the service code into publishable JAR packages for deployment and running in different environments. This article will introduce in detail how to package Dubbo services into JAR packages and provide corresponding configuration and steps.

Preparation

Before starting packaging, make sure your project has successfully used Dubbo and has server and client configured. If you haven't done this yet, please refer to the official Dubbo documentation for configuration.

Server package

1. Configure the Maven plug-in

To package Dubbo services into JAR packages, we usually use the Maven plugin. First, you need to​​Add the following plug-in configuration to the file:

<build>
    <plugins>
        <plugin>
            <groupId></groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.1</version><!-- Please adjust according to the actual version number -->
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>Your main class full path</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

Among them,​mainClass​The attribute needs to be replaced with the full path of the main class you serve, for example:​​​。

2. Add Main method

If your service class does not contain a​main​​ method, you need to add one so that it can be called to start the service when running the JAR package. Here is a simple example:

public class DubboServerApplication {
    public static void main(String[] args) {
        // Initialize Spring application context        (, args);
    }
}

3. Package server JAR package

In a Maven project, you can package the server JAR package by running the following command:

mvn clean package

After packaging, you can find a file named dubbo-server-1.0.​ in the project's target directory (assuming your project is named dubbo-server, version 1.0.0).

Client Packaging

The packaging process of the client is similar to that of the server, but usually does not need to add the main method because the client does not need to run directly. You just need to make sure the client's dependencies are correct and then package them with Maven's ​​jar​​ plugin.

Configuration File

During the packaging process, you may need to add some configuration files (such as​​​) Included in the JAR package. You can use it in​​Adding resource filters to the file to achieve this:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

This will tell Maven to the package process​src/main/resources​​ All resource files in the directory are filtered and included in the JAR package.

Things to note

  • Before packaging, make sure your project has no compile errors and that all dependencies are configured correctly.
  • If your service uses third-party libraries, make sure that the version and dependencies of these libraries are correct and are included correctly during the packaging process.
  • If you use Spring Boot, make sure your Dubbo service configuration is consistent with Spring Boot's conventions so that the packaging process goes smoothly.

Summarize

Through the above steps, you can package the Dubbo service into a JAR package for deployment and running in different environments. Remember that the packaging process is only a small part of the deployment of Dubbo service. To ensure that your service runs stably in a production environment requires load testing, monitoring configuration and other tasks. In real-life applications, you may need to package the Dubbo service into a publishable Jar package for deployment and use in other environments. Here is a simple example showing how to package a Spring Boot-based Dubbo service into a Jar package.

First, you need to make sure your service is properly configured with Dubbo and can run locally. If you don't have a ready-made Dubbo service, you can create a simple service, such as implementing an interface and exposing it to a Dubbo service.

Here is a simple Dubbo service interface and implementation example:

// Service interfacepublic interface GreetingService {
    String sayHello(String name);
}
 
// Service implementationpublic class GreetingServiceImpl implements GreetingService {
    @Override
    public String sayHello(String name) {
        return "Hello, " + name;
    }
}

Next, you need to register this service in the Spring Boot application and configure Dubbo. Here is a simple Spring Boot application configuration class:

@Configuration
public class AppConfig {
 
    @Bean
    public GreetingService greetingService() {
        return new GreetingServiceImpl();
    }
 
    // Dubbo configuration    @Bean
    public ReferenceBean&lt;GreetingService&gt; greetingServiceReference() {
        ReferenceBean&lt;GreetingService&gt; reference = new ReferenceBean&lt;&gt;();
        ();
        return reference;
    }
 
    // Dubbo Registration Center Configuration    @Bean
    public RegistryFactoryBean registryFactoryBean() {
        RegistryFactoryBean registry = new RegistryFactoryBean();
        ("zookeeper://127.0.0.1:2181");
        return registry;
    }
}

Now that you have a Dubbo service that can run, you can use Maven or Gradle to package it. Here is an example packaged using Maven:

<build>
    <plugins>
        <plugin>
            <groupId></groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId></groupId>
                        <artifactId>lombok</artifactId>
                    </exclude>
                </excludes>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

This Maven configuration tells the Spring Boot Maven plugin to exclude tool classes such as lombok when packaging and execute the repackage target, which will make your application an executable Jar package.

Finally, you can use Maven's​package​Commands to package your application:

mvn package

After the package is complete, you will be in the project​target​Find one in the directory​.jar​​A file with an extension, this file is the Dubbo service Jar package that can be deployed.

Please note that this example is a very simplified version, and the actual Dubbo service may require more configuration and dependencies depending on your application scenario and requirements. In addition, Dubbo services usually need to be used with a registry such as ZooKeeper to ensure that the service can be discovered and invoked. In Dubbo, packing a service into a Jar package usually involves the following steps:

  1. Create a POM file: First, you need to create a Maven project and add the necessary dependencies. Here is an example of a POM file:
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;project xmlns="/POM/4.0.0"
         xmlns:xsi="http:///2001/XMLSchema-instance"
         xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0."&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
 
    &lt;groupId&gt;&lt;/groupId&gt;
    &lt;artifactId&gt;dubbo-service&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
 
    &lt;properties&gt;
        &lt;&gt;1.8&lt;/&gt;
        &lt;&gt;2.7.8&lt;/&gt;
    &lt;/properties&gt;
 
    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;&lt;/groupId&gt;
            &lt;artifactId&gt;dubbo&lt;/artifactId&gt;
            &lt;version&gt;${}&lt;/version&gt;
        &lt;/dependency&gt;
 
        &lt;!-- If your service needs to be provided to other languages,The following dependencies can be added --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;&lt;/groupId&gt;
            &lt;artifactId&gt;dubbo-rpc-dubbo&lt;/artifactId&gt;
            &lt;version&gt;${}&lt;/version&gt;
        &lt;/dependency&gt;
 
        &lt;!-- If your service needs to be usedzookeeperAs a registration center,Need to add the following dependencies --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;&lt;/groupId&gt;
            &lt;artifactId&gt;zookeeper&lt;/artifactId&gt;
            &lt;version&gt;3.4.14&lt;/version&gt;
        &lt;/dependency&gt;
 
        &lt;!-- If your service needs to be usedredisAs a cache,Need to add the following dependencies --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;&lt;/groupId&gt;
            &lt;artifactId&gt;dubbo-rpc-redis&lt;/artifactId&gt;
            &lt;version&gt;${}&lt;/version&gt;
        &lt;/dependency&gt;
 
        &lt;!-- If your service needs to be usedMySQLAs a data source,Need to add the following dependencies --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;mysql&lt;/groupId&gt;
            &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
            &lt;version&gt;8.0.19&lt;/version&gt;
        &lt;/dependency&gt;
 
        &lt;!-- If your service needs to be usedSpringAs a container,Need to add the following dependencies --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;&lt;/groupId&gt;
            &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
            &lt;version&gt;5.2.&lt;/version&gt;
        &lt;/dependency&gt;
 
        &lt;!-- If your service needs to be usedLog4jAs a logging tool,Need to add the following dependencies --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;log4j&lt;/groupId&gt;
            &lt;artifactId&gt;log4j&lt;/artifactId&gt;
            &lt;version&gt;1.2.17&lt;/version&gt;
        &lt;/dependency&gt;
 
        &lt;!-- If your service needs to be usedJUnitPerform unit tests,Need to add the following dependencies --&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;junit&lt;/groupId&gt;
            &lt;artifactId&gt;junit&lt;/artifactId&gt;
            &lt;version&gt;4.12&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
 
    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;&lt;/groupId&gt;
                &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.8.1&lt;/version&gt;
                &lt;configuration&gt;
                    &lt;source&gt;${}&lt;/source&gt;
                    &lt;target&gt;${}&lt;/target&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;&lt;/groupId&gt;
                &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.1.2&lt;/version&gt;
                &lt;configuration&gt;
                    &lt;archive&gt;
                        &lt;manifest&gt;

The above is the detailed operation steps for packaging Dubbo services into Jar packages. For more information about packaging Dubbo into Jar packages, please follow my other related articles!