alibaba nacos config 读取有中文的配置报错

2020-07-27 18:21:23.845 ERROR [qwe,,,] 352 --- [           main] c.a.c.n.c.NacosPropertySourceBuilder     : parse data from Nacos error,dataId:qwe-dev.yaml,data:qwe:
  asd: 4531534 #测试,

org.yaml.snakeyaml.error.YAMLException: java.nio.charset.MalformedInputException: Input length = 1
        at org.yaml.snakeyaml.reader.StreamReader.update(StreamReader.java:218) ~[snakeyaml-1.25.jar!/:na]
        at org.yaml.snakeyaml.reader.StreamReader.ensureEnoughData(StreamReader.java:176) ~[snakeyaml-1.25.jar!/:na]
        at org.yaml.snakeyaml.reader.StreamReader.ensureEnoughData(StreamReader.java:171) ~[snakeyaml-1.25.jar!/:na]
        at org.yaml.snakeyaml.reader.StreamReader.peek(StreamReader.java:126) ~[snakeyaml-1.25.jar!/:na]
        at org.yaml.snakeyaml.scanner.ScannerImpl.scanToNextToken(ScannerImpl.java:1177) ~[snakeyaml-1.25.jar!/:na]
        at org.yaml.snakeyaml.scanner.ScannerImpl.fetchMoreTokens(ScannerImpl.java:287) ~[snakeyaml-1.25.jar!/:na]
        at org.yaml.snakeyaml.scanner.ScannerImpl.checkToken(ScannerImpl.java:227) ~[snakeyaml-1.25.jar!/:na]
        at org.yaml.snakeyaml.parser.ParserImpl$ParseImplicitDocumentStart.produce(ParserImpl.java:195) ~[snakeyaml-1.25.jar!/:na]
        at org.yaml.snakeyaml.parser.ParserImpl.peekEvent(ParserImpl.java:158) ~[snakeyaml-1.25.jar!/:na]
        at org.yaml.snakeyaml.parser.ParserImpl.checkEvent(ParserImpl.java:148) ~[snakeyaml-1.25.jar!/:na]
        at org.yaml.snakeyaml.composer.Composer.checkNode(Composer.java:72) ~[snakeyaml-1.25.jar!/:na]
        at org.yaml.snakeyaml.constructor.BaseConstructor.checkData(BaseConstructor.java:114) ~[snakeyaml-1.25.jar!/:na]
        at org.yaml.snakeyaml.Yaml$1.hasNext(Yaml.java:543) ~[snakeyaml-1.25.jar!/:na]
        at org.springframework.beans.factory.config.YamlProcessor.process(YamlProcessor.java:200) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
        at org.springframework.beans.factory.config.YamlProcessor.process(YamlProcessor.java:164) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
        at org.springframework.beans.factory.config.YamlMapFactoryBean.createMap(YamlMapFactoryBean.java:124) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
        at org.springframework.beans.factory.config.YamlMapFactoryBean.getObject(YamlMapFactoryBean.java:104) ~[spring-beans-5.2.8.RELEASE.jar!/:5.2.8.RELEASE]
        at com.alibaba.cloud.nacos.parser.NacosDataYamlParser.doParse(NacosDataYamlParser.java:40) ~[spring-cloud-starter-alibaba-nacos-config-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
        at com.alibaba.cloud.nacos.parser.AbstractNacosDataParser.parseNacosData(AbstractNacosDataParser.java:80) ~[spring-cloud-starter-alibaba-nacos-config-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
        at com.alibaba.cloud.nacos.parser.AbstractNacosDataParser.parseNacosData(AbstractNacosDataParser.java:85) ~[spring-cloud-starter-alibaba-nacos-config-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
        at com.alibaba.cloud.nacos.parser.NacosDataParserHandler.parseNacosData(NacosDataParserHandler.java:45) ~[spring-cloud-starter-alibaba-nacos-config-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
        at com.alibaba.cloud.nacos.client.NacosPropertySourceBuilder.loadNacosData(NacosPropertySourceBuilder.java:98) [spring-cloud-starter-alibaba-nacos-config-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
        at com.alibaba.cloud.nacos.client.NacosPropertySourceBuilder.build(NacosPropertySourceBuilder.java:74) [spring-cloud-starter-alibaba-nacos-config-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
        at com.alibaba.cloud.nacos.client.NacosPropertySourceLocator.loadNacosPropertySource(NacosPropertySourceLocator.java:204) [spring-cloud-starter-alibaba-nacos-config-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
        at com.alibaba.cloud.nacos.client.NacosPropertySourceLocator.loadNacosDataIfPresent(NacosPropertySourceLocator.java:191) [spring-cloud-starter-alibaba-nacos-config-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
        at com.alibaba.cloud.nacos.client.NacosPropertySourceLocator.loadApplicationConfiguration(NacosPropertySourceLocator.java:150) [spring-cloud-starter-alibaba-nacos-config-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
        at com.alibaba.cloud.nacos.client.NacosPropertySourceLocator.locate(NacosPropertySourceLocator.java:103) [spring-cloud-starter-alibaba-nacos-config-2.2.1.RELEASE.jar!/:2.2.1.RELEASE]
        at org.springframework.cloud.bootstrap.config.PropertySourceLocator.locateCollection(PropertySourceLocator.java:52) [spring-cloud-context-2.2.3.RELEASE.jar!/:2.2.3.RELEASE]
        at org.springframework.cloud.bootstrap.config.PropertySourceLocator.locateCollection(PropertySourceLocator.java:47) [spring-cloud-context-2.2.3.RELEASE.jar!/:2.2.3.RELEASE]
        at org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.initialize(PropertySourceBootstrapConfiguration.java:98) [spring-cloud-context-2.2.3.RELEASE.jar!/:2.2.3.RELEASE]
        at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:626) [spring-boot-2.2.9.RELEASE.jar!/:2.2.9.RELEASE]
        at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:370) [spring-boot-2.2.9.RELEASE.jar!/:2.2.9.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-2.2.9.RELEASE.jar!/:2.2.9.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.9.RELEASE.jar!/:2.2.9.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.9.RELEASE.jar!/:2.2.9.RELEASE]
        at com.example.auth.AuthApplication.main(AuthApplication.java:22) [classes!/:0.0.1-SNAPSHOT]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_202]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_202]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_202]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_202]
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:51) [auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:52) [auth-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
Caused by: java.nio.charset.MalformedInputException: Input length = 1
        at java.nio.charset.CoderResult.throwException(CoderResult.java:281) ~[na:1.8.0_202]
        at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:339) ~[na:1.8.0_202]
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178) ~[na:1.8.0_202]
        at java.io.InputStreamReader.read(InputStreamReader.java:184) ~[na:1.8.0_202]
        at org.yaml.snakeyaml.reader.UnicodeReader.read(UnicodeReader.java:125) ~[snakeyaml-1.25.jar!/:na]
        at org.yaml.snakeyaml.reader.StreamReader.update(StreamReader.java:183) ~[snakeyaml-1.25.jar!/:na]
        ... 43 common frames omitted
  • 原因

主要发生在开发中或者windows运行时候才会发生这种事,而且经过测试目前只有读取yaml的才会

主要报错原因是读取到的配置中有中文,而在windows运行时候控制台默认编码为GBK,而读取到的配置文件为UTF-8编码,导致的报错

用GBK去解析UTF-8,没有中文没问题,有中文当然报错,即使是注释也不行

  • 方法一:加启动参数

解决办法很简单,加上  -Dfile.encoding=utf-8 参数启动就可以了

java -Dfile.encoding=utf-8 -jar qwe.jar 
  • 方法二:修改源码

但是呢,如果有部分特殊项目必须用GBK编码或者其他编码启动着就不行了

但是读不到配置项目也启动不了

这就得自己去修改源码了

去github上把源码拉下来

地址:https://github.com/alibaba/spring-cloud-alibaba

找到  NacosDataYamlParser 这个类,是解析yaml格式配置文件的类

找到这行代码

yamlFactory.setResources(new ByteArrayResource(data.getBytes()));

这行代码,data就是读取到配置文件的数据,data.getBytes() 用的是默认启动的编码,我们给他指定UTF-8读取就可以了

yamlFactory.setResources(new ByteArrayResource(data.getBytes("UTF-8")));

然后打包,然后install到自己的maven库

需要改版本号的可以改成自己的版本号在根目录的pom.xml里面控制了

        <revision>2.2.1.RELEASE</revision>

然后在自己的项目中引入对应的就行了

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值