Spring Cloud Configuration Server无法使用本地属性文件

use*_*760 14 java spring spring-cloud

我一直在github上玩Spring Cloud项目:https://github.com/spring-cloud/spring-cloud-config

但是我遇到了一些问题,让它读取本地属性文件而不是从github中提取属性.即使我删除了对github的所有引用,spring似乎忽略了本地文件.此处发布了类似的问题:Spring-Cloud配置服务器忽略配置属性文件

但我还没有看到任何好的答案.我想知道是否有人能指出我的一个例子?我想在本地设置我的属性,而不是使用任何类型的git repo.我假设有人之前遇到过这种情况,如果有某个例子,我真的很想看到它,这样我才能朝着正确的方向前进.

spe*_*ibb 17

我所有的代码都在这里https://github.com/spencergibb/communityanswers/tree/so27131143

的src /主/爪哇/ Application.java

@Configuration
@EnableAutoConfiguration
@EnableConfigServer
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

SRC /主/资源/ application.yml

spring:
  application:
     name: myconfigserver
  profiles:
     active: native

my:
  property: myvalue
Run Code Online (Sandbox Code Playgroud)

SRC /主/资源/ myapp.yml

my:
  otherprop: myotherval
Run Code Online (Sandbox Code Playgroud)

要获取已命名应用程序的属性myapp,请执行以下操作.

curl http://localhost:8080/myapp/default

{
     "name": "default",
     "label": "master",
     "propertySources": [
          {
                "name": "applicationConfig: [classpath:/myapp.yml]",
                "source": {
                     "my.otherprop": "myotherval"
                }
          },
          {
                "name": "applicationConfig: [classpath:/application.yml]",
                "source": {
                     "spring.application.name": "myconfigserver",
                     "spring.profiles.active": "native",
                     "my.property": "myvalue"
                }
          }
     ]
}
Run Code Online (Sandbox Code Playgroud)

  • 目前,无法通过http api获得单个值。如果您在Spring应用程序环境中使用`spring-cloud-config-client`。这些值可分别通过http:// localhost:8080 / myapp-default.yml和http:// localhost:8080 / myapp-default.properties作为YAML或Java属性文档使用。 (2认同)
  • 当前,在Spring Boot 2.x中,您不会在响应中获得application.properties。根据Spring Cloud Config的文档:“ searchLocations的默认值与本地Spring Boot应用程序(...)相同。这不会将application.properties从服务器显示给所有客户端,因为服务器先删除,然后再发送给客户端。” (2认同)

ris*_*690 7

我能够使用Spring配置服务器读取apple-service(测试微服务)的配置.

实施例application.yml弹簧配置应用程序的

spring:
    profiles:
        active: native
    cloud:
        config:
            server:
                native:
                    searchLocations: classpath:config/
server:
  port: 8888


endpoints:
    restart:
      enabled: true
Run Code Online (Sandbox Code Playgroud)

将.properties或.yml文件放在src\main\resources\config文件夹中.确保此文件的名称应与 您的微服务的spring.application.name匹配.

例如,如果spring.application.name =苹果服务,然后属性文件应该是apple-service.propertiesSRC \主\资源\ CONFIG文件夹中.

apple-service的bootstrap.yml示例:

spring:
  application:
    name: apple-service

cloud:
  config:
    uri: http://localhost:8888
Run Code Online (Sandbox Code Playgroud)