【Springboot】解决 MacOS M1 上 Netty 的 DNS 解析错误

jwensh@2023.12.26

错误

  • 使用 springboot2.7.18 上使用 spring-boot-starter-webfluxWebClient 上使用 netty 的 connector 来进行

  • 引用

java 复制代码
implementation("org.springframework.boot:spring-boot-starter-webflux")
  • 使用
java 复制代码
import reactor.netty.http.client.HttpClient;

···

HttpClient httpClient = HttpClient.create()
                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000);

WebClient webClient = builder.clone()
                .defaultCookie("cookie-name", "cookie-value")
                .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                .clientConnector(new ReactorClientHttpConnector(httpClient))
                .build();
  • 错误
text 复制代码
2023-12-26 09:52:20.123 [restartedMain] ERROR i.n.resolver.dns.DnsServerAddressStreamProviders - 
	Unable to load io.netty.resolver.dns.macos.MacOSDnsServerAddressStreamProvider,
	fallback to system defaults. This may result in incorrect DNS resolutions on 
	MacOS. Check whether you have a dependency on 'io.netty:netty-resolver-dns-native-macos'.
	Use DEBUG level to see the full stack: java.lang.UnsatisfiedLinkError: failed to load the required native library 

是使用 Netty 库时遇到的 DNS 解析错误;此错误源于 Netty 无法访问本机 MacOS DNS 解析器。这样做的后果是可能出现不正确的 DNS 解析,这可能会导致应用程序中出现大量与网络相关的问题;

原因

Netty 使用本机代码与系统的 DNS 解析器交互以获得最佳性能。在 MacOS 上,尤其是使用 M1 芯片的系统上,可能会缺少所需的本机库,从而导致上述错误。

解决方案是显式提供此本机库。

解决方案

对于使用 Gradle 管理的项目,您需要在 build.gradle 文件中包含以下依赖项:

java 复制代码
runtimeOnly("io.netty:netty-resolver-dns-native-macos:4.1.76.Final:osx-aarch_64")

参考

相关推荐
自不量力的A同学17 小时前
Spring Boot 4.0.0 正式发布
java·spring boot·后端
q***985219 小时前
什么是Spring Boot 应用开发?
java·spring boot·后端
豆奶特浓619 小时前
Java面试模拟:当搞笑程序员谢飞机遇到电商秒杀与AIGC客服场景
java·spring boot·微服务·面试·aigc·高并发·电商
踏浪无痕21 小时前
PageHelper 防坑指南:从兜底方案到根治方案
spring boot·后端
通往曙光的路上1 天前
焚决糟糕篇
java·spring boot·tomcat
6***v4171 天前
spring boot 项目打印sql日志和结果,使用logback或配置文件
spring boot·sql·logback
3***g2051 天前
如何使用Spring Boot框架整合Redis:超详细案例教程
spring boot·redis·后端
s***35301 天前
Spring Boot3.x集成Flowable7.x(一)Spring Boot集成与设计、部署、发起、完成简单流程
java·spring boot·后端
3***16101 天前
【监控】Spring Boot+Prometheus+Grafana实现可视化监控
spring boot·grafana·prometheus
学习非暴力沟通的程序员1 天前
Mac 自动定时打开指定URL(无日历版)操作文档
macos