Dubbo Maven Plugin 的配置方式

Dubbo Maven Plugin 的配置方式

本文主要讲解 Dubbo Maven Plugin 的配置方式。

当前 Dubbo Maven Plugin 支持以下功能:

  • Dubbo Maven Plugin For Protobuf:生成 Dubbo 服务接口的 Stub 代码
  • Dubbo Maven Plugin For Native Image:基于 AOT 机制,生成 Native Image 必要的 Metadata 信息

Dubbo Maven Plugin For Protobuf

如何使用 Dubbo Maven Plugin 生成 Protobuf Stub 代码

1. 编写 .proto 文件

greeter.proto

  1. syntax = "proto3";
  2. option java_multiple_files = true;
  3. option java_package = "org.apache.dubbo.demo";
  4. option java_outer_classname = "DemoServiceProto";
  5. option objc_class_prefix = "DEMOSRV";
  6. package demoservice;
  7. // The demo service definition.
  8. service DemoService {
  9. rpc SayHello (HelloRequest) returns (HelloReply) {}
  10. }
  11. // The request message containing the user's name.
  12. message HelloRequest {
  13. string name = 1;
  14. }
  15. // The response message containing the greetings
  16. message HelloReply {
  17. string message = 1;
  18. }

2. 引入 dubbo-maven-plugin 依赖

  1. <plugin>
  2. <groupId>org.apache.dubbo</groupId>
  3. <artifactId>dubbo-maven-plugin</artifactId>
  4. <version>${dubbo.version}</version>
  5. <executions>
  6. <execution>
  7. <goals>
  8. <goal>compile</goal>
  9. </goals>
  10. </execution>
  11. </executions>
  12. </plugin>

3. 生成文件示例

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.dubbo.demo;
  18. import java.util.concurrent.CompletableFuture;
  19. import java.util.concurrent.atomic.AtomicBoolean;
  20. public final class DemoServiceDubbo {
  21. private static final AtomicBoolean registered = new AtomicBoolean();
  22. private static Class<?> init() {
  23. Class<?> clazz = null;
  24. try {
  25. clazz = Class.forName(DemoServiceDubbo.class.getName());
  26. if (registered.compareAndSet(false, true)) {
  27. org.apache.dubbo.common.serialize.protobuf.support.ProtobufUtils.marshaller(
  28. org.apache.dubbo.demo.HelloReply.getDefaultInstance());
  29. org.apache.dubbo.common.serialize.protobuf.support.ProtobufUtils.marshaller(
  30. org.apache.dubbo.demo.HelloRequest.getDefaultInstance());
  31. }
  32. } catch (ClassNotFoundException e) {
  33. // ignore
  34. }
  35. return clazz;
  36. }
  37. private DemoServiceDubbo() {
  38. }
  39. public static final String SERVICE_NAME = "org.apache.dubbo.demo.DemoService";
  40. /**
  41. * Code generated for Dubbo
  42. */
  43. public interface IDemoService extends org.apache.dubbo.rpc.model.DubboStub {
  44. static Class<?> clazz = init();
  45. org.apache.dubbo.demo.HelloReply sayHello(org.apache.dubbo.demo.HelloRequest request);
  46. CompletableFuture<org.apache.dubbo.demo.HelloReply> sayHelloAsync(org.apache.dubbo.demo.HelloRequest request);
  47. }
  48. }

配置列表

Dubbo Maven Plugin For Protobuf 支持以下配置:

配置参数必填解释默认值示例
dubboVersiontrueDubbo 版本号,用于查找 dubbo-compiler 组件${dubbo.version}3.3.0
dubboGenerateTypetrue生成的文件类型tritri 或 tri-reactor
protocExecutablefalse可执行的 protoc 路径,如不配置将自动从 Maven 仓库下载protoc
protocArtifactfalseprotoc 在 Maven 仓库中定位,用于下载 protoc 文件,如不配置将自动识别操作系统类型并下载com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}
protoSourceDirtrueproto 文件目录${basedir}/src/main/proto./proto
outputDirtrue生成文件的目标目录${project.build.directory}/generated-sources/protobuf/java${basedir}/src/main/java
protocPluginDirectoryfalse临时存储 protoc 的目录${project.build.directory}/protoc-plugins./target/protoc-plugins

Dubbo Maven Plugin For Native Image

示例

  1. <plugin>
  2. <groupId>org.apache.dubbo</groupId>
  3. <artifactId>dubbo-maven-plugin</artifactId>
  4. <version>${dubbo.version}</version>
  5. <configuration>
  6. <mainClass>com.example.nativedemo.NativeDemoApplication</mainClass>
  7. </configuration>
  8. <executions>
  9. <execution>
  10. <phase>process-sources</phase>
  11. <goals>
  12. <goal>dubbo-process-aot</goal>
  13. </goals>
  14. </execution>
  15. </executions>
  16. </plugin>

配置列表

Dubbo Maven Plugin For Native Image 支持以下配置:

配置参数必填解释默认值示例
mainClasstrue启动类名com.example.nativedemo.NativeDemoApplication

最后修改 September 13, 2024: Refactor website structure (#2860) (1a4b998f54b)