Debian 中 Java 网络配置实践
一 系统网络先行
auto eth0
iface eth0 inet dhcp
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8 8.8.4.4
nmcli con mod "ens160" ipv4.addresses 192.168.125.137/24
nmcli con mod "ens160" ipv4.gateway 192.168.125.2
nmcli con mod "ens160" ipv4.dns "8.8.8.8 8.8.4.4"
nmcli con mod "ens160" ipv4.method manual
nmcli con down "ens160" && nmcli con up "ens160"
二 代理场景与配置方式
export http_proxy=http://proxy.example.com:3128
export https_proxy=http://proxy.example.com:3128
export no_proxy=localhost,127.0.0.1,.example.com
Acquire::http::Proxy "http://proxy.example.com:3128";
java \
-Dhttp.proxyHost=proxy.example.com \
-Dhttp.proxyPort=3128 \
-Dhttps.proxyHost=proxy.example.com \
-Dhttps.proxyPort=3128 \
-Dhttp.nonProxyHosts="localhost|127.0.0.1|.example.com" \
-Dhttps.nonProxyHosts="localhost|127.0.0.1|.example.com" \
YourApp
-Dhttp.proxyUser=username -Dhttp.proxyPassword=password
System.setProperty("http.proxyHost", "proxy.example.com");
System.setProperty("http.proxyPort", "3128");
System.setProperty("https.proxyHost", "proxy.example.com");
System.setProperty("https.proxyPort", "3128");
System.setProperty("http.nonProxyHosts", "localhost|127.0.0.1|.example.com");
System.setProperty("https.nonProxyHosts", "localhost|127.0.0.1|.example.com");
System.setProperty("java.net.useSystemProxies", "true");
// 之后通过 ProxySelector 获取并选择代理
forward-socks5 / 127.0.0.1:1080 .
listen-address 127.0.0.1:8118
-Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=8118
-Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=8118
说明:JVM 代理参数与代码设置二选一即可;命令行工具(如 curl/wget/apt)与 Java 应用可分别配置不同代理。
三 验证与排错
四 常见注意点