关于elasticsearch是什么、elasticsearch的原理及elasticsearch能干什么,就不多说了,主要记录下自己的一个使用过程。

  1、安装

  elasticsearch是用java编写的,所以它的运行离不开jdk,jdk的安装这里不再啰嗦,我使用的是虚拟机是centos7,已经装好了jdk1.8,下面说下自己安装elasticsearch的过程。

  (1)到官网 https://www.elastic.co/guide/en/elasticsearch/reference/current/install-elasticsearch.html,查看各个操作系统的安装方式,找到对应的,我的是centos7,以root身份登录,执行 wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.5.1.tar.gz。

  (2)解压 tar -zvxf elasticsearch-6.5.1.tar.gz -C /usr/local

  (3)默认ES 6.X 是不允许root用户运行的,否则ES运行的时候会报错,所以我们需要创建新的用户,groupadd es ——>useradd es -g es——>passwd es 按照提示设置密码即可。修改权限 chown -R es:es elasticsearch-6.5.1

  (4)修改配置,支持外网访问(记得关防火墙),在合适的位置创建两个文件夹,分别用来存储elasticsearch的数据和日志,执行vim config/elasticsearch.yml,取消下列对应的项的注释并修改      

  cluster.name: li-application #集群名称,可以自行修改
  node.name: linux-2 #节点名称,自行修改
  network.host: 0.0.0.0 #主机地址,这里写本机IP
  http.port: 9200 #端口
  path.data: /var/lib/es 数据存储位置
  path.logs: /var/logs/es 日志存储位置

  (5)执行vim /etc/security/limits.conf,在结尾加上如下配置,其中es是用户名

  es soft nofile 65536
  es hard nofile 131072
  es soft nproc 4096
  es hard nproc 4096

  (6)执行 vi /etc/sysctl.conf,在文件末尾添加 vm.max_map_count=655360,保存,并执行命令 sysctl -p

  (7)切换到es用户,进入/usr/local/elasticsearch-6.5.1,执行./bin/elasticsearch -d (后台启动)。

  (8)执行 curl -i localhost:9200,会有一个测试信息的响应,表示安装成功。

  2、整合springboot2.0

  (1)引入依赖

      <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency> <!--集合工具包-->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>

  (2)添加配置,注意端口号是9300

spring.data.elasticsearch.cluster-name=my-application
spring.data.elasticsearch.cluster-nodes=192.168.1.126:9300
spring.data.elasticsearch.repositories.enabled=true
server.port=11111

  (3)编写对应的代码

package com.example.demo;

import org.springframework.data.elasticsearch.annotations.Document;

import com.fasterxml.jackson.annotation.JsonProperty;

//indexName代表所以名称,type代表表名称
@Document(indexName = "wantu_notice_info", type = "doc")
public class Notice { //id
@JsonProperty("auto_id")
private Long id; //标题
@JsonProperty("title")
private String title; //公告标签
@JsonProperty("exchange_mc")
private String exchangeMc; //公告发布时间
@JsonProperty("create_time")
private String originCreateTime; //公告阅读数量
@JsonProperty("read_count")
private Integer readCount; 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 getExchangeMc() {
return exchangeMc;
} public void setExchangeMc(String exchangeMc) {
this.exchangeMc = exchangeMc;
} public String getOriginCreateTime() {
return originCreateTime;
} public void setOriginCreateTime(String originCreateTime) {
this.originCreateTime = originCreateTime;
} public Integer getReadCount() {
return readCount;
} public void setReadCount(Integer readCount) {
this.readCount = readCount;
} public Notice(Long id, String title, String exchangeMc, String originCreateTime, Integer readCount) {
super();
this.id = id;
this.title = title;
this.exchangeMc = exchangeMc;
this.originCreateTime = originCreateTime;
this.readCount = readCount;
} public Notice() {
super();
} }
package com.example.demo;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component; @Component
public interface NoticeRepository extends ElasticsearchRepository<Notice, Long> { }
package com.example.demo;

import java.util.ArrayList;
import java.util.List; import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.google.common.collect.Lists; @RestController
@RequestMapping("/api/v1/article")
public class NoticeController { @Autowired
private NoticeRepository nticeRepository; @GetMapping("/save")
public Object save(long id, String title){ Notice article = new Notice();
article.setId(id);
article.setReadCount(123);
article.setTitle(title);
return nticeRepository.save(article);
} /**
* @param title 搜索标题
* @param pageable page = 第几页参数, value = 每页显示条数
*/
@GetMapping("/search")
public Object search(String title ,@PageableDefault() Pageable pag){ //按标题进行搜索
QueryBuilder builder = QueryBuilders.matchQuery("title", title);
//如果实体和数据的名称对应就会自动封装,pageable分页参数
Iterable<Notice> listIt = nticeRepository.search(builder, pag);
//Iterable转list
List<Notice> list= Lists.newArrayList(listIt); return list;
} @RequestMapping("/all")
public List<Notice> all() throws Exception {
Iterable<Notice> data = nticeRepository.findAll();
List<Notice> ds = new ArrayList<>();
for (Notice d : data) {
ds.add(d);
}
return ds;
}
@RequestMapping("/id")
public Object findid(long id) throws Exception {
return nticeRepository.findById(id).get(); } }

  启动项目,先添加几条数据,然后依次访问全部记录、带"三"的记录、带"吃饭"的记录,效果如下:

elasticsearch安装及与springboot2.x整合的更多相关文章

  1. SpringBoot2.x整合Prometheus+Grafana【附源码+视频】

    图文并茂,新手入门教程,建议收藏 SpringBoot2.x整合Prometheus+Grafana[附源码+视频] 附源码+视频 目录 工程简介 简介 Prometheus grafana Spri ...

  2. 基于ELK5.1(ElasticSearch, Logstash, Kibana)的一次整合测试

    前言开源实时日志分析ELK平台(ElasticSearch, Logstash, Kibana组成),能很方便的帮我们收集日志,进行集中化的管理,并且能很方便的进行日志的统计和检索,下面基于ELK的最 ...

  3. 基于ELK5.1(ElasticSearch, Logstash, Kibana)的一次整合

    前言开源实时日志分析ELK平台(ElasticSearch, Logstash, Kibana组成),能很方便的帮我们收集日志,进行集中化的管理,并且能很方便的进行日志的统计和检索,下面基于ELK的最 ...

  4. 【SpringBoot】息队列介绍和SpringBoot2.x整合RockketMQ、ActiveMQ

    ========================13.消息队列介绍和SpringBoot2.x整合RockketMQ.ActiveMQ ======================= 1.JMS介绍和 ...

  5. 消息队列介绍和SpringBoot2.x整合RockketMQ、ActiveMQ 9节课

    1.JMS介绍和使用场景及基础编程模型     简介:讲解什么是小写队列,JMS的基础知识和使用场景     1.什么是JMS: Java消息服务(Java Message Service),Java ...

  6. SpringBoot2.x整合Redis实战 4节课

    1.分布式缓存Redis介绍      简介:讲解为什么要用缓存和介绍什么是Redis,新手练习工具 1.redis官网 https://redis.io/download          2.新手 ...

  7. SpringBoot2.x 整合Spring-Session实现Session共享

    SpringBoot2.x 整合Spring-Session实现Session共享 1.前言 发展至今,已经很少还存在单服务的应用架构,不说都使用分布式架构部署, 至少也是多点高可用服务.在多个服务器 ...

  8. Elasticsearch安装、原理学习总结

    ElasticSearch ElasticSearch概念 Elasticsearch是Elastic Stack核心的分布式搜索和分析引擎. 什么是Elastic Stack Elastic Sta ...

  9. ElasticSearch安装及部署

    安装及部署 一.环境配置 操作系统:Cent OS 7ElasticSearch版本:1.3.2JDK版本:1.7.0_51SSH Secure Shell版本:XShell 5elasticsear ...

随机推荐

  1. unity 确定敌人行走路线

    一开始搞这个问题很头疼,无从下手. 1.敌人在随机地点产生后,每个敌人有要有自己自动的行走路线,目的地是保护地,而且行走路线要多样化. 2.敌人在看到玩家时,改变行走路线,向玩家的方向行进,且到了一定 ...

  2. 深入理解java虚拟机(二)HotSpot Java对象创建,内存布局以及访问方式

    内存中对象的创建.对象的结构以及访问方式. 一.对象的创建 在语言层面上,对象的创建只不过是一个new关键字而已,那么在虚拟机中又是一个怎样的过程呢? (一)判断类是否加载.虚拟机遇到一条new指令的 ...

  3. airpods2代连接macbook失败

    更新至最新系统(10.14.4),更新完毕,重启电脑再次连接即可. 参考连接: http://dq.tieba.com/p/6082366443

  4. sersync服务搭建

    前言: 一.为什么要用Rsync+sersync架构? 1.sersync是基于Inotify开发的,类似于Inotify-tools的工具 2.sersync可以记录下被监听目录中发生变化的(包括增 ...

  5. Partition--分区总结

    1. 在SQL SERVER 2008 R2 SP2之前版本,对分区只支持到1000个分区,之后版本支持到15000个分区.2. 分区索引对齐并不要求索引和表使用同一分区方案,但要求两者使用的分区方案 ...

  6. 关于人脸识别引擎FaceRecognitionDotNet的实例

    根据我上篇文章的分享,我提到了FaceRecognitionDotNet,它是python语言开发的一个项目face_recognition移植.结果真是有喜有忧,喜的是很多去关注了,进行了下载,我看 ...

  7. 解除SVN的控制

    win10操作就是把文件夹隐藏的对勾勾上 然后就可以看见一个.SVN的文件夹 把他直接删除即可 然后刷新 自己亲测的 win10 家庭版

  8. c#设计模式系列:观察者模式(Observer Pattern)

    引言 在现实生活中,处处可见观察者模式,例如,微信中的订阅号,订阅博客和QQ微博中关注好友,这些都属于观察者模式的应用.在这一章将分享我对观察者模式的理解,废话不多说了,直接进入今天的主题. 观察者模 ...

  9. django静态文件路径配置

    在settings.py中加入 STATICFILES_DIRS = [ os.path.join(BASE_DIR,'static') ] 即可在html中引用该目录下的静态文件 <!DOCT ...

  10. Visual Studio 2015 Update 2 发布

    2016年3月30日,微软发布了Visual Studio 2015 Update 2 . 更新内容: Visual Studio  Visual Studio Tools for Apache Co ...