Spring Native 可以正式使用了么?
一、前言
hello 大家好,我是如夢技術(shù)(春哥 L.cm),大家可能在很多開源項目里看到過我的身影。今天我?guī)ьI(lǐng)大家實戰(zhàn)一下spring-native。內(nèi)容偏硬核,建議大家坐穩(wěn)扶好(關(guān)注、收藏)。
對 Graalvm 和 Spring native 我們一直都有關(guān)注,并且已經(jīng)發(fā)表過多篇公眾號文章。對于 demo 級別的使用這里不做過多介紹,感興趣的可以查看冷神(pig 冷冷)之前的文章 Spring Native 入門實戰(zhàn)。
二、spring native
2.1 graalvm native image 配置生成
在spring native項目(mica-native-test)編譯之后會生成下面的這些 graalvm native image 配置。

可以對動態(tài)代理、反射、資源文件和序列化進行配置。
2.2 spring native hints
spring native開放了很多的hints,用于對native image不支持的動態(tài)代理、反射、資源文件等進行配置。主要的hints如下圖:

這些 hits 會將我們自定義的配置生成到proxy-config.json、reflect-config.json、resource-config.json、serialization-config.json中。
三、mica 的適配
本節(jié)文章拿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文件已經(jīng)添加進resource-config.json。
最后運行項目:

測試mica-ip2region(完美):

3.2 mica-captcha
mica-captcha主要是幾個字體文件需要添加下面的配置,具體過程同上這里不做過多描述。
@NativeHint(resources = @ResourceHint(patterns = "^fonts/.*.ttf"))
注意:由于驗證碼涉及到字體和 awt 會涉及到下面2個問題。
以
docker編譯運行native image會遇到字體問題需要安裝字體:
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,如下圖:

在添加下面這么多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 讀取緩存又開始報異常了?。?!

至此再也不想折騰了,周末的上午全在折騰這玩意了。
四、總結(jié)
spring-native目前還是處于孵化階段,如果是未使用第三方組件簡單的項目大家可以試試,稍大型建議大家還是再等等。我們也會持續(xù)關(guān)注并輸出相關(guān)文章。
啊,又是一個充實的周末!歡迎大家關(guān)注我們,我是春哥,咱們下次再見!
