Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9a2c192

Browse files
committed
Track 2 files into repository.
- modified client/transport-client.md - modified client/xpack-transport-client.md Auto commit by GitBook Editor
1 parent 932a6da commit 9a2c192

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

‎client/transport-client.md‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
### Transport Client
3+
4+
#### 不设置集群名称
5+
6+
```
7+
// on startup
8+
9+
//此步骤添加IP,至少一个,如果设置了"client.transport.sniff"= true 一个就够了,因为添加了自动嗅探配置
10+
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
11+
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host1"), 9300))
12+
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host2"), 9300));
13+
14+
// on shutdown 关闭client
15+
16+
client.close();
17+
```
18+
19+
#### 设置集群名称
20+
21+
```
22+
Settings settings = Settings.builder()
23+
.put("cluster.name", "myClusterName").build(); //设置ES实例的名称
24+
TransportClient client = new PreBuiltTransportClient(settings); //自动嗅探整个集群的状态,把集群中其他ES节点的ip添加到本地的客户端列表中
25+
//Add transport addresses and do something with the client...
26+
```
27+
28+
#### 增加自动嗅探配置
29+
```
30+
Settings settings = Settings.builder()
31+
.put("client.transport.sniff", true).build();
32+
TransportClient client = new PreBuiltTransportClient(settings);
33+
```
34+
35+
#### 其他配置
36+
37+
```
38+
client.transport.ignore_cluster_name //设置 true ,忽略连接节点集群名验证
39+
client.transport.ping_timeout //ping一个节点的响应时间 默认5秒
40+
client.transport.nodes_sampler_interval //sample/ping 节点的时间间隔,默认是5s
41+
```
42+
> 对于ES Client,有两种形式,一个是TransportClient,一个是NodeClient。两个的区别为:
43+
TransportClient作为一个外部访问者,通过HTTP去请求ES的集群,对于集群而言,它是一个外部因素。
44+
NodeClient顾名思义,是作为ES集群的一个节点,它是ES中的一环,其他的节点对它是感知的,不像TransportClient那样,ES集群对它一无所知。NodeClient通信的性能会更好,但是因为是ES的一环,所以它出问题,也会给ES集群带来问题。NodeClient可以设置不作为数据节点,在elasticsearch.yml中设置,这样就不会在此节点上分配数据。
45+
46+
如果用ES的节点,大家仁者见仁智者见智,各按所需。
47+
48+
#### 实例
49+
50+
```
51+
Settings esSettings = Settings.builder()
52+
53+
.put("cluster.name", clusterName) //设置ES实例的名称
54+
55+
.put("client.transport.sniff", true) //自动嗅探整个集群的状态,把集群中其他ES节点的ip添加到本地的客户端列表中
56+
57+
.build();
58+
59+
client = new PreBuiltTransportClient(esSettings);//初始化client较老版本发生了变化,此方法有几个重载方法,初始化插件等。
60+
61+
//此步骤添加IP,至少一个,其实一个就够了,因为添加了自动嗅探配置
62+
63+
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(ip), esPort));
64+
65+
```

‎client/xpack-transport-client.md‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
### XPackTransportClient
3+
如果 `ElasticSearch ` 服务安装了 `x-pack` 插件,需要`PreBuiltXPackTransportClient`实例才能访问
4+
5+
6+
使用Maven管理项目,把下面代码增加到`pom.xml`;
7+
8+
> 一定要修改默认仓库地址为https://artifacts.elastic.co/maven,因为这个库没有上传到Maven中央仓库
9+
10+
```
11+
<project ...>
12+
13+
<repositories>
14+
<!-- add the elasticsearch repo -->
15+
<repository>
16+
<id>elasticsearch-releases</id>
17+
<url>https://artifacts.elastic.co/maven</url>
18+
<releases>
19+
<enabled>true</enabled>
20+
</releases>
21+
<snapshots>
22+
<enabled>false</enabled>
23+
</snapshots>
24+
</repository>
25+
...
26+
</repositories>
27+
...
28+
29+
<dependencies>
30+
<!-- add the x-pack jar as a dependency -->
31+
<dependency>
32+
<groupId>org.elasticsearch.client</groupId>
33+
<artifactId>x-pack-transport</artifactId>
34+
<version>5.6.3</version>
35+
</dependency>
36+
...
37+
</dependencies>
38+
...
39+
40+
</project>
41+
```
42+
43+
#### 实例
44+
45+
46+
```
47+
Settings settings = Settings.builder().put("cluster.name", "xxx")
48+
.put("xpack.security.transport.ssl.enabled", false)
49+
.put("xpack.security.user", "xxx:xxx")
50+
.put("client.transport.sniff", true).build();
51+
try {
52+
client = new PreBuiltXPackTransportClient(settings)
53+
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("xxx.xxx.xxx.xxx"), 9300))
54+
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("xxx.xxx.xxx.xxx"), 9300));
55+
} catch (UnknownHostException e) {
56+
e.printStackTrace();
57+
}
58+
```
59+
60+
更多请浏览 dayu-spring-boot-starter 开源项目

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /