es集群搭建
#編寫compose.yml配置文件
#每個節(jié)點都創(chuàng)建一個elasticsearch.yml文件
用到的命令:
systemctl start docker
docker-compose up
compose.yml
es和kibana版本必須一樣
- environment:配置容器內(nèi)的環(huán)境變量
- networks:創(chuàng)建一個名為elastic的局域網(wǎng),讓各節(jié)點以及kibana,es-head相互聯(lián)系
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:意為監(jiān)聽一切地址,可有效避免發(fā)生fail to bind的報錯
- http.port: 各節(jié)點的端口號,不能相同
- transport.tcp.port:節(jié)點間交互的端口,必須要配置,且必須相同,否則無法形成多節(jié)點集群
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虛擬機配置靜態(tài)IP
用到的命令: -
cd /etc/sysconfig/network-scripts/
-
vi ifcfg-ens33 #未必是ens33,這和你的DEVICE名稱相同,以實際為準
-
配好后,記得systemctl restart network.service重啟一下網(wǎng)絡服務
虛擬機配置靜態(tài)ip:
- 必須要添加IPADDR,GATEWAY,DNS1三個配置,否則,虛擬機將無法訪問網(wǎng)絡
- GATAWAY和DNS1保持一致,與vmware的NAT網(wǎng)絡設置的值相同
- IPADDR前三位和GATAWAY一樣,最后一位隨意
- 具體值在此處查看
必須將IP設置為靜態(tài)IP,否則將無法用瀏覽器訪問!
springboot集成es
#引入依賴
#創(chuàng)建application.yml配置文件
#創(chuàng)建實體類,順便通過注解創(chuàng)建Index索引
#創(chuàng)建esUtil工具類
#創(chuàng)建啟動類和控制類
引入依賴
<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
使用注解創(chuàng)建索引(和MyBatis類似)
文章來源:http://www.zghlxwxcb.cn/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命名,將實體類映射到數(shù)據(jù)庫,類似于@TableName
- @Id:將一個字段標記為主鍵
- @Field:配置一個字段的基本屬性,用analyzer屬性指定自定義分詞器
查詢es數(shù)據(jù)庫:
使用ElasticsearchRestTemplate對象的方法即可文章來源地址http://www.zghlxwxcb.cn/news/detail-777216.html
到了這里,關于elasticSearch集群 springboot集成es 完全解析的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!