使用 Java 客户端通过 HTTPS 连接到 Easysearch
Easysearch 一直致力于提高易用性,这也是我们的核心宗旨,然而之前一直没有官方的 Java 客户端,也对用户使用造成了一些困扰,现在,我们正式发布了第一个 Java 客户端 Easysearch-client:1.0.1。
这一里程碑式的更新为开发人员带来了前所未有的便利性,使得与 Easysearch 集群的交互变得更加简洁和直观。通过 Easysearch-client,开发者可以直接使用 Java 方法和数据结构来进行交互,而不再需要依赖于传统的 HTTP 方法和 JSON。
这一变化大大简化了操作流程,使得数据管理和索引更加高效。Java 客户端的功能范围包括处理数据操作,管理集群,包括查看和维护集群的健康状态,并对 Security 模块全面兼容。它提供了一系列 API,用于管理角色、用户、权限、角色映射和账户。
这意味着安全性和访问控制现在可以更加细粒度地管理,确保了数据的安全性和合规性。
在这篇博客中,你将学习如何配置 Easysearch-client 客户端以通过 HTTPS 连接到 Easysearch。为了演示目的,我将首先设置一个带有 SSL 证书的 Easysearch 服务器。如果你已经有一个在运行,你可以跳过这一步。
接下来,我将引导你完成在 Java 应用程序中配置和使用 Java 客户端的步骤。
设置 Easysearch 服务器
首先从极限科技官网下载最新的 Mac 版本。我使用的是 1.6.1 版本,这是我写这篇文章时的最新版本。
wget https://dl-global.infinilabs.com/easysearch/stable/easysearch-1.6.1-214-mac-amd64.zip
确保您的系统已经安装并设置了 java 环境变量,版本在 11 以上。
解压下载文件。
unzip easysearch-1.6.1-214-mac-amd64.zip -d easysearch-1.6.1
cd 到 easysearch-1.6.1 执行初始化脚本来生成证书并自动下载插件。
bin/initialize.sh
脚本执行后会自动输出随机生成的 admin 用户密码。
启动 Easysearch
bin/easysearch
此时,您的服务器已经准备就绪。您可以查看 logs/initialize.log 里显示的 curl 命令来进行验证。
curl -ku admin:xxxxxxxxx https://localhost:9200
显示类似的输出响应
{
"name" : "MacBook-Pro.local",
"cluster_name" : "easysearch",
"cluster_uuid" : "1gRYQ6ssTiKGqcyuEN0Dbg",
"version" : {
"distribution" : "easysearch",
"number" : "1.6.1",
"distributor" : "INFINI Labs",
"build_hash" : "14846e460e9976ba6d68c80bb9eca52af1179dcf",
"build_date" : "2023-10-19T14:43:02.636639Z",
"build_snapshot" : false,
"lucene_version" : "8.11.2",
"minimum_wire_lucene_version" : "7.7.0",
"minimum_lucene_index_compatibility_version" : "7.7.0"
},
"tagline" : "You Know, For Easy Search!"
}
下面我们来看如何设置和使用客户端。
设置 Java 客户端
Easysearch 的 Java 客户端可在 中央仓库:https://repo1.maven.org/maven2/ 上获得。将其作为依赖项添加到你的 Java 应用程序中。
对于 Gradle 构建系统,在项目的 build.gradle 文件中包含以下依赖项:
dependencies {
implementation 'com.infinilabs:easysearch-client:1.0.1'
implementation "org.apache.logging.log4j:log4j-api:2.19.0"
implementation "org.apache.logging.log4j:log4j-core:2.19.0"
implementation 'org.apache.httpcomponents:httpclient:4.5.10'
implementation 'org.apache.httpcomponents:httpcore-nio:4.4.12'
implementation 'org.apache.httpcomponents:httpasyncclient:4.1.4'
implementation 'joda-time:joda-time:2.10.4'
implementation ('org.apache.lucene:lucene-core:8.11.2') {
exclude group: '*', module: '*'
}
implementation ('org.apache.lucene:lucene-analyzers-common:8.11.2') {
exclude group: '*', module: '*'
}
implementation ('org.apache.lucene:lucene-backward-codecs:8.11.2') {
exclude group: '*', module: '*'
}
implementation ('org.apache.lucene:lucene-grouping:8.11.2') {
exclude group: '*', module: '*'
}
implementation ('org.apache.lucene:lucene-highlighter:8.11.2') {
exclude group: '*', module: '*'
}
implementation ('org.apache.lucene:lucene-join:8.11.2') {
exclude group: '*', module: '*'
}
implementation ('org.apache.lucene:lucene-memory:8.11.2') {
exclude group: '*', module: '*'
}
implementation ('org.apache.lucene:lucene-misc:8.11.2') {
exclude group: '*', module: '*'
}
implementation ('org.apache.lucene:lucene-queries:8.11.2') {
exclude group: '*', module: '*'
}
implementation ('org.apache.lucene:lucene-queryparser:8.11.2') {
exclude group: '*', module: '*'
}
implementation ('org.apache.lucene:lucene-sandbox:8.11.2') {
exclude group: '*', module: '*'
}
implementation ('org.apache.lucene:lucene-spatial3d:8.11.2') {
exclude group: '*', module: '*'
}
implementation ('org.apache.lucene:lucene-suggest:8.11.2') {
exclude group: '*', module: '*'
}
......
}
对于 Maven 构建系统,在项目的 pom.xml 文件中包含以下依赖项:
<dependencies>
<dependency>
<groupId>com.infinilabs</groupId>
<artifactId>easysearch-client</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.10</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore-nio</artifactId>
<version>4.4.12</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<version>4.1.4</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.10.4</version>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-core</artifactId>
<version>8.11.2</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-analyzers-common</artifactId>
<version>8.11.2</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-backward-codecs</artifactId>
<version>8.11.2</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-grouping</artifactId>
<version>8.11.2</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-highlighter</artifactId>
<version>8.11.2</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-join</artifactId>
<version>8.11.2</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-memory</artifactId>
<version>8.11.2</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-misc</artifactId>
<version>8.11.2</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queries</artifactId>
<version>8.11.2</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-queryparser</artifactId>
<version>8.11.2</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-sandbox</artifactId>
<version>8.11.2</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-spatial3d</artifactId>
<version>8.11.2</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.lucene</groupId>
<artifactId>lucene-suggest</artifactId>
<version>8.11.2</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>*</artifactId>
<groupId>*</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
接下来,在你的 Java 应用程序中创建一个 client 实例,并使用它在 Easysearch 中创建索引并插入一些数据。但在此之前,为了使其工作,你需要将签署服务器证书的根机构证书添加到你的应用程序信任库中。让我们看看如何配置 Java 应用程序的信任库。
为了使用 java client,你需要将根 CA 证书 ca.crt 添加到应用程序信任库中。这告诉你的 Java 应用程序信任由此根机构签署的任何证书。easysearch-1.6.1/config/ 目录下已经生成了 ca.crt 文件。你可以将其添加到自定义信任库中,并在 Java 应用程序中使用该自定义信任库。
使用 Java keytool 创建一个自定义信任库并导入证书。keytool 不理解 .pem 格式,所以你需要首先使用 openssl 加密库将证书转换为 .der 格式,然后使用 Java keytool 将其添加到自定义信任库中。假设您的操作系统已经预装了 openssl。
第 1 步:将 CA 证书从 .pem 格式转换为 .der 格式。
openssl x509 -in easysearch-1.6.1/config/ca.crt -inform pem -out ca.der --outform der
第 2 步:创建自定义信任库并添加 ca.der 证书。
将 ca 证书添加到应用程序信任库中,表示应用程序信任由此 CA 签署的任何证书。
keytool -import -file ca.der -alias easysearch -keystore myTrustStore
过程中会提示您输入密钥库口令: 我为了测试用输入的 123456。
通过列出信任库中的证书来确认操作成功,这里的 123456 是我上面设置的密码,会显示出 easysearch 证书。
keytool -keystore myTrustStore -storepass 123456 -list
第 3 步:在 Java 应用程序代码中设置指向自定义信任库的系统属性,并连接集群,创建索引,插入数据。
可以通过设置系统属性,以指定 SSL/TLS 通信时使用的信任库:
System.setProperty("javax.net.ssl.trustStore", "/full/path/to/myTrustStore");
System.setProperty("javax.net.ssl.trustStorePassword", "123456");
HttpHost[] httpHostArray = new HttpHost[1];
// infini.cloud 和 CN=infini.cloud 保持一致
httpHostArray[0] = new HttpHost("infini.cloud", 9200, "https");
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("admin", "1933791fb2b9f6c6146d"));
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(httpHostArray)
.setHttpClientConfigCallback((HttpAsyncClientBuilder httpAsyncClientBuilder) -> {
httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
return httpAsyncClientBuilder;
}));
CreateIndexRequest createIndexRequest = new CreateIndexRequest("test-index");
createIndexRequest.settings(Settings.builder()
.put("index.number_of_shards", 1)
.put("index.number_of_replicas", 1)
);
//Create index
client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
// Bulk
BulkRequest bulkRequest = new BulkRequest();
for (int i = 0; i < 10; i++) {
IndexRequest indexRequest = new IndexRequest("test-index")
.id(Integer.toString(i))
.source("{\"field1\":\"value" + i + "\"}", XContentType.JSON);
bulkRequest.add(indexRequest);
}
BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println(Strings.toString(bulkResponse));
信任已签署 Easysearch 正在使用的证书的 CA 的示例,当 CA 证书以 PEM 编码文件的形式可用时:
Path caCertificatePath = Paths.get("/easysearch-test/easysearch-1.6.1/config/ca.crt");
CertificateFactory factory = CertificateFactory.getInstance("X.509");
Certificate trustedCa;
try (InputStream is = Files.newInputStream(caCertificatePath)) {
trustedCa = factory.generateCertificate(is);
}
KeyStore trustStore = KeyStore.getInstance("pkcs12");
trustStore.load(null, null);
trustStore.setCertificateEntry("ca", trustedCa);
SSLContextBuilder sslContextBuilder = SSLContexts.custom()
.loadTrustMaterial(trustStore, null);
final SSLContext sslContext = sslContextBuilder.build();
HttpHost[] httpHostArray = new HttpHost[1];
httpHostArray[0] = new HttpHost("infini.cloud", 9200, "https");
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("admin", "1933791fb2b9f6c6146d"));
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(httpHostArray)
.setHttpClientConfigCallback((HttpAsyncClientBuilder httpAsyncClientBuilder) -> {
httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
httpAsyncClientBuilder.setSSLContext(sslContext);
return httpAsyncClientBuilder;
}));
现在,您已经成功设置了 Java 客户端,并以安全的 HTTPS 通道连接到了 Easysearch 集群。除此之外,Java 客户端还具备强大的权限控制管理 API,具体请参考我们的官网文档:https://www.infinilabs.com/docs/latest/easysearch/references/client/security/
关于 Easysearch

INFINI Easysearch 是一个分布式的近实时搜索与分析引擎,核心引擎基于开源的 Apache Lucene。Easysearch 的目标是提供一个轻量级的 Elasticsearch 可替代版本,并继续完善和支持更多的企业级功能。 与 Elasticsearch 相比,Easysearch 更关注在搜索业务场景的优化和继续保持其产品的简洁与易用性。
官网文档:https://www.infinilabs.com/docs/latest/easysearch
下载地址:https://www.infinilabs.com/download
使用 Java 客户端通过 HTTPS 连接到 Easysearch的更多相关文章
- java客户端验证https连接(忽略证书验证和证书验证两种方式)
首先根据如下操作生成证书,配置springboot https,生成一个简单的https web服务 https://www.cnblogs.com/qq931399960/p/11889349.ht ...
- 使用poco 的NetSSL_OpenSSL 搭建https 服务端,使用C++客户端,java 客户端访问,python访问(python还没找到带证书访问的代码.)
V20161028 由于项目原因,需要用到https去做一些事情. 这儿做了一些相应的研究. 这个https 用起来也是折腾人,还是研究了一周多+之前的一些积累. 目录 1,java client 通 ...
- 从入门到精通(分布式文件系统架构)-FastDFS,FastDFS-Nginx整合,合并存储,存储缩略图,图片压缩,Java客户端
导读 互联网环境中的文件如何存储? 不能存本地应用服务器 NFS(采用mount挂载) HDFS(适合大文件) FastDFS(强力推荐
- 盘点一下Redis中常用的Java客户端,或者咱们手写一个?
Java中那些Redis的客户端 前面我们的所有操作都是基于redis-cli来完成的,那么我们要在Java中操作Redis,怎么做呢?首先我们先来了解一下Redis Serialization Pr ...
- 由Memcached升级到 Couchbase的 Java 客户端的过程记录(一)
背景: 在项目启动的选用了Memcached 作为缓存服务器,采用了Xmemcached作为客户端.在项目中使用了Shiro,为了给 Shiro 配置缓存的时候,采用了开源代码 https://g ...
- Java与WCF交互(一):Java客户端调用WCF服务
最近开始了解WCF,写了个最简单的Helloworld,想通过java客户端实现通信.没想到以我的基础,居然花了整整两天(当然是工作以外的时间,呵呵),整个过程大费周折,特写下此文,以供有需要的朋友参 ...
- memcached学习——常用命令+基于java客户端的3种简单实现(二)
常用命令: memcached设计的原则就是简单,所以支持的命令也不是特别多~ 1.查看memcached的状态,主要用于分析内存的使用状况.优化内存分配等 stats 查看memcached的运行状 ...
- Java客户端工具选择:HTML?Swing?XML?
整理下面的文章是因为个人觉得写的很好,关于java的客户端了解也并不是太多.看了下面的文章觉得很有必要贴出来,方便自己以后了解java客户端编程. Java软件设计师和管理人员经常会面临这样的难题:在 ...
- Memcached学习笔记 — 第四部分:Memcached Java 客户端-gwhalin(1)-介绍及使用
介绍 Memcached java client是官方推荐的最早的memcached java客户端.最新版本:java_memcached-release_2.6.1. 官方下载地址:http ...
- java客户端与服务端交互通用处理 框架解析
一.综述 java 客户端与服务端交互过程中,采用NIO通讯是异步的,客户端基本采用同一处理范式,来进行同异步的调用处理. 处理模型有以下几个要素: 1. NIO发送消息后返回的Future 2. 每 ...
随机推荐
- 蚂蚁一面:GC垃圾回收时,内存分配和回收策略有哪些?
文章首发于公众号:腐烂的橘子 蚂蚁面试主要为电话面试,期间也会要求使用编辑器手写算法题.作为一线互联网大厂,Java 基础知识是必备的,其中垃圾回收也是面试过程中的重中之重. Java 内存的自动管理 ...
- 科普达人丨漫画图解SGX加密计算黑科技
介: 运行态的数据也可以被加密,实现数据可用不可见. 01 从一场朋友圈的"赛富"说起 最近,小明买基金赚了不少钱,开始膨胀了,开始在朋友圈里晒豪车.晒爱马仕.小红表示不服,&qu ...
- 云原生微服务的下一站,微服务引擎 MSE 重磅升级
简介:管好微服务,成为云原生时代的新难题. 管好微服务,成为云原生时代的新难题. 从建好微服务到管好微服务,差的虽是一个字,连接起两边的却需要大量的微服务落地经验.因为软件架构的核心挑战是解决业务快 ...
- 钉钉宜搭亮相“第二届ISIG中国产业智能大会”:云钉低代码,构建企业酷应用
简介:低代码年度行业盛会!钉钉宜搭创始人叶周全受邀出席并发表主题演讲. 12月8日,由中国电子技术标准化研究院指导,RPA中国.LowCode低码时代.信创中国联合举办的"第二届ISIG中 ...
- 开源自建/托管与商业化自研 Trace,如何选择?
简介: 随着微服务架构的兴起,服务端的调用依赖愈加复杂,为了快速定位异常组件与性能瓶颈,接入分布式链路追踪 Trace 已经成为 IT 运维领域的共识.但是,开源自建.开源托管或商业化自研 Trac ...
- 重磅发布|新一代云原生数据仓库AnalyticDB「SQL智能诊断」功能详解
简介: AnalyticDB For MySQL为用户提供了高效.实时.功能丰富并且智能化的「SQL智能诊断」和「SQL智能调优」功能,提供用户SQL性能调优的思路.方向和具体的方法,降低用户使用成 ...
- Oracle "脑残" CBO 优化案例
今天晚上下班回来才有空看群,群友发了一条很简单的慢SQL问怎么优化. 非常简单,我自己模拟的数据. 表结构: -- auto-generated definition CREATE TABLE HHH ...
- van-tab吸顶后头部透明色渐变响应
方法一:监听滚动事件 $('.scrollContent').bind('touchmove', function(e){ var winHeight = $(window) ...
- SpringBoot实现WebSocket发送接收消息 + Vue实现SocketJs接收发送消息
SpringBoot实现WebSocket发送接收消息 + Vue实现SocketJs接收发送消息 参考: 1.https://www.mchweb.net/index.php/dev/887.htm ...
- Go-Zero从0到1实现微服务项目开发(二)
前言 书接上回,继续更新GoZero微服务实战系列文章. 上一篇被GoZero作者万总点赞了,更文动力倍增,也建议大家先看巧一篇,欢迎粉丝股东们三连支持一波:Go-zero微服务快速入门和最佳实践(一 ...