SpringBoot整合elasticsearch(三)

Docker安装elasticsearch

启动注意2点,1是内存,2是线程数(此处进行简单安装,后面会详细补充es文档)

 1 [[email protected] ~]# docker images
 2 REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
 3 elasticsearch             latest              874179f19603        12 days ago         771 MB
 4 springbootdemo4docker     latest              cd13bc7f56a0        2 weeks ago         678 MB
 5 docker.io/tomcat          latest              ee48881b3e82        4 weeks ago         506 MB
 6 docker.io/rabbitmq        latest              a00bc560660a        4 weeks ago         147 MB
 7 docker.io/centos          latest              67fa590cfc1c        7 weeks ago         202 MB
 8 docker.io/redis           latest              f7302e4ab3a8        8 weeks ago         98.2 MB
 9 docker.io/rabbitmq        3.7.16-management   3f92e6354d11        2 months ago        177 MB
10 docker.io/elasticsearch   6.8.0               d0b291d7093b        4 months ago        895 MB
11 docker.io/hello-world     latest              fce289e99eb9        9 months ago        1.84 kB
12 docker.io/java            8                   d23bdf5b1b1b        2 years ago         643 MB
13 [[email protected] ~]# docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 --name myES d0b291d7093b
14 5c4e5ffac1630f57260f739d200050535daf3ab10b1256441400a04fef70434b
15 [[email protected] ~]# docker ps -l
16 CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                                            NAMES
17 5c4e5ffac163        d0b291d7093b        "/usr/local/bin/do..."   6 seconds ago       Up 5 seconds        0.0.0.0:9200->9200/tcp, 0.0.0.0:9300->9300/tcp   myES
18 [[email protected] ~]# docker ps -l
19 CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS                       PORTS               NAMES
20 5c4e5ffac163        d0b291d7093b        "/usr/local/bin/do..."   About a minute ago   Exited (78) 34 seconds ago                       myES
21 [[email protected] ~]# docker logs -tf --tail 200 5c4e5ffac163
22 2019-10-11T15:33:21.335717000Z OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
?
发现ES容器停止了
1 2019-10-11T15:34:04.379276000Z [2019-10-11T15:34:04,372][INFO ][o.e.t.TransportService   ] [om43m7F] publish_address {172.17.0.4:9300}, bound_addresses {[::]:9300}
2 2019-10-11T15:34:04.432432000Z [2019-10-11T15:34:04,393][INFO ][o.e.b.BootstrapChecks    ] [om43m7F] bound or publishing to a non-loopback address, enforcing bootstrap checks
3 2019-10-11T15:34:04.441501000Z ERROR: [1] bootstrap checks failed
4 2019-10-11T15:34:04.441844000Z [1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
5 2019-10-11T15:34:04.497725000Z [2019-10-11T15:34:04,454][INFO ][o.e.n.Node               ] [om43m7F] stopping ...
6 2019-10-11T15:34:04.559075000Z [2019-10-11T15:34:04,557][INFO ][o.e.n.Node               ] [om43m7F] stopped
7 2019-10-11T15:34:04.559461000Z [2019-10-11T15:34:04,557][INFO ][o.e.n.Node               ] [om43m7F] closing ...
8 2019-10-11T15:34:04.580693000Z [2019-10-11T15:34:04,577][INFO ][o.e.n.Node               ] [om43m7F] closed
9 2019-10-11T15:34:04.584525000Z [2019-10-11T15:34:04,581][INFO ][o.e.x.m.p.NativeController] [om43m7F] Native controller process has stopped - no new native processes can be started
解决:

在宿主机执行:

sysctl -w vm.max_map_count=262144
原因:

vm.max_map_count参数,是允许一个进程在VMAs拥有最大数量(VMA:虚拟内存地址, 一个连续的虚拟地址空间),当进程占用内存超过时, 直接OOM。

elasticsearch占用内存较高。官方要求max_map_count需要配置到最小262144。

重新启动:
http://192.168.180.113:9200/_cat/health?v
epoch      timestamp cluster        status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1570808429 15:40:29  docker-cluster green           1         1      0   0    0    0        0             0                  -                100.0%

Springboot整合

pom.xml

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

配置文件

1 spring
2     data:
3     elasticsearch:
4       cluster-nodes: 192.168.180.113:9300
5       cluster-name: docker-cluster

启动类(采用第二种)

 1 /**
 2  * SpringBoot默认支持两种技术来和ES交互;
 3  * 1、Jest(默认不生效)
 4  *  需要导入jest的工具包(io.searchbox.client.JestClient)
 5  * 2、SpringData ElasticSearch【ES版本有可能不合适】
 6  *      版本适配说明:https://github.com/spring-projects/spring-data-elasticsearch
 7  *      如果版本不适配:2.4.6
 8  *          1)、升级SpringBoot版本
 9  *          2)、安装对应版本的ES
10  *
11  *      1)、Client 节点信息clusterNodes;clusterName
12  *      2)、ElasticsearchTemplate 操作es
13  *      3)、编写一个 ElasticsearchRepository 的子接口来操作ES;
14  *  两种用法:https://github.com/spring-projects/spring-data-elasticsearch
15  *  1)、编写一个 ElasticsearchRepository
16  */
17 @SpringBootApplication
18 public class Springboot03ElasticApplication {
19 ?
20     public static void main(String[] args) {
21         SpringApplication.run(Springboot03ElasticApplication.class, args);
22     }
23 }
发现启动项目报错:
nested exception is java.lang.IllegalStateException: availableProcessors is already set to [4], rejecting [4]

我网上查询了一下,有人是是因为整合了Redis的原因。但是我把Redis相关的配置去掉后,问题还是没有解决,最后有人说是因为netty冲突的问题。 也有人给出了解决方式就是在项目初始化钱设置一下一个属性。在初始化之前加上System.setProperty("es.set.netty.runtime.available.processors", "false");

1 @Configuration
2 public class ElasticSearchConfig {
3     @PostConstruct
4     void init() {
5         System.setProperty("es.set.netty.runtime.available.processors", "false");
6     }
7 }

Bo类

 1 @Document(indexName = "topcheer",type = "book" )
 2 @Slf4j
 3 @Data
 4 @Builder
 5 public class Book implements Serializable {
 6 ?
 7     private Integer id;
 8     private String name;
 9     private String author;
10 ?
11     public Book(String name, String author) {
12         this.name = name;
13         this.author = author;
14     }
15 ?
16     public Book(Integer id, String name, String author) {
17         this.id = id;
18         this.name = name;
19         this.author = author;
20     }
21 ?
22     public Book() {
23     }
24 }

Repository类

1 public interface BookRepository extends ElasticsearchRepository<Book,Integer> {
2 ?
3     //参照
4     // https://docs.spring.io/spring-data/elasticsearch/docs/3.0.6.RELEASE/reference/html/
5    public List<Book> findByNameLike(String name);
6 ?
7 }

测试类:

 1 @Autowired
 2     BookRepository bookRepository;
 3 ?
 4     @Test
 5     public void test02() {
 6         Book book = new Book();
 7         book.setId(1);
 8         book.setName("西游记");
 9         book.setAuthor("吴承恩");
10         bookRepository.index(book);
11 ?
12 ?
13         for (Book book1 : bookRepository.findByNameLike("游")) {
14             System.out.println(book1);
15         };
16 ?
17     }

结果:

1 2019-10-12 00:27:03.827  INFO --- [           main] Oss6ApplicationTests : Started Oss6ApplicationTests in 17.532 seconds (JVM running for 19.428)
2 2019-10-12 00:27:03.829 DEBUG --- [           main] ls.restart.Restarter : Creating new Restarter for thread Thread[main,5,main]
3 Book(id=1, name=西游记, author=吴承恩)
4 2019-10-12 00:27:04.851 DEBUG --- [       Thread-3] ebApplicationContext : Closing org.s[email protected]20f5281c, started on Sat Oct 12 00:26:47 CST 2019
5 2019-10-12 00:27:04.859  INFO --- [       Thread-3] ageListenerContainer : Waiting for workers to finish.
6 http://192.168.180.113:9200/topcheer/book/_search
7 {"took":7,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":1.0,"hits":[{"_index":"topcheer","_type":"book","_id":"1","_score":1.0,"_source":{"id":1,"name":"西游记","author":"吴承恩"}}]}}

原文地址:https://www.cnblogs.com/dalianpai/p/11657945.html

时间: 2024-10-01 14:58:11

SpringBoot整合elasticsearch(三)的相关文章

SpringBoot整合ElasticSearch实现多版本的兼容

前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和SpringBoot进行结合使用. ElasticSearch介绍 ElasticSearch是一个基于Lucene的搜索服务器,其实就是对Lucene进行封装,提供了 REST API 的操作接口 ElasticSearch作为一个高度可拓展的开源全文搜索和分析引擎,可用于快速地对大数据进行存储,搜索和分析.

SpringBoot整合Thymeleaf(三)

? Thymeleaf是SpringBoot官方推荐的用来渲染页面的一个模板,如果你对模板和模板引擎没什么概念的话,可以简单理解为Thymeleaf是一个高级简洁的JSP.如果学过MVC设计模式,那么Thymeleaf就是视图层(view)的主要核心内容. 为什么要整合Thymeleaf SpringBoot在内置Tomcat服务器,并且以Jar包方式运行,传统JSP页面不在适合这种模式开发 使用Thymeleaf有助于前后端协作,因为它在无网络环境下也可以运行,前端开发者便于在静态页面查看页面

SpringBoot整合Elasticsearch框架

新建SpringBoot项目: 修改pom.xml文件,引入spring-boot-data-elasticsearch Jar 包: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> <version>2.2.2.RELEASE</ver

【SpringBoot整合Elasticsearch】SpringBoot整合ElasticSearch

一.Linux下安装ElasticSearch 1.检测是否安装了Elasticsearch 1 ps aux |grep elasticsearch 2.安装JDK 3.下载Elasticsearch 1 wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.0.0.tar.gz 解压Elasticsearch tar -zxvf elasticsearch-6.0.0.tar.gz 移动Elasti

swagger+springBoot整合集成(三步看明白)

swagger是什么? swagger是一个后台人员专门测试的一个简单工具,特别适合做前后端分离的项目,之前我们一直都是用的postman,但是这个有点复杂,路径需要自己,而swagger则直接使用,接下来我们就直接上代码说一说swagger,文采不好往见谅! 1.首先需要两个jar包 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifac

springboot 整合 elasticsearch

1引入jar包 <!--elasticsearch--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> 2实体类 package com.miracle.config.elasticsearch.ent

springboot整合elasticsearch遇到的错误

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.2018-12-29 11:54:39.572 ERROR 7563 --- [ main] o.s.boot.SpringApplication : Application startup failed org.springframework.beans.

SpringBoot整合ElasticSearch

1.docker安装ElasticSearch ps:前提是电脑必须安装好java并配置好环境变量 从远程仓库拉取镜像 docker pull elasticsearch:6.8.0 由于es需要的内存比较大,所以配置JVM修改内存 docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 --name elastic elasticsearch:6.8.0 原文地址:https://www.

SpringBoot整合LayUI和Thymeleaf制作简单登录页面

前面已经学习过SpringBoot整合Thymeleaf,这次主要把上次提到的简单登录界面用博文形式写出来 记录一个小Demo的学习,如果没看过SpringBoot整合Thymeleaf可以看一下SpringBoot整合Thymeleaf(三) 先上页面效果图: Demo所涉及的知识点 1.SpringBoot请求映射 2.static和templates静态资源映射 只要简单了解这两个知识点,就可以做出简单的登录的页面 Demo所涉及的目录结构图 Demo所涉及的Pom文件的主要依赖 <dep