Windows集群搭建(高可用)

好处 

  1. 单台机器存储容量有限
  2. 单服务器容易出现单点故障,无法实现高可用
  3. 单服务的并发处理能力有限

步骤

  • 复制三个 elasticsearch 服务,重命名

  • 修改集群文件目录中每个节点的 config/elasticsearch.yml 配置文件(删除data文件夹与logs日志)

node-1001 节点

#节点 1 的配置信息:
#集群名称,节点之间要保持一致
cluster.name: my-elasticsearch
#节点名称,集群内要唯一
node.name: node-1001
node.master: true
node.data: true
#ip 地址
network.host: localhost
#http 端口
http.port: 1001
#tcp 监听端口
transport.tcp.port: 9301
#集群内的可以被选为主节点的节点列表
#cluster.initial_master_nodes: ["node-1", "node-2","node-3"]
#跨域配置
#action.destructive_requires_name: true
http.cors.enabled: true
http.cors.allow-origin: "*"

node-1002节点

#节点 1 的配置信息:
#集群名称,节点之间要保持一致
cluster.name: my-elasticsearch
#节点名称,集群内要唯一
node.name: node-1002
node.master: true
node.data: true
#ip 地址
network.host: localhost
#http 端口
http.port: 1002
#tcp 监听端口
transport.tcp.port: 9302
discovery.seed_hosts: ["localhost:9301"]
discovery.zen.fd.ping_timeout: 1m
discovery.zen.fd.ping_retries: 5
#集群内的可以被选为主节点的节点列表
#cluster.initial_master_nodes: ["node-1", "node-2","node-3"]
#跨域配置
#action.destructive_requires_name: true
http.cors.enabled: true
http.cors.allow-origin: "*"

node-1003 节点

#节点 3 的配置信息:
#集群名称,节点之间要保持一致
cluster.name: my-elasticsearch
#节点名称,集群内要唯一
node.name: node-1003
node.master: true
node.data: true
#ip 地址
network.host: localhost
#http 端口
http.port: 1003
#tcp 监听端口
transport.tcp.port: 9303
#候选主节点的地址,在开启服务后可以被选为主节点
discovery.seed_hosts: ["localhost:9301", "localhost:9302"]
discovery.zen.fd.ping_timeout: 1m
discovery.zen.fd.ping_retries: 5
#集群内的可以被选为主节点的节点列表
#cluster.initial_master_nodes: ["node-1", "node-2","node-3"]
#跨域配置
#action.destructive_requires_name: true
http.cors.enabled: true
http.cors.allow-origin: "*"

查看集群状态

Linux安装es

解压软件

# 解压缩 tar -zxvf elasticsearch-7.8.0-linux-x86_64.tar.gz -C /opt/module

# 改名 mv elasticsearch-7.8.0 es

新建用户

因为安全问题,Elasticsearch 不允许 root 用户直接运行,所以要创建新用户,在 root 用 户中创建新用户

useradd wl #新增 wl 用户
passwd wl #为 wl 用户设置密码
userdel -r wl #如果错了,可以删除再加
chown -R wl:wl /opt/module/es #文件夹所有者

修改配置文件

修改/opt/module/es/config/elasticsearch.yml 文件

# 加入如下配置
cluster.name: elasticsearch
node.name: node-1
network.host: 0.0.0.0
http.port: 9200
cluster.initial_master_nodes: ["node-1"]

修改/etc/security/limits.conf

# 在文件末尾中增加下面内容
# 每个进程可以打开的文件数的限制
wl soft nofile 65536
wl hard nofile 65536

修改/etc/security/limits.d/20-nproc.conf

# 在文件末尾中增加下面内容
# 每个进程可以打开的文件数的限制
wl soft nofile 65536
wl hard nofile 65536
# 操作系统级别对每个用户创建的进程数的限制
* hard nproc 4096
# 注:* 带表 Linux 所有用户名称

修改/etc/sysctl.conf

# 在文件中增加下面内容
# 一个进程可以拥有的 VMA(虚拟内存区域)的数量,默认值为 65536
vm.max_map_count=655360

重新加载: sysctl -p

使用wl用户启动

cd /opt/module/es/
#启动
bin/elasticsearch
#后台启动
bin/elasticsearch -d

单节点集群

创建名为 users 的索引,创建时分配 3 个主分片和一份副本(每个主分片拥有一个副本分片)

{"settings" : {"number_of_shards" : 3,"number_of_replicas" : 1}
}

故障转移

启动了第二个节点,集群将会拥有两个节点的集群 : 所有主分片和副本分 片都已被分配

水平扩容

主节点宕机时与redis一样,不影响其他节点,重新连上时要配置.yml文件

#候选主节点的地址,在开启服务后可以被选为主节点
discovery.seed_hosts: ["localhost:9302", "localhost:9303"]

路由计算(确定文档应该存放到哪个分片

公式:shard = hash(routing) % number_of_primary_shards

routing 是一个可变值,默认是文档的 _id ,也可以设置成一个自定义的值。 routing 通过 hash 函数生成一个数字,然后这个数字再除以 number_of_primary_shards (主分片的数量)后得到余数 。这个分布在 0 到 number_of_primary_shards-1 之间的余数,就是我们所寻求 的文档所在分片的位置。

注:要在创建索引的时候就确定好主分片的数量 并且永远不会改变 这个数量:因为如果数量变化了,那么所有之前路由的值都会无效,文档也再也找不到了。

读写流程

当发送请求的时候, 为了扩展负载,更好的做法是轮询集群中所有的节点。

写流程

读流程

文档搜索

早期的全文检索会为整个文档集合建立一个很大的倒排索引并将其写入到磁盘。 一旦新的索引就绪,旧的就会被其替换,这样最近的变化便可以被检索到。 倒排索引被写入磁盘后是不可改变的:它永远不会修改。

不变性有重要的价值:

 不需要锁。如果你从来不更新索引,你就不需要担心多进程同时修改数据的问题。

 一旦索引被读入内核的文件系统缓存,便会留在哪里,由于其不变性。只要文件系统缓存中还有足够 的空间,那么大部分读请求会直接请求内存,而不会命中磁盘。这提供了很大的性能提升。

 其它缓存(像 filter 缓存),在索引的生命周期内始终有效。它们不需要在每次数据改变时被重建,因为 数据不会变化。

 写入单个大的倒排索引允许数据被压缩,减少磁盘 I/O 和 需要被缓存到内存的索引的使用量。

当然,一个不变的索引也有不好的地方。主要事实是它是不可变的! 你不能修改它。如 果你需要让一个新的文档可被搜索,你需要重建整个索引。这要么对一个索引所能包含的 数据量造成了很大的限制,要么对索引可被更新的频率造成了很大的限制。

动态更新索引

如何在保留不变性的前提下实现倒排索引的更新?

答案是: 用更多的索引。通过增加新的补充索引来反映新近的修改,而不是直接重写整个倒排索引。每一个倒排索引都会被轮流查询到,从最早的开始查询完后再对结果进行合并(按段搜索)。

文档冲突

例:使用 Elasticsearch 存储我们网上 商城商品库存的数量, 每次我们卖一个商品的时候,我们在 Elasticsearch 中将库存数量减 少。有一天,管理层决定做一次促销。突然地,我们一秒要卖好几个商品。 假设有两个 web 程序并行运行,每一个都同时处理所有商品的销售

  • 悲观并发控制

  • 乐观并发控制

Elasticsearch 集成

Spring Data 框架集成

Spring Data 是一个用于简化数据库、非关系型数据库、索引库访问,并支持云服务的 开源框架。

    <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.6.RELEASE</version><relativePath/></parent><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>7.8.0</version></dependency><!-- elasticsearch 的客户端 --><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>7.8.0</version></dependency><!-- elasticsearch 依赖 2.x 的 log4j --><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-api</artifactId><version>2.8.2</version></dependency><dependency><groupId>org.apache.logging.log4j</groupId><artifactId>log4j-core</artifactId><version>2.8.2</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.9.9</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-test</artifactId></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId></dependency>
package com.wl.spingdata;import lombok.Data;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;@ConfigurationProperties(prefix = "elasticsearch")
@Configuration
@Data
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {private String host ;private Integer port ;//重写父类方法@Overridepublic RestHighLevelClient elasticsearchClient() {RestClientBuilder builder = RestClient.builder(new HttpHost(host, port));RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);return restHighLevelClient;}
}
package com.wl.spingdata;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Document(indexName = "shopping", shards = 3, replicas = 1)
public class Product {//必须有 id,这里的 id 是全局唯一的标识,等同于 es 中的"_id"@Idprivate Long id;//商品唯一标识/*** type : 字段数据类型* analyzer : 分词器类型* index : 是否索引(默认:true)* Keyword : 短语,不进行分词*/@Field(type = FieldType.Text, analyzer = "ik_max_word")private String title;//商品名称@Field(type = FieldType.Keyword)private String category;//分类名称@Field(type = FieldType.Double)private Double price;//商品价格@Field(type = FieldType.Keyword, index = false)private String images;//图片地址}
package com.wl.spingdata;import com.wl.spingdata.Product;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;@Repository
public interface ProductDao extends ElasticsearchRepository<Product,Long> {
}
package com.wl;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringDataElasticSearchMainApplication {public static void main(String[] args) {SpringApplication.run(SpringDataElasticSearchMainApplication.class,args);}
}
package com.wl;import com.wl.spingdata.Product;
import com.wl.spingdata.ProductDao;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.test.context.junit4.SpringRunner;import java.util.ArrayList;
import java.util.List;@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringDataESIndexTest {//注入 ElasticsearchRestTemplate@Autowiredprivate ElasticsearchRestTemplate elasticsearchRestTemplate;//创建索引并增加映射配置@Testpublic void createIndex() {//创建索引,系统初始化会自动创建索引System.out.println("创建索引");}@Testpublic void deleteIndex() {//创建索引,系统初始化会自动创建索引boolean flg = elasticsearchRestTemplate.deleteIndex(Product.class);System.out.println("删除索引 = " + flg);}@Autowiredprivate ProductDao productDao;/*** 新增*/@Testpublic void save() {Product product = new Product();product.setId(2L);product.setTitle("华为手机");product.setCategory("手机");product.setPrice(2999.0);product.setImages("http://www.baidu/hw.jpg");productDao.save(product);}//修改@Testpublic void update() {Product product = new Product();product.setId(1L);product.setTitle("小米 2 手机");product.setCategory("手机");product.setPrice(9999.0);product.setImages("http://www.baidu/xm.jpg");productDao.save(product);}//根据 id 查询@Testpublic void findById() {Product product = productDao.findById(1L).get();System.out.println(product);}//查询所有@Testpublic void findAll() {Iterable<Product> products = productDao.findAll();for (Product product : products) {System.out.println(product);}}//删除@Testpublic void delete() {Product product = new Product();product.setId(1L);productDao.delete(product);}//批量新增@Testpublic void saveAll() {List<Product> productList = new ArrayList<>();for (int i = 0; i < 10; i++) {Product product = new Product();product.setId(Long.valueOf(i));product.setTitle("[" + i + "]小米手机");product.setCategory("手机");product.setPrice(1999.0 + i);product.setImages("http://www.baidu/xm.jpg");productList.add(product);}productDao.saveAll(productList);}//分页查询@Testpublic void findByPageable() {//设置排序(排序方式,正序还是倒序,排序的 id)Sort sort = Sort.by(Sort.Direction.DESC, "id");int currentPage = 0;//当前页,第一页从 0 开始,1 表示第二页int pageSize = 5;//每页显示多少条//设置查询分页PageRequest pageRequest = PageRequest.of(currentPage, pageSize, sort);//分页查询Page<Product> productPage = productDao.findAll(pageRequest);for (Product Product : productPage.getContent()) {System.out.println(Product);}}/*** term 查询* search(termQueryBuilder) 调用搜索方法,参数查询构建器对象*/@Testpublic void termQuery() {TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("category", "手机");Iterable<Product> products = productDao.search(termQueryBuilder);for (Product product : products) {System.out.println(product);}}/*** term 查询加分页*/@Testpublic void termQueryByPage() {int currentPage = 0;int pageSize = 5;//设置查询分页PageRequest pageRequest = PageRequest.of(currentPage, pageSize);TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("category", "手机");Iterable<Product> products = productDao.search(termQueryBuilder, pageRequest);for (Product product : products) {System.out.println(product);}}
}

ElasticSearch搜索引擎(高级)相关推荐

  1. 【SpringBoot高级篇】SpringBoot集成Elasticsearch搜索引擎

    [SpringBoot高级篇]SpringBoot集成Elasticsearch搜索引擎 1. 什么是Elasticsearch? 2. 安装并运行Elasticsearch 2.1 拉取镜像 2.2 ...

  2. ElasticSearch搜索引擎详解-持续更新中

    ElasticSearch搜索引擎详解 1. ElasticSearch概述 1.1 elasticsearch是什么 1.2 全文搜索引擎 1.3 elasticsearch and solr 1. ...

  3. Elasticsearch搜索引擎(一)——基础使用

    Elasticsearch搜索引擎 关键词是中文的建议使用,英文和数字不要,模糊就行 如果普通数据库查询,无法解决如下问题 如果表记录上千万上亿了这个性能问题,另外一个如果有一个本文字段要在里面模糊配 ...

  4. ElasticSearch搜索引擎API笔记

      ElasticSearch搜索引擎API笔记 1. pom.xml <dependency> <groupId>org.elasticsearch.client</g ...

  5. ElasticSearch搜索引擎-2_学习笔记2021.4.18)

    ElasticSearch搜索引擎-2_学习笔记(2021.4.18) 前言: RESTful 介绍 , 我们通过RESTful来操作ElasticSearch (所有请求都是通过Postman ) ...

  6. 第三百六十二节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)基本的索引和文档CRUD操作、增、删、改、查...

    第三百六十二节,Python分布式爬虫打造搜索引擎Scrapy精讲-elasticsearch(搜索引擎)基本的索引和文档CRUD操作.增.删.改.查 elasticsearch(搜索引擎)基本的索引 ...

  7. ElasticSearch搜索引擎: 内存分析与设置

    在 Elasticsearch 的运行过程中,如何合理分配与设置内存是一件十分重要的事情,否则十分容易出现各种问题. 一.Elasticsearch为什么吃内存: 我们先看下 ES 服务器的总体内存消 ...

  8. ElasticSearch搜索引擎常见面试题总结

    一.ElasticSearch基础: 1.什么是Elasticsearch: Elasticsearch 是基于 Lucene 的 Restful 的分布式实时全文搜索引擎,每个字段都被索引并可被搜索 ...

  9. 第三百六十节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的基本概念...

    第三百六十节,Python分布式爬虫打造搜索引擎Scrapy精讲-elasticsearch(搜索引擎)的基本概念 elasticsearch的基本概念 1.集群:一个或者多个节点组织在一起 2.节点 ...

  10. 【Spring Data ElasticSearch】高级查询,聚合

    [Spring Data ElasticSearch]高级查询,聚合 1. 高级查询 1.1 基本查询 1.2 自定义查询 1.3 分页查询 1.4 排序 2. 聚合 2.1 聚合为桶 2.2 嵌套聚 ...

最新文章

  1. ROS image_transport使用笔记
  2. redis操作大数据
  3. 请MM吃饭之工厂模式实现
  4. lumion自动保存_LUMION吊打MAX,轻松玩转规划渲染!
  5. 最长无重复字符子串?
  6. asp.net中有关URL的信息
  7. 中移动总裁李跃:发展Wifi才是长久之计
  8. 智慧型数据中心露峥嵘
  9. mysql group by 用法解析(详细)
  10. 智慧校园安防平台应用解决方案
  11. 《物流系统_SSM》项目研发总结
  12. 小说网站系统源码|PHP付费小说网站源码带app
  13. 卢菲菲记忆课程(一、了解记忆认识大脑)
  14. 向下兼容性格什么意思_成大事者都有一种高段位能力:向下兼容
  15. 解决“Windows 平台 FIPS 验证的加密算法的一部分”问题
  16. godaddy mysql不存在_Godaddy免费空间问题及解决方案总结
  17. 挂耳耳机哪个牌子好,盘点五款挂耳式蓝牙耳机推荐
  18. Linux怎么安装ca证书,如何在CentOS上安装自定义CA证书?
  19. 计算机考研高校改初试科目,考研初试科目修改!报考的考生注意了!
  20. Python基础1——读取数据(公众号数据科学实践)

热门文章

  1. 华为机试--字符串ASC码+偏移量实现加密
  2. OpenCV 颜色检测| color detection
  3. 当代人类不可或缺的至宝—WiFi
  4. 关于144hz屏幕观看网页视频时存在黑屏屏闪
  5. Aquarius's Trial F - 6 HDU - 2102 A计划
  6. lightroom 闪退_微信QQ一碰就闪退,别人永远看不了你的隐私~
  7. request_threaded_irq()参数
  8. 场效应管和三极管的区别
  9. Flume简介及配置
  10. 无广告、无推送、无新闻,这7款手机浏览器实用且优秀