ElasticSearch系列(三) 集群搭建

本文介绍如何使用两台配置相同的机器搭建Elasticsearch集群。详细讲述了集群配置文件elasticsearch.yml的设置方法,包括网络配置、节点发现等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文,讲述下集群的整体搭建。

篇幅很短,主要基于前面的文章所写:

集群规模:两台机器,配置基本完全一样。

机器A:

JDK版本:1.8.0_111

操作系统版本:Linux version 2.6.32-504.el6.x86_64 (mockbuild@c6b9.bsys.dev.centos.org) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC) ) #1 SMP Wed Oct 15 04:27:16 UTC 2014

这里:想要查看操作系统版本,可以通过cat /proc/version 命令查看,可能linux不同版本的操作系统文件不同

机器B:

JDK版本:1.8.0_111;这说明,只要都是JDK 1.8即可,具体更新版本不重要。

操作系统版本:Linux version 2.6.32-504.el6.x86_64 (mockbuild@c6b9.bsys.dev.centos.org) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC) ) #1 SMP Wed Oct 15 04:27:16 UTC 2014

对于ElasticSearch来说,最重要的其实就是elasticsearch.yml文件,下面附上两台机器的配置文件。

机器A:作为master节点,数据节点

# ======================== Elasticsearch Configuration =========================
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: elasticsearch
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#
node.name: node-1
node.master: true
node.data: true
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#
#path.data: /path/to/data
#
# Path to log files:
#
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 192.168.100.34
#http.host: 192.168.100.34
#
# Set a custom port for HTTP:
#
http.port: 9200
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#
discovery.zen.ping.unicast.hosts: ["192.168.100.34", "192.168.100.35","192.168.100.112"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
discovery.zen.minimum_master_nodes: 2
#
# For more information, consult the zen discovery module documentation.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true

机器B:主节点,数据节点

# ======================== Elasticsearch Configuration =========================
# ---------------------------------- Cluster -----------------------------------
#
# Use a descriptive name for your cluster:
#
cluster.name: elasticsearch
#
# ------------------------------------ Node ------------------------------------
#
# Use a descriptive name for the node:
#节点在集群中的名称
node.name: node-2
#是否是master节点
node.master: true
#是否是数据节点
node.data: true
#
# Add custom attributes to the node:
#
#node.attr.rack: r1
#
# ----------------------------------- Paths ------------------------------------
#
# Path to directory where to store the data (separate multiple locations by comma):
#数据存储路径
#path.data: /path/to/data
#日志存储路径
#这两个都是默认的,不用修改就行
# Path to log files:
#
#path.logs: /path/to/logs
#
# ----------------------------------- Memory -----------------------------------
#
# Lock the memory on startup:
#
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
#
# Make sure that the heap size is set to about half the memory available
# on the system and that the owner of the process is allowed to use this
# limit.
#
# Elasticsearch performs poorly when the system is swapping the memory.
#
# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#本机IP
network.host: 192.168.100.35
#http.host: 192.168.100.35
#
# Set a custom port for HTTP:
#端口号
http.port: 9200
#transport.tcp.port:9300
#
# For more information, consult the network module documentation.
#
# --------------------------------- Discovery ----------------------------------
#
# Pass an initial list of hosts to perform discovery when new node is started:
# The default list of hosts is ["127.0.0.1", "[::1]"]
#这里用的是单播,节点之间互相发现
discovery.zen.ping.unicast.hosts: ["192.168.100.34", "192.168.100.35","192.168.100.112"]
#
# Prevent the "split brain" by configuring the majority of nodes (total number of master-eligible nodes / 2 + 1):
#
discovery.zen.minimum_master_nodes: 2
#
# For more information, consult the zen discovery module documentation.
#
# ---------------------------------- Gateway -----------------------------------
#
# Block initial recovery after a full cluster restart until N nodes are started:
#
#gateway.recover_after_nodes: 3
#
# For more information, consult the gateway module documentation.
#
# ---------------------------------- Various -----------------------------------
#
# Require explicit names when deleting indices:
#
#action.destructive_requires_name: true

启动即可,机器之间会自动发现,并联系。

这里有一个需要注意的问题:

如果第二台机器上的ES是从第一台机器上复制过去的,如果在data下有数据的话,请一定要清除掉,具体报什么错不记得了,但报错的话,注意下这一点。

然后,访问:hostA:9200/_cluster/healthy?pretty

{
  "cluster_name" : "elasticsearch",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 2,
  "number_of_data_nodes" : 2,
  "active_primary_shards" : 26,
  "active_shards" : 52,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0,
  "task_max_waiting_in_queue_millis" : 0,
  "active_shards_percent_as_number" : 100.0
}

上面需要注意节点:

  1. 集群健康状态:这时候应该是status:green。
  2. 集群节点个数:number_of_nodes : 2
  3. 活跃的分片是主分片数量的2倍,因为我这边只有两台数据节点
  4. 未分配的分片数目是0:unassigned_shards。

满足上面这四点,集群才算真正搭建成功。

### 使用Spring Boot搭建Elasticsearch自定义集群集成应用的最佳实践 #### 1. 添加Maven依赖 为了使Spring Boot项目能够与Elasticsearch交互,需在`pom.xml`文件中加入必要的依赖项。对于较新的版本组合,推荐使用如下配置: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> <!-- 版本号应匹配所使用的Spring Boot和Elasticsearch --> </dependency> <!-- 如果需要更高级别的REST API调用功能,则可以添加此依赖 --> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <!-- 同样注意保持版本一致性 --> </dependency> ``` 上述依赖分别提供了对Spring Data Elasticsearch的支持以及通过HTTP协议操作Elasticsearch的能力[^2]。 #### 2. 配置Elasticsearch连接参数 编辑项目的application.properties或application.yml来指定目标Elasticsearch集群的信息,包括但不限于主机地址、端口号等基本信息;当涉及到安全性设置时(如启用了X-Pack),还需提供认证凭证等相关细节。 ```yaml spring: data: elasticsearch: cluster-name: my-cluster # 替换成实际的集群名称 cluster-nodes: localhost:9300 # 列表形式给出所有节点IP及传输端口,默认为TCP/9300 # 对于远程部署的情况,请替换为主机的真实公网IP elasticsearch: rest: uris: http://localhost:9200 # RESTful接口URL列表,逗号分隔多个URI username: elastic # 若已启用安全特性,则填写对应的用户名 password: changeme # 密码也在此处声明 ``` 这段配置指定了如何定位并接入特定的Elasticsearch集群,并处理可能存在的身份验证需求[^4]。 #### 3. 创建实体类映射至索引结构 基于领域模型创建Java对象表示文档数据,在此类中标注字段属性以便框架自动完成JSON序列化过程中的转换工作。例如: ```java import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.Document; @Document(indexName = "product", type = "_doc") // 定义存储这些记录的目标索引名及其类型 public class Product { @Id private String id; // 文档唯一标识符 private String name; // 商品名字 private double price; // 单价 // Getters and Setters... } ``` 此处展示了怎样利用注解机制将业务逻辑层的对象关联到具体的Elasticsearch索引之上[^1]。 #### 4. 编写Repository接口继承预设好的CRUD方法集 借助Spring Data提供的模板代码快速实现基本的数据存取服务,只需定义好泛型参数即可获得一系列现成的操作函数供开发者调用。 ```java import org.springframework.data.repository.CrudRepository; interface ProductRepository extends CrudRepository<Product, Long> { } ``` 这一步骤简化了针对持久化层的具体编码任务,使得应用程序可以直接专注于核心业务流程的设计与开发。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值