Pav*_*llo 6 java spring-boot spring-cloud
我完全陷入了配置客户端无法从配置文件服务器获取配置的问题。我正在尝试使用 spring.config.import 通过“新方式”来做到这一点,但看起来我错过了一些东西。
我将属性文件存储在私有 github 存储库中。这是我的配置文件服务器:application.properties:
spring.application.name=config-server
server.port=8888
spring.cloud.config.server.git.uri=https://github.com/Pavello/simple-microservices-app-config-files-repo
spring.cloud.config.server.git.username=//My username
spring.cloud.config.server.git.password=//Here I am using my private token
spring.cloud.config.server.git.default-label=master
management.endpoints.web.exposure.include=busrefresh
spring.rabbitmq.host=localhost
spring.rabbitmq.user=guest
spring.rabbitmq.password=guest
spring.rabbitmq.port=5672
Run Code Online (Sandbox Code Playgroud)
pom.xml:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-monitor</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我的客户端应用程序如下所示:application.properties:
server.port=${PORT:0}
spring.application.name=user-service
eureka.client.serviceUrl.defaultZone=http://localhost:8010/eureka
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.application.name}:${spring.application.instance_id:${random.value}}
spring.h2.console.enabled=true
spring.h2.console.settings.web-allow-others=true
spring.config.import=optional:configserver:http://localhost:8888
management.endpoints.web.exposure.include=*
Run Code Online (Sandbox Code Playgroud)
pom.xml 依赖项:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-monitor</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
运行我的配置文件服务器后,我在 /user-service/default 下收到以下响应:
{
"name": "user-service",
"profiles": [
"default"
],
"label": null,
"version": "889d16cb37b5467ed4546d617fc5ba8280c96f2d",
"state": null,
"propertySources": [
{
"name": "https://github.com/Pavello/simple-microservices-app-config-files-repo/file:C:\\Users\\PAWEL~1.KAN\\AppData\\Local\\Temp\\config-repo-15000982762999853489\\user-service.properties",
"source": {
"login.url.path": "/users/login",
"jwt.expiration": "864002324",
"jwt.key": "passwordpasswordpasswordblabla",
"logging.level.org.springframework.web.filter.CommonsRequestLoggingFilter": "DEBUG",
"spring.datasource.url": "jdbc:postgresql://127.0.0.1:5432/postgres",
"spring.datasource.driver-class-name": "org.postgresql.Driver",
"spring.datasource.jdbc-url": "jdbc:postgresql://127.0.0.1:5432/postgres",
"spring.datasource.username": "postgres",
"spring.datasource.password": "postgres",
"spring.datasource.initialization-mode": "always"
}
},
{
"name": "https://github.com/Pavello/simple-microservices-app-config-files-repo/file:C:\\Users\\PAWEL~1.KAN\\AppData\\Local\\Temp\\config-repo-15000982762999853489\\application.properties",
"source": {
"login.url.path": "/users/login",
"jwt.expiration": "864002324",
"jwt.key": "passwordpasswordpasswordblabla",
"logging.level.org.springframework.web.filter.CommonsRequestLoggingFilter": "DEBUG",
"spring.datasource.url": "jdbc:postgresql://127.0.0.1:5432/postgres",
"spring.datasource.driver-class-name": "org.postgresql.Driver",
"spring.datasource.jdbc-url": "jdbc:postgresql://127.0.0.1:5432/postgres",
"spring.datasource.username": "postgres",
"spring.datasource.password": "postgres",
"spring.datasource.initialization-mode": "always"
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
所以看起来它正在正确获取配置,但在客户端我没有这些值,并且收到此错误:
java.lang.IllegalStateException: Unable to load config data from 'optional:configserver:http://localhost:8888'
at org.springframework.boot.context.config.StandardConfigDataLocationResolver.getReferences(StandardConfigDataLocationResolver.java:141)
at org.springframework.boot.context.config.StandardConfigDataLocationResolver.getReferences(StandardConfigDataLocationResolver.java:126)
at org.springframework.boot.context.config.StandardConfigDataLocationResolver.resolve(StandardConfigDataLocationResolver.java:119)
at org.springframework.boot.context.config.ConfigDataLocationResolvers.lambda$resolve$1(ConfigDataLocationResolvers.java:115)
at org.springframework.boot.context.config.ConfigDataLocationResolvers.resolve(ConfigDataLocationResolvers.java:126)
at org.springframework.boot.context.config.ConfigDataLocationResolvers.resolve(ConfigDataLocationResolvers.java:115)
at org.springframework.boot.context.config.ConfigDataLocationResolvers.resolve(ConfigDataLocationResolvers.java:107)
at org.springframework.boot.context.config.ConfigDataImporter.resolve(ConfigDataImporter.java:105)
at org.springframework.boot.context.config.ConfigDataImporter.resolve(ConfigDataImporter.java:97)
at org.springframework.boot.context.config.ConfigDataImporter.resolveAndLoad(ConfigDataImporter.java:85)
at org.springframework.boot.context.config.ConfigDataEnvironmentContributors.withProcessedImports(ConfigDataEnvironmentContributors.java:121)
at org.springframework.boot.context.config.ConfigDataEnvironment.processInitial(ConfigDataEnvironment.java:240)
at org.springframework.boot.context.config.ConfigDataEnvironment.processAndApply(ConfigDataEnvironment.java:227)
at org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor.postProcessEnvironment(ConfigDataEnvironmentPostProcessor.java:102)
at org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor.postProcessEnvironment(ConfigDataEnvironmentPostProcessor.java:94)
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:82)
at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:63)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:117)
at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:111)
at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:62)
at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:374)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:332)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332)
at io.github.pavello.userservice.UserServiceApplication.main(UserServiceApplication.java:13)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.IllegalStateException: File extension is not known to any PropertySourceLoader. If the location is meant to reference a directory, it must end in '/' or File.separator
at org.springframework.boot.context.config.StandardConfigDataLocationResolver.getReferencesForFile(StandardConfigDataLocationResolver.java:229)
at org.springframework.boot.context.config.StandardConfigDataLocationResolver.getReferences(StandardConfigDataLocationResolver.java:138)
... 36 common frames omitted
Run Code Online (Sandbox Code Playgroud)
我读过一些与 Spring Boot 和 Spring Cloud 版本不匹配相关的类似问题,但我想我正在使用 Spring Boot (2.5.6) 和 Spring Cloud (2020.0.4) 的最新(目前)版本)。我尝试将 spring cloud bootstrap 依赖项添加到我的用户服务中(尽管我认为没有必要)(就像建议的其他一些问题一样):
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.0.4</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
但这也没有帮助。有人可以帮忙吗?