|
| 1 | +# WxJava Quarkus/GraalVM Native Image Support |
| 2 | + |
| 3 | +## 概述 |
| 4 | + |
| 5 | +从 4.7.8.B 版本开始,WxJava 提供了对 Quarkus 和 GraalVM Native Image 的支持。这允许您将使用 WxJava 的应用程序编译为原生可执行文件,从而获得更快的启动速度和更低的内存占用。 |
| 6 | + |
| 7 | +## 问题背景 |
| 8 | + |
| 9 | +在之前的版本中,使用 Quarkus 构建 Native Image 时会遇到以下错误: |
| 10 | + |
| 11 | +``` |
| 12 | +Error: Unsupported features in 3 methods |
| 13 | +Detailed message: |
| 14 | +Error: Detected an instance of Random/SplittableRandom class in the image heap. |
| 15 | +Instances created during image generation have cached seed values and don't behave as expected. |
| 16 | +The culprit object has been instantiated by the 'org.apache.http.impl.auth.NTLMEngineImpl' class initializer |
| 17 | +``` |
| 18 | + |
| 19 | +## 解决方案 |
| 20 | + |
| 21 | +为了解决这个问题,WxJava 进行了以下改进: |
| 22 | + |
| 23 | +### 1. Random 实例的延迟初始化 |
| 24 | + |
| 25 | +所有 `java.util.Random` 实例都已改为延迟初始化,避免在类加载时创建: |
| 26 | + |
| 27 | +- `RandomUtils` - 使用双重检查锁定模式延迟初始化 |
| 28 | +- `SignUtils` - 使用双重检查锁定模式延迟初始化 |
| 29 | +- `WxCryptUtil` - 使用双重检查锁定模式延迟初始化 |
| 30 | + |
| 31 | +### 2. Native Image 配置 |
| 32 | + |
| 33 | +在 `weixin-java-common` 模块中添加了 GraalVM Native Image 配置文件: |
| 34 | + |
| 35 | +- `META-INF/native-image/com.github.binarywang/weixin-java-common/native-image.properties` |
| 36 | + - 配置 Apache HttpClient 相关类在运行时初始化,避免在构建时创建 SecureRandom 实例 |
| 37 | + |
| 38 | +- `META-INF/native-image/com.github.binarywang/weixin-java-common/reflect-config.json` |
| 39 | + - 配置反射访问的类和方法 |
| 40 | + |
| 41 | +## 使用方式 |
| 42 | + |
| 43 | +### Quarkus 项目配置 |
| 44 | + |
| 45 | +在您的 Quarkus 项目中使用 WxJava,只需正常引入依赖即可: |
| 46 | + |
| 47 | +```xml |
| 48 | +<dependency> |
| 49 | + <groupId>com.github.binarywang</groupId> |
| 50 | + <artifactId>weixin-java-miniapp</artifactId> <!-- 或其他模块 --> |
| 51 | + <version>4.7.8.B</version> |
| 52 | +</dependency> |
| 53 | +``` |
| 54 | + |
| 55 | +### 构建 Native Image |
| 56 | + |
| 57 | +使用 Quarkus 构建原生可执行文件: |
| 58 | + |
| 59 | +```bash |
| 60 | +./mvnw package -Pnative |
| 61 | +``` |
| 62 | + |
| 63 | +或使用容器构建: |
| 64 | + |
| 65 | +```bash |
| 66 | +./mvnw package -Pnative -Dquarkus.native.container-build=true |
| 67 | +``` |
| 68 | + |
| 69 | +### GraalVM Native Image |
| 70 | + |
| 71 | +如果直接使用 GraalVM Native Image 工具: |
| 72 | + |
| 73 | +```bash |
| 74 | +native-image --no-fallback \ |
| 75 | + -H:+ReportExceptionStackTraces \ |
| 76 | + -jar your-application.jar |
| 77 | +``` |
| 78 | + |
| 79 | +WxJava 的配置文件会自动被 Native Image 工具识别和应用。 |
| 80 | + |
| 81 | +## 测试验证 |
| 82 | + |
| 83 | +建议在构建 Native Image 后进行以下测试: |
| 84 | + |
| 85 | +1. 验证应用程序可以正常启动 |
| 86 | +2. 验证微信 API 调用功能正常 |
| 87 | +3. 验证随机字符串生成功能正常 |
| 88 | +4. 验证加密/解密功能正常 |
| 89 | + |
| 90 | +## 已知限制 |
| 91 | + |
| 92 | +- 本配置主要针对 Quarkus 3.x 和 GraalVM 22.x+ 版本进行测试 |
| 93 | +- 如果使用其他 Native Image 构建工具(如 Spring Native),可能需要额外配置 |
| 94 | +- 部分反射使用可能需要根据实际使用的 WxJava 功能进行调整 |
| 95 | + |
| 96 | +## 问题反馈 |
| 97 | + |
| 98 | +如果在使用 Quarkus/GraalVM Native Image 时遇到问题,请通过以下方式反馈: |
| 99 | + |
| 100 | +1. 在 [GitHub Issues](https://github.com/binarywang/WxJava/issues) 提交问题 |
| 101 | +2. 提供详细的错误信息和 Native Image 构建日志 |
| 102 | +3. 说明使用的 Quarkus 版本和 GraalVM 版本 |
| 103 | + |
| 104 | +## 参考资料 |
| 105 | + |
| 106 | +- [Quarkus 官方文档](https://quarkus.io/) |
| 107 | +- [GraalVM Native Image 文档](https://www.graalvm.org/latest/reference-manual/native-image/) |
| 108 | +- [Quarkus Tips for Writing Native Applications](https://quarkus.io/guides/writing-native-applications-tips) |
| 109 | + |
| 110 | +## 贡献 |
| 111 | + |
| 112 | +欢迎提交 PR 完善 Quarkus/GraalVM 支持!如果您发现了新的兼容性问题或有改进建议,请参考 [代码贡献指南](CONTRIBUTING.md)。 |
0 commit comments