hello 各位好!,我是似梦技术性(春哥 L.cm),大伙儿很有可能在许多开源软件里见到过我的影子。今天我领着大伙儿实战演练一下spring-native。內容偏顶势,提议大伙儿独站扶好(关心、个人收藏)。
对 Graalvm 和 Spring native 大家一直都是有关心,而且早已发布过数篇微信文章。针对 demo 等级的应用这儿不做太多详细介绍,有兴趣的能够查询冷神(pig 冷冷)以前的文章内容 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的一部分部件做为实例,来详细介绍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 会牵涉到下边两个难题。
- yum install fontconfig -y && fc-cache --force
大量详细:https://github.com/oracle/graal/issues/817
实际详细:https://github.com/oracle/graal/issues/2842
3.3 mica-caffeine
因为caffeine中应用了许多的unsafe,因此 加上了 mica-caffeine 依靠以后,mica-native-test能运行取得成功都瞎折腾了我很长期。各种各样出错,但是都是有提醒,我们可以依照提醒加上Hints,如下图:
image.png
在加上下边这么多Hints以后总算能够运行成功了!!!
- @NativeHint(types = {
- @TypeHint(types = CaffeineAutoCacheManager.class, access = AccessBits.ALL),
- @TypeHint(types = CaffeineCacheManager.class, access = AccessBits.ALL),
- @TypeHint(typeNames = "com.github.benmanes.caffeine.cache.UnsafeAccess",
- fields = @FieldHint(name = "UNSAFE", allowUnsafeAccess = true),
- access = AccessBits.PUBLIC_METHODS
- ),
- @TypeHint(types = Thread.class, access = AccessBits.DECLARED_FIELDS),
- @TypeHint(typeNames = "com.github.benmanes.caffeine.cache.PS",
- fields = {
- @FieldHint(name = "key", allowUnsafeAccess = true),
- @FieldHint(name = "value", allowUnsafeAccess = true)
- },
- access = AccessBits.DECLARED_CONSTRUCTORS
- ),
- @TypeHint(typeNames = "com.github.benmanes.caffeine.cache.PSA",
- fields = @FieldHint(name = "accessTime", allowUnsafeAccess = true),
- access = AccessBits.DECLARED_CONSTRUCTORS
- ),
- @TypeHint(typeNames = "com.github.benmanes.caffeine.cache.PSW",
- fields = @FieldHint(name = "writeTime", allowUnsafeAccess = true),
- access = AccessBits.DECLARED_CONSTRUCTORS
- ),
- @TypeHint(typeNames = "com.github.benmanes.caffeine.cache.StripedBuffer",
- fields = {@FieldHint(name = "tableBusy", allowUnsafeAccess = true)},
- access = AccessBits.ALL
- ),
- @TypeHint(typeNames = "com.github.benmanes.caffeine.cache.PSWMS", access = AccessBits.DECLARED_CONSTRUCTORS),
- @TypeHint(typeNames = "com.github.benmanes.caffeine.cache.SSA", access = AccessBits.ALL),
- @TypeHint(typeNames = "com.github.benmanes.caffeine.cache.SSLA", access = AccessBits.DECLARED_CONSTRUCTORS),
- @TypeHint(typeNames = "com.github.benmanes.caffeine.cache.SSLMSW", access = AccessBits.DECLARED_CONSTRUCTORS),
- @TypeHint(typeNames = "com.github.benmanes.caffeine.cache.SSMSW", access = AccessBits.DECLARED_CONSTRUCTORS),
- @TypeHint(typeNames = "com.github.benmanes.caffeine.cache.BoundedBuffer", access = AccessBits.ALL),
- @TypeHint(typeNames = "com.github.benmanes.caffeine.cache.BoundedBuffer$RingBuffer", access = AccessBits.ALL),
- @TypeHint(typeNames = "com.github.benmanes.caffeine.cache.BLCHeader$DrainStatusRef",
- fields = @FieldHint(name = "drainStatus", allowUnsafeAccess = true),
- access = AccessBits.ALL
- )
- })
普大喜奔,就行了???是真的吗???caffeinecache 载入缓存文件又逐渐报出现异常了!!!
到此从此不愿瞎折腾了,礼拜天的早上全在瞎折腾这东西了。
spring-native现阶段或是处在卵化环节,如果是未应用第三方部件简易的新项目大伙儿能够试一下,稍大中型提议大伙儿或是再等等。大家也会不断关心并輸出类似文章。
【责编:武晓燕 TEL:(010)68476606】