1. 程式人生 > >解決 Spring Boot v2.1.3.RELEASE 的 while scanning an alias 問題(附優雅停服shuntdown配置)

解決 Spring Boot v2.1.3.RELEASE 的 while scanning an alias 問題(附優雅停服shuntdown配置)

    初學 Spring Boot 框架,在搭建“優雅停服”的時候遇到問題,怎麼也配置不通,有一系列問題(都是我自己的原因,太菜)。下面簡單說一下,首先,我是用的 application.yml 配置檔案進行配置的。這個就導致了配置以下

management.endpoints.web.exposure.include: *

再啟動專案,會出現報錯:

09:56:06.571 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
java.lang.IllegalStateException: Failed to load property source from location 'classpath:/application.yml'
	at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:554)
	at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.loadForFileExtension(ConfigFileApplicationListener.java:500)
	at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:467)
	at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.lambda$null$6(ConfigFileApplicationListener.java:448)
	at java.lang.Iterable.forEach(Iterable.java:75)
	at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.lambda$load$7(ConfigFileApplicationListener.java:447)
	at java.lang.Iterable.forEach(Iterable.java:75)
	at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:444)
	at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:332)
	at org.springframework.boot.context.config.ConfigFileApplicationListener.addPropertySources(ConfigFileApplicationListener.java:207)
	at org.springframework.boot.context.config.ConfigFileApplicationListener.postProcessEnvironment(ConfigFileApplicationListener.java:190)
	at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEnvironmentPreparedEvent(ConfigFileApplicationListener.java:177)
	at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEvent(ConfigFileApplicationListener.java:163)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)
	at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:75)
	at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)
	at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:347)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:306)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
	at com.eric.diary.DiaryApplication.main(DiaryApplication.java:12)
Caused by: org.yaml.snakeyaml.scanner.ScannerException: while scanning an alias
 in 'reader', line 25, column 44:
     ... endpoints.web.exposure.include: *
                                         ^
(13)
 in 'reader', line 25, column 45:
     ... ndpoints.web.exposure.include: *
                                         ^

	at org.yaml.snakeyaml.scanner.ScannerImpl.scanAnchor(ScannerImpl.java:1444)
	at org.yaml.snakeyaml.scanner.ScannerImpl.fetchAlias(ScannerImpl.java:916)
	at org.yaml.snakeyaml.scanner.ScannerImpl.fetchMoreTokens(ScannerImpl.java:364)
	at org.yaml.snakeyaml.scanner.ScannerImpl.checkToken(ScannerImpl.java:227)
	at org.yaml.snakeyaml.parser.ParserImpl$ParseBlockMappingValue.produce(ParserImpl.java:586)
	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.composeNode(Composer.java:124)
	at org.yaml.snakeyaml.composer.Composer.composeValueNode(Composer.java:236)
	at org.yaml.snakeyaml.composer.Composer.composeMappingChildren(Composer.java:227)
	at org.yaml.snakeyaml.composer.Composer.composeMappingNode(Composer.java:215)
	at org.yaml.snakeyaml.composer.Composer.composeNode(Composer.java:144)
	at org.yaml.snakeyaml.composer.Composer.getNode(Composer.java:85)
	at org.yaml.snakeyaml.constructor.BaseConstructor.getData(BaseConstructor.java:123)
	at org.yaml.snakeyaml.Yaml$1.next(Yaml.java:547)
	at org.springframework.beans.factory.config.YamlProcessor.process(YamlProcessor.java:161)
	at org.springframework.beans.factory.config.YamlProcessor.process(YamlProcessor.java:134)
	at org.springframework.boot.env.OriginTrackedYamlLoader.load(OriginTrackedYamlLoader.java:75)
	at org.springframework.boot.env.YamlPropertySourceLoader.load(YamlPropertySourceLoader.java:50)
	at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.loadDocuments(ConfigFileApplicationListener.java:572)
	at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:526)
	... 23 common frames omitted
Disconnected from the target VM, address: '127.0.0.1:59532', transport: 'socket'

Process finished with exit code 1

查了一些資料,發現 yml 的配置檔案的特殊符號要加上單引號,按如下配置就可以解決此問題:

management.endpoints.web.exposure.include: '*'

 

題外話:

    配置“優雅停服”本來都快成功了,出現這個錯誤,一度以為自己又配錯了,又重新配了,耽誤了半天。其實 Spring Boot 配置的 1.x 和 2.x 配置不一樣,然後 application.properties 和 application.yml 也有一點查別。下面放出 2.1.3 的 Spring Boot “優雅停服”的配置,也算是一個記錄吧:

#/actuator轉換成/eric(你也可以寫任意的,預設是/actuator)
management.endpoints.web.base-path: /eric
#啟用所有的端點的web方式請求(也可以單獨只暴露shutdown斷點,有些人管這個叫暴露)
management.endpoints.web.exposure.include: '*'
#啟用shutdown(啟用Spring Boot的shutdown功能)
management.endpoint.shutdown.enabled: true