es集群搭建
#编写compose.yml配置文件
#每个节点都创建一个elasticsearch.yml文件
用到的命令:
systemctl start docker
docker-compose up
compose.yml
es和kibana版本必须一样
- environment:配置容器内的环境变量
- networks:创建一个名为elastic的局域网,让各节点以及kibana,es-head相互联系
version: '3'
services:
es01:
image: elasticsearch:7.6.2
environment:
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ports:
- 9201:9201
volumes:
- ./9201/conf/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
- ./9201/data:/usr/share/elasticsearch/data
- ./9201/plugins:/usr/share/elasticsearch/plugins
networks:
- elastic
es02:
image: elasticsearch:7.6.2
environment:
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ports:
- 9202:9202
volumes:
- ./9202/conf/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
- ./9202/data:/usr/share/elasticsearch/data
- ./9202/plugins:/usr/share/elasticsearch/plugins
networks:
- elastic
es03:
image: elasticsearch:7.6.2
environment:
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
ports:
- 9203:9203
volumes:
- ./9203/conf/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
- ./9203/data:/usr/share/elasticsearch/data
- ./9203/plugins:/usr/share/elasticsearch/plugins
networks:
- elastic
kibana:
image: kibana:7.6.2
ports:
- 5601:5601
depends_on:
- es01
- es02
- es03
volumes:
- ./kibana.yml:/usr/share/kibana/config/kibana.yml
elasticsearch-head:
image: wallbase/elasticsearch-head:6-alpine
environment:
TZ: 'Asia/Shanghai'
ports:
- '9100:9100'
networks:
- elastic
networks:
elastic:
driver: bridge
elasticsearch.yml
- network.host: 0.0.0.0:意为监听一切地址,可有效避免发生fail to bind的报错
- http.port: 各节点的端口号,不能相同
- transport.tcp.port:节点间交互的端口,必须要配置,且必须相同,否则无法形成多节点集群
node.name: es01
cluster.name: es-cluster
network.host: 0.0.0.0
http.port: 9201
transport.tcp.port: 9310
discovery.seed_hosts: ["es01", "es02", "es03"]
cluster.initial_master_nodes: ["es01","es02","es03"]
node.master: true
node.data: true
http.cors.enabled: true
network.tcp.keep_alive: true
network.tcp.no_delay: true
transport.tcp.compress: true
http.cors.allow-origin: "*"
http.max_content_length: 200mb
gateway.recover_after_nodes: 2
cluster.routing.allocation.cluster_concurrent_rebalance: 16
cluster.routing.allocation.node_concurrent_recoveries: 16
cluster.routing.allocation.node_initial_primaries_recoveries: 16
-
采用vmware安装centos7来模拟服务器
-
采用NAT模式
-
centos7虚拟机配置静态IP
用到的命令: -
cd /etc/sysconfig/network-scripts/
-
vi ifcfg-ens33 #未必是ens33,这和你的DEVICE名称相同,以实际为准
-
配好后,记得systemctl restart network.service重启一下网络服务
虚拟机配置静态ip:
- 必须要添加IPADDR,GATEWAY,DNS1三个配置,否则,虚拟机将无法访问网络
- GATAWAY和DNS1保持一致,与vmware的NAT网络设置的值相同
- IPADDR前三位和GATAWAY一样,最后一位随意
- 具体值在此处查看
必须将IP设置为静态IP,否则将无法用浏览器访问!
springboot集成es
#引入依赖
#创建application.yml配置文件
#创建实体类,顺便通过注解创建Index索引
#创建esUtil工具类
#创建启动类和控制类
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
application.yml:
server:
port: xxxx
spring:
elasticsearch:
uris:
- http://192.168.xxx.xxx:9201
- http://192.168.xxx.xxx:9202
- http://192.168.xxx.xxx:9203
使用注解创建索引(和MyBatis类似)
文章来源:https://www.toymoban.com/news/detail-777216.html
import lombok.Data;
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
@Document(indexName = "user",createIndex = true)
public class EsEntity {
@Id
@Field(type = FieldType.Auto)
private Long id;
@Field(type = FieldType.Text)
private String userName;
}
- @Document():给index命名,将实体类映射到数据库,类似于@TableName
- @Id:将一个字段标记为主键
- @Field:配置一个字段的基本属性,用analyzer属性指定自定义分词器
查询es数据库:
使用ElasticsearchRestTemplate对象的方法即可文章来源地址https://www.toymoban.com/news/detail-777216.html
到了这里,关于elasticSearch集群 springboot集成es 完全解析的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!