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</version>
</dependency>

修改application.yml文件,引入elasticsearch配置:

spring:
  data:
    elasticsearch:
      ##集群名称,elasticsearch.ymlcluster.name: chenxi配置
      ##详情见 http://chenxitag.elasticsearch.cluster.com
      cluster-name: chenxi
      ##集群地址逗号分隔,注意此地方用的端口为9300,Es集群TCP协议端口
      cluster-nodes: 192.168.0.1:9300,192.168.0.2:9300

新建测试Entity:

import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;


@Document(indexName = "chenxi", type = "user"/*shards = 1, replicas = 2 ##可指定分片数和副本数*/)
@Data
public class UserEntity {
    @Id
    private Integer id;
    private String name;
    private Integer age;
}

新建测试Dao:

import com.es.entity.UserEntity;
import org.springframework.data.repository.CrudRepository;

public interface UserDao extends CrudRepository<UserEntity, Integer> {
}

新建Test类测试:

import com.es.dao.UserDao;
import com.es.entity.UserEntity;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;

@SpringBootTest
@Slf4j
class EsTestApplicationTests {

   @Test
   void contextLoads() {
   }

   @Autowired
   private UserDao userDao;

   @Autowired
   private ElasticsearchTemplate esTemplate;

   @Test
   void esSave(){

      UserEntity esEntity = new UserEntity();
      esEntity.setId(1);
      esEntity.setName("chenxi");
      esEntity.setAge(22);

      userDao.save(esEntity);
   }

   @Test
   void esFind(){
      log.info(new Gson().toJson(userDao.findById(1)));
      //info out "{"value":{"id":1,"name":"chenxi","age":22}}"
   }

   @Test
   void esTemplate(){
      log.info(String.valueOf(esTemplate.createIndex("template_index")));
      //info out "true"
   }

错误信息:NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{P20ipqqfSNCzjirh0puSTQ}{192.168.0.1}{192.168.0.1:9300}, {#transport#-2}{I4slCrNuTbmVTOSZ3DG7hA}{192.168.0.2}{192.168.0.2:9300}]

请检查Es是否启动,以及Es环境elasticsearch.yml集群配置项:

# ———————————- Cluster ———————————–
#
# Use a descriptive name for your cluster:
# 节点集群名称,保证三台服务器节点集群名称相同
cluster.name: chenxi

 

Centos环境Kibana可视化平台搭建

Kibana环境搭建:

1.下载kibana包上传到服务器,下载地址:https://www.elastic.co/cn/downloads/kibana

2.解压下载的Tar包(过程比较久、大概30秒左右)

[root@chenxi software]# tar -zxvf kibana-7.6.0-linux-x86_64.tar.gz

3.重命名文件夹名称
[root@chenxi software]# mv kibana-7.6.0-linux-x86_64 kibana

4.进入kibana目录下
[root@chenxi software]# cd kibana

5.修改kibana.yml配置
[root@chenxi kibana]# vim config/kibana.yml

# Kibana is served by a back end server. This setting specifies the port to use.
# 取消注释,开放端口
server.port: 5601

# Specifies the address to which the Kibana server will bind. IP addresses and host names are both valid values.
# The default is ‘localhost’, which usually means remote machines will not be able to connect.
# To allow connections from remote users, set this parameter to a non-loopback address.
# 取消注释,配置服务器IP地址
server.host: “192.168.0.1”

# The URLs of the Elasticsearch instances to use for all your queries.
# 取消注释,配置ES环境IP地址以及端口
elasticsearch.url: “http://192.168.0.1:9200”

6.由于kibana安全性问题不能使用root启动,新建用户,并赋予文件夹权限或者启动命令后追加 –allow-root
[root@chenxi kibana]# groupadd kgroup # “kgroup” (组名)
[root@chenxi kibana]# useradd kuser # “kuser” (用户名)
[root@chenxi kibana]# chown -R kuser:kgroup /chenxi/software/kibana
#”/chenxi/software/kibana” 为安装目录

7.切换用户
[root@chenxi kibana]# su kuser

8.启动Kibana
[kuser@chenxi kibana]# bin/kibana

##注意防火墙开启5601端口和9200端口
##注意Kibana与Es版本号兼容问题
##注意低版本的Kibana不包含x-pack插件

控制台输出:
log [07:00:18.179] [info][listening] Server running at http://192.168.0.1:5601

访问地址:http://IP:5601
用户名amin/123456