1,连接es
新建EsController类
package com.example.demo.controller; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import javax.annotation.PostConstruct; import java.net.InetAddress; @Controller public class EsController { private final static Log logger = LogFactory.getLog(EsController.class); @Value("${es.cluster.name}") String es_cluster_name; @Value("${es.cluster.ip}") String es_cluster_ip; @Value("${es.cluster.trans.port}") String es_cluster_trans_port; @Value("${es.shield.user.password}") String es_shield_user_password; @Value("${es.shield.enable}") private Boolean shieldEnable = Boolean.TRUE; @PostConstruct public TransportClient init() { Settings settings = Settings.settingsBuilder().put("cluster.name", es_cluster_name).put("shield.user", es_shield_user_password).build(); TransportClient client = TransportClient.builder().addPlugin(ShieldPlugin.class).settings(settings).build(); try { String[] ips = es_cluster_ip.split(","); for (int i = 0; i < ips.length; i++) { InetAddress esIP = InetAddress.getByName(ips[i]); int esPORT = Integer.parseInt(es_cluster_trans_port); client.addTransportAddress(new InetSocketTransportAddress(esIP, esPORT)); } if (client == null) { logger.warn("es无法连接,client为空"); } } catch (Exception e) { logger.error("es连接时错误"); } return client; } }
会发现
1.Settings.settingsBuilder()
2.TransportClient.builder().addPlugin(ShieldPlugin.class).settings(settings).build()
标红,证明缺少jar包;
已知Settings类在org.elasticsearch.common.*;下,所以猜测一定是版本不对
在pom文件中添加依赖
<dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>2.3.5</version> </dependency>
发现1编译通过,2依然不对。
再加依赖
<dependency> <groupId>org.elasticsearch.plugin</groupId> <artifactId>shield</artifactId> <version>2.2.0</version> </dependency>
1和2都调试通过。
但此时
,不用管,不影响程序。
先这样,明天补,今天下班了