一、前言
hello 大家好,我是如梦技术(春哥 L.cm),大家可能在很多开源项目里看到过我的身影。今天我带领大家实战一下spring-native。内容偏硬核,建议大家坐稳扶好(关注、收藏)。
对 Graalvm 和 Spring native 我们一直都有关注,并且已经发表过多篇公众号文章。对于 demo 级别的使用这里不做过多介绍,感兴趣的可以查看冷神(pig 冷冷)之前的文章 Spring Native 入门实战。
二、spring native
2.1 graalvm native image 配置生成
在spring native项目(mica-native-test)编译之后会生成下面的这些 graalvm native image 配置。
可以对动态代理、反射、资源文件和序列化进行配置。
2.2 spring native hints
spring native开放了很多的hints,用于对native image不支持的动态代理、反射、资源文件等进行配置。主要的hints如下图:
这些 hits 会将我们自定义的配置生成到proxy-config.json、reflect-config.json、resource-config.json、serialization-config.json中。
三、mica 的适配
本节文章拿mica的部分组件作为示例,来介绍spring native hints的使用。
3.1 mica-ip2region
mica-ip2region中涉及到一个 ip 地址信息的ip2region.db文件,所以我们需要自定义资源文件的配置。
首先给mica-ip2region添加spring-native依赖。
- <dependency>
- <groupId>org.springframework.experimental</groupId>
- <artifactId>spring-native</artifactId>
- <version>${spring-native.version}</version>
- <scope>provided</scope>
- </dependency>
然后在Ip2regionConfiguration代码中添加NativeHint注解配置ip2region.db资源文件。
- @Configuration(proxyBeanMethods = false)
- @EnableConfigurationProperties(Ip2regionProperties.class)
- @NativeHint(resources = @ResourceHint(patterns = "^ip2region/ip2region.db"))
- public class Ip2regionConfiguration {
- @Bean
- public Ip2regionSearcher ip2regionSearcher(ResourceLoader resourceLoader,
- Ip2regionProperties properties) {
- return new Ip2regionSearcherImpl(resourceLoader, properties);
- }
- }
再次编译spring native项目(mica-native-test)我们可以看见ip2region.db文件已经添加进resource-config.json。
最后运行项目:
测试mica-ip2region(完美):
3.2 mica-captcha
mica-captcha主要是几个字体文件需要添加下面的配置,具体过程同上这里不做过多描述。
- @NativeHint(resources = @ResourceHint(patterns = "^fonts/.*.ttf"))
注意:由于验证码涉及到字体和 awt 会涉及到下面2个问题。
- 以docker编译运行native image会遇到字体问题需要安装字体: