【问题标题】:Spring Cloud Configuration Server not working with local properties fileSpring Cloud Configuration Server 不使用本地属性文件
【发布时间】:2015-01-23 17:13:57
【问题描述】:

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

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

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

【问题讨论】:

  • 您是否尝试使用spring.profiles.active=native 运行它(应该使用1.0.0.M3 或快照)?
  • 是的,我尝试添加它,但我没有任何运气。
  • 将 spring.profiles.active=native 添加到 application.properties 文件后对我有用

标签: java spring spring-cloud


【解决方案1】:

我所有的代码都在这里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"
                }
          }
     ]
}

【讨论】:

  • 这真的很酷。效果很好!谢谢!我的下一个问题是:要获得一个特定的值,你可以在 application.yml 或一些像这样的属性文件中指定它:foo=bar,然后使用一些 curl 命令获取 foo 的值吗?喜欢curl http://localhost:8080/myapp/foo
  • 当前单个值不能通过 http api 获得。如果您在 Spring 应用程序环境中使用 spring-cloud-config-client。这些值分别通过 http://localhost:8080/myapp-default.ymlhttp://localhost:8080/myapp-default.properties 以 YAML 或 Java 属性文档的形式提供。
  • 目前,使用 Spring Boot 2.x 您将不会在响应中获得 application.properties。根据 Spring Cloud Config 的文档:“searchLocations 的默认值与本地 Spring Boot 应用程序相同(...)这不会将 application.properties 从服务器公开给所有客户端,因为任何属性源都存在于服务器在发送到客户端之前被删除。”
【解决方案2】:

我可以使用 Spring 配置服务器读取 apple-service(Test Micro Service) 的配置。

spring config 应用示例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 那么属性文件应该是 apple-service.propertiessrc\main\resources\config 文件夹。

apple-service的bootstrap.yml示例:

spring:
  application:
    name: apple-service

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

【讨论】:

    【解决方案3】:

    这是我所做的:

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

    1) !!不要忘记你的虚拟机选项!!:

    -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

    【讨论】:

      【解决方案4】:

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

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

      但来自 URL 的响应

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

      {"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 视为属性源。

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

      【讨论】:

      • 您找到解决方案了吗?端口和本机配置文件。
      • 你可以把 spring.profiles.active=native 放到 bootstrap.properties 并把它放到 src/main/resources 或者当你启动你的 spring boot 时你可以这样做: mvn spring-boot:运行 -Drun.profiles=native 现在试一试...
      • 您好,它工作正常,但是在客户端应用程序中使用 @Value("${info.description}") 注入时,它不起作用,出现错误,因为无法解析值中的占位符“消息”“ ${info.description}" 。我没有使用文件系统。
      【解决方案5】:

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

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

      spring:
        profiles:
          active: native
      

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

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

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

      【讨论】:

        【解决方案6】:

        如果配置服务器的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”?
        【解决方案7】:

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

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

        还要注意Users前面有3个斜线:

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

        或者我使用:

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

        原生属性是:

        spring.profiles.active=native
        

        【讨论】:

          【解决方案8】:

          我能够让它与本地 repo 和引导配置一起工作:

          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
          

          【讨论】:

            【解决方案9】:

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

            @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 
            

            【讨论】:

              【解决方案10】:

              我使用的是spring boot 2.6.0-SNAPSHOT版

              如果您尝试在 .properties 文件中使用本地 repo,则将它们包含在服务器的 application.properties 文件中

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

              您可以通过在 git repo 目录中的终端上键入 pwd(ubuntu) 来获取到本地 git repo 的链接。

              重点:确保 spring.profiles.active 的值是 git 而不是 native

              然后输入

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

              在浏览器标签中

              【讨论】:

                猜你喜欢
                • 2021-04-25
                • 2021-01-30
                • 1970-01-01
                • 2022-01-13
                • 2012-05-19
                • 1970-01-01
                • 2017-10-03
                • 2018-12-08
                • 2016-04-01
                相关资源
                最近更新 更多