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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27131143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:51:48  来源:igfitidea点击:

Spring Cloud Configuration Server not working with local properties file

javaspringspring-cloud

提问by user3270760

I have been playing around with the Spring Cloud project on github located here: https://github.com/spring-cloud/spring-cloud-config

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

However I have been running into some problems getting it to read a local properties file instead of pulling the properties from github. It seems that spring is ignoring the local file even when I remove all the references to github. There is a similar question posted here: Spring-Cloud configuration server ignores configuration properties file

但是,我遇到了一些问题,让它读取本地属性文件而不是从 github 中提取属性。即使我删除了对 github 的所有引用,spring 似乎也忽略了本地文件。这里有一个类似的问题:Spring-Cloud configuration server ignores configuration properties file

But I haven't seen any good answers yet. I'm wondering if anyone can point me to an example of this? I'd like to set my properties locally instead of using a git repo of any kind. I assume someone has encountered this before, and if there is an example of it somewhere, I'd really like to see it so that I can get moving in the right direction.

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

采纳答案by spencergibb

All my code is here https://github.com/spencergibb/communityanswers/tree/so27131143

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

src/main/java/Application.java

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

src/main/resources/application.yml

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

my:
  property: myvalue

src/main/resources/myapp.yml

src/main/resources/myapp.yml

my:
  otherprop: myotherval

To get the properties for an app named myapp, do the following.

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

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

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"
                }
          }
     ]
}

回答by MrkK

Using spring.profiles.active=native is that what Spring documentation seems to suggest, but I couldn't get it to work either. My application.properties file is

使用 spring.profiles.active=native 是 Spring 文档似乎建议的,但我也无法让它工作。我的 application.properties 文件是

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

but the response from the URL

但是来自 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"}}]}

which indicates that native profile was ignored and the server still considering github as property source.

这表明本机配置文件被忽略,服务器仍然将 github 作为属性源。

A small additional problem I encountered is the config service default port. According to the Sprin Cloud Config documentation it should be 8888. If I remove server.port=8888 from my application.properties the config server starts on port 8080 which is default Spring Boot port, but not the one config server should use.

我遇到的一个小问题是配置服务默认端口。根据 Sprin Cloud Config 文档,它应该是 8888。如果我从 application.properties 中删除 server.port=8888,则配置服务器在端口 8080 上启动,这是默认的 Spring Boot 端口,但不是一个配置服务器应该使用的端口。

回答by Genadyk

I was able to get it work with local repo and bootstrap configuration:

我能够使用本地存储库和引导程序配置使其工作:

java -jar spring-cloud-config-server-1.0.0.M3-exec.jar --spring.config.name=bootstrap

The bootstrap.yml file is placed in ./config/ folder.

bootstrap.yml 文件放在 ./config/ 文件夹中。

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

回答by Oreste

I had the same problem when running the Configuration Server in Mac OS environment. This did not happen in Linux or Windows.

我在 Mac OS 环境中运行配置服务器时遇到了同样的问题。这在 Linux 或 Windows 中没有发生。

I had the native property set in the bootstrap.ymlfile like this:

我在bootstrap.yml文件中设置了本机属性,如下所示:

spring:
  profiles:
    active: native

Finally the way it worked for me on the mac was to pass the active profile to the jar file, like this.

最后,它在 mac 上为我工作的方式是将活动配置文件传递给 jar 文件,就像这样。

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

I still don't know why it behaves differently in Mac OS.

我仍然不知道为什么它在 Mac OS 中的行为不同。

回答by rish1690

I am able to read configuration for apple-service(Test Micro Service) using Spring config server.

我能够使用 Spring 配置服务器读取苹果服务(测试微服务)的配置。

Example application.ymlof spring config application

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

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


endpoints:
    restart:
      enabled: true

Put your .properties or .yml files inside src\main\resources\config folder. Make sure name of this files should match spring.application.nameof your micro service.

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

For example if spring.application.name=apple-servicethen property file should be apple-service.propertiesin src\main\resources\configfolder.

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

Example bootstrap.ymlof apple-service:

苹果服务的bootstrap.yml示例:

spring:
  application:
    name: apple-service

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

回答by Kim Huang

The config server will read the local properties files if the application.propertiesof config server contains:

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

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

at /source/tmpdirectory, you store the local property file for client, for example:

/source/tmp目录中,您存储客户端的本地属性文件,例如:

http://localhost:8888/a-bootiful-client/default

you will get:

你会得到:

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

回答by EdwinSZ

I had the same issue on my local machine, but it works fine on my remote server.

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

@Oreste's answer does work for me. So I checked the error log again, And I found

@Oreste 的回答对我有用。所以我再次检查了错误日志,我发现

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

So, the root reason for my case is I set an environment variable before, but I forget to remove it. And it overrides the application properties files config.

所以,我的情况的根本原因是我之前设置了一个环境变量,但我忘记删除它。它会覆盖应用程序属性文件配置。

Hope you guys won't make the silly mistake like me. Fixed by:

希望大家不要像我一样犯这种愚蠢的错误。修正者:

 unset spring_profiles_active 

回答by Kirill Ch

I've had this issue on Mac when there is a spacein a path to a file:

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

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

Also pay attention that there are 3 slashes before Users:

还要注意Users前有 3 个斜杠:

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

or I use:

或者我使用:

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

The nativeproperty is:

原生属性是:

spring.profiles.active=native

回答by Roland Roos

Here is what I did:

这是我所做的:

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

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

1) !!dont forget your VM options!!:

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

-Dspring.profiles.active=native

(in Netbeans : configserver->project->properties>Run->VM options

(在 Netbeans 中:configserver->project->properties>Run->VM options

2) Either in application.properties

2) 要么在 application.properties 中

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

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

or applications.yml

或应用程序.yml

spring:

    cloud:

         config:

            server:

                native:

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

Notes:

笔记:

n1: search-locations and searchLocations both work

n1:search-locations 和 searchLocations 都有效

n2: file:/// => hard file path

n2: file:/// => 硬文件路径

Like

喜欢

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

n3: for your profile config files

n3:用于您的配置文件配置文件

classpath:/

类路径:/

Like

喜欢

Other sources/src/main/resources/config/app-admin.yml

n4: I couldnt made file paths work without setting the vm options. Dont forget! Just setting spring.profiles.active=native in your application config does not do the trick for me

n4:我无法在不设置 vm 选项的情况下使文件路径工作。不要忘记!只是在你的应用程序配置中设置 spring.profiles.active=native 对我来说不起作用

n5: example http queries

n5:示例 http 查询

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

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

gives app-admin.yml

给 app-admin.yml

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

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

gives app-admin-develop.yml

给 app-admin-develop.yml