Spring Api-Gateway: (M1) java.lang.UnsatisfiedLinkError: no netty_resolver_dns_native_macos_aarch_64

Spring Api-Gateway: (M1) java.lang.UnsatisfiedLinkError: no netty_resolver_dns_native_macos_aarch_64

澄清一下,这只发生在 M1 Mac,intel 版本似乎没有这个问题。

JDK17

openjdk version "17.0.1" 2021-10-19
OpenJDK Runtime Environment Homebrew (build 17.0.1+1)
OpenJDK 64-Bit Server VM Homebrew (build 17.0.1+1, mixed mode, sharing)

# List of all JDK
Matching Java Virtual Machines (2):
    17.0.1 (arm64) "Homebrew" - "OpenJDK 17.0.1" /opt/homebrew/Cellar/openjdk/17.0.1_1/libexec/openjdk.jdk/Contents/Home
    16.0.2 (arm64) "Azul Systems, Inc." - "Zulu 16.32.15" /Users/xxxxxx/Library/Java/JavaVirtualMachines/azul-16.0.2/Contents/Home
/opt/homebrew/Cellar/openjdk/17.0.1_1/libexec/openjdk.jdk/Contents/Home

项目图

问题:

java.lang.UnsatisfiedLinkError 尝试通过 API-Gateway 访问其中一个微服务时抛出异常。例如,http://localhost:8082/API-USER/users/status/ok 将抛出此异常。

return 值是正确的,API-网关继续使用 运行 尽管有烦人的异常。

Caused by: java.lang.UnsatisfiedLinkError: failed to load the required native library
    at io.netty.resolver.dns.macos.MacOSDnsServerAddressStreamProvider.ensureAvailability(MacOSDnsServerAddressStreamProvider.java:110) ~[netty-resolver-dns-classes-macos-4.1.75.Final.jar:4.1.75.Final]
Caused by: java.lang.UnsatisfiedLinkError: failed to load the required native library

    at io.netty.resolver.dns.macos.MacOSDnsServerAddressStreamProvider.<init>(MacOSDnsServerAddressStreamProvider.java:120) ~[netty-resolver-dns-classes-macos-4.1.75.Final.jar:4.1.75.Final]
    ... 202 common frames omitted
Caused by: java.lang.UnsatisfiedLinkError: could not load a native library: netty_resolver_dns_native_macos_aarch_64
    at io.netty.util.internal.NativeLibraryLoader.load(NativeLibraryLoader.java:224) ~[netty-common-4.1.75.Final.jar:4.1.75.Final]
    at io.netty.resolver.dns.macos.MacOSDnsServerAddressStreamProvider.loadNativeLibrary(MacOSDnsServerAddressStreamProvider.java:92) ~[netty-resolver-dns-classes-macos-4.1.75.Final.jar:4.1.75.Final]
    at io.netty.resolver.dns.macos.MacOSDnsServerAddressStreamProvider.<clinit>(MacOSDnsServerAddressStreamProvider.java:77) ~[netty-resolver-dns-classes-macos-4.1.75.Final.jar:4.1.75.Final]
    at java.base/java.lang.Class.forName0(Native Method) ~[na:na]
Caused by: java.lang.UnsatisfiedLinkError: could not load a native library: netty_resolver_dns_native_macos_aarch_64

    at java.base/java.lang.Class.forName(Class.java:467) ~[na:na]
    at io.netty.resolver.dns.DnsServerAddressStreamProviders.run(DnsServerAddressStreamProviders.java:50) ~[netty-resolver-dns-4.1.75.Final.jar:4.1.75.Final]
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:318) ~[na:na]
    at io.netty.resolver.dns.DnsServerAddressStreamProviders.<clinit>(DnsServerAddressStreamProviders.java:46) ~[netty-resolver-dns-4.1.75.Final.jar:4.1.75.Final]
    ... 196 common frames omitted
    Suppressed: java.lang.UnsatisfiedLinkError: could not load a native library: netty_resolver_dns_native_macos
        at io.netty.util.internal.NativeLibraryLoader.load(NativeLibraryLoader.java:224) ~[netty-common-4.1.75.Final.jar:4.1.75.Final]
        at io.netty.resolver.dns.macos.MacOSDnsServerAddressStreamProvider.loadNativeLibrary(MacOSDnsServerAddressStreamProvider.java:95) ~[netty-resolver-dns-classes-macos-4.1.75.Final.jar:4.1.75.Final]
        ... 202 common frames omitted
    Caused by: java.io.FileNotFoundException: META-INF/native/libnetty_resolver_dns_native_macos.jnilib
        at io.netty.util.internal.NativeLibraryLoader.load(NativeLibraryLoader.java:166)
    Caused by: java.io.FileNotFoundException: META-INF/native/libnetty_resolver_dns_native_macos.jnilib

        ... 203 common frames omitted
        Suppressed: java.lang.UnsatisfiedLinkError: no netty_resolver_dns_native_macos in java.library.path: /Users/erichuang/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
            at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2429)
            at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:818)
            at java.base/java.lang.System.loadLibrary(System.java:1989)
            at io.netty.util.internal.NativeLibraryUtil.loadLibrary(NativeLibraryUtil.java:38)
            at io.netty.util.internal.NativeLibraryLoader.loadLibrary(NativeLibraryLoader.java:376)
            at io.netty.util.internal.NativeLibraryLoader.load(NativeLibraryLoader.java:146)
            ... 203 common frames omitted
            Suppressed: java.lang.UnsatisfiedLinkError: no netty_resolver_dns_native_macos in java.library.path: /Users/erichuang/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
                at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2429)
                at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:818)
                at java.base/java.lang.System.loadLibrary(System.java:1989)
                at io.netty.util.internal.NativeLibraryUtil.loadLibrary(NativeLibraryUtil.java:38)
                at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
                at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at java.base/java.lang.reflect.Method.invoke(Method.java:568)
                at io.netty.util.internal.NativeLibraryLoader.run(NativeLibraryLoader.java:410)
                at java.base/java.security.AccessController.doPrivileged(AccessController.java:318)
                at io.netty.util.internal.NativeLibraryLoader.loadLibraryByHelper(NativeLibraryLoader.java:402)
                at io.netty.util.internal.NativeLibraryLoader.loadLibrary(NativeLibraryLoader.java:368)
                ... 204 common frames omitted
Caused by: java.io.FileNotFoundException: META-INF/native/libnetty_resolver_dns_native_macos_aarch_64.jnilib
    at io.netty.util.internal.NativeLibraryLoader.load(NativeLibraryLoader.java:166) ~[netty-common-4.1.75.Final.jar:4.1.75.Final]
    ... 203 common frames omitted
    Suppressed: java.lang.UnsatisfiedLinkError: no netty_resolver_dns_native_macos_aarch_64 in java.library.path: /Users/erichuang/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
        at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2429) ~[na:na]
Caused by: java.io.FileNotFoundException: META-INF/native/libnetty_resolver_dns_native_macos_aarch_64.jnilib

        at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:818) ~[na:na]
        at java.base/java.lang.System.loadLibrary(System.java:1989) ~[na:na]
        at io.netty.util.internal.NativeLibraryUtil.loadLibrary(NativeLibraryUtil.java:38) ~[netty-common-4.1.75.Final.jar:4.1.75.Final]
        at io.netty.util.internal.NativeLibraryLoader.loadLibrary(NativeLibraryLoader.java:376) ~[netty-common-4.1.75.Final.jar:4.1.75.Final]
        at io.netty.util.internal.NativeLibraryLoader.load(NativeLibraryLoader.java:146) ~[netty-common-4.1.75.Final.jar:4.1.75.Final]
        ... 203 common frames omitted
        Suppressed: java.lang.UnsatisfiedLinkError: no netty_resolver_dns_native_macos_aarch_64 in java.library.path: /Users/erichuang/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
            at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2429)
            at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:818)
            at java.base/java.lang.System.loadLibrary(System.java:1989)
            at io.netty.util.internal.NativeLibraryUtil.loadLibrary(NativeLibraryUtil.java:38)
            at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
            at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.base/java.lang.reflect.Method.invoke(Method.java:568)
            at io.netty.util.internal.NativeLibraryLoader.run(NativeLibraryLoader.java:410)
            at java.base/java.security.AccessController.doPrivileged(AccessController.java:318)
            at io.netty.util.internal.NativeLibraryLoader.loadLibraryByHelper(NativeLibraryLoader.java:402)
            at io.netty.util.internal.NativeLibraryLoader.loadLibrary(NativeLibraryLoader.java:368)
            ... 204 common frames omitted

问题

有谁知道如何减轻这一错误?

我累了...

这两个链接都建议添加 netty-tcnative-boringssl-static 依赖项,但效果不佳....

API-网关:build.gradle

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'

    // https://mvnrepository.com/artifact/io.netty/netty-tcnative-boringssl-static
    implementation group: 'io.netty', name: 'netty-tcnative-boringssl-static', version: '2.0.51.Final'


}

Thanks for helping

您必须将正确的分类器添加到 netty-resolver-dns-native-macos 工件中,以便加载正确版本的本机库:

implementation("io.netty:netty-resolver-dns-native-macos:4.1.75.Final") {
    artifact {
        classifier = "osx-aarch_64"
    }
}

在 github 上发现了一个问题 https://github.com/netty/netty/issues/11020

获取最新版本https://mvnrepository.com/artifact/io.netty/netty-resolver-dns-native-macos

仍然没有解释为什么会出现这个问题,但在这里已经为我解决了这个问题。

行家

        <dependency>
          <groupId>io.netty</groupId>
          <artifactId>netty-resolver-dns-native-macos</artifactId>
          <version>XXX</version>
          <classifier>osx-x86_64</classifier>
        </dependency>

Gradle

implementation group: 'io.netty', name: 'netty-resolver-dns-native-macos', version: 'xxx', classifier: 'osx-aarch_64'