2017-07-19 281 views
0

我想在某些application.properties文件中設置彈性搜索自定義連接參數。 像彈簧引導自定義配置

myelastic.server = 192.168.1.1 
myelastic.port = 11111 

我需要加載應用程序啓動這個值並創建傳輸客戶端組件的彈性搜索5.4

我怎樣才能在啓動時加載此值模型/屬性。 ?

謝謝。

回答

1

請你的模型類使用下面的代碼:

@Value("${myelastic.server}") 
String server; 

@Value("${myelastic.port}") 
int port; 
1

您可以從春天訪問或者使用@Value註釋或自動裝配環境屬性。

使用@Value獲取的屬性值:

@Value("${myelastic.server}") 
private String elasticServer; 

@Value("${myelastic.port}") 
private int elasticPort; 

@Value("${elasticsearch.clustername}") 
private String EsClusterName; 

和下面創建傳輸客戶端:

@Bean 
public Client client() throws Exception { 

    Settings esSettings = Settings.settingsBuilder() 
      .put("cluster.name", EsClusterName) 
      .build(); 

    return TransportClient.builder() 
      .settings(esSettings) 
      .build() 
      .addTransportAddress(
       new InetSocketTransportAddress(InetAddress.getByName(elasticServer), elasticPort)); 
}