我成功地访问了我的配置服务器的属性文件一次。我的配置似乎发生了一些变化,我无法重复......
此时我已经绝望了。两天以来我一直在努力解决这个问题,但我的心理健康开始受到影响。
我的pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demos</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demos</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
<spring-cloud.version>2020.0.0</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
最后是项目中唯一的课程
package com.example.demos;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class DemosApplication {
public static void main(String[] args) {
SpringApplication.run(DemosApplication.class, args);
}
}
因为我没有使用 git,而是使用本地文件,所以我的资源文件夹中的 application.properties 文件如下所示:
server.port=8888
spring.profiles.active=native
spring.cloud.config.server.native.searchLocations=/home/leobuilt/IdeaProjects/demos/src/main/resources/config
management.security.enabled=false
指向我的配置所在的本地文件夹
这是名为ever.properties的配置文件:
server=Hello world
整个项目结构:
请问...通过浏览器访问此文件的属性的正确 URL 是什么?
我发现了几个教程,人们以这种方式访问属性文件。 即使我也成功地做到了一次,如下所示。(在这个项目中该文件被命名为 newsservice-config.properties,但配置是相同的)
任何人都可以在不使用任何 git 的情况下解释这一切背后的一些逻辑吗?
我认为您需要编写代码来列出您的属性,然后在网页上显示此方法返回的数据。我们使用以下方法来做到这一点:
private Map<String, Map<String, String>> getEnvironmentPropertyGroups() {
MutablePropertySources propertySources = ((AbstractEnvironment) env).getPropertySources();
Map<String, Map<String, String>> propertyGroups = new LinkedHashMap<>();
propertySources.forEach(propertySource -> {
if (propertySource instanceof EnumerablePropertySource) {
Map<String, String> properties = new LinkedHashMap<>();
EnumerablePropertySource enumerablePropertySource = (EnumerablePropertySource) propertySource;
for (String key : enumerablePropertySource.getPropertyNames()) {
Object value = enumerablePropertySource.getProperty(key);
properties.put(key, getValueForKey(key, value));
}
propertyGroups.put(propertySource.getName(), properties);
}
});
return propertyGroups;
}
希望您的应用程序能够在您的 spring 依赖项下正常运行。如果是的话,
spring.cloud.config.server.native.searchLocations=/home/leobuilt/IdeaProjects/demos/src/main/resources/config
请检查以上路径是否正确。由于您的资源文件夹中的配置请尝试以下,
spring.cloud.config.server.native.search-locations=classpath:/config
or
spring.cloud.config.server.native.searchLocations=classpath:/config
如果您的应用程序运行良好,可以通过
http://localhost:8888/ever.properties
访问值
在
application.properties
文件中定义以下属性就足够了
server.port=8888
spring.profiles.active=native
spring.cloud.config.server.native.search-locations=classpath:/config