ElasticSearch mounted and integrated into the Centos7 springboot

Search engine search framework elasticsearch knowledge and basic introduction
        elasticsearch: for a particularly large amount of data, PB, TB
          pure java development, springboot use, version 5.6
          es upgrade 4-> 5 version, the big change, but after 5 version, the modifications are minor
    elasticSearch main feature

        1, features: full-text search, structured retrieval, statistics, analysis, near real-time processing, distributed search (can deploy hundreds of servers), PB-level data processing
            search error correction, auto-complete
        2, usage scenarios: log search , data aggregation, data monitoring, reporting, statistical analysis
        3, users at home and abroad: Wikipedia, Stack Overflow, GitHub

    New features explain
        1,6.2.x version is based on Lucene 7.x, faster, further enhance performance, the corresponding sequence of components, upgrading to 2.8 Jackson
            MySQL: Database the Table rocord
            es: index of the type (can only be one) document

        2, recommended the introduction of version 5.0 of the Java REST / HTTP client, less dependent, more convenient to use than Transport, in benchmark tests, the performance is not lost on Transport client,

        Version 5.0 to 6.0, each with a corresponding API updates the document also shows that, in this way development is recommended to use, load balancing across all available nodes
        failover in case of failure and the node-specific response code, failed connection penalty (whether to retry the failed node depends on the number of consecutive failed; the longer the wait, the more the number of failures fail, the client node again before attempting the same time)
        3 (important) no longer supports a library index inside multiple type, 6.x version of the index which has banned more than a type, so a library index index can have only 1 type

        Official documentation:
        1,6.0 update feature
         https://www.elastic.co/guide/en/elasticsearch/reference/6.0/release-notes-6.0.0.html#breaking-java-6.0.0
        2,6.1 update feature 
        https://www.elastic.co/guide/en/elasticsearch/reference/6.1/release-notes-6.1.0.html

ES installed under Centos7 environment and remote access

  •  elasticsearch jdk need support, I installed the jdk1.8. Configuration jdk1.8, Download following, or may be downloaded from the official website. Link: https: //pan.baidu.com/s/1rzmEDtcsoIUKgjGpBVadzg extraction code: 4ovu 
  • #解压命令
    tar -zxvf jdk-8u131-linux-x64.tar.gz

    è§ £ åJDKå®è £ å.png

        è§ £ åå®æ.png

  • Configuration jdk
#进入/etc/profile
vim /etc/profile
#在profile文件末尾输入以下文字
JAVA_HOME=/usr/local/java/jdk1.8.0_131
CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME
export CLASSPATH
export PATH
#保存并退出
esc : wq

   éç½®JDK.png

  • Refresh configuration file to verify whether the configuration JDK
source /etc/profile
java -version

éªè¯.png

  • The installation package downloaded elasticserach decompress the next conf after extracting the root directory. Then open the file elasticsearch.yml
vi elasticsearch.yml

Modify configuration items to network.host: 0.0.0.0 below, save and exit.

PS:elasticserach所需内存比较大,如果服务器的内存不够会报内存溢出的错误,此时可以通过修改conf下的jvm.options文件来解决问题。修改下图位置,将启动内存设置成128M

  • 考虑安全原因elasticsearch是不允许root用户启动的,需要创建用户

创建用户

adduser 用户名

设置密码

passwd 用户名
  • 启动elasticsearch。在bin目录下执行启动命令(需要切换到创建的用户 su 用户名)
./elasticsearch

此时启动之后就无法再执行其他命令了,这样会很不方便。可以执行如下命令,进行后台启动

./elasticsearch -d

启动成功之后本地浏览器输入http://IP:9200进行访问,出现下图信息即可

接下来进行springboot集成elasticserach

  • pom文件下加入相关jar包依赖
   <!--添加ES相关jar包-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.1</version>
		</dependency>
  • application.properties配置文件中添加配置信息
##=========Elasticsearch配置=========
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=IP:9300
spring.data.elasticsearch.repositories.enabled=true 
  • 创建Article.java实体类
import org.springframework.data.elasticsearch.annotations.Document;

import java.io.Serializable;

/**
 * @Author:Lyd
 * @Date 2020/1/8 11:08
 * @Description 文章对象
 */
@Document(indexName="blog",type = "article")
public class Article implements Serializable {
    private long id;
    private String title;
    private String summary;
    private String content;
    private int pv;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public int getPv() {
        return pv;
    }

    public void setPv(int pv) {
        this.pv = pv;
    }
}
  • 创建ArticleRespository.java接口类, 接口继承ElasticSearchRepository,里面有很多默认实现。
import com.lyd.pojo.Article;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component;

/**
 * @Author:Lyd
 * @Date 2020/1/8 14:35
 * @Description
 */
@Component
//@Repository
public interface ArticleRespository extends  ElasticsearchRepository<Article, Long> {
}
  • 创建controller控制层类ArticalController.java
import com.lyd.pojo.Article;
import com.lyd.pojo.JsonData;
import com.lyd.respository.ArticleRespository;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @Author:Lyd
 * @Date 2020/1/8 14:50
 * @Description  es搜索引擎控制层实现
 */
@RestController
@RequestMapping("/api/v1/artical")
public class ArticalController {
    @Autowired
    private ArticleRespository articleRespository;

    /**
     * 保存
     * @return
     */
    @GetMapping(value = "save")
    public  Object save(){
        Article article=new Article();
        article.setId(1L);
        article.setContent("this is 内容");
        article.setPv(888);
        article.setTitle("springboot集成Elasticsearch 搜索引擎学习");
        article.setSummary("概要搜索");
        articleRespository.save(article);
        return JsonData.buildSuccess();
    }

    /**
     * ES查询数据
     * @param title
     * @return json格式查询数据
     */
    @GetMapping(value="search")
    public Object search(String title){
        QueryBuilder   queryBuilder= QueryBuilders.matchQuery("title",title);
        Iterable<Article> list=articleRespository.search(queryBuilder);
        return JsonData.buildSuccess(list);
    }
}

     启动项目浏览器中访问localhost:8080/api/v1/artical/save,执行数据保存操作,然后通过localhost:8080/api/v1/artical/search进行查询操作

PS:如果服务器在阿里云上需要开发9200、9300端口

配置es出现相关问题处理(阿里云、腾讯云,亚马逊云安装问题集合)
        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]
        Solution: non-root users
            to add user: useradd -m provided user name and password passwd username 

        3. Question three
            ./elasticsearch
            Exception in the Thread "main" java.nio.file.AccessDeniedException: /usr/local/software/temp/elasticsearch-6.2.2/config/jvm.options
           solved: insufficient privileges chmod 777 -R current es directory

        Common Configuration Problems Information: https: //www.jianshu.com/p/c5d6ec0f35e0

Published 44 original articles · won praise 36 · views 80000 +

Guess you like

Origin blog.csdn.net/m0_37027631/article/details/104050738