Spring Cloud配置服务器无法使用本地属性文件

24

我一直在github上尝试Spring Cloud项目,它位于这里:https://github.com/spring-cloud/spring-cloud-config

然而,在将属性从github中拉取改为读取本地属性文件时,遇到了一些问题。即使我删除了所有对github的引用,spring似乎仍然忽略本地文件。这里有一个类似的问题:Spring-Cloud configuration server ignores configuration properties file

但是我还没有看到任何好的答案。我想知道是否有人可以向我指出一个此类示例?我想在本地设置我的属性,而不使用任何类型的git repo。我觉得肯定有人遇到过这种情况,如果有这方面的示例,我真的很想看到它,以便我能朝着正确的方向前进。


2
你尝试过使用 spring.profiles.active=native 运行它吗?(应该适用于1.0.0.M3或快照版本)? - Dave Syer
1
是的,我尝试添加了那个,但是没有什么运气。 - user3270760
在 application.properties 文件中添加 spring.profiles.active=native 后,它对我有效。 - wmfairuz
11个回答

25

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

src/main/java/Application.java

@Configuration
@EnableAutoConfiguration
@EnableConfigServer
public class Application {

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

src/main/resources/application.yml

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

my:
  property: myvalue

src/main/resources/myapp.yml

my:
  otherprop: myotherval

要获取名为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"
                }
          }
     ]
}

1
太酷了!它运行得非常好!谢谢!我的下一个问题是:为了获取特定的值,您可以在application.yml或类似这样的属性文件中指定它:foo=bar,然后使用一些curl命令获取foo的值吗?例如 curl http://localhost:8080/myapp/foo - user3270760
3
目前个别值在HTTP API中不可用。如果您在Spring应用环境中使用spring-cloud-config-client,则可以通过http://localhost:8080/myapp-default.ymlhttp://localhost:8080/myapp-default.properties分别作为YAML或Java属性文档获得这些值。请注意,这是翻译后的内容,不包含任何解释或其他信息。 - spencergibb
2
目前,使用Spring Boot 2.x时,您将无法在响应中获取application.properties。根据Spring Cloud Config的文档:“searchLocations的默认值与本地Spring Boot应用程序相同(...)这不会将服务器上存在的任何属性源公开给所有客户端,因为在发送到客户端之前,服务器上存在的任何属性源都将被删除。” - BartoszMiller

13
我能够使用Spring配置服务器读取苹果服务(测试微服务)的配置。
Spring配置应用程序的示例application.yml:
spring:
    profiles:
        active: native
    cloud:
        config:
            server:
                native:
                    searchLocations: classpath:config/
server:
  port: 8888


endpoints:
    restart:
      enabled: true

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

例如,如果spring.application.name=apple-service,则属性文件应该是src \ main \ resources \ config 文件夹中的apple-service.properties

apple-service的示例bootstrap.yml

spring:
  application:
    name: apple-service

cloud:
  config:
    uri: http://localhost:8888

4

以下是我所做的:

遵循https://medium.com/@danismaz.furkan/spring-cloud-config-with-file-system-backend-c18ae16b7ad5

1) !!不要忘记你的VM选项!!:

-Dspring.profiles.active=native

(在Netbeans中:configserver->project->properties>Run->VM选项)

2)或者在application.properties文件中

#spring.cloud.config.server.native.searchLocations=file:///C:/tmp/config

spring.cloud.config.server.native.searchLocations=classpath:/config

或者 applications.yml
spring:

    cloud:

         config:

            server:

                native:

                    search-locations  : file:///C:/tmp/config
                    #search-locations : classpath:/config

注意事项:

n1:search-locations和searchLocations都可以使用。

n2:file:/// => 表示硬盘文件路径。

示例:

c:/temp/config/
           app-admin.yml
           app-admin-develop.yml
           .... 

n3:针对您的档案配置文件
类路径:/
例如
Other sources/src/main/resources/config/app-admin.yml

n4: 如果没有设置vm选项,我无法使文件路径工作。 不要忘记!仅在应用程序配置中设置spring.profiles.active = native是不够的。

n5: http查询示例

http://localhost:8998/app-admin/default/yml

返回app-admin.yml

http://localhost:8998/app-admin/develop/yml

返回app-admin-develop.yml


3

使用spring.profiles.active=native是Spring文档建议的方法,但是我也无法使其正常工作。我的application.properties文件如下:

server.port=8888
spring.cloud.config.profiles=native 

但是来自URL的响应

http://localhost:8888/config-server/env

is

{"name":"env","label":"master","propertySources":[{"name":"https://github.com/spring-cloud-samples/config-repo/application.yml","source":{"info.url":"https://github.com/spring-cloud-samples","info.description":"Spring Cloud Samples"}}]}

这段话的意思是,原生配置被忽略了,服务器仍然将github视为属性源。另外一个小问题是,配置服务默认端口应该是8888,但如果我从application.properties中移除server.port=8888,配置服务器将会在8080端口启动,这是Spring Boot的默认端口,而不是配置服务器应该使用的端口。

1
你找到解决方案了吗?端口和本地配置文件的问题。 - Kumar Sambhav
你可以在bootstrap.properties中添加spring.profiles.active=native并将其放置在src/main/resources目录下,或者在启动Spring Boot时使用以下命令:mvn spring-boot:run -Drun.profiles=native。现在试一试吧... - Sal Prima
嗨,它运行良好,但是在客户端应用程序中使用@Value(“${info.description}”)进行注入时,它无法工作,会出现“无法解析占位符' message 'in value "${info.description}"”的错误。我没有使用文件系统。 - Rajanikanta Pradhan

3
我在Mac OS环境下运行配置服务器时遇到了同样的问题,但在Linux或Windows中没有发生这种情况。
我在bootstrap.yml文件中设置了本地属性,如下所示:
spring:
  profiles:
    active: native

最终在 Mac 上对我有效的方法是将活动 profile 传递给 jar 文件,如下所示。

java -jar config-server.jar --spring.profiles.active=native

我仍然不知道为什么它在Mac OS上的表现不同。


3

如果配置服务器中的application.properties包含以下内容,配置服务器将读取本地属性文件:

spring.profiles.active=native
**spring.cloud.config.server.native.searchLocations=file:/source/tmp**

在位于 /source/tmp 目录下,您可以存储客户端的本地属性文件,例如:
http://localhost:8888/a-bootiful-client/default

您将获得:

{"name":"a-bootiful-client","profiles":["default"],"label":null,"version":null,"state":null,"propertySources":[{"name":"file:/source/tmp/a-bootiful-client.properties","source":{"message":"Kim"}}]}

为什么计数是“-1”? - Kim Huang

3

当文件路径中含有空格时,我在Mac上遇到了这个问题:

spring.cloud.config.server.native.search-locations=file:///Users/.../Development/Folder with a space /prj

还要注意,在用户之前有三个斜杠:

spring.cloud.config.server.native.search-locations=file:///Users/...

我使用以下方法:

spring.cloud.config.server.native.search-locations=file://${user.home}/Desktop

native 属性是:

spring.profiles.active=native

1
我能够通过本地存储库和引导配置使其工作:
java -jar spring-cloud-config-server-1.0.0.M3-exec.jar --spring.config.name=bootstrap

bootstrap.yml文件位于./config/文件夹中。

server:
  port: 8080
spring:
  config:
    name: cfg_server
  cloud:
    config:
      server:
       git:
        uri: /home/us/config_repo
        searchPaths: pmsvc,shpsvc

1

我在本地机器上遇到了同样的问题,但在远程服务器上运行良好。

@Oreste的答案对我有效。因此,我再次检查了错误日志,并发现

 2018-06-25 16:09:49.789  INFO 4397 --- [           main] t.p.a.s.api.user.UserServiceApplication  : The following profiles are active:  someProfileISetBefore

所以,我的问题的根本原因是我之前设置了一个环境变量,但是我忘了删除它。这个环境变量覆盖了应用程序属性文件的配置。
希望你们不会像我一样犯这种愚蠢的错误。解决方法如下:
 unset spring_profiles_active 

0

我添加了以下两个属性,它开始工作了:

spring.cloud.config.server.git.default-label=main

spring.cloud.config.server.git.try-master-branch=true


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接