如何将SpringBoot与SpringData绑定到CloudFoundry中的Elasticsearch?

时间:2018-02-08 12:12:14

标签: spring-boot cloudfoundry pivotal-cloud-foundry

在我的开发机器上本地启动Elasticsearch时,我可以从SpringBoot连接到Elasticsearch并且我的JUnit测试用例可以工作。

但是当我尝试连接到Cloud Foundry Elasticsearch时,它无效。

从命令行开始,即将端口9300重定向到Cloud Foundry:

public class ElasticsearchConfiguration {

@Bean
public Client client() throws Exception {
    Settings settings = Settings.builder()
        .put("spring.data.elasticsearch.cluster-nodes", "localhost:9300")
        .build();

    TransportClient client = TransportClient.builder()
        .settings(settings)
        .build()
        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
    return client;
}

@Bean
public ElasticsearchOperations elasticsearchTemplate() throws Exception {
    return new ElasticsearchTemplate(client());
}

我在SpringBoot中为本地Elasticsearch和Cloud Foundry Elasticsearch使用相同的端口和配置:

Caused by: NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{localhost}{127.0.0.1:9300}]]
at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:326)
at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:223)
at org.elasticsearch.client.transport.support.TransportProxyClient.execute(TransportProxyClient.java:55)
at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:295)
at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:359)
at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:348)
at org.elasticsearch.client.support.AbstractClient$IndicesAdmin.execute(AbstractClient.java:1221)
at org.elasticsearch.client.support.AbstractClient$IndicesAdmin.exists(AbstractClient.java:1241)
at org.springframework.data.elasticsearch.core.ElasticsearchTemplate.indexExists(ElasticsearchTemplate.java:622)
at org.springframework.data.elasticsearch.core.ElasticsearchTemplate.deleteIndex(ElasticsearchTemplate.java:639)
at ch.stockscan.config.elasticsearch.IndexReinitializer.resetIndex(IndexReinitializer.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134)

运行我的JUnit测试用例尝试使用Cloud Foundry向ElasticsearchRepository保存时出现以下错误:

http://localhost:9300/_cat/indices?v

From Postman我可以从我的本地机器上获取GET,我从Cloud Foundry获得结果:

  health status index               pri rep docs.count docs.deleted store.size pri.store.size 
    yellow open   parsed-2018.02.07     5   1        177            0      343kb          343kb 
    yellow open   parsed-2018.02.08     5   1        196            0    503.2kb        503.2kb 
    yellow open   .kibana               1   1          4            0     16.7kb         16.7kb 
...

输出:

cboxTitle

我不确定为什么SpringBoot与Cloud Foundry不起作用?

1 个答案:

答案 0 :(得分:0)

它不适合你的原因,在你的bean定义中,它指向localhost,在云代工厂没有任何意义。

创建服务实例(cf create-service --help)。然后将该服务实例绑定到示例应用程序,并查看env属性(cf env)。那应该给你钥匙的名字。

然后修改你的代码,设置如下属性......

  

@Value(" $ {vcap.services.elastisearch.url:http://localhost:9300}&#34)   private String elastisearchEndpoint;

并使用它,而不是硬编码localhost。

以下是一些供参考的链接:

另一个选择是有两个不同的bean实现。 LocalConfig中的一个用于 @profile ="默认" CloudConfig用于 @profile ="云"

应用在cloud个人资料下的Cloud Foundry中运行。