其他分享
首页 > 其他分享> > springboot 整合 elasticsearch + kibana

springboot 整合 elasticsearch + kibana

作者:互联网

(1)使用 docker-compose 安装 elasticsearch 和 kibana

version: '3.0'
services:
  elasticsearch:
    image: daocloud.io/library/elasticsearch:6.5.4
    restart: always
    container_name: elasticsearch
    volumes:
      - elasticsearch_config:/usr/share/elasticsearch/config
      - elasticsearch_data:/usr/share/elasticsearch/data
      - elasticsearch_log:/usr/share/elasticsearch/logs
    ports:
      - 9200:9200
      - 9300:9300
    environment:
      ES_JAVA_OPTS: -Xms256m -Xmx256m
    networks:
      - mongo_log
  kibana:
    image: daocloud.io/library/kibana:6.5.4
    restart: always
    container_name: kibana
    volumes:
      - kibana_config:/usr/share/kibana/config
    ports:
      - 5601:5601
    environment:
      - elasticsearch_url=http://192.168.17.150:9200 #修改成 elasticsearch 所在服务器的 ip
    depends_on:
      - elasticsearch
    networks:
      - mongo_log
networks:
  mongo_log:
volumes:
  elasticsearch_config:
  elasticsearch_data:
  elasticsearch_log:
  kibana_config:

(2)springboot 项目,引入 elasticsearch 依赖:

<!-- elasticsearch -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
            <version>2.2.5.RELEASE</version>
        </dependency>

(3)application.yml 配置文件配置 elasticsearch 配置

spring:
  data:
    elasticsearch:
      #集群名字(和 elasticsearch安装所在服务器的 elasticsearch.yml 配置文件中的集群名字一致)
      cluster-name: mongo-elasticsearch
      #elasticsearch 所在服务器地址,节点端口映射为 9300,不是 9200
      cluster-nodes: 192.168.17.150:9300

(4)接下来使用和普通的数据库 crud 模式一样,创建实体类,dao 层

实体类:

package com.model.esEntity;
import lombok.Data;
import org.springframework.data.elasticsearch.annotations.Document;
import javax.persistence.Id;
/**
 * @author Mongo
 * @return
 */
@Document(indexName = "mongo",type = "user")
@Data
public class UserEntity {
    @Id
    private String id;
    private String name;
    private Integer age;
    private String sex;
}

dao 层:

package com.daoEs;
import com.model.esEntity.UserEntity;
import org.springframework.data.repository.CrudRepository;
/**
 * @author Mongo
 * @return
 */
public interface UserDao extends CrudRepository<UserEntity,String> {
    
}

controller 层测试:

package com.controller;
import com.common.result.ResultsBean;
import com.common.utils.*;
import com.daoEs.UserDao;
import com.model.bean.PostParamsBean;
import com.model.esEntity.UserEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * @author Mongo
 * @return
 */
@RestController
@Slf4j
public class TestController {
    @Autowired
    UserDao userDao;
    
    @PostMapping("/test/addEsUser")
    public ResultsBean addEsUser(@RequestBody UserEntity userEntity) {
        log.info("==== 开始 es 保存数据 ====");
        UserEntity save = userDao.save(userEntity);
        log.info("==== 结束 es 保存数据 ====");
        return ResultsBean.ok(save);
    }

    @PostMapping("/test/findEsUser")
    public ResultsBean findEsUser(@RequestBody UserEntity userEntity) {
        log.info("==== 开始 es 查询数据 ====");
        String id = userEntity.getId();
        Optional<UserEntity> byId = userDao.findById(id);
        log.info("==== 结束 es 查询数据 ====");
        return ResultsBean.ok(byId);
    }
}

postman 测试:

 

 

  kibana 界面查看:

 

 

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

 注意事项:

(1)elasticsearch 安装好后,需要修改配置文件,因为 docker 映射了路径,直接在  /var/lib/docker/volumes 下面的 docker_elasticsearch_config/_data 内修改 elasticsearch.yml 文件:

vi /var/lib/docker/volumes/docker_elasticsearch_config/_data/elasticsearch.yml

 

 

cluster.name: xxxx  #这个参数要和 springboot 项目中的 application.yml 文件中的 es 配置里面的 cluster-name: mongo-elasticsearch 一致!

其他两个配置如图所示,一般到这里就可以正常使用了。

注意:如果启动 es(elasticsearch 简称) 后,springboot 请求接口后报错,提示:

  None of the configured nodes are available: [{#transport#-1}{GMeURoP8SfSHo9z-SuSHFg}{192.168.17.150}{192.168.17.150:9200}]

解决方法:

  1、类似这样的错误,记得 docker-compose.yml 文件中,9300 端口要写上映射,不写会报这个错,

  2、es 中的 elasticsearch.yml 文件中的 cluster.name 要和 springboot  application.yml 文件中的 cluster-name 一致

  3、查看 docker-compose logs -f 日志,看到提示:java.lang.IllegalStateException: Received message from unsupported version: [5.2.2] minimal compatible version is: [5.6.0],这种问题就是 maven 依赖 jar 包版本的问题,尝试更换 maven: spring-boot-starter-data-elasticsearch 依赖的版本,也可以解决问题

标签:springboot,data,kibana,elasticsearch,import,com,log
来源: https://www.cnblogs.com/xuehuashanghe/p/16537595.html