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";
3. option java_multiple_files = true;
4. option java_package = "org.apache.dubbo.demo";
5. option java_outer_classname = "DemoServiceProto";
6. option objc_class_prefix = "DEMOSRV";
8. package demoservice;
10. // The demo service definition.
11. service DemoService {
12. rpc SayHello (HelloRequest) returns (HelloReply) {}
13. }
15. // The request message containing the user's name.
16. message HelloRequest {
17. string name = 1;
18. }
20. // The response message containing the greetings
21. message HelloReply {
22. string message = 1;
23. }
2. 引入 dubbo-maven-plugin 依赖
2. <plugin>
3. <groupId>org.apache.dubbo</groupId>
4. <artifactId>dubbo-maven-plugin</artifactId>
5. <version>${dubbo.version}</version>
6. <executions>
7. <execution>
8. <goals>
9. <goal>compile</goal>
10. </goals>
11. </execution>
12. </executions>
13. </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. */
18. package org.apache.dubbo.demo;
20. import java.util.concurrent.CompletableFuture;
21. import java.util.concurrent.atomic.AtomicBoolean;
23. public final class DemoServiceDubbo {
24. private static final AtomicBoolean registered = new AtomicBoolean();
26. private static Class<?> init() {
27. Class<?> clazz = null;
28. try {
29. clazz = Class.forName(DemoServiceDubbo.class.getName());
30. if (registered.compareAndSet(false, true)) {
31. org.apache.dubbo.common.serialize.protobuf.support.ProtobufUtils.marshaller(
32. org.apache.dubbo.demo.HelloReply.getDefaultInstance());
33. org.apache.dubbo.common.serialize.protobuf.support.ProtobufUtils.marshaller(
34. org.apache.dubbo.demo.HelloRequest.getDefaultInstance());
35. }
36. } catch (ClassNotFoundException e) {
37. // ignore
38. }
39. return clazz;
40. }
42. private DemoServiceDubbo() {
43. }
45. public static final String SERVICE_NAME = "org.apache.dubbo.demo.DemoService";
47. /**
48. * Code generated for Dubbo
49. */
50. public interface IDemoService extends org.apache.dubbo.rpc.model.DubboStub {
52. static Class<?> clazz = init();
54. org.apache.dubbo.demo.HelloReply sayHello(org.apache.dubbo.demo.HelloRequest request);
56. CompletableFuture<org.apache.dubbo.demo.HelloReply> sayHelloAsync(org.apache.dubbo.demo.HelloRequest request);
59. }
61. }
配置列表
Dubbo Maven Plugin For Protobuf 支持以下配置:
| 配置参数 | 必填 | 解释 | 默认值 | 示例 |
|---|---|---|---|---|
| dubboVersion | true | Dubbo 版本号,用于查找 dubbo-compiler 组件 | ${dubbo.version} | 3.3.0 |
| dubboGenerateType | true | 生成的文件类型 | tri | tri 或 tri-reactor |
| protocExecutable | false | 可执行的 protoc 路径,如不配置将自动从 Maven 仓库下载 | protoc | |
| protocArtifact | false | protoc 在 Maven 仓库中定位,用于下载 protoc 文件,如不配置将自动识别操作系统类型并下载 | com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier} | |
| protoSourceDir | true | proto 文件目录 | ${basedir}/src/main/proto | ./proto |
| outputDir | true | 生成文件的目标目录 | ${project.build.directory}/generated-sources/protobuf/java | ${basedir}/src/main/java |
| protocPluginDirectory | false | 临时存储 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 支持以下配置:
| 配置参数 | 必填 | 解释 | 默认值 | 示例 |
|---|---|---|---|---|
| mainClass | true | 启动类名 | com.example.nativedemo.NativeDemoApplication |
最后修改 September 13, 2024: Refactor website structure (#2860) (1a4b998f54b)
