【问题标题】:EnableConfigServer is not working for native location in spring bootEnableConfigServer 在 Spring Boot 中不适用于本机位置
【发布时间】:2021-01-05 16:56:40
【问题描述】:

我正在尝试在 Spring Boot 中使用 @EnableConfigServer 为我们应用程序中的所有属性配置配置服务器。请看下面的代码:

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }

}

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 http://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.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.cdk.config</groupId>
    <artifactId>configserver</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>configserver</name>
    <description>Contains all the configurations/properties required by all the services</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <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>

</project>

application.properties

spring.application.name=config-server
server.port=9090
spring.cloud.config.server.native.searchLocations=file://Users/Sankest/StarterProjects/MicroServices/AllConfigurations/
spring.profiles.active=native

将所有属性文件复制到:/Users/Sankest/StarterProjects/MicroServices/AllConfigurations/

但是当我尝试访问 http://localhost:9090/config-server/default 的 url 时,我没有看到任何属性文件并得到以下响应:

{"name":"config-server","profiles":["default"],"label":null,"version":null,"state":null,"propertySources":[]}

【问题讨论】:

  • 您在哪个操作系统上运行应用程序?
  • 你可以试试spring.cloud.config.server.native.searchLocations=file:/Users/Sankest/StarterProjects/MicroServices/AllConfigurations吗?我想额外的/ 是问题所在。
  • 我正在使用 mac 并尝试删除额外的 / 。但这没有用
  • 你试过spring.cloud.config.server.native.searchLocations=file:/Users/Sankest/StarterProjects/MicroServices/AllConfigurations吗?在file: 之后只有一个斜线。我也一样。

标签: java spring spring-boot microservices


【解决方案1】:
  1. 正确的值应该是spring.cloud.config.server.native.searchLocations=file:///Users/Sankest/StarterProjects/MicroServices/AllConfigurations/,在file: 后面加上3 个斜线。即使不运行应用程序,验证路径是否正确的一种方法是将路径粘贴到浏览器中并检查它是否显示所有文件。
  2. 对于默认配置文件,请确保文件名是 application.yml 或 application.properties。
  3. 对于其他配置文件,例如dev,文件名应为application-dev.yml 或application-dev.properties(如果都在同一个文件夹中),那么http://localhost:9090/config-server/dev 将同时显示dev 和default 配置文件条目。

【讨论】:

    【解决方案2】:

    我不认为您正在访问正确的端点。

    例如:

    如果您的 AllConfigurations 文件夹中有三个文件。

    文件可以是yml或者properties。

    1. application-dev.yml

    2. application-test.yml

    3. application-prod.yml

    -左侧的名称是应用程序名称,右侧的名称是配置文件。

    所以 application-dev.yml 的端点将是

     http://localhost:9090/application/dev

    从客户端访问配置服务器中的特定配置文件您需要设置活动配置文件。

    如果您的客户端应用程序名称是 test spring.profile.active=dev

    然后它会从配置服务器返回test-dev文件给你的客户端。

    【讨论】:

      【解决方案3】:

      添加依赖项

          <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-config-server</artifactId>
      </dependency>
      

      【讨论】:

        猜你喜欢
        • 2020-11-24
        • 1970-01-01
        • 2019-03-26
        • 1970-01-01
        • 2015-04-13
        • 2018-10-18
        • 2020-09-06
        • 2018-10-30
        • 2018-07-04
        相关资源
        最近更新 更多