如何在PCF中配置Spring App连接到Elasticsearch

时间:2018-10-13 06:11:31

标签: java spring elasticsearch pcfdev

我有一个spring应用,当连接到本地Elasticsearch服务器时,该应用运行正常。主机和端口在application.properties文件中配置。我什至不需要ElasticsearchConfig.java文件来实现elasticsearchTemplate() bean。

然后我尝试将其推送到Pivotal Cloud Foundry。首先,我创建了一个简单的es-small服务,然后编辑manifest.yml文件将应用程序绑定到该es-small服务。我还使用了一个空的application.properties文件,假定将自动获取连接信息。但是,在我执行“ cf push”之后,该应用程序无法启动。错误消息是“没有可用的名为'elasticsearchTemplate'的豆”。

我试图添加一个新的appplication.yml文件,例如:

spring:
  datasource:
    url: ${vcap.services.es-small.credentials.host}
    username: ${vcap.services.es-small.credentials.username}
    password: ${vcap.services.es-small.credentials.password}

但是我不断收到诸如“名称或服务未知”之类的错误消息:

   2018-10-12T22:14:58.72-0700 [APP/PROC/WEB/0] OUT Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'client' defined in class path resource [com/javatechstack/datajpa/ElasticSearchConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.elasticsearch.client.Client]: Factory method 'client' threw exception; nested exception is java.net.UnknownHostException: http://d412092.service.dc1.a9s-elasticsearch-consul:9200: Name or service not known

在绑定并使用“ cf env”后,我的VCAP信息如下:

{
 "VCAP_SERVICES": {
  "a9s-elasticsearch6": [
   {
    "credentials": {
     "dns_servers": [
      "192.168.24.4",
      "192.168.24.5",
      "192.168.24.6"
     ],
     "host": [
      "http://d412092.service.dc1.a9s-elasticsearch-consul:9200"
     ],
     "host_ip": [
      "http://192.168.28.8:9200"
     ],
     "password": "a9s5d93c1563f60d0702e6ca24928993aae76948a00",
     "username": "a9s96eb080f8089c5fa32f214e9d8e7c6752de7db0a"
    },
    "label": "a9s-elasticsearch6",
    "name": "es-small",
    "plan": "elasticsearch-single-small",
    "provider": null,
    "syslog_drain_url": null,
    "tags": [
     "searchengine"
    ],
    "volume_mounts": []
   }
  ]
 }
}

我还添加了如下的ElasticsearchConfig.java:

public class ElasticSearchConfig {

    //@Value("${elasticsearch.host}")
    @Value("${vcap.services.es-small.credentials.host}")
    private String EsHost;
/*
    //@Value("${elasticsearch.port}")
    @Value("${vcap.services.es-small.credentials.username}")
    private int EsPort;

    //@Value("${elasticsearch.clustername}")
    @Value("${vcap.services.es-small.credentials.password}")
    private String EsClusterName;
*/
    @Bean
    public Client client() throws Exception {

        return new PreBuiltTransportClient(Settings.EMPTY)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(EsHost), 9300));
    }

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

谁能提供有关如何为我的spring boot应用程序代码设置VCAP连接信息的任何信息?我四处搜寻,但无法在线找到它。

0 个答案:

没有答案