Spring 使用本地存储库的云配置服务器配置

Spring Cloud Config Server configuration with local repository

我正在尝试使用后端存储库(文件系统)设置 Spring 云配置服务器,但是端点(http://localhost:8888/licensingservice/default)returns 如下:

{"name":"licensingservice","profiles":["default"],"label":null,"version":null,"state":null,"propertySources":[]}

主线:

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

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

application.yml:

server:
   port: 8888
spring:
   profiles:
      active: native
    cloud:
       config:
          server:
             native:
                searchLocations: file:///Users/josedavi/Desenvolvimento/WorkSpace/Pessoal/sample-spring-microservices/sample-spring-microservices/config-server/src/main/resources/config

licensingservice.yml:

tracer.property: "I AM THE DEFAULT"
spring.jpa.database: "POSTGRESQL"
spring.datasource.platform: "postgres"
spring.jpa.show-sql: "true"
spring.database.driverClassName: "org.postgresql.Driver"
spring.datasource.url: "jdbc:postgresql://database:5432/eagle_eye_local"
spring.datasource.username: "postgres"
spring.datasource.password: "p0stgr@s"
spring.datasource.testWhileIdle: "true"
spring.datasource.validationQuery: "SELECT 1"
spring.jpa.properties.hibernate.dialect: "org.hibernate.dialect.PostgreSQLDialect"

服务配置路径:

C:\Users\josedavi\Desenvolvimento\WorkSpace\Pessoal\sample-spring-microservices\sample-spring-microservices\config-server\src\main\resources\config

项目: https://github.com/jdavid-araujo/sample-spring-microservices

在您的 application.yml 配置服务中添加以下格式:

[classpath:/, classpath:/config, classpath:/config/{application}, classpath:/config/{application}/{profile}]

以上格式从 config 文件夹搜索位置,下一个文件夹分别具有 application 名称、application 名称和 profile

spring:
   profiles:
      active: native
   cloud:
       config:
          server:
             native:
                searchLocations: "[classpath:/, classpath:/config, classpath:/config/{application}, classpath:/config/{application}/{profile}]"

看来是你的searchLocations属性出了问题。路径必须到达 licensingservice 文件夹本身,如果服务器为多个服务提供配置,则必须为每个服务设置路径(以逗号分隔)。

试试这个方法:

...
spring:
  ...
  cloud:
    config:
      server:
        native:
          searchLocations: file:///C:/Users/josedavi/Desenvolvimento/WorkSpace/Pessoal/sample-spring-microservices/sample-spring-microservices/config-server/src/main/resources/config/licensingservice

或者,您可以使用相对路径:

        ...
          searchLocations: classpath:config/licensingservice

此外,如果您正在阅读 Spring Microservices in Action book (Chapter 3), you can take a look at the source code example 本身。

config server folder structure

然后在 application.yaml 文件中 云: 配置: 服务器: 本国的: 搜索位置:“[classpath:/, classpath:/config, classpath:/config/{application}, classpath:/config/{application}/{profile}]”

它将完美运行