sun.jnu.encoding ANSI_X3.4-1968导致显示乱码
问题
最近使用jenkins更换为docker启动之后,发布java包并启动遇到了一个问题,店铺端显示乱码。
使用arthas---阿里出品的java调试工具,进入指定jar之后,显示:sun.jnu.encoding ANSI_X3.4-1968
场景
ubuntu20.04 上使用镜像jenkins/jenkins:2.419
使用1panel安装的jenkins
在终端中查看locale
发现显示的是C.UTF-8
查看dockerfile文件内容如下
查看远端服务器的locale
发现正常。
这里补充一下C.UTF-8和en_US.UTF-8的区别
- 查看C.UTF-8和en-US.UTF-8语言环境差异
- 可能会有一些影响,因为它们在排序顺序、大小写关系、排序顺序、数千分隔符、默认货币符号等方面有所不同。
- C.utf8=POSIX标准兼容的默认语言环境。只有严格的ASCII字符才是有效的,扩展后允许基本使用UTF-8
- en_US.utf8=美式英语UTF-8语言环境。
- 虽然我不确定您可能遇到的具体效果,但我相信如果需要,您可以在应用程序中设置语言环境和编码。
- 一般来说,C指的是计算机,en_US指的是我们中说英语的人(以及其他想要同样行为的人)。
- computer的意味着字符串有时更标准(但仍然是英语),因此程序的输出可以从其他程序读取。使用en_US,字符串可以得到改进,字母顺序可以得到改进(可能通过芝加哥风格规则的新规则等)。
- 所以更方便用户,但可能不太稳定。注意:语言环境不仅用于字符串的转换,还用于排序(字母顺序、数字(例如千位分隔符)、货币(我认为可以安全地预测美元和小数点后两位)、月份、星期几等。
- 在您的例子中,它只是两个地区的UTF-8版本。
- 一般来说,这不重要。我通常更喜欢enúUS.UTF-8,但通常不重要,在您的情况下(服务器应用程序),它应该只更改日志和错误消息(如果您使用locale.setlocale())。
- 您应该在应用程序中处理客户端区域设置。从其他程序读取的程序应该在打开管道之前设置C,所以这并不重要。
- 如你所见,也许这无关紧要。您还可以使用POSIX语言环境,也可以在Debian中定义。您可以使用locale -a获得已安装区域设置的列表。
- 注意:微优化将规定C/C.UTF-8语言环境:不翻译文件(gettext),以及关于排序和数字格式的简单规则,但这应仅在服务器端可见。
解决
翻阅资料发现,使用ubuntu的ssh在登录时候,会发送本地的ENV LC_CTYPE到远端,导致启动jar时候使用携带的变量,手动改变服务器启动加载配置变量即可。
添加到指定用户的~/.bashrc文件中,source一下即可解决
#通过手动改变服务器启动加载的配置文件/etc/profile,强制显示地设置locale参数来Fix这个问题,这也是比较推荐的方式(相对于改变java参数来说)。
export LANG=en_US.UTF-8
export LC_CTYPE=en_US.UTF-8
export LC_NUMERIC=en_US.UTF-8
export LC_TIME=en_US.UTF-8
export LC_COLLATE=en_US.UTF-8
export LC_MONETARY=en_US.UTF-8
export LC_MESSAGES=en_US.UTF-8
export LC_PAPER=en_US.UTF-8
export LC_NAME=en_US.UTF-8
export LC_ADDRESS=en_US.UTF-8
export LC_TELEPHONE=en_US.UTF-8
export LC_MEASUREMENT=en_US.UTF-8
export LC_IDENTIFICATION=en_US.UTF-8
export LC_ALL=en_US.UTF-8?
使用ssh镜像内连接远端ip:22,-v打印debug调试,显示如下
debug1: Sending environment.
debug1: Sending env LANG = C.UTF-8
root@34949713db29:/# ssh -v -p22 user@ip
OpenSSH_8.4p1 Debian-5+deb11u1, OpenSSL 1.1.1n 15 Mar 2022
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: include /etc/ssh/ssh_config.d/*.conf matched no files
debug1: /etc/ssh/ssh_config line 21: Applying options for *
debug1: Connecting to ip [ip] port 22000.
debug1: Connection established.
debug1: identity file /root/.ssh/id_rsa type 0
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: identity file /root/.ssh/id_dsa type -1
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: identity file /root/.ssh/id_ecdsa type -1
debug1: identity file /root/.ssh/id_ecdsa-cert type -1
debug1: identity file /root/.ssh/id_ecdsa_sk type -1
debug1: identity file /root/.ssh/id_ecdsa_sk-cert type -1
debug1: identity file /root/.ssh/id_ed25519 type -1
debug1: identity file /root/.ssh/id_ed25519-cert type -1
debug1: identity file /root/.ssh/id_ed25519_sk type -1
debug1: identity file /root/.ssh/id_ed25519_sk-cert type -1
debug1: identity file /root/.ssh/id_xmss type -1
debug1: identity file /root/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.4p1 Debian-5+deb11u1
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4
debug1: match: OpenSSH_7.4 pat OpenSSH_7.0*,OpenSSH_7.1*,OpenSSH_7.2*,OpenSSH_7.3*,OpenSSH_7.4*,OpenSSH_7.5*,OpenSSH_7.6*,OpenSSH_7.7* compat 0x04000002
debug1: Authenticating to ip:22 as 'user'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:vfuKGC+mqkLmH7hOIC4nCQh9DerUngbO53Pxd67Q4bg
debug1: Host '[ip]:22' is known and matches the ECDSA host key.
debug1: Found key in /root/.ssh/known_hosts:2
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: /root/.ssh/id_rsa RSA SHA256:OgtH7dicFAtoPsIT39ynmT8vl1PHu4TcDeo7Pn1bgIc
debug1: Will attempt key: /root/.ssh/id_dsa
debug1: Will attempt key: /root/.ssh/id_ecdsa
debug1: Will attempt key: /root/.ssh/id_ecdsa_sk
debug1: Will attempt key: /root/.ssh/id_ed25519
debug1: Will attempt key: /root/.ssh/id_ed25519_sk
debug1: Will attempt key: /root/.ssh/id_xmss
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: gssapi-with-mic
debug1: Unspecified GSS failure. Minor code may provide more information
No Kerberos credentials available (default cache: FILE:/tmp/krb5cc_0)
debug1: Unspecified GSS failure. Minor code may provide more information
No Kerberos credentials available (default cache: FILE:/tmp/krb5cc_0)
debug1: Next authentication method: publickey
debug1: Offering public key: /root/.ssh/id_rsa RSA SHA256:OgtH7dicFAtoPsIT39ynmT8vl1PHu4TcDeo7Pn1bgIc
debug1: Server accepts key: /root/.ssh/id_rsa RSA SHA256:OgtH7dicFAtoPsIT39ynmT8vl1PHu4TcDeo7Pn1bgIc
debug1: Authentication succeeded (publickey).
Authenticated to ip (ip]:22).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: pledge: network
debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
debug1: Sending environment.
debug1: Sending env LANG = C.UTF-8
Last login: Wed Sep 13 18:05:43 2023 from ip2
参考链接:
java运行时参数file.encoding和sun.jnu.encoding详解
持续学习
欢迎大家提出不一样的观点,我们一起讨论,
我是辣个男人,一个运维人。
本文暂时没有评论,来添加一个吧(●'◡'●)