2016-08-03 114 views
5

環境變量我配置的Spring配置雲服務器這樣的:Spring雲配置服務器。在性能

@SpringBootApplication 
@EnableAutoConfiguration 
@EnableConfigServer 
public class ConfigServer { 

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

我使用「天然」配置文件,以便屬性從文件系統拿起:

server.port=8888 
spring.profiles.active=native 
spring.cloud.config.server.native.search-locations: classpath:/global 

現在棘手部分屬性包含環境變量。在屬性「全球/ application-production.properties」可以這樣來配置:

test=${DOCKER_HOST} 

當我啓動配置服務器 - 一切正常。然而,當我訪問http://localhost:8888/testapp/production我看到這一點:

{ 
    name: "testapp", 
    profiles: [ 
     "production" 
], 
    label: null, 
    version: null, 
    propertySources: [ 
     { 
      name: "classpath:/global/application-production.properties", 
      source: { 
       test: "${DOCKER_HOST}" 
      } 
     } 
    ] 
} 

所以從ENV變量值而不是取代$ {} DOCKER_HOST而把返回的。

但是,如果我訪問http://localhost:8888/application-production.properties那麼結果就是非JSON而是純文本:

test: tcp://192.168.99.100:2376 

春文件說:

的YAML和屬性表示有一個附加的標誌(一個布爾值規定查詢參數resolvePlaceholders)以標誌Spring $ {...}形式表示源文檔中佔位符的信號應在渲染前儘可能在輸出中解析。對於不瞭解Spring佔位符約定的消費者來說,這是一個有用的功能。

出於某種原因resolvePlaceholders沒有爲JSON表示工作因而服務器配置的客戶端需要知道服務器上配置的所有ENV變量。

是否可以強制JSON表示resolvePlaceholders與純文本(屬性)表示法相同?

回答

0

有一個更新爲了做到這一點,下面合併enter link description here我發現resolvePlaceholders的實現。這給了我創建一個使用EnvironmentController的新控制器的想法。這將允許你解決配置問題,這是一個很好的引導。

import com.fasterxml.jackson.databind.ObjectMapper; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.cloud.config.server.environment.EnvironmentController; 
import org.springframework.cloud.config.server.environment.EnvironmentRepository; 
import org.springframework.http.ResponseEntity; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RestController; 

@RestController 
@RequestMapping(method = RequestMethod.GET, path = "resolved/${spring.cloud.config.server.prefix:}") 
public class ReplacedEnvironmentController { 

    private EnvironmentController environmentController; 

    @Autowired 
    public ReplacedEnvironmentController(EnvironmentRepository repository) { 
    environmentController = new EnvironmentController(repository, new ObjectMapper()); 
    } 

    public ReplacedEnvironmentController(EnvironmentRepository repository, 
     ObjectMapper objectMapper) { 
    environmentController = new EnvironmentController(repository, objectMapper); 

    } 

    @RequestMapping("/{name}/{profiles:.*[^-].*}") 
    public ResponseEntity<String> resolvedDefaultLabel(@PathVariable String name, 
     @PathVariable String profiles) throws Exception { 
    return resolvedLabelled(name, profiles, null); 
    } 

    @RequestMapping("/{name}/{profiles}/{label:.*}") 
    public ResponseEntity<String> resolvedLabelled(@PathVariable String name, @PathVariable String profiles, 
     @PathVariable String label) throws Exception { 
    return environmentController.labelledJsonProperties(name, profiles, label, true); 
    } 

} 
相關問題