NACOS MalformedInputException 无法读取中文配置问题

1. 问题描述

在windows平台中打包运行springboot jar包提示如下错误

  • 在idea中运行正常
org.yaml.snakeyaml.error.YAMLException: java.nio.charset.MalformedInputException: Input length = 1
        at org.yaml.snakeyaml.reader.StreamReader.update(StreamReader.java:254) ~[snakeyaml-1.19.jar!/:na]
        at org.yaml.snakeyaml.reader.StreamReader.(StreamReader.java:58) ~[snakeyaml-1.19.jar!/:na]
        at org.yaml.snakeyaml.Yaml.loadAll(Yaml.java:537) ~[snakeyaml-1.19.jar!/:na]
        at org.springframework.beans.factory.config.YamlProcessor.process(YamlProcessor.java:166) ~[spring-beans-5.0.12.RELEASE.jar!/:5.0.12.RELEASE]
        at org.springframework.beans.factory.config.YamlProcessor.process(YamlProcessor.java:139) ~[spring-beans-5.0.12.RELEASE.jar!/:5.0.12.RELEASE]
        at org.springframework.beans.factory.config.YamlPropertiesFactoryBean.createProperties(YamlPropertiesFactoryBean.java:135) ~[spring-beans-5.0.12.RELEASE.jar!/:5.0.12.RELEASE]
        at org.springframework.beans.factory.config.YamlPropertiesFactoryBean.getObject(YamlPropertiesFactoryBean.java:115) ~[spring-beans-5.0.12.RELEASE.jar!/:5.0.12.RELEASE]
        at org.springframework.cloud.alibaba.nacos.client.NacosPropertySourceBuilder.loadNacosData(NacosPropertySourceBuilder.java:100) [spring-cloud-alibaba-nacos-config-0.9.0.RELEASE.jar!/:0.9.0.RELEASE]
        at org.springframework.cloud.alibaba.nacos.client.NacosPropertySourceBuilder.build(NacosPropertySourceBuilder.java:75) [spring-cloud-alibaba-nacos-config-0.9.0.RELEASE.jar!/:0.9.0.RELEASE]
        at org.springframework.cloud.alibaba.nacos.client.NacosPropertySourceLocator.loadNacosDataIfPresent(NacosPropertySourceLocator.java:179) [spring-cloud-alibaba-nacos-config-0.9.0.RELEASE.jar!/:0.9.0.RELEASE]
        at org.springframework.cloud.alibaba.nacos.client.NacosPropertySourceLocator.loadApplicationConfiguration(NacosPropertySourceLocator.java:155) [spring-cloud-alibaba-nacos-config-0.9.0.RELEASE.jar!/:0.9.0.RELEASE]
        at org.springframework.cloud.alibaba.nacos.client.NacosPropertySourceLocator.locate(NacosPropertySourceLocator.java:88) [spring-cloud-alibaba-nacos-config-0.9.0.RELEASE.jar!/:0.9.0.RELEASE]
        at org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.initialize(PropertySourceBootstrapConfiguration.java:94) [spring-cloud-context-2.0.0.RELEASE.jar!/:2.0.0.RELEASE]
        at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:628) [spring-boot-2.0.8.RELEASE.jar!/:2.0.8.RELEASE]
        at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:364) [spring-boot-2.0.8.RELEASE.jar!/:2.0.8.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:305) [spring-boot-2.0.8.RELEASE.jar!/:2.0.8.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1242) [spring-boot-2.0.8.RELEASE.jar!/:2.0.8.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1230) [spring-boot-2.0.8.RELEASE.jar!/:2.0.8.RELEASE]
        at com.tba.sc.gateway.GatewayApplication.main(GatewayApplication.java:15) [classes!/:1.0-SNAPSHOT]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_91]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_91]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_91]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_91]
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [tba-gateway.jar:1.0-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [tba-gateway.jar:1.0-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [tba-gateway.jar:1.0-SNAPSHOT]
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [tba-gateway.jar:1.0-SNAPSHOT]
Caused by: java.nio.charset.MalformedInputException: Input length = 1
        at java.nio.charset.CoderResult.throwException(Unknown Source) ~[na:1.8.0_91]
        at sun.nio.cs.StreamDecoder.implRead(Unknown Source) ~[na:1.8.0_91]
        at sun.nio.cs.StreamDecoder.read(Unknown Source) ~[na:1.8.0_91]
        at java.io.InputStreamReader.read(Unknown Source) ~[na:1.8.0_91]
        at org.yaml.snakeyaml.reader.UnicodeReader.read(UnicodeReader.java:125) ~[snakeyaml-1.19.jar!/:na]
        at org.yaml.snakeyaml.reader.StreamReader.update(StreamReader.java:223) ~[snakeyaml-1.19.jar!/:na]

2. 处理方式

在nacos中删除中文也可以处理此问题,但不是最佳方案。
在idea中启动项目时,因为 idea相关配置中设置UTF-8的文件编码格式file.encoding=utf-8。打包运行后,该设置需要手动指定,如下

java  -D'file.encoding=utf-8' -D'server.port=10001' -jar .\tba-gateway.jar 
  • 注意其中JVM参数需要放在-jar命令前
  • windows平台需要使用''符号包裹参数,否则会有如下异常
错误: 找不到或无法加载主类 .encoding=utf-8

你可能感兴趣的:(NACOS MalformedInputException 无法读取中文配置问题)