日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
SpringNative可以正式使用了么?

本文轉(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依賴。

  
 
 
 
  1.  
  2.     org.springframework.experimental 
  3.     spring-native 
  4.     ${spring-native.version} 
  5.     provided 
  6.  

然后在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項(xiàng)目(mica-native-test)我們可以看見ip2region.db文件已經(jīng)添加進(jìn)resource-config.json。

最后運(yùn)行項(xiàng)目:

測試mica-ip2region(完美):

3.2 mica-captcha

mica-captcha主要是幾個(gè)字體文件需要添加下面的配置,具體過程同上這里不做過多描述。

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

注意:由于驗(yàn)證碼涉及到字體和 awt 會(huì)涉及到下面2個(gè)問題。

  • 以docker編譯運(yùn)行native image會(huì)遇到字體問題需要安裝字體:
  
 
 
 
  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能啟動(dòng)成功都折騰了我很長時(shí)間。各種報(bào)錯(cuò),不過都有提示,我們可以按照提示添加Hints,如下圖:

image.png

在添加下面這么多Hints之后終于可以啟動(dòng)成功了!!!

  
 
 
 
  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 讀取緩存又開始報(bào)異常了!!!

至此再也不想折騰了,周末的上午全在折騰這玩意了。

四、總結(jié)

spring-native目前還是處于孵化階段,如果是未使用第三方組件簡單的項(xiàng)目大家可以試試,稍大型建議大家還是再等等。我們也會(huì)持續(xù)關(guān)注并輸出相關(guān)文章。

【責(zé)任編輯:武曉燕 TEL:(010)68476606】


網(wǎng)頁標(biāo)題:SpringNative可以正式使用了么?
鏈接地址:http://www.dlmjj.cn/article/dhcisjp.html