elasticsearch 的安装配置与spring boot的整合应用
linux上的elasticsearch安装
一、下载elasticsearch
直接进入elasticsearch的官网,下载最新的安装包:https://www.elastic.co/downloads/elasticsearch,此教程使用的是5.1.1版本。
二、安装elasticsearch
将下载的安装包上传到centos,或者直接在centos使用wget命令下载。
1、解压
unzip elasticsearch-5.1.1.zip
2、安装jdk1.8
要安装elasticsearch官方建议使用jdk1.8的版本,所以先安装并部署好jdk,关于jdk的部署参考网上其他资料。
3、运行
cd bin
./elasticsearch
可能会出现错误,下面列出常见的错误:
(1)elasticsearch不能以root权限来运行,会出现这种错误:
Exception in thread "main" java.lang.RuntimeException: don't run elasticsearch as root。
因为安全问题elasticsearch 不让用root用户直接运行,所以要创建新用户解决办法:
第一步:liunx创建新用户 adduser XXX 然后给创建的用户加密码 passwd XXX 输入两次密码。
第二步:切换刚才创建的用户 su XXX 然后执行elasticsearch 会显示Permission denied 权限不足。
第三步:给新建的XXX赋权限,chmod 777 * 这个不行,因为这个用户本身就没有权限,肯定自己不能给自己付权限。所以要用root用户登录付权限。
第四步:root给XXX赋权限,chown -R XXX /你的elasticsearch安装目录。
在linux上集群配置可能需要配置linux的最大进程数、缓存大小具体看个人安装时的报错 注意看报错!!!如下修改
以下数据根据报错的情况修改可能版本不同数据会各不相同
vi /etc/security/limits.conf
添加如下内容:
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
2 vi /etc/security/limits.d/90-nproc.conf
找到如下内容:
* soft nproc 1024
修改为
* soft nproc 2048
3 vi /etc/sysctl.conf
添加下面配置:
vm.max_map_count=655360
并执行命令:
sysctl -p
4 文件打开数,要求大于65535
ulimit -n 65536
elasticsearch.yml文件配置:
cluster.name: elasticsearch
#这是集群名字,我们 起名为 elasticsearch
#es启动后会将具有相同集群名字的节点放到一个集群下。
node.name: "es-node1"
#节点名字。
discovery.zen.minimum_master_nodes: 2
#指定集群中的节点中有几个有master资格的节点。
#对于大集群可以写3个以上。
discovery.zen.ping.timeout: 40s
#默认是3s,这是设置集群中自动发现其它节点时ping连接超时时间,
discovery.zen.ping.multicast.enabled: false
#设置是否打开多播发现节点,默认是true。
network.bind_host: 192.168.137.100
#设置绑定的ip地址,这是我的master虚拟机的IP。
network.publish_host: 192.168.137.100
#设置其它节点和该节点交互的ip地址。
network.host: 192.168.137.100
#同时设置bind_host和publish_host上面两个参数。
discovery.zen.ping.unicast.hosts: ["192.168.137.100", "192.168.137.101","192.168.137.100:9301"]
#discovery.zen.ping.unicast.hosts:["节点1的 ip","节点2 的ip","节点3的ip"]
#指明集群中其它可能为master的节点ip,
#以防es启动后发现不了集群中的其他节点。
#第一对引号里是node1,默认端口是9300,
#第二个是 node2 ,在另外一台机器上,
#第三个引号里是node3,因为它和node1在一台机器上,所以指定了9301端口。
http.cors.enabled: true
http.cors.allow-origin: "*"
head插件需要的配置 head可以访问elasticsearch
head插件的安装
下载node.js ,网址:https://nodejs.org/en/
安装node到D盘。如D:\nodejs。
把NODE_HOME设置到环境变量里(安装包也可以自动加入PATH环境变量)。测试一下node是否生效:
安装grunt
grunt是一个很方便的构建工具,可以进行打包压缩、测试、执行等等的工作,5.0里的head插件就是通过grunt启动的。因此需要安装grunt:
注意:路径切到D:\nodejs下。
npm install -g grunt-cli
-g代表全局安装。安装路径为C:\Users\yourname\AppData\Roaming\npm,并且自动加入PATH变量。安装完成后检查一下:
把head插件的源码git clone下来:
git clone git://github.com/mobz/elasticsearch-head.git
效果如图:
修改head源码
由于head的代码还是2.6版本的,直接执行有很多限制,比如无法跨机器访问。因此需要用户修改两个地方:
目录:head/Gruntfile.js:
复制代码
connect: {
server: {
options: {
port: 9100,
hostname: '*',
base: '.',
keepalive: true
}
}
}
复制代码
增加hostname属性,设置为*
修改连接地址:
目录:head/_site/app.js
修改head的连接地址:
this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://localhost:9200";
把localhost修改成你es的服务器地址,如:
this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://10.10.10.10:9200";
然后在head源码目录中,执行npm install 下载的包:
npm install
如果在这里有报错的话,就把那npm的镜像修改为淘宝的镜像
1.通过config命令
npm config set registry https://registry.npm.taobao.org
npm info underscore (如果上面配置正确这个命令会有字符串response)
然后在执行
npm install
上一步好了以后执行
grunt server
接下来就可以在浏览器上打开localhost:9100(或本机ip:9100)运行head插件了
|
|
|
在centos上使用curl插入数据到elasticsearch里面的正确格式
curl -H "Content-Type: application/json" -XPOST 192.168.14.173:32000/test_index_1221/test_type/5 -d '{"user_name":"xiaoming"}'
在windows上使用curl插入数据到elasticsearch里面的正确格式
curl -H "Content-Type: application/json" -XPOST 192.168.14.173:32000/test_index_1221/test_type/5 -d "{"""user_name""":"""xiaoming"""}"
spring boot整合elasticsearch
application.properties配置
//开启 Elasticsearch 仓库。(默认值:true。)
spring.data.elasticsearch.repositories.enabled = true
//集群的地址
spring.data.elasticsearch.cluster-nodes =192.168.175.131:9300
//集群名。(默认值: elasticsearch)
spring.data.elasticsearch.cluster-name= es
pom.xml依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
controller类:
package com.example.demo;
import com.example.demo.User;
import com.example.demo.dom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class controller {
@Autowired
private dom userRepository;
@ResponseBody
//http://localhost:8888/save
@GetMapping("save")
public String save(){
User user = new User(System.currentTimeMillis(),"商品","这是一个测试商品",15,"2018-4-25 11:07:42");
userRepository.save(user);
return "success";
}
//http://localhost:8888/delete?id=1525415333329
@GetMapping("delete")
public String delete(long id){
userRepository.deleteById(id);
return "success";
}
//http://localhost:8888/update?id=1525417362754&name=修改&description=修改
@GetMapping("update")
public String update(long id,String name,String description,Integer age,String createtm){
User user = new User(id,
name,description,age,createtm);
userRepository.save(user);
return "success";
}
}
dom类继承ElasticsearchRepository:
package com.example.demo;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component;
import com.example.demo.User;
@Component
public interface dom extends ElasticsearchRepository<User, Long>{
}
User类:
package com.example.demo;
import java.io.Serializable;
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = "q1", type = "user")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
/** 编号 */
private Long id;
/** 姓名 */
private String name;
/** 年龄 */
private Integer age;
/** 描述 */
private String description;
/** 创建时间 */
private String createtm;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getCreatetm() {
return createtm;
}
public void setCreatetm(String createtm) {
this.createtm = createtm;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
public User(Long id, String name, String description,Integer age,String createtm) {
this.id = id;
this.name = name;
this.description = description;
this.age=age;
this.createtm=createtm;
}
}
然后在浏览器输入localhost:8080/save
浏览器会显示success
然后在head中看到以下数据

到这里就spring boot整合成功!
=
=======================================================================================
elasticsearch 的安装配置与spring boot的整合应用的更多相关文章
- liunx 安装ActiveMQ 及 spring boot 初步整合 activemq
源码地址: https://gitee.com/kevin9401/microservice.git 一.安装 ActiveMQ: 1. 下载 ActiveMQ wget https://arch ...
- SpringBoot零XML配置的Spring Boot Application
Spring Boot 提供了一种统一的方式来管理应用的配置,允许开发人员使用属性properties文件.YAML 文件.环境变量和命令行参数来定义优先级不同的配置值.零XML配置的Spring B ...
- ElasticSearch搜索引擎安装配置拼音插件pinyin
近几篇ElasticSearch系列: 1.阿里云服务器Linux系统安装配置ElasticSearch搜索引擎 2.Linux系统中ElasticSearch搜索引擎安装配置Head插件 3.Ela ...
- ElasticSearch搜索引擎安装配置中文分词器IK插件
近几篇ElasticSearch系列: 1.阿里云服务器Linux系统安装配置ElasticSearch搜索引擎 2.Linux系统中ElasticSearch搜索引擎安装配置Head插件 3.Ela ...
- Linux系统中ElasticSearch搜索引擎安装配置Head插件
近几篇ElasticSearch系列: 1.阿里云服务器Linux系统安装配置ElasticSearch搜索引擎 2.Linux系统中ElasticSearch搜索引擎安装配置Head插件 3.Ela ...
- Spring Boot Security 整合 JWT 实现 无状态的分布式API接口
简介 JSON Web Token(缩写 JWT)是目前最流行的跨域认证解决方案.JSON Web Token 入门教程 - 阮一峰,这篇文章可以帮你了解JWT的概念.本文重点讲解Spring Boo ...
- Spring boot Mybatis 整合(完整版)
个人开源项目 springboot+mybatis+thymeleaf+docker构建的个人站点开源项目(集成了个人主页.个人作品.个人博客) 朋友自制的springboot接口文档组件swagge ...
- Spring boot Mybatis 整合
PS: 参考博客 PS: spring boot配置mybatis和事务管理 PS: Spring boot Mybatis 整合(完整版) 这篇博客里用到了怎样 生成 mybatis 插件来写程 ...
- spring boot 学习(二)spring boot 框架整合 thymeleaf
spring boot 框架整合 thymeleaf spring boot 的官方文档中建议开发者使用模板引擎,避免使用 JSP.因为若一定要使用 JSP 将无法使用. 注意:本文主要参考学习了大神 ...
随机推荐
- Java进阶(十三)servlet监听器
servlet监听器 Listener是Servlet的监听器,它可以监听客户端的请求.服务端的操作等.通过监听器,可以自动激发一些操作,比如监听在线的用户的数量.当 增加一个HttpSession时 ...
- LeetCode之“链表”:Reverse Nodes in k-Group
题目链接 题目要求: Given a linked list, reverse the nodes of a linked list k at a time and return its modifi ...
- android Titlebar一行代码实现沉浸式效果
github地址 一个简单易用的导航栏TitleBar,可以轻松实现IOS导航栏的各种效果 整个代码全部集中在TitleBar.java中,所有控件都动态生成,动态布局.不需要引用任何资源文件,拷贝 ...
- 加载SpriteBuilder中的scene为何不能带后缀
我们在Xcode中切换SpriteBuilder中的scene时,一般使用的是如下代码: -(void)exitButtonPressed:(CCControl*)sender{ CCLOG(@&qu ...
- 新版MATERIAL DESIGN 官方动效指南(三)
运动 Material design 的动效会被类似真实世界中的力的影响,类似重力. 物体在屏幕内的运动 屏幕内物体在两点之间的运动,是沿着一条自然.凹陷的弧线.屏幕上所有的运动都可以使用标准曲线. ...
- Leetcode_228_Summary Ranges
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/46762039 Given a sorted integer ...
- Android的Binder的起源-android学习之旅(100)
George Hoffman任职1991年Be公司的工程师,他启动了一个"openBinder"的项目,该项目的宗旨是研究一个高效的信号传递工具,允许多个软件相互合作,构成一个软件 ...
- jquery选择器项目实例分析
首先废话一句,jQuery选择器真心很强大! 在项目中遇到这么一个问题easyui的问题 如图所示,当前页面显示的是"原始报文查询"的页面,当时左侧导航栏却选中的是"重 ...
- 数据库scheme设计(9.4 小结)
通过这一章的内容,希望能够让大家明白一个道理,“数据库系统的性能不是优化出来的,更多的是设计出来的”.数据库Schema 的设计并不如很多人想象的那样只是一个简单的对象对应实现,而是一个系统工程.要想 ...
- ROS:使用Qt Creator创建GUI程序(一)
开发环境: Ubuntu14.04 ROS indigo version Qt Creator 3.0.1 based on Qt 5.2.1 步骤如下:(按照下面命令一步步来,亲测可行) (一)安装 ...