es中的DiscoveryModule

1、初始化

node初始化时,构造函数中会创建DiscoveryModule

final DiscoveryModule discoveryModule = new DiscoveryModule(settings, transportService, namedWriteableRegistry,
                networkService, clusterService.getMasterService(), clusterService.getClusterApplierService(),
                clusterService.getClusterSettings(), pluginsService.filterPlugins(DiscoveryPlugin.class),
                clusterModule.getAllocationService(), environment.configFile(), gatewayMetaState, rerouteService,
                fsHealthService);

并且会将Discovery与DiscoryModule的discovery绑定到juice容器中。

 b.bind(Discovery.class).toInstance(discoveryModule.getDiscovery());

2、DiscoveryModule

2.1 支持的发现类型

只支持两种类型zen和single-node。发现类型通过属性discovery.type配置。

2.2 SeedHostsProvider

SeedHostsProvider提供作为参与选举的结点地址提供者。支持配置和文件两种方式。

 SettingsBasedSeedHostsProvider:通过配置discovery.seed_hosts来设置参与选举的结点地址。

FileBasedSeedHostsProvider:通过configPath目录下的unicast_hosts.txt来设置参与选举的结点地址。

2.3 ElectionStrategy

选举策略的抽象类,抽象方法为satisfiesAdditionalQuorumConstraints,匿名类DEFAULT_INSTANCE实现的satisfiesAdditionalQuorumConstraints方法返回true。

 2.4 CustomNameResolver

将字符串解析成地址的接口,通过DiscoveryPlugin获取得到提供者。

2.5 DicoveryPlugin

发现插件,提供了扩展机制,提供CustomNameResolver, SeedHostsProvider, ElectoryStrategy。

 AzureDiscoreyPlugin:微软提供发现插件

Ec2DiscoveryPlugin:亚马逊提供的发现插件

GceDiscoveryPlugin:谷歌提供的发现插件

2.6 Discovery

继承接口ClusterStatePublisher,提供startInitialJoin开始选举接口,stats发现状态接口。

Coordinate是Discovery的实现类

# ======================== Elasticsearch Configuration ========================= # # NOTE: Elasticsearch comes with reasonable defaults for most settings. # Before you set out to tweak and tune the configuration, make sure you # understand what are you trying to accomplish and the consequences. # # The primary way of configuring a node is via this file. This template lists # the most important settings you may want to configure for a production cluster. # # Please consult the documentation for further information on configuration options: # https://www.elastic.co/guide/en/elasticsearch/reference/index.html # # ---------------------------------- Cluster ----------------------------------- # # Use a descriptive name for your cluster: # #cluster.name: my-application cluster.name: douban-cluster # # ------------------------------------ Node ------------------------------------ # # Use a descriptive name for the node: # #node.name: node-1 node.name: windows-node # # 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.data: E:\elasticsearch-9.0.1-windows-x86_64\elasticsearch-9.0.1\data # # Path to log files: # #path.logs: /path/to/logs path.logs: E:\elasticsearch-9.0.1-windows-x86_64\elasticsearch-9.0.1\logs # # ----------------------------------- Memory ----------------------------------- # # Lock the memory on startup: # #bootstrap.memory_lock: true # # 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 ----------------------------------- # # By default Elasticsearch is only accessible on localhost. Set a different # address here to expose this node on the network: # #network.host: 192.168.0.1 network.host: 0.0.0.0 # By default Elasticsearch listens for HTTP traffic on the first free port it # finds starting at 9200. Set a specific HTTP port here: # #http.port: 9200 http.port: 9200 # # For more information, consult the network module documentation. # # --------------------------------- Discovery ---------------------------------- # # Pass an initial list of hosts to perform discovery when this node is started: # The default list of hosts is ["127.0.0.1", "[::1]"] # # discovery.seed_hosts: ["host1", "host2"] # discovery.type: single-node # Bootstrap the cluster using an initial set of master-eligible nodes: # # # For more information, consult the discovery and cluster formation module documentation. # # ---------------------------------- Various ----------------------------------- # # Allow wildcard deletion of indices: # # action.destructive_requires_name: false #----------------------- BEGIN SECURITY AUTO CONFIGURATION ----------------------- # # The following settings, TLS certificates, and keys have been automatically # generated to configure Elasticsearch security features on 26-05-2025 02:15:52 # # -------------------------------------------------------------------------------- # Enable security features xpack.security.enabled: false xpack.security.enrollment.enabled: true # Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents xpack.security.http.ssl: enabled: true keystore.path: certs/http.p12 # Enable encryption and mutual authentication between cluster nodes xpack.security.transport.ssl: enabled: true verification_mode: certificate keystore.path: certs/transport.p12 truststore.path: certs/transport.p12 # Create a new cluster with the current node only # Additional nodes can still join the cluster later cluster.initial_master_nodes: ["DESKTOP-QRJI156"] # Allow HTTP API connections from anywhere # Connections are encrypted and require user authentication http.host: 0.0.0.0 # Allow other nodes to join the cluster from anywhere # Connections are encrypted and mutually authenticated #transport.host: 0.0.0.0 #----------------------- END SECURITY AUTO CONFIGURATION ------------------------- 运行出现[2025-05-26T15:23:17,442][ERROR][o.e.b.Elasticsearch ] [windows-node] fatal exception while booting Elasticsearchjava.lang.IllegalArgumentException: setting [cluster.initial_master_nodes] is not allowed when [discovery.type] is set to [single-node] at org.elasticsearch.server@9.0.1/org.elasticsearch.cluster.coordination.ClusterBootstrapService.<init>(ClusterBootstrapService.java:88) at org.elasticsearch.server@9.0.1/org.elasticsearch.cluster.coordination.Coordinator.<init>(Coordinator.java:300) at org.elasticsearch.server@9.0.1/org.elasticsearch.discovery.DiscoveryModule.<init>(DiscoveryModule.java:188) at org.elasticsearch.server@9.0.1/org.elasticsearch.node.NodeConstruction.createDiscoveryModule(NodeConstruction.java:1600) at org.elasticsearch.server@9.0.1/org.elasticsearch.node.NodeConstruction.construct(NodeConstruction.java:1069) See logs for more details. ERROR: Elasticsearch did not exit normally - check the logs at E:\elasticsearch-9.0.1-windows-x86_64\elasticsearch-9.0.1\logs\douban-cluster.log
最新发布
05-27
### 解决方案 当 `discovery.type` 被设置为 `single-node` 时,Elasticsearch 不允许同时配置 `cluster.initial_master_nodes` 参数。这是因为在单节点模式下,Elasticsearch 假设只有一个节点运行,并自动处理主节点选举逻辑[^1]。 如果尝试在同一配置中保留 `cluster.initial_master_nodes`,将会触发以下异常: ```plaintext java.lang.IllegalArgumentException: setting [cluster.initial_master_nodes] is not allowed when [discovery.type] is set to [single-node] ``` 为了避免这一错误,可以采取以下措施之一来调整 Elasticsearch 的配置文件 (`elasticsearch.yml`) 或者 Docker 启动命令中的环境变量。 #### 方法一:移除 `cluster.initial_master_nodes` 配置 在单节点模式下,不需要显式指定初始主节点集合。因此可以直接注释掉或删除 `cluster.initial_master_nodes` 参数。修改后的配置示例如下: ```yaml # cluster.initial_master_nodes: ["node-1"] discovery.type: single-node ``` 通过这种方式,Elasticsearch 将不会抛出非法参数异常[^3]。 #### 方法二:仅使用 `-e "discovery.type=single-node"` 环境变量启动容器 如果通过 Docker 启动 Elasticsearch,则可以通过传递环境变量的方式禁用集群发现机制,而不需额外编辑配置文件。例如: ```bash docker run -d \ -e "discovery.type=single-node" \ -v $(pwd)/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \ --name elasticsearch \ -p 9200:9200 \ -p 9300:9300 \ elasticsearch:7.10.0 ``` 在此场景下,即使原始配置文件中仍然存在 `cluster.initial_master_nodes` 参数,Docker 中的环境变量会覆盖它并强制启用单节点模式[^1]。 #### 方法三:重新初始化 Keystore 并清理旧数据 某些情况下,可能需要清除之前的 keystore 数据以确保新配置生效。这一步骤尤其适用于曾经作为多节点集群成员运行过的实例。操作流程如下: 1. 进入正在运行的 Elasticsearch 容器 CLI 控制台: ```bash docker exec -it <container_id> /bin/bash ``` 2. 删除现有的 keystore 文件(如果有): ```bash rm -f /usr/share/elasticsearch/config/elasticsearch.keystore ``` 3. 创建新的 keystore 实例: ```bash ./bin/elasticsearch-keystore create ``` 完成以上步骤后重启服务即可应用更改[^3]。 --- ### 总结代码片段 以下是完整的解决方案脚本示例: ```bash # 移除旧版Keystore (可选) docker exec -it <container_id> bash -c 'rm -f /usr/share/elasticsearch/config/elasticsearch.keystore' # 初始化新的Keystore docker exec -it <container_id> bash -c './bin/elasticsearch-keystore create' # 使用单节点模式启动Elasticsearch容器 docker run -d \ -e "discovery.type=single-node" \ -v $(pwd)/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \ --name elasticsearch \ -p 9200:9200 \ -p 9300:9300 \ elasticsearch:7.10.0 ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值