springboot elasticsearch docker

首先先用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 {br/>dao層
@Component
public interface ArticleRepository extends ElasticsearchRepository<Article, Long> {
保存
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 用戶名 進入設置密碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章