簡體   English   中英

Spring 雲配置服務器無法使用本地屬性文件

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

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

但是,我遇到了一些問題,讓它讀取本地屬性文件而不是從 github 中提取屬性。即使我刪除了對 github 的所有引用,spring 似乎也忽略了本地文件。這里有一個類似的問題: Spring-Cloud 配置服務器忽略配置屬性文件

但我還沒有看到任何好的答案。 我想知道是否有人可以指出我的例子? 我想在本地設置我的屬性,而不是使用任何類型的 git 存儲庫。 我假設有人以前遇到過這個,如果某處有這樣的例子,我真的很想看看它,這樣我就可以朝着正確的方向前進。

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

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

彈簧配置應用程序的實施例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

使用spring.profiles.active = native是Spring文檔所建議的,但是我也無法使它正常工作。 我的application.properties文件是

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

但網址的回應

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,則配置服務器將從默認的Spring Boot端口8080啟動,但不應使用一個配置服務器。

在Mac OS環境中運行配置服務器時,我遇到了同樣的問題。 在Linux或Windows中沒有發生這種情況。

我在bootstrap.yml文件中設置了本機屬性,如下所示:

spring:
  profiles:
    active: native

最終,它在Mac上對我有用的方法是將活動配置文件傳遞給jar文件,就像這樣。

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

我仍然不知道為什么它在Mac OS中表現不同。

如果配置服務器的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"}}]}

我能夠使其與本地存儲庫和引導程序配置一起使用:

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

我在本地計算機上遇到了同樣的問題,但在遠程服務器上工作正常。

@Oreste的答案對我有用。 所以我再次檢查了錯誤日志,發現

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

因此,造成這種情況的根本原因是我之前設置了環境變量,但是卻忘記刪除它。 並且它會覆蓋應用程序屬性文件config。

希望你們不要像我一樣犯傻的錯誤。 固定於:

 unset spring_profiles_active 

在Mac上,當文件路徑中有空格時,我遇到了這個問題:

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

另外請注意,在用戶之前有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

這是我所做的:

以下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

我添加了以下兩個屬性,它開始工作了:

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

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

我使用的是 spring 引導版本 2.6.0-SNAPSHOT

如果您嘗試在 a.properties 文件中使用本地存儲庫,請將這些包含在服務器的 application.properties 文件中

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

您可以在 git 存儲庫目錄中通過在終端上鍵入pwd (ubuntu) 來獲取本地 git 存儲庫的鏈接。

強調:確保 spring.profiles.active 的值是git不是原生的

然后輸入

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

在瀏覽器選項卡中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM