简体   繁体   English

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

[英]Spring Cloud Configuration Server not working with local properties file

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 配置服务器忽略配置属性文件

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.我想在本地设置我的属性,而不是使用任何类型的 git 存储库。 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.我假设有人以前遇到过这个,如果某处有这样的例子,我真的很想看看它,这样我就可以朝着正确的方向前进。

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

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

I am able to read configuration for apple-service(Test Micro Service) using Spring config server. 我可以使用Spring配置服务器读取apple-service(Test Micro Service)的配置。

Example application.yml of 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. 将.properties或.yml文件放入src \\ main \\ resources \\ config文件夹中。 Make sure name of this files should match spring.application.name of your micro service. 确保此文件的名称应与您的微服务的spring.application.name匹配。

For example if spring.application.name=apple-service then property file should be apple-service.properties in src\\main\\resources\\config folder. 例如,如果spring.application.name = apple-service,则属性文件应为src \\ main \\ resources \\ config文件夹中的apple-service.properties

Example bootstrap.yml of apple-service: apple-service的示例bootstrap.yml

spring:
  application:
    name: apple-service

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

Using spring.profiles.active=native is that what Spring documentation seems to suggest, but I couldn't get it to work either. 使用spring.profiles.active = native是Spring文档所建议的,但是我也无法使它正常工作。 My application.properties file is 我的application.properties文件是

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

but the response from the 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,则配置服务器将从默认的Spring Boot端口8080启动,但不应使用一个配置服务器。

I had the same problem when running the Configuration Server in Mac OS environment. 在Mac OS环境中运行配置服务器时,我遇到了同样的问题。 This did not happen in Linux or Windows. 在Linux或Windows中没有发生这种情况。

I had the native property set in the bootstrap.yml file 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中表现不同。

The config server will read the local properties files if the application.properties of config server contains: 如果配置服务器的application.properties包含以下内容,则配置服务器将读取本地属性文件:

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

at /source/tmp directory, 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"}}]}

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

I had the same issue on my local machine, but it works fine on my remote server. 我在本地计算机上遇到了同样的问题,但在远程服务器上工作正常。

@Oreste's answer does work for me. @Oreste的答案对我有用。 So I checked the error log again, And I found 所以我再次检查了错误日志,发现

 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. 并且它会覆盖应用程序属性文件config。

Hope you guys won't make the silly mistake like me. 希望你们不要像我一样犯傻的错误。 Fixed by: 固定于:

 unset spring_profiles_active 

I've had this issue on Mac when there is a space in 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 : 另外请注意,在用户之前有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 native property is: 属性是:

spring.profiles.active=native

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选项

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 或Applications.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. n4:我无法在不设置vm选项的情况下使文件路径正常工作。 Dont forget! 不要忘记! Just setting spring.profiles.active=native in your application config does not do the trick for me 仅在您的应用程序配置中设置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

I added following two properties and it stared working:我添加了以下两个属性,它开始工作了:

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

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

I am using spring boot version 2.6.0-SNAPSHOT我使用的是 spring 引导版本 2.6.0-SNAPSHOT

If you are trying to use a local repo in a.properties file, then include these in your application.properties file of the server如果您尝试在 a.properties 文件中使用本地存储库,请将这些包含在服务器的 application.properties 文件中

server.port=8888
spring.profiles.active=git
spring.cloud.config.server.git.uri=file:/link-to-local-git-repo

You can get the link to the local git repo by typing pwd (ubuntu) on the terminal while in the git repo directory.您可以在 git 存储库目录中通过在终端上键入pwd (ubuntu) 来获取本地 git 存储库的链接。

Emphasis: be sure the value of spring.profiles.active is git not native强调:确保 spring.profiles.active 的值是git不是原生的

Then type然后输入

http://localhost:8888/<name-of-file>/default

In the browser tab在浏览器选项卡中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 云配置服务器不使用本地属性文件 - 需要使用复合配置 - Spring Cloud Configuration Server not working with local properties file - getting need to use a composite configuration Spring-Cloud配置服务器忽略配置属性文件 - Spring-Cloud configuration server ignores configuration properties file Spring Cloud本地与远程Config Server配置 - Spring Cloud local vs remote Config Server configuration Spring Cloud 配置 - 访问所有微服务中的共享属性文件 - Spring Cloud Configuration - Access shared properties file in all microservices 如果配置服务器关闭,Spring Cloud 配置客户端会加载本地属性 - Spring cloud config client load local properties if the config server is down Spring Cloud - 配置客户端缓存属性 - Spring Cloud - Configuration client caching properties 未从bootstrap.properties或外部配置服务(spring-cloud-config)中选择日志文件位置 - Log file location not being picked from bootstrap.properties or external configuration service(spring-cloud-config) Spring 云卡夫卡消费者属性不工作 - Spring Cloud Kafka Consumer Properties not working 在Spring Bean配置文件中从Weblogic Server加载属性文件 - Loading properties file from Weblogic Server inside Spring Bean Configuration file 通过Spring配置XML映射.properties文件 - Mapping of .properties file via Spring configuration XML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM