這裡的SpringCloudAlibaba的版本2021.0.4.0、SpringCloud版本2021.0.5、SpringBoot版本2.6.13,nacos服務端版本2.1.2,一定要做好版本控制,否則服務啟動會出一堆莫名奇妙的錯誤
需要用到的依賴:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
在項目中建立bootstrap.yml作為服務的啟動引導配置,然後在nacos中添加應用的配置
bootstrap.yml的内容:
spring:
application:
name: cloud-gateway
cloud:
nacos:
config:
server-addr: http://192.168.137.18:8848
file-extension: yaml
profiles: #多環境配置
active: dev
nacos中的應用配置
啟動服務異常:
D:\scion\jdk1.8.0_251\bin\java.exe -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:61719,suspend=y,server=n -Xms128m -Xmx256m -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true -javaagent:C:\Users\admin\AppData\Local\JetBrains\IntelliJIdea2021.3\captureAgent\debugger-agent.jar=file:/C:/Users/admin/AppData/Local/Temp/capture.props -Dfile.encoding=UTF-8 -classpath "D:\scion\jdk1.8.0_251\jre\lib\charsets.jar;D:\scion\jdk1.8.0_251\jre\lib\deploy.jar;D:\scion\jdk1.8.0_251\jre\lib\ext\access-bridge-64.jar;D:\scion\jdk1.8.0_251\jre\lib\ext\cldrdata.jar;D:\scion\jdk1.8.0_251\jre\lib\ext\dnsns.jar;D:\scion\jdk1.8.0_251\jre\lib\ext\jaccess.jar;D:\scion\jdk1.8.0_251\jre\lib\ext\jfxrt.jar;D:\scion\jdk1.8.0_251\jre\lib\ext\localedata.jar;D:\scion\jdk1.8.0_251\jre\lib\ext\nashorn.jar;D:\scion\jdk1.8.0_251\jre\lib\ext\sunec.jar;D:\scion\jdk1.8.0_251\jre\lib\ext\sunjce_provider.jar;D:\scion\jdk1.8.0_251\jre\lib\ext\sunmscapi.jar;D:\scion\jdk1.8.0_251\jre\lib\ext\sunpkcs11.jar;D:\scion\jdk1.8.0_251\jre\lib\ext\zipfs.jar;D:\scion\jdk1.8.0_251\jre\lib\javaws.jar;D:\scion\jdk1.8.0_251\jre\lib\jce.jar;D:\scion\jdk1.8.0_251\jre\lib\jfr.jar;D:\scion\jdk1.8.0_251\jre\lib\jfxswt.jar;D:\scion\jdk1.8.0_251\jre\lib\jsse.jar;D:\scion\jdk1.8.0_251\jre\lib\management-agent.jar;D:\scion\jdk1.8.0_251\jre\lib\plugin.jar;D:\scion\jdk1.8.0_251\jre\lib\resources.jar;D:\scion\jdk1.8.0_251\jre\lib\rt.jar;D:\scion\workspace\spring-cloud-alibaba-learn\spring-cloud-learn-gateway\target\classes;D:\scion\local\repo\org\springframework\cloud\spring-cloud-starter-gateway\3.1.4\spring-cloud-starter-gateway-3.1.4.jar;D:\scion\local\repo\org\springframework\cloud\spring-cloud-starter\3.1.5\spring-cloud-starter-3.1.5.jar;D:\scion\local\repo\org\springframework\security\spring-security-rsa\1.0.11.RELEASE\spring-security-rsa-1.0.11.RELEASE.jar;D:\scion\local\repo\org\bouncycastle\bcpkix-jdk15on\1.69\bcpkix-jdk15on-1.69.jar;D:\scion\local\repo\org\bouncycastle\bcprov-jdk15on\1.69\bcprov-jdk15on-1.69.jar;D:\scion\local\repo\org\bouncycastle\bcutil-jdk15on\1.69\bcutil-jdk15on-1.69.jar;D:\scion\local\repo\org\springframework\cloud\spring-cloud-gateway-server\3.1.4\spring-cloud-gateway-server-3.1.4.jar;D:\scion\local\repo\org\springframework\boot\spring-boot-starter-validation\2.6.13\spring-boot-starter-validation-2.6.13.jar;D:\scion\local\repo\org\apache\tomcat\embed\tomcat-embed-el\9.0.68\tomcat-embed-el-9.0.68.jar;D:\scion\local\repo\org\hibernate\validator\hibernate-validator\6.2.5.Final\hibernate-validator-6.2.5.Final.jar;D:\scion\local\repo\jakarta\validation\jakarta.validation-api\2.0.2\jakarta.validation-api-2.0.2.jar;D:\scion\local\repo\org\jboss\logging\jboss-logging\3.4.3.Final\jboss-logging-3.4.3.Final.jar;D:\scion\local\repo\com\fasterxml\classmate\1.5.1\classmate-1.5.1.jar;D:\scion\local\repo\io\projectreactor\addons\reactor-extra\3.4.8\reactor-extra-3.4.8.jar;D:\scion\local\repo\io\projectreactor\reactor-core\3.4.24\reactor-core-3.4.24.jar;D:\scion\local\repo\org\reactivestreams\reactive-streams\1.0.4\reactive-streams-1.0.4.jar;D:\scion\local\repo\org\springframework\boot\spring-boot-starter-webflux\2.6.13\spring-boot-starter-webflux-2.6.13.jar;D:\scion\local\repo\org\springframework\boot\spring-boot-starter-json\2.6.13\spring-boot-starter-json-2.6.13.jar;D:\scion\local\repo\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.13.4\jackson-datatype-jdk8-2.13.4.jar;D:\scion\local\repo\com\fasterxml\jackson\module\jackson-module-parameter-names\2.13.4\jackson-module-parameter-names-2.13.4.jar;D:\scion\local\repo\org\springframework\boot\spring-boot-starter-reactor-netty\2.6.13\spring-boot-starter-reactor-netty-2.6.13.jar;D:\scion\local\repo\io\projectreactor\netty\reactor-netty-http\1.0.24\reactor-netty-http-1.0.24.jar;D:\scion\local\repo\io\netty\netty-codec-http\4.1.84.Final\netty-codec-http-4.1.84.Final.jar;D:\scion\local\repo\io\netty\netty-common\4.1.84.Final\netty-common-4.1.84.Final.jar;D:\scion\local\repo\io\netty\netty-buffer\4.1.84.Final\netty-buffer-4.1.84.Final.jar;D:\scion\local\repo\io\netty\netty-transport\4.1.84.Final\netty-transport-4.1.84.Final.jar;D:\scion\local\repo\io\netty\netty-codec\4.1.84.Final\netty-codec-4.1.84.Final.jar;D:\scion\local\repo\io\netty\netty-handler\4.1.84.Final\netty-handler-4.1.84.Final.jar;D:\scion\local\repo\io\netty\netty-codec-http2\4.1.84.Final\netty-codec-http2-4.1.84.Final.jar;D:\scion\local\repo\io\netty\netty-resolver-dns\4.1.84.Final\netty-resolver-dns-4.1.84.Final.jar;D:\scion\local\repo\io\netty\netty-resolver\4.1.84.Final\netty-resolver-4.1.84.Final.jar;D:\scion\local\repo\io\netty\netty-codec-dns\4.1.84.Final\netty-codec-dns-4.1.84.Final.jar;D:\scion\local\repo\io\netty\netty-resolver-dns-native-macos\4.1.84.Final\netty-resolver-dns-native-macos-4.1.84.Final-osx-x86_64.jar;D:\scion\local\repo\io\netty\netty-resolver-dns-classes-macos\4.1.84.Final\netty-resolver-dns-classes-macos-4.1.84.Final.jar;D:\scion\local\repo\io\netty\netty-transport-native-epoll\4.1.84.Final\netty-transport-native-epoll-4.1.84.Final-linux-x86_64.jar;D:\scion\local\repo\io\netty\netty-transport-native-unix-common\4.1.84.Final\netty-transport-native-unix-common-4.1.84.Final.jar;D:\scion\local\repo\io\netty\netty-transport-classes-epoll\4.1.84.Final\netty-transport-classes-epoll-4.1.84.Final.jar;D:\scion\local\repo\io\projectreactor\netty\reactor-netty-core\1.0.24\reactor-netty-core-1.0.24.jar;D:\scion\local\repo\io\netty\netty-handler-proxy\4.1.84.Final\netty-handler-proxy-4.1.84.Final.jar;D:\scion\local\repo\io\netty\netty-codec-socks\4.1.84.Final\netty-codec-socks-4.1.84.Final.jar;D:\scion\local\repo\org\springframework\spring-web\5.3.23\spring-web-5.3.23.jar;D:\scion\local\repo\org\springframework\spring-beans\5.3.23\spring-beans-5.3.23.jar;D:\scion\local\repo\org\springframework\spring-webflux\5.3.23\spring-webflux-5.3.23.jar;D:\scion\local\repo\org\springframework\boot\spring-boot-starter-actuator\2.6.13\spring-boot-starter-actuator-2.6.13.jar;D:\scion\local\repo\org\springframework\boot\spring-boot-starter\2.6.13\spring-boot-starter-2.6.13.jar;D:\scion\local\repo\org\springframework\boot\spring-boot\2.6.13\spring-boot-2.6.13.jar;D:\scion\local\repo\org\springframework\spring-context\5.3.23\spring-context-5.3.23.jar;D:\scion\local\repo\org\springframework\spring-aop\5.3.23\spring-aop-5.3.23.jar;D:\scion\local\repo\org\springframework\spring-expression\5.3.23\spring-expression-5.3.23.jar;D:\scion\local\repo\org\springframework\boot\spring-boot-autoconfigure\2.6.13\spring-boot-autoconfigure-2.6.13.jar;D:\scion\local\repo\org\springframework\boot\spring-boot-starter-logging\2.6.13\spring-boot-starter-logging-2.6.13.jar;D:\scion\local\repo\ch\qos\logback\logback-classic\1.2.11\logback-classic-1.2.11.jar;D:\scion\local\repo\ch\qos\logback\logback-core\1.2.11\logback-core-1.2.11.jar;D:\scion\local\repo\org\apache\logging\log4j\log4j-to-slf4j\2.17.2\log4j-to-slf4j-2.17.2.jar;D:\scion\local\repo\org\apache\logging\log4j\log4j-api\2.17.2\log4j-api-2.17.2.jar;D:\scion\local\repo\org\slf4j\jul-to-slf4j\1.7.36\jul-to-slf4j-1.7.36.jar;D:\scion\local\repo\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;D:\scion\local\repo\org\springframework\spring-core\5.3.23\spring-core-5.3.23.jar;D:\scion\local\repo\org\springframework\spring-jcl\5.3.23\spring-jcl-5.3.23.jar;D:\scion\local\repo\org\yaml\snakeyaml\1.29\snakeyaml-1.29.jar;D:\scion\local\repo\org\springframework\boot\spring-boot-actuator-autoconfigure\2.6.13\spring-boot-actuator-autoconfigure-2.6.13.jar;D:\scion\local\repo\org\springframework\boot\spring-boot-actuator\2.6.13\spring-boot-actuator-2.6.13.jar;D:\scion\local\repo\com\fasterxml\jackson\core\jackson-databind\2.13.4.2\jackson-databind-2.13.4.2.jar;D:\scion\local\repo\com\fasterxml\jackson\core\jackson-annotations\2.13.4\jackson-annotations-2.13.4.jar;D:\scion\local\repo\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.13.4\jackson-datatype-jsr310-2.13.4.jar;D:\scion\local\repo\io\micrometer\micrometer-core\1.8.11\micrometer-core-1.8.11.jar;D:\scion\local\repo\org\hdrhistogram\HdrHistogram\2.1.12\HdrHistogram-2.1.12.jar;D:\scion\local\repo\org\latencyutils\LatencyUtils\2.0.3\LatencyUtils-2.0.3.jar;D:\scion\local\repo\com\alibaba\cloud\spring-cloud-starter-alibaba-nacos-config\2021.0.4.0\spring-cloud-starter-alibaba-nacos-config-2021.0.4.0.jar;D:\scion\local\repo\com\alibaba\cloud\spring-cloud-alibaba-commons\2021.0.4.0\spring-cloud-alibaba-commons-2021.0.4.0.jar;D:\scion\local\repo\com\alibaba\spring\spring-context-support\1.0.11\spring-context-support-1.0.11.jar;D:\scion\local\repo\com\alibaba\nacos\nacos-client\2.0.4\nacos-client-2.0.4.jar;D:\scion\local\repo\commons-codec\commons-codec\1.15\commons-codec-1.15.jar;D:\scion\local\repo\com\fasterxml\jackson\core\jackson-core\2.13.4\jackson-core-2.13.4.jar;D:\scion\local\repo\org\apache\httpcomponents\httpasyncclient\4.1.5\httpasyncclient-4.1.5.jar;D:\scion\local\repo\org\apache\httpcomponents\httpcore\4.4.15\httpcore-4.4.15.jar;D:\scion\local\repo\org\apache\httpcomponents\httpcore-nio\4.4.15\httpcore-nio-4.4.15.jar;D:\scion\local\repo\org\apache\httpcomponents\httpclient\4.5.13\httpclient-4.5.13.jar;D:\scion\local\repo\org\reflections\reflections\0.9.11\reflections-0.9.11.jar;D:\scion\local\repo\com\google\guava\guava\20.0\guava-20.0.jar;D:\scion\local\repo\org\javassist\javassist\3.21.0-GA\javassist-3.21.0-GA.jar;D:\scion\local\repo\io\prometheus\simpleclient\0.12.0\simpleclient-0.12.0.jar;D:\scion\local\repo\io\prometheus\simpleclient_tracer_otel\0.12.0\simpleclient_tracer_otel-0.12.0.jar;D:\scion\local\repo\io\prometheus\simpleclient_tracer_common\0.12.0\simpleclient_tracer_common-0.12.0.jar;D:\scion\local\repo\io\prometheus\simpleclient_tracer_otel_agent\0.12.0\simpleclient_tracer_otel_agent-0.12.0.jar;D:\scion\local\repo\org\springframework\cloud\spring-cloud-commons\3.1.5\spring-cloud-commons-3.1.5.jar;D:\scion\local\repo\org\springframework\security\spring-security-crypto\5.6.8\spring-security-crypto-5.6.8.jar;D:\scion\local\repo\org\springframework\cloud\spring-cloud-context\3.1.5\spring-cloud-context-3.1.5.jar;D:\scion\local\repo\org\slf4j\slf4j-api\1.7.36\slf4j-api-1.7.36.jar;D:\scion\IntelliJ IDEA 2021.3.3\lib\idea_rt.jar" com.firegy.GatewayApplicationStarter
Connected to the target VM, address: '127.0.0.1:61719', transport: 'socket'
10:00:37.724 [main] DEBUG org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter - Application failed to start due to an exception
org.springframework.cloud.commons.ConfigDataMissingEnvironmentPostProcessor$ImportException: No spring.config.import set
at org.springframework.cloud.commons.ConfigDataMissingEnvironmentPostProcessor.postProcessEnvironment(ConfigDataMissingEnvironmentPostProcessor.java:82)
at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEnvironmentPreparedEvent(EnvironmentPostProcessorApplicationListener.java:102)
at org.springframework.boot.env.EnvironmentPostProcessorApplicationListener.onApplicationEvent(EnvironmentPostProcessorApplicationListener.java:87)
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131)
at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:85)
at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:66)
at java.util.ArrayList.forEach(ArrayList.java:1257)
at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:120)
at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:114)
at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:65)
at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:343)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:301)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1317)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306)
at com.firegy.GatewayApplicationStarter.main(GatewayApplicationStarter.java:13)
10:00:37.727 [main] ERROR org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter -
***************************
APPLICATION FAILED TO START
***************************
Description:
No spring.config.import property has been defined
Action:
Add a spring.config.import=nacos: property to your configuration.
If configuration is not required add spring.config.import=optional:nacos: instead.
To disable this check, set spring.cloud.nacos.config.import-check.enabled=false.
Disconnected from the target VM, address: '127.0.0.1:61719', transport: 'socket'
Process finished with exit code 1
這裡因為在SpringCloud的common元件中沒有加載配置系統啟動的相關配置,需要添加依賴解決:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>