现象:
1. NacosPropertySourceBuilder - parse data from Nacos error,dataId:itm-portal-dev.yml
2. org.yaml.snakeyaml.error.YAMLException: java.nio.charset.MalformedInputException: Input length = 1
2021-06-29 13:00:00 [LAPTOP-PGQVAIOM] [background-preinit] [,] INFO org.hibernate.validator.internal.util.Version - HV000001: Hibernate Validator 6.1.5.Final
2021-06-29 13:00:01 [LAPTOP-PGQVAIOM] [main] [,] INFO c.a.n.client.config.impl.LocalConfigInfoProcessor - LOCAL_SNAPSHOT_PATH:C:\Users\shersfy\nacos\config
2021-06-29 13:00:02 [LAPTOP-PGQVAIOM] [main] [,] INFO com.alibaba.nacos.client.config.impl.Limiter - limitTime:5.0
2021-06-29 13:00:02 [LAPTOP-PGQVAIOM] [main] [,] WARN c.a.cloud.nacos.client.NacosPropertySourceBuilder - Ignore the empty nacos configuration and get it based on dataId[itm-portal] & group[DEFAULT_GROUP]
2021-06-29 13:00:02 [LAPTOP-PGQVAIOM] [main] [,] WARN c.a.cloud.nacos.client.NacosPropertySourceBuilder - Ignore the empty nacos configuration and get it based on dataId[itm-portal.yml] & group[DEFAULT_GROUP]
2021-06-29 13:00:02 [LAPTOP-PGQVAIOM] [main] [,] INFO com.alibaba.nacos.client.config.utils.JvmUtil - isMultiInstance:false
2021-06-29 13:00:02 [LAPTOP-PGQVAIOM] [main] [,] ERROR c.a.cloud.nacos.client.NacosPropertySourceBuilder - parse data from Nacos error,dataId:itm-portal-dev.yml,data:server:
port: 8080
# upgrade
upgrade:
enabled: true
version: v3.0.2
application-name: ${spring.application.name}
upgrade-script: classpath:/upgrade/itm_portal_v3.0.0-v3.0.2.sql
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
druid:
# 验证连接是否有效。此参数必须设置为非空字符串,下面三项设置成true才能生效
validation-query: SELECT 1
# 连接是否被空闲连接回收器(如果有)进行检验. 如果检测失败, 则连接将被从池中去除
org.yaml.snakeyaml.error.YAMLException: java.nio.charset.MalformedInputException: Input length = 1
at org.yaml.snakeyaml.reader.StreamReader.update(StreamReader.java:218)
at org.yaml.snakeyaml.reader.StreamReader.ensureEnoughData(StreamReader.java:176)
at org.yaml.snakeyaml.reader.StreamReader.ensureEnoughData(StreamReader.java:171)
at org.yaml.snakeyaml.reader.StreamReader.peek(StreamReader.java:126)
at org.yaml.snakeyaml.scanner.ScannerImpl.scanToNextToken(ScannerImpl.java:1177)
at org.yaml.snakeyaml.scanner.ScannerImpl.fetchMoreTokens(ScannerImpl.java:287)
at org.yaml.snakeyaml.scanner.ScannerImpl.checkToken(ScannerImpl.java:227)
at org.yaml.snakeyaml.parser.ParserImpl$ParseImplicitDocumentStart.produce(ParserImpl.java:195)
at org.yaml.snakeyaml.parser.ParserImpl.peekEvent(ParserImpl.java:158)
at org.yaml.snakeyaml.parser.ParserImpl.checkEvent(ParserImpl.java:148)
at org.yaml.snakeyaml.composer.Composer.checkNode(Composer.java:82)
at org.yaml.snakeyaml.constructor.BaseConstructor.checkData(BaseConstructor.java:123)
at org.yaml.snakeyaml.Yaml$1.hasNext(Yaml.java:489)
at org.springframework.beans.factory.config.YamlProcessor.process(YamlProcessor.java:200)
at org.springframework.beans.factory.config.YamlProcessor.process(YamlProcessor.java:164)
at org.springframework.beans.factory.config.YamlMapFactoryBean.createMap(YamlMapFactoryBean.java:124)
at org.springframework.beans.factory.config.YamlMapFactoryBean.getObject(YamlMapFactoryBean.java:104)
at com.alibaba.cloud.nacos.parser.NacosDataYamlParser.doParse(NacosDataYamlParser.java:40)
at com.alibaba.cloud.nacos.parser.AbstractNacosDataParser.parseNacosData(AbstractNacosDataParser.java:80)
at com.alibaba.cloud.nacos.parser.AbstractNacosDataParser.parseNacosData(AbstractNacosDataParser.java:85)
at com.alibaba.cloud.nacos.parser.NacosDataParserHandler.parseNacosData(NacosDataParserHandler.java:45)
at com.alibaba.cloud.nacos.client.NacosPropertySourceBuilder.loadNacosData(NacosPropertySourceBuilder.java:98)
at com.alibaba.cloud.nacos.client.NacosPropertySourceBuilder.build(NacosPropertySourceBuilder.java:74)
at com.alibaba.cloud.nacos.client.NacosPropertySourceLocator.loadNacosPropertySource(NacosPropertySourceLocator.java:204)
at com.alibaba.cloud.nacos.client.NacosPropertySourceLocator.loadNacosDataIfPresent(NacosPropertySourceLocator.java:191)
at com.alibaba.cloud.nacos.client.NacosPropertySourceLocator.loadApplicationConfiguration(NacosPropertySourceLocator.java:150)
at com.alibaba.cloud.nacos.client.NacosPropertySourceLocator.locate(NacosPropertySourceLocator.java:103)
at org.springframework.cloud.bootstrap.config.PropertySourceLocator.locateCollection(PropertySourceLocator.java:52)
at org.springframework.cloud.bootstrap.config.PropertySourceLocator.locateCollection(PropertySourceLocator.java:47)
at org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.initialize(PropertySourceBootstrapConfiguration.java:98)
at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:626)
at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:370)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
at com.edp.itm.portal.Application.main(Application.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:109)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88)
Caused by: java.nio.charset.MalformedInputException: Input length = 1
at java.nio.charset.CoderResult.throwException(CoderResult.java:281)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:339)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at org.yaml.snakeyaml.reader.UnicodeReader.read(UnicodeReader.java:125)
at org.yaml.snakeyaml.reader.StreamReader.update(StreamReader.java:183)
... 43 common frames omitted
2021-06-29 13:00:02 [LAPTOP-PGQVAIOM] [main] [,] INFO o.s.c.b.c.PropertySourceBootstrapConfiguration - Located property source: [BootstrapPropertySource {name='bootstrapProperties-itm-portal-dev.yml,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-itm-portal.yml,DEFAULT_GROUP'}, BootstrapPropertySource {name='bootstrapProperties-itm-portal,DEFAULT_GROUP'}]
2021-06-29 13:00:02 [LAPTOP-PGQVAIOM] [main] [,] INFO com.edp.itm.portal.Application - The following profiles are active: dev
原因分析
1. nacos YAML配置文件中包含中文注释;
2. 运行环境启动服务不是UTF8字符集;
解决
方案1:把中文注释去掉或写英文注释;
方案2:启动服务时指定字符集参数为utf8,这里以启动jar为例:
java -Dfile.encoding=utf8 -jar itm-portal-3.0.3.jar