Spring Native 可以正式使用了么?

架构 2023-07-05 17:29:38
43阅读

一、序言

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依靠。

 
  1. <dependency> 
  2.     <groupId>org.springframework.experimental</groupId> 
  3.     <artifactId>spring-native</artifactId> 
  4.     <version>${spring-native.version}</version> 
  5.     <scope>provided</scope> 
  6. </dependency> 

随后在Ip2regionConfiguration编码中加上NativeHint注释配备ip2region.db資源文档。

 
  1. @Configuration(proxyBeanMethods = false
  2. @EnableConfigurationProperties(Ip2regionProperties.class) 
  3. @NativeHint(resources = @ResourceHint(patterns = "^ip2region/ip2region.db")) 
  4. public class Ip2regionConfiguration { 
  5.    @Bean 
  6.    public Ip2regionSearcher ip2regionSearcher(ResourceLoader resourceLoader, 
  7.                                     Ip2regionProperties properties) { 
  8.       return new Ip2regionSearcherImpl(resourceLoader, properties); 
  9.    } 

再度编译程序spring native新项目(mica-native-test)我们可以看到ip2region.db文件早已加上进resource-config.json。

最终运作新项目:

检测mica-ip2region(极致):

3.2 mica-captcha

mica-captcha主要是好多个字体包必须加上下边的配备,实际全过程跟上面一样这儿不做太多叙述。

 
  1. @NativeHint(resources = @ResourceHint(patterns = "^fonts/.*.ttf")) 

留意:因为短信验证码牵涉到字体样式和 awt 会牵涉到下边两个难题。

  • 以docker编译程序运作native image会碰到字体样式难题必须添加字体:
 
  1. yum install fontconfig -y && fc-cache --force 

大量详细:https://github.com/oracle/graal/issues/817

  • java.lang.UnsatisfiedLinkError: no awt in java.library.path出现异常现阶段graalvm 21.1.0Mac 上或是有什么问题。

实际详细:https://github.com/oracle/graal/issues/2842

3.3 mica-caffeine

因为caffeine中应用了许多的unsafe,因此 加上了 mica-caffeine 依靠以后,mica-native-test能运行取得成功都瞎折腾了我很长期。各种各样出错,但是都是有提醒,我们可以依照提醒加上Hints,如下图:

image.png

在加上下边这么多Hints以后总算能够运行成功了!!!

 
  1. @NativeHint(types = { 
  2.    @TypeHint(types = CaffeineAutoCacheManager.class, access = AccessBits.ALL), 
  3.    @TypeHint(types = CaffeineCacheManager.class, access = AccessBits.ALL), 
  4.    @TypeHint(typeNames = "com.github.benmanes.caffeine.cache.UnsafeAccess"
  5.       fields = @FieldHint(name = "UNSAFE", allowUnsafeAccess = true), 
  6.       access = AccessBits.PUBLIC_METHODS 
  7.    ), 
  8.    @TypeHint(types = Thread.class, access = AccessBits.DECLARED_FIELDS), 
  9.    @TypeHint(typeNames = "com.github.benmanes.caffeine.cache.PS"
  10.       fields = { 
  11.          @FieldHint(name = "key", allowUnsafeAccess = true), 
  12.          @FieldHint(name = "value", allowUnsafeAccess = true
  13.       }, 
  14.       access = AccessBits.DECLARED_CONSTRUCTORS 
  15.    ), 
  16.    @TypeHint(typeNames = "com.github.benmanes.caffeine.cache.PSA"
  17.       fields = @FieldHint(name = "accessTime", allowUnsafeAccess = true), 
  18.       access = AccessBits.DECLARED_CONSTRUCTORS 
  19.    ), 
  20.    @TypeHint(typeNames = "com.github.benmanes.caffeine.cache.PSW"
  21.       fields = @FieldHint(name = "writeTime", allowUnsafeAccess = true), 
  22.       access = AccessBits.DECLARED_CONSTRUCTORS 
  23.    ), 
  24.    @TypeHint(typeNames = "com.github.benmanes.caffeine.cache.StripedBuffer"
  25.       fields = {@FieldHint(name = "tableBusy", allowUnsafeAccess = true)}, 
  26.       access = AccessBits.ALL 
  27.    ), 
  28.    @TypeHint(typeNames = "com.github.benmanes.caffeine.cache.PSWMS", access = AccessBits.DECLARED_CONSTRUCTORS), 
  29.    @TypeHint(typeNames = "com.github.benmanes.caffeine.cache.SSA", access = AccessBits.ALL), 
  30.    @TypeHint(typeNames = "com.github.benmanes.caffeine.cache.SSLA", access = AccessBits.DECLARED_CONSTRUCTORS), 
  31.    @TypeHint(typeNames = "com.github.benmanes.caffeine.cache.SSLMSW", access = AccessBits.DECLARED_CONSTRUCTORS), 
  32.    @TypeHint(typeNames = "com.github.benmanes.caffeine.cache.SSMSW", access = AccessBits.DECLARED_CONSTRUCTORS), 
  33.    @TypeHint(typeNames = "com.github.benmanes.caffeine.cache.BoundedBuffer", access = AccessBits.ALL), 
  34.    @TypeHint(typeNames = "com.github.benmanes.caffeine.cache.BoundedBuffer$RingBuffer", access = AccessBits.ALL), 
  35.    @TypeHint(typeNames = "com.github.benmanes.caffeine.cache.BLCHeader$DrainStatusRef"
  36.       fields = @FieldHint(name = "drainStatus", allowUnsafeAccess = true), 
  37.       access = AccessBits.ALL 
  38.    ) 
  39. }) 

普大喜奔,就行了???是真的吗???caffeinecache 载入缓存文件又逐渐报出现异常了!!!

到此从此不愿瞎折腾了,礼拜天的早上全在瞎折腾这东西了。

四、汇总

spring-native现阶段或是处在卵化环节,如果是未应用第三方部件简易的新项目大伙儿能够试一下,稍大中型提议大伙儿或是再等等。大家也会不断关心并輸出类似文章。

【责编:武晓燕 TEL:(010)68476606】


关注点赞 0
the end
免责声明:本文不代表本站的观点和立场,如有侵权请联系本站删除!本站仅提供信息存储空间服务。