elasticsearch8.x版本docker部署说明和集成springboot

本文详细描述了一个在多节点环境中部署Elasticsearch(包括创建目录、配置YML文件、启动服务、Kibana集成和SpringBoot集成)的过程,以及如何在SpringBoot应用中使用Elasticsearch客户端。
摘要由CSDN通过智能技术生成

前提,当前部署没有涉及证书和https访问
1、环境说明,我采用三个节点,每个节点启动两个es,用端口区分

主机角色ip和端口
服务器Amaster192.168.2.223:9200
服务器Adata192.168.2.223:9201
服务器Bdata,master192.168.2.224:9200
服务器Bdata192.168.2.224:9201
服务器Cdata,master192.168.2.225:9200
服务器Cdata192.168.2.225:9201

2、三个节点都需要执行创建部署文件目录

#es1需要的文件目录
mkdir -p /mydata/es/node1/data
chmod 777  /mydata/es/node1/data
mkdir -p /mydata/es/node1/conf
chmod 777  /mydata/es/node1/conf
mkdir -p /mydata/es/node1/plugins
chmod 777  /mydata/es/node1/plugins
mkdir -p /mydata/es/node1/logs
chmod 777  /mydata/es/node1/logs
#es2需要的文件目录
mkdir -p /mydata/es/node2/data
chmod 777  /mydata/es/node2/data
mkdir -p /mydata/es/node2/conf
chmod 777  /mydata/es/node2/conf
mkdir -p /mydata/es/node2/plugins
chmod 777  /mydata/es/node2/plugins
mkdir -p /mydata/es/node2/logs
chmod 777  /mydata/es/node2/logs

2、服务器A编写es1的yml文件

cd /mydata/es/node1/conf
vi elasticsearch.yml
#输入以下内容

cluster.name: elasticsearch-cluster
#节点名称
node.name: es-node1
#节点通信ip
network.bind_host: 0.0.0.0
#节点ip
network.publish_host: 192.168.2.223
#节点通信端口
http.port: 9200
transport.port: 9300
#跨域配置
http.cors.enabled: true
http.cors.allow-origin: "*"
#节点角色
node.roles: [ master]
#和其他节点通信的ip端口
discovery.seed_hosts: ["192.168.2.223:9300","192.168.2.223:9301", "192.168.2.224:9300", "192.168.2.224:9301","192.168.2.225:9300","192.168.2.225:9301"]
#初始化master节点的配置,用于选举的mater节点ip,后续的节点不需要该配置
cluster.initial_master_nodes: [192.168.2.223,192.168.2.224,192.168.2.225]
#http配置
# Enable security features
xpack.security.enabled: false
xpack.security.enrollment.enabled: false
# Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents
xpack.security.http.ssl:
  enabled: false
  #keystore.path: certs/http.p12

# Enable encryption and mutual authentication between cluster nodes
xpack.security.transport.ssl:
  enabled: false
  #verification_mode: certificate
  #keystore.path: certs/transport.p12
  #truststore.path: certs/transport.p12

保存退出
2、服务器A编写es2的yml文件

cd /mydata/es/node2/conf
vi elasticsearch.yml
#输入以下内容

cluster.name: elasticsearch-cluster
node.name: es-node2
network.bind_host: 0.0.0.0
network.publish_host: 192.168.2.223
http.port: 9201
transport.port: 9301
http.cors.enabled: true
http.cors.allow-origin: "*"
node.roles: [ data]
discovery.seed_hosts: ["192.168.2.223:9300","192.168.2.223:9301", "192.168.2.224:9300", "192.168.2.224:9301","192.168.2.225:9300","192.168.2.225:9301"]

# Enable security features
xpack.security.enabled: false
xpack.security.enrollment.enabled: false

# Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents
xpack.security.http.ssl:
  enabled: false
  #keystore.path: certs/http.p12

# Enable encryption and mutual authentication between cluster nodes
xpack.security.transport.ssl:
  enabled: false
  #verification_mode: certificate
  #keystore.path: certs/transport.p12
  #truststore.path: certs/transport.p12

4、启动es1和es2

#设置yml文件权限,不然会报错
chmod 777  /mydata/es/node1/conf/*
chmod 777  /mydata/es/node2/conf/*
#启动
docker run --restart=always  -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 -v /mydata/es/node1/conf/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /mydata/es/node1/data:/usr/share/elasticsearch/data -v /mydata/es/node1/plugins:/usr/share/elasticsearch/plugins --name es01 elasticsearch:8.6.2

docker run --restart=always  -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9201:9201 -p 9301:9301 -v /mydata/es/node2/conf/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml -v /mydata/es/node2/data:/usr/share/elasticsearch/data -v /mydata/es/node2/plugins:/usr/share/elasticsearch/plugins --name es02 elasticsearch:8.6.2
 vi /etc/sysctl.conf    # 在最后面追加下面内容
 vm.max_map_count=655360
  执行  sysctl -p

5、服务器B和服务器C配置参考服务器A进行部署,需要调整的就是ip和端口就行

6、安装kibana

docker run -d \
--name kibana \
-e ELASTICSEARCH_HOSTS=http://192.168.2.223:9200  \
-p 5601:5601  \
kibana:8.6.2

7、谷歌插件安装es-head,下载解压,拖到谷歌浏览器的插件上就好
百度云链接:https://pan.baidu.com/s/1V1tMlgPUQpcaKtqrFjGf2A
提取码:yzwf

8、验证,输入http://192.168.2.223:9200/_cat/nodes,生产环境需要自己去定义角色,自己去了解8.x的相关角色和架构,我这里就不解释了。
在这里插入图片描述
在这里插入图片描述
9、角色相关说明推荐博主博文链接
http://www.manongjc.com/detail/62-gdexfeqaydpetga.html

10、springboot集成8x,在springboot的pom文件中导入依赖,导入中如果有报错,很可能是jackson版本和自身的springboot版本不匹配

<dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.11.4</version>
        </dependency>

        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>jakarta.json</artifactId>
            <version>2.0.1</version>
        </dependency>

        <dependency>
            <groupId>co.elastic.clients</groupId>
            <artifactId>elasticsearch-java</artifactId>
            <version>8.6.2</version>
        </dependency>
        <!--重写springboot的client版本,不然会报错>-->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>8.6.2</version>
        </dependency>

11、编写配置类

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ElasticSearchConfig {
    //注入IOC容器
    @Bean
    public ElasticsearchClient elasticsearchClient(){
        RestClient client = RestClient.builder(new HttpHost("192.168.2.223", 9200,"http")).build();
        ElasticsearchTransport transport = new RestClientTransport(client,new JacksonJsonpMapper());
        return new ElasticsearchClient(transport);
    }
}

12、编写控制测试类

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.IndexResponse;
import co.elastic.clients.elasticsearch.indices.CreateIndexResponse;
import co.elastic.clients.elasticsearch.indices.GetIndexResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
@RequestMapping("/v1/test")
public class Test {
    @Autowired
    private ElasticsearchClient client;

    @PostMapping("/v1/createTest")
    public void createTest() throws IOException {

        //写法比RestHighLevelClient更加简洁
        CreateIndexResponse indexResponse = client.indices().create(c -> c.index("user"));
    }
    @GetMapping("/v1/queryTest")
    public void queryTest() throws IOException {
        GetIndexResponse getIndexResponse = client.indices().get(i -> i.index("user"));
    }
    @PostMapping("/v1/addDocumentTest")
    public void addDocumentTest() throws IOException {

        User user = new User("xkc", 18);
        IndexResponse indexResponse = client.index(i -> i
                .index("user")
                //设置id
                .id("xkc")
                //传入user对象
                .document(user));

    }
}
要构建一个基于上述技术栈的应用程序,涉及多个组件和技术,下面是一些关键点的简介: 1. **Spring Boot**: 是一个快速开发框架,简化了Java应用的配置和启动过程。 - 示例:用于创建简单的RESTful API服务[^4]。 2. **Spring Cloud**: 提供了一组工具和服务来扩展微服务架构。 - 功能包括服务发现、配置中心、API网关等[^5]。 3. **RabbitMQ**: 消息队列服务,支持异步通信和解耦。 - 在Spring Cloud中集成,可以用来实现消息驱动架构[^6]。 4. **Redis**: 缓存数据库,提高应用程序性能。 - 可以缓存热点数据或会话信息[^7]。 5. **Elasticsearch**: 分布式搜索和分析引擎,常用于全文检索。 - 支持复杂查询和实时数据分析[^8]。 6. **Xxl-sso**: 企业级权限管理系统,用于身份验证和授权[^9]。 7. **LCN**: 可能指的是Linux容器网络,Docker的基础组件。 - 管理容器间的网络连接[^10]。 8. **Nginx**: 反向代理服务器,优化HTTP请求和负载均衡。 - 与Spring Boot结合时,可能作为API Gateway[^11]。 9. **七牛云**: 对象存储服务,用于文件上传和管理。 - 存储静态资源[^12]。 10. **Swagger2**: 开源API文档生成工具。 - 用于自动生成API文档[^13]。 11. **MySQL**: 关系型数据库,存储业务数据。 - 数据持久化[^14]。 12. **Maven**: 项目管理和依赖管理工具。 - 用于构建和打包项目[^15]。 13. **GitLab**: 代码版本控制系统,用于版本控制和协作开发。 - 版本控制和CI/CD[^16]。 14. **Docker**: 虚拟化平台,便于部署和运行应用。 - 快速构建可移植的环境[^10]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值