Java客户端
Ignite 3 的客户端通过标准套接字连接接入集群。与 Ignite 2 不同,Ignite 3 中没有单独的胖和瘦客户端,只有瘦
客户端。
客户端不会成为集群拓扑的一部分,不保存任何数据,也不会用来承载计算任务。
1.开始
1.1.环境要求
运行 Java 瘦客户端需要 Java 11 或更高版本。
1.2.安装
可以使用 Maven 将 Java 客户端导入项目中:
xml
<dependency>
<groupId>org.apache.ignite</groupId>
<artifactId>ignite-client</artifactId>
<version>3.0.0</version>
</dependency>
2.接入集群
首先要使用IgniteClient
初始化客户端:
java
try (IgniteClient client = IgniteClient.builder()
.addresses("127.0.0.1:10800")
.build()
) {
// Your code goes here
}
3.认证
要传递身份验证信息,需要使用IgniteClientAuthenticator
类并将其传递给IgniteClient
:
java
IgniteClientAuthenticator auth = BasicAuthenticator.builder().username("myUser").password("myPassword").build();
IgniteClient.builder()
.addresses("127.0.0.1:10800")
.authenticator(auth)
.build();
4.日志
loggerFactory
可用于配置客户端日志:
java
IgniteClient client = IgniteClient.builder()
.addresses("127.0.0.1")
.loggerFactory(System::getLogger)
.build();
客户端会记录连接错误、重新连接和重试。
5.客户端指标
在Java客户端中启用客户端指标的代码如下:
java
IgniteClient client = IgniteClient.builder()
.addresses("127.0.0.1:10800")
.metricsEnabled(true)
.build();
之后,任何 Java 监控工具(例如 JDK Mission Control)都可以抓取客户端的指标数据。
5.1.支持的指标
指标名称 | 描述 |
---|---|
ConnectionsActive | 当前的在线连接数 |
ConnectionsEstablished | 已建立的连接数 |
ConnectionsLost | 丢失的连接数 |
ConnectionsLostTimeout | 由于超时而丢失的连接数 |
HandshakesFailed | 握手失败的次数 |
HandshakesFailedTimeout | 由于超时而失败的握手次数 |
RequestsActive | 当前的线上请求数 |
RequestsSent | 发送的请求数 |
RequestsCompleted | 已完成的请求数。收到响应后,请求即完成 |
RequestsRetried | 请求重试次数 |
RequestsFailed | 失败的请求数 |
BytesSent | 发送的字节数 |
BytesReceived | 接收的字节数 |
StreamerBatchesSent | 发送的数据流批次数 |
StreamerItemsSent | 发送的数据流项目数 |
StreamerBatchesActive | 当前线上的数据流批次数 |
StreamerItemsQueued | 当前正在排队的数据流项目数 |
6.客户端连接配置
有许多参数可以对客户端和 Ignite 集群之间的连接进行调整:
java
IgniteClient client = IgniteClient.builder()
.addresses("127.0.0.1:10800")
.connectTimeout(5000)
.heartbeatInterval(30000)
.heartbeatTimeout(5000)
.operationTimeout(3000)
.backgroundReconnectInterval(30000)
.retryPolicy(new RetryLimitPolicy().retryLimit(8))
.build();
参数 | 描述 |
---|---|
connectTimeout | 客户端连接超时(毫秒) |
heartbeatInterval | 心跳消息间隔(毫秒) |
heartbeatTimeout | 心跳消息超时(毫秒) |
operationTimeout | 操作超时(毫秒) |
backgroundReconnectInterval | 后台重连时间间隔(毫秒) |
retryPolicy | 重试策略,默认所有的读操作会最多重试 16 次,而写操作不会重试 |
18624049226