自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Spring Native 可以正式使用了么?

開發(fā) 架構(gòu)
對(duì) Graalvm 和 Spring native 我們一直都有關(guān)注,并且已經(jīng)發(fā)表過多篇公眾號(hào)文章。對(duì)于 demo 級(jí)別的使用這里不做過多介紹,感興趣的可以查看冷神(pig 冷冷)之前的文章 Spring Native 入門實(shí)戰(zhàn)。

[[398344]]

本文轉(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. <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項(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è)字體文件需要添加下面的配置,具體過程同上這里不做過多描述。

  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)成功都折騰了我很長(zhǎ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目前還是處于孵化階段,如果是未使用第三方組件簡(jiǎn)單的項(xiàng)目大家可以試試,稍大型建議大家還是再等等。我們也會(huì)持續(xù)關(guān)注并輸出相關(guān)文章。

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

 

責(zé)任編輯:武曉燕 來源: JAVA架構(gòu)日記
相關(guān)推薦

2021-07-25 21:36:24

Windows操作系統(tǒng)功能

2023-09-07 10:31:27

2023-06-24 17:09:06

React前端

2020-07-16 10:30:03

iOS 13.6iPhone車鑰匙

2021-10-21 22:01:54

GNOME桌面主題桌面應(yīng)用

2020-12-02 11:06:25

Windows 10Android

2019-12-18 15:11:42

數(shù)組集合數(shù)據(jù)

2022-08-11 17:14:37

Java

2022-12-09 09:46:55

插件Lombok

2025-02-24 10:36:15

2011-03-03 17:03:04

零缺陷系統(tǒng)

2018-06-28 09:06:27

DNS技術(shù)CDN

2011-04-25 10:21:00

2021-08-18 10:36:43

Sping社區(qū)實(shí)驗(yàn)項(xiàng)目服務(wù)器

2017-03-21 21:37:06

組件UI測(cè)試架構(gòu)

2013-03-01 14:11:34

谷歌眼鏡

2020-09-25 07:49:36

策略模式Spring

2021-02-01 12:18:55

策略模式Spring

2024-05-30 07:37:30

2022-08-01 07:02:06

SpringEasyExcel場(chǎng)景
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)