运维开发网

springboot elasticsearch docker

运维开发网 https://www.qedev.com 2020-02-23 13:06 出处:51CTO 作者:林博轩
首先先用docker搭建es搜索es+springboot大都又是在本地搭建的es。。于是觉得自己边搜边写一篇博客作为记录使用docker:首先安装dockerpullelasticsearchdockerrun-d--namees-p9200:9200-e"discovery.type=single-node"id打开服务器ip:9200可以正常打开dockerexec-it

首先先用docker 搭建 es

springboot elasticsearch docker

搜索 es+springboot 大都又是 在本地 搭建的es。。于是觉得自己边搜边写一篇博客 作为记录使用

docker:

首先安装

docker pull elasticsearch

docker run -d --name es -p 9200:9200 -e "discovery.type=single-node" id

打开 服务器ip:9200 可以正常打开

docker exec -it es /bin/bash

cd plugins/

plugin install mobz/elasticsearch-head

服务器ip:9200/_plugin/head/ 打开管理插件

但登陆elasticsearch-head点击连接9200 没反应

docker exec -it /bin/bash

cd config

vi elsaticsearch.yml

增加以下两行

http.cors.enabled: true

http.cors.allow-origin: "*"即可

点击连接 解决问题

整合springboot过程及其痛苦 搜了无数博客 都不行 就是愣连接不上9300端口 。。 于是乎 暂停dockr部署 转为命令启动方式以后有时间在去看看


命令:

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6.8.tar.gz

tar -zxvf elasticsearch-5.6.8.tar.gz

外网:

config目录下面elasticsearch.yml

修改为 network.host: 0.0.0.0

启动

bin/.elasticsearch

Spring Data 9300端口集成

Restful API 9200端口集成


springboot整合es

实体类

indexName为索引库

type为表

@Document(indexName = "blog", type = "article")

public class Article implements Serializable {dao层

@Component
br/>dao层

@Component
保存

articleRepository.save(new Article());

查询

QueryBuilder queryBuilder = QueryBuilders.matchQuery("title",title);

Iterable<Article> list = articleRepository.search(queryBuilder);


报错解决方案:

ERROR: [2] bootstrap checks failed

[1]: max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]

编辑 /etc/security/limits.conf,追加以下内容;

  • soft nofile 65536
  • hard nofile 65536

    此文件修改后需要重新登录用户,才会生效

    [2]: max Virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

    编辑 /etc/sysctl.conf,追加以下内容:

    vm.max_map_count=655360

    保存后,执行:

    sysctl -p

    重新启动,成功。

    1、问题一

    Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000c5330000, 986513408, 0) failed; error='Cannot allocate memory' (errno=12)

    #

    There is insufficient memory for the Java Runtime Environment to continue.

        #Native memory allocation (mmap) failed to map 986513408 bytes for committing reserved memory.
        #An error report file with more information is saved as:
        #/usr/local/software/temp/elasticsearch-6.2.2/hs_err_pid1912.log
    解决:内存不够,购买阿里云的机器可以动态增加内存
    
    2、问题二
        [root@iZwz95j86y235aroi85ht0Z bin]# ./elasticsearch
        [2018-02-22T20:14:04,870][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [] uncaught exception in thread [main]
        org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
        at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:125) ~[elasticsearch-6.2.2.jar:6.2.2]
        at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:112) ~[elasticsearch-6.2.2.jar:6.2.2]
        at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-6.2.2.jar:6.2.2]
        at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:124) ~[elasticsearch-cli-6.2.2.jar:6.2.2]
    解决:用非root用户
        添加用户:useradd -m 用户名  然后设置密码  passwd 用户名
    
    3、问题三
        ./elasticsearch
        Exception in thread "main" java.nio.file.AccessDeniedException: /usr/local/software/temp/elasticsearch-6.2.2/config/jvm.options
       解决:权限不够 chmod 777 -R 当前es目录

项目起不来

启动的时候报错:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'controller': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'goodsRepository': Cannot resolve reference to bean 'elasticsearchTemplate' while setting bean property 'elasticsearchOperations'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'elasticsearchTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchDataAutoConfiguration.class]: Unsatisfied dependency expressed through method 'elasticsearchTemplate' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'elasticsearchClient' defined in class path resource [org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.elasticsearch.client.transport.TransportClient]: Factory method 'elasticsearchClient' threw exception; nested exception is java.lang.IllegalStateException: availableProcessors is already set to [12], rejecting [12]


docker vim

继续搜索解决方案 也没其他好办法 挨个博客试呗, 有的博客是让添加配置的 可是vim elasticsearch.yml 的时候报错:bash: vim: command not found

在使用Docker容器时,有时候里边没有安装vim,敲vim命令时提示说:vim: command not found,这个时候就需要安装vim,可是当你敲apt-get install vim命令时,提示:

Reading package lists... Done

Building dependency tree

Reading state information... Done

E: Unable to locate package vim

1)这时候需要敲:apt-get update,这个命令的作用是:同步 /etc/apt/sources.list 和 /etc/apt/sources.list.d 中列出的源的索引,这样才能获取到最新的软件包。

2)等更新完毕以后再敲命令:apt-get install vim命令即可。


es redis

好像是加了redis冲突了??ok 去掉redis配置

adding transport node : 60.205.217.216:9200特别慢 然后error 崩溃啊!

failed to load elasticsearch nodes : org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: [{#transport#-1}{jU1jEJLVTweRhMI98Kgf8A}{60.205.217.216}{60.205.217.216:9200}]

崩溃了。。弄了一天了,把项目中关于es的都删了,找个教程重新走一遍


Linux 添加用户 并设置密码

因为es不能root用户起(root权限太大,危险)所以新建了用户

useradd -r 添加系统用用户

passwd 用户名 进入设置密码

0

精彩评论

暂无评论...
验证码 换一张
取 消