Spring Native 可以正式使用了么?
本文轉(zhuǎn)載自微信公眾號(hào)「JAVA架構(gòu)日記」,作者如夢(mèng)技術(shù)。轉(zhuǎn)載本文請(qǐng)聯(lián)系JAVA架構(gòu)日記公眾號(hào)。
一、前言
hello 大家好,我是如夢(mèng)技術(shù)(春哥 L.cm),大家可能在很多開源項(xiàng)目里看到過我的身影。今天我?guī)ьI(lǐng)大家實(shí)戰(zhàn)一下spring-native。內(nèi)容偏硬核,建議大家坐穩(wěn)扶好(關(guān)注、收藏)。
對(duì) Graalvm 和 Spring native 我們一直都有關(guān)注,并且已經(jīng)發(fā)表過多篇公眾號(hào)文章。對(duì)于 demo 級(jí)別的使用這里不做過多介紹,感興趣的可以查看冷神(pig 冷冷)之前的文章 Spring Native 入門實(shí)戰(zhàn)。
二、spring native
2.1 graalvm native image 配置生成
在spring native項(xiàng)目(mica-native-test)編譯之后會(huì)生成下面的這些 graalvm native image 配置。
可以對(duì)動(dòng)態(tài)代理、反射、資源文件和序列化進(jìn)行配置。
2.2 spring native hints
spring native開放了很多的hints,用于對(duì)native image不支持的動(dòng)態(tài)代理、反射、資源文件等進(jìn)行配置。主要的hints如下圖:
這些 hits 會(huì)將我們自定義的配置生成到proxy-config.json、reflect-config.json、resource-config.json、serialization-config.json中。
三、mica 的適配
本節(jié)文章拿mica的部分組件作為示例,來介紹spring native hints的使用。
3.1 mica-ip2region
mica-ip2region中涉及到一個(gè) 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項(xiàng)目(mica-native-test)我們可以看見ip2region.db文件已經(jīng)添加進(jìn)resource-config.json。
最后運(yùn)行項(xiàng)目:
測(cè)試mica-ip2region(完美):
3.2 mica-captcha
mica-captcha主要是幾個(gè)字體文件需要添加下面的配置,具體過程同上這里不做過多描述。
- @NativeHint(resources = @ResourceHint(patterns = "^fonts/.*.ttf"))
注意:由于驗(yàn)證碼涉及到字體和 awt 會(huì)涉及到下面2個(gè)問題。
- 以docker編譯運(yùn)行native image會(huì)遇到字體問題需要安裝字體:
- 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能啟動(dòng)成功都折騰了我很長(zhǎng)時(shí)間。各種報(bào)錯(cuò),不過都有提示,我們可以按照提示添加Hints,如下圖:
image.png
在添加下面這么多Hints之后終于可以啟動(dòng)成功了!!!
- @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 讀取緩存又開始報(bào)異常了!!!
至此再也不想折騰了,周末的上午全在折騰這玩意了。
四、總結(jié)
spring-native目前還是處于孵化階段,如果是未使用第三方組件簡(jiǎn)單的項(xiàng)目大家可以試試,稍大型建議大家還是再等等。我們也會(huì)持續(xù)關(guān)注并輸出相關(guān)文章。
【責(zé)任編輯:武曉燕 TEL:(010)68476606】