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整合成功!

=

=======================================================================================

posted @ 2018-05-18 23:47  超级大猩猩  阅读(764)  评论(0编辑  收藏  举报