首页 > 代码库 > ElasticSearch client API

ElasticSearch client API

技术分享

技术分享

技术分享

 

 技术分享

 

 

技术分享

技术分享

从运行结果看并没有打印节点信息出来

 

 

技术分享

 

 技术分享

从结果看出来,集群节点信道打印出来了,不过这种方法有个问题,就是当我们连接的节点挂掉了,就没法连接整个集群了,这个时候我们就利用他的一个嗅探的功能。

 

技术分享

技术分享

从这里我们可以看到,通过嗅探功能把集群的三个节点都打印出来了

实际上我们只传入master节点,但是同过master节点探测到其他两个节点信息

 

但是但应用重启之后master节点挂掉了就不能连接集群了,为了防止容错性,我们就多设置几个节点

技术分享

技术分享

 

参考代码ESTestClient.java

  1 package com.dajiangtai.djt_spider.elasticsearch;
  2 
  3 import java.net.InetAddress;
  4 import java.net.UnknownHostException;
  5 import java.util.List;
  6 
  7 import org.elasticsearch.client.transport.TransportClient;
  8 import org.elasticsearch.cluster.node.DiscoveryNode;
  9 import org.elasticsearch.common.settings.Settings;
 10 import org.elasticsearch.common.transport.InetSocketTransportAddress;
 11 import org.junit.Before;
 12 import org.junit.Test;
 13 /**
 14  * 获取TransportClient
 15  * @author 大讲台
 16  *
 17  */
 18 public class ESTestClient {
 19 
 20     /**
 21      * 测试使用Java API 连接ElasticSearch 集群
 22      * 
 23      * @throws UnknownHostException
 24      */
 25     @Test
 26     public void test1() throws UnknownHostException {
 27         // on startup
 28         // 获取TransportClient
 29         TransportClient client = TransportClient
 30                 .builder()
 31                 .build()
 32                 .addTransportAddress(
 33                         new InetSocketTransportAddress(InetAddress
 34                                 .getByName("master"), 9300));
 35         
 36         List<DiscoveryNode> connectedNodes = client.connectedNodes();
 37         for (DiscoveryNode discoveryNode : connectedNodes) {
 38             System.out.println("集群节点:"+discoveryNode.getHostName());
 39         }
 40         // on shutdown
 41         client.close();
 42     }
 43 
 44     /**
 45      * 生成环境下,ElasticSearch集群名称非默认需要显示设置
 46      * 
 47      * @throws UnknownHostException
 48      */
 49     @Test
 50     public void test2() throws UnknownHostException {
 51         Settings settings = Settings.settingsBuilder()
 52                 .put("cluster.name", "escluster").build();
 53         // on startup
 54         // 获取TransportClient
 55         TransportClient client = TransportClient
 56                 .builder()
 57                 .settings(settings)
 58                 .build()
 59                 .addTransportAddress(
 60                         new InetSocketTransportAddress(InetAddress
 61                                 .getByName("master"), 9300));
 62         
 63         List<DiscoveryNode> connectedNodes = client.connectedNodes();
 64         for (DiscoveryNode discoveryNode : connectedNodes) {
 65             System.out.println("集群节点:"+discoveryNode.getHostName());
 66         }
 67 
 68         // on shutdown
 69         client.close();
 70     }
 71 
 72     /**
 73      * 1、启动应用程序之后,client.transport.sniff能保证即使master挂掉也能连接上集群
 74      * 2、master节点挂机同时应用程序重启,则无法连接ElasticSearch集群
 75      * 
 76      * @throws UnknownHostException
 77      */
 78     @Test
 79     public void test3() throws UnknownHostException {
 80 
 81         // 开启client.transport.sniff功能,探测集群所有节点
 82         Settings settings = Settings.settingsBuilder()
 83                 .put("cluster.name", "escluster")
 84                 .put("client.transport.sniff", true).build();
 85         // on startup
 86         // 获取TransportClient
 87         TransportClient client = TransportClient
 88                 .builder()
 89                 .settings(settings)
 90                 .build()
 91                 .addTransportAddress(
 92                         new InetSocketTransportAddress(InetAddress
 93                                 .getByName("master"), 9300));
 94 
 95         List<DiscoveryNode> connectedNodes = client.connectedNodes();
 96         for (DiscoveryNode discoveryNode : connectedNodes) {
 97             System.out.println("集群节点:"+discoveryNode.getHostName());
 98         }
 99 
100         // on shutdown
101         client.close();
102     }
103 
104     /**
105      * 1、启动应用程序之后,client.transport.sniff能保证即使master挂掉也能连接上集群
106      * 2、设置多节点,防止其中一个节点挂机同时应用程序重启,无法连接ElasticSearch集群问题
107      * 
108      * @throws UnknownHostException
109      */
110     @Test
111     public void test4() throws UnknownHostException {
112 
113         // 开启client.transport.sniff功能,探测集群所有节点
114         Settings settings = Settings.settingsBuilder()
115                 .put("cluster.name", "escluster")
116                 .put("client.transport.sniff", true).build();
117         // on startup
118         // 获取TransportClient
119         TransportClient client = TransportClient
120                 .builder()
121                 .settings(settings)
122                 .build()
123                 .addTransportAddress(
124                         new InetSocketTransportAddress(InetAddress
125                                 .getByName("master"), 9300))
126                 .addTransportAddress(
127                         new InetSocketTransportAddress(InetAddress
128                                 .getByName("slave1"), 9300))
129                 .addTransportAddress(
130                         new InetSocketTransportAddress(InetAddress
131                                 .getByName("slave2"), 9300));
132         
133         List<DiscoveryNode> connectedNodes = client.connectedNodes();
134         for (DiscoveryNode discoveryNode : connectedNodes) {
135             System.out.println("集群节点:"+discoveryNode.getHostName());
136         }
137 
138         // on shutdown
139         client.close();
140     }
141 }

 

ElasticSearch client API