方案:
- 使用
HAproxy
:当其中一台ElasticSearch Master
宕掉时,ElasticSearch集群
会自动将运行正常的节点提升为Master
,但HAproxy
不会将失败的请求重新分发到新的Master Node
。 - 使用
ElasticSearch
:单search load balancer(外层负载均衡节点)
、双coordinator(调度节点)
、若干workhorse(数据节点)
。先后在200并发Index、200并发Update测试下(跑在虚拟机下,线程太多就卡爆了),并前后分别测试了Down掉一台主coordinator
、Down掉一台workhorse
,都没有引起数据异常,集群工作正常
。
先贴一下使用HAproxy
搭建集群失败的配置吧:
#全局配置
global
daemon
nbproc 4
pidfile /tmp/haproxy.pid
#默认配置
defaults
mode http #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OK
retries 2 #两次连接失败就认为是服务器不可用,也可以通过后面设置
option redispatch #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器
option httpclose #HAProxy会针对客户端的第一条请求的返回添加cookie并返回给客户端,客户端发送后续请求时会发送此cookie到HAProxy
#option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接
maxconn 4096 #默认的最大连接数
timeout connect 5000ms #连接超时
timeout client 30000ms #客户端超时
timeout server 30000ms #服务器超时
timeout check 2000 #心跳检测超时
log 127.0.0.1 local0 err #[err warning info debug]
#统计页面配置
listen admin_stats
bind 0.0.0.0:8888 #监听端口
mode http #http的7层模式
option httplog #采用http日志格式
#log 127.0.0.1 local0 err
maxconn 10
stats refresh 30s #统计页面自动刷新时间
stats uri / #统计页面url
stats realm XingCloud\ Haproxy #统计页面密码框上提示文本
stats auth admin:admin #统计页面用户名和密码设置
#stats hide-version #隐藏统计页面上HAProxy的版本信息
#ElasticSearch Frontend
frontend eshttp
bind 0.0.0.0:9200
mode tcp
use_backend eshttp_server
#ElasticSearch Backend
backend eshttp_server
server eshttp1 vm12:9200 cookie 1 check inter 2000 rise 3 fall 3 weight 2
server eshttp2 vm13:9200 cookie 2 check inter 2000 rise 3 fall 3 weight 1
server eshttp3_bk vm14:9200 cookie 3 check inter 1000 rise 3 fall 3 backup
采用ElasticSearch
搭建集群的关键几个配置:
search load balancer(LB负载均衡节点):
cluster.name: harold #集群名称
node.name: "harold_lb" #节点名称
# 3. You want this node to be neither master nor data node, but
# to act as a "search load balancer" (fetching data from nodes,
# aggregating results, etc.)
#
node.master: false
node.data: false
discovery.zen.ping.unicast.hosts: ["vm11", "vm12", "vm13", "vm14", "vm15", "vm16"] #自动发现节点hosts
coordinator(Master调度节点):
cluster.name: harold #集群名称
node.name: "harold_coordinator_1" #节点名称
# 2. You want this node to only serve as a master: to not store any data and
# to have free resources. This will be the "coordinator" of your cluster.
#
node.master: true
node.data: false
discovery.zen.ping.unicast.hosts: ["vm11", "vm12", "vm13", "vm14", "vm15", "vm16"] #自动发现节点hosts
workhorse(Data数据节点):
cluster.name: harold #集群名称
node.name: "harold_data_1" #节点名称
# 1. You want this node to never become a master node, only to hold data.
# This will be the "workhorse" of your cluster.
#
node.master: false
node.data: true
discovery.zen.ping.unicast.hosts: ["vm11", "vm12", "vm13", "vm14", "vm15", "vm16"] #自动发现节点hosts
配置完,启动后,/_plugin/head/页面应该是这个样子:
这样配置完的集群应该就是类似这样的:
可以使用curl初始化Index的主分片
与复制分片
:
curl -XPUT -d'{"settings":{"number_of_shards":6, "number_of_replicas":1}}' http://vm11:9200/app1
Tip:
number_of_shards
主分片在集群中的总数量number_of_replicas
每个主分片的复制分片数量
#复制分片在今后的分布式集群变化过程中,随时都可以根据业务进行新增或减少:
curl -XPUT -d'{"number_of_replicas":2}' http://vm11:9200/app1/_settings
#另外,ElasticSearch在没有任何索引的情况下新增一个文档,便自动创建了索引,为避免发生这种情况,可以在配置文件中添加:
action.auto_create_index: false
删除Index:
curl -XDELETE http://vm11:9200/app1
在当前版本中,这样组建集群会有一个小问题:
当单独把Master Coordinator
Down掉后,/_plugin/head/插件页面会是这个样子:
但可喜的是,并不影响集群与集群客户端之间数据的CRUD操作。
数据有所改变而且较长一段时间后(大约10几分钟?),/_plugin/head/插件页面会恢复正常。
贴下PHP操作ElasticSearch的多进程并发测试代码吧,做下记录:
<?php
class es extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'es:test';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description.';
private $hosts = ["vm11:9200"];
private $index = "app1";
private $type = "users1";
private $process = 200;
private $sum = 10000;
private $num_per_proc;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
$this->sum % $this->process !== 0 && die("invalid num. \n");
$this->num_per_proc = $this->sum / $this->process;
}
private function insert()
{
$es = new ClientBuilder();
$es->setHosts($this->hosts);
$client = $es->build();
$words = str_split("abcdefghijklmnopqrstuvwxyz");
$birth_year = [];
for ($i = 1; $i <= 50; $i++) {
$birth_year[] = 1960 + $i;
}
$type = ['1', '2', '3', '4'];
$process = [];
for ($p = 0; $p < $this->process; $p++) {
$process[] = new \swoole_process(function () use ($client, $birth_year, $type, $words, $p) {
for ($i = $this->num_per_proc * $p; $i < $this->num_per_proc * ($p + 1); $i++) {
$client->index([
'index' => $this->index,
'type' => $this->type,
'id' => $i,
'body' => [
'birth_year' => $birth_year[array_rand($birth_year)],
'type' => $type[array_rand($type)],
'name' => $words[mt_rand(0, 25)] . $words[mt_rand(0, 25)] . $words[mt_rand(0, 25)] . $words[mt_rand(0, 25)],
'height' => mt_rand(150, 200),
'weight' => mt_rand(40, 200),
'test' => 1,
'userid' => $i
]
]);
}
});
}
foreach ($process as $p) {
$pid = $p->start();
echo $pid . "\n";
}
}
private function update()
{
$es = new ClientBuilder();
$es->setHosts($this->hosts);
$client = $es->build();
$process = [];
for ($i = 0; $i < $this->process; $i++) {
$process[] = new \swoole_process(function () use ($client, $i) {
$response = $client->search([
'index' => $this->index,
'type' => $this->type,
'size' => $this->num_per_proc,
'from' => $this->num_per_proc * $i,
'sort' => "userid:asc"
]);
foreach ($response['hits']['hits'] as $v) {
$id = $v['_id'];
$test = $v['_source']['test'];
$test++;
file_put_contents("/tmp/s", $test . "\n", FILE_APPEND);
$client->update([
'index' => $this->index,
'type' => $this->type,
'id' => $id,
'body' => [
'doc' => [
'test' => $test
]
]
]);
}
});
}
foreach ($process as $p) {
$pid = $p->start();
echo $pid . "\n";
}
}
private function gets()
{
$es = new ClientBuilder();
$es->setHosts($this->hosts);
$client = $es->build();
$response = $client->search([
'index' => $this->index,
'type' => $this->type,
'size' => 5000,
'from' => 500,
'sort' => "userid:asc"
]);
foreach ($response['hits']['hits'] as $v) {
$id = $v['_id'];
$test = $v['_source']['test'];
// file_put_contents("/tmp/s", $test . "\n", FILE_APPEND);
var_dump($test);
}
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->insert();
}
}
ElasticSearch 高可用分布式集群搭建,与PHP多线程测试的更多相关文章
-
Dubbo+zookeeper构建高可用分布式集群(二)-集群部署
在Dubbo+zookeeper构建高可用分布式集群(一)-单机部署中我们讲了如何单机部署.但没有将如何配置微服务.下面分别介绍单机与集群微服务如何配置注册中心. Zookeeper单机配置:方式一. ...
-
一张图讲解最少机器搭建FastDFS高可用分布式集群安装说明
很幸运参与零售云快消平台的公有云搭建及孵化项目.零售云快消平台源于零售云家电3C平台私有项目,是与公司业务强耦合的.为了适用于全场景全品类平台,集团要求项目平台化,我们抢先并承担了此任务.并由我来主 ...
-
Hadoop-HA(高可用)集群搭建
Hadoop-HA集群搭建 一.基础准备工作 1.准备好5台Linux系统虚拟服务器或物理服务器 我这里演示采用虚拟服务器搭建Hadoop-HA集群,各自功能分配如下: NameNode节点:vt-s ...
-
redis高可用分布式集群
一,高可用 高可用(High Availability),是当一台服务器停止服务后,对于业务及用户毫无影响. 停止服务的原因可能由于网卡.路由器.机房.CPU负载过高.内存溢出.自然灾害等不可预期的原 ...
-
Redis 高可用分布式集群
一,高可用 高可用(High Availability),是当一台服务器停止服务后,对于业务及用户毫无影响. 停止服务的原因可能由于网卡.路由器.机房.CPU负载过高.内存溢出.自然灾害等不可预期的原 ...
-
redis详解(四)-- 高可用分布式集群
一,高可用 高可用(High Availability),是当一台服务器停止服务后,对于业务及用户毫无影响. 停止服务的原因可能由于网卡.路由器.机房.CPU负载过高.内存溢出.自然灾害等不可预期的原 ...
-
高可用k8s集群搭建
虚拟机选择 Win10 Hyper-V 总体架构 三个master,三个node master的组件 etcd kube-apiserver kube-controller-manager kube- ...
-
高可用mysql集群搭建
对web系统来说,瓶颈大多在数据库和磁盘IO上面,而不是服务器的计算能力.对于系统伸缩性我们一般有2种解决方案,scale-up(纵向扩展)和scale-out(横向扩展).前者如扩内存,增加单机性能 ...
-
4 种高可用 RocketMQ 集群搭建方案!
背景 笔者所在的业务线,最初化分为三个服务,由于业务初期业务复杂度相对简单,三个业务服务都能很好的独立完成业务功能. 随着产品迭代,业务功能越来越多后慢慢也要面对高并发.业务解耦.分布式事务等问题,所 ...
随机推荐
-
jquery指index
$(selector).index(element) 获得元素相对于选择器的 index 位置.<ul> <li><a href="#nogo" ...
-
Idea_idea代码调试debug篇
主要看图,看图一目了然. 断点的设定和eclipse一样,只要点一下就可以,下面是我设定的几个断点,再下面的三个窗口是用来调试代码的,这个和eclipse类似 调试常用的快捷键 ? 1 2 3 4 5 ...
-
[转]easyui常用控件及样式API中文收藏
[转]easyui常用控件及样式收藏 2013-05-06 23:01 30612人阅读 评论(0) 收藏 举报 分类: java ee(5) 目录(?)[+] CSS类定义: div easyu ...
-
【转载】MySQL被慢sql hang住了,用shell脚本快速清除不断增长的慢sql的办法
原文地址:MySQL被慢sql hang住了,用shell脚本快速清除不断增长的慢sql的办法 作者:mchdba 某个初级dba误删index,mysql漫山遍野全是10S以上的慢sql,mysql ...
-
jdbc 连接Oracle informix Mysql
package com.basicSql; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Res ...
-
URL 传+号到后台变空格问题解决方案
今天巧合遇到这个问题,下面是网上找的解决方案. 原文:http://blog.sina.com.cn/s/blog_a0949eec01010xta.html 今天在调试客户端向服务器传递参数时,参数 ...
-
多阶段构建Docker镜像
在Docker 17.05及更高的版本中支持支持一种全新的构建镜像模式:多阶段构建: 多阶段构建Docker镜像的最大好处是使构建出来的镜像变得更小: 目前常见的两个构建镜像的方式为: 1.直接使用某 ...
-
MySQL 相关记录
删除courseID_tr之前: 新建之后: show create trigger: 下面是mysql-5.7.21-win64解压目录下的my.ini文件中的内容 [client] default ...
-
WPF-利用Blend写的平面控制闸门开关动画
原文:WPF-利用Blend写的平面控制闸门开关动画 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.****.net/yangyisen0713/article/de ...
-
关于调用数据库函数executeUpdate抛出异常
2018.06.11写一个web程序的时候发现了一个问题,解决了好几天都没解决,并且也找不到问题所在.偶然百度找到了根源所在,希望可以帮到大家. 1 在调用这个函数的时候一直抛异常.刚开始我还以为是代 ...