springBoot整合elk

使用docker-compose部署elk

准备docker-compose.yml文件

[root@192 mall-elk]# pwd
/root/docker/mall-elk
[root@192 mall-elk]# vim docker-compose.yml

内容如下:

version: '3'
services:
  elasticsearch:
    image: elasticsearch:7.10.1
    container_name: elasticsearch
    environment:
      - "cluster.name=elasticsearch" #设置集群名称为elasticsearch
      - "discovery.type=single-node" #以单一节点模式启动
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m" #设置使用jvm内存大小
    volumes:
      - /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins #插件文件挂载
      - /mydata/elasticsearch/data:/usr/share/elasticsearch/data #数据文件挂载
    ports:
      - 9200:9200
  kibana:
    image: kibana:7.10.1
    container_name: kibana
    links:
      - elasticsearch:es #可以用es这个域名访问elasticsearch服务
    depends_on:
      - elasticsearch #kibana在elasticsearch启动之后再启动
    environment:
      - "elasticsearch.hosts=http://es:9200" #设置访问elasticsearch的地址
    ports:
      - 5601:5601
  logstash:
    image: logstash:7.10.1
    container_name: logstash
    volumes:
      - /mydata/logstash/logstash-springboot.conf:/usr/share/logstash/pipeline/logstash.conf #挂载logstash的配置文件
    depends_on:
      - elasticsearch #kibana在elasticsearch启动之后再启动
    links:
      - elasticsearch:es #可以用es这个域名访问elasticsearch服务
    ports:
      - 4560:4560

准备logstash的配置文件 logstash-springboot.conf
放到 /mydata/logstash/ 目录下,没有这个额目录旧新建下

input {
  tcp {
    mode => "server"
    host => "0.0.0.0"
    port => 4560
    codec => json_lines
  }
}
output {
  elasticsearch {
    hosts => "es:9200"
    index => "springboot-logstash-%{+YYYY.MM.dd}"
  }
}

执行docker-compose命令运行elk,需要在docker-compose.yml文件目录下执行,默认找的就是这个文件名

[root@192 mall-elk]# pwd
/root/docker/mall-elk
[root@192 mall-elk]# docker-compose up -d
Creating elasticsearch ... done
Creating logstash      ... error
Creating kibana        ...


## docker ps 看下,都运行起来了
[root@192 mall-elk]# docker ps
CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS              PORTS                                        NAMES
d20f10adccc9        logstash:7.10.1        "/usr/local/bin/dock…"   3 minutes ago       Up 41 seconds       5044/tcp, 0.0.0.0:4560->4560/tcp, 9600/tcp   logstash
365fc65ccde0        kibana:7.10.1          "/usr/local/bin/dumb…"   3 minutes ago       Up 3 minutes        0.0.0.0:5601->5601/tcp                       kibana
e027176d54af        elasticsearch:7.10.1   "/tini -- /usr/local…"   3 minutes ago       Up 41 seconds       0.0.0.0:9200->9200/tcp, 9300/tcp             elasticsearch

测试es是否正常

[root@192 mall-elk]# curl localhost:9200
curl: (7) Failed connect to localhost:9200; 拒绝连接

# 发现运行不正常,查看下es的日志
[root@192 mall-elk]# docker logs elasticsearch
...
uncaught exception in thread [main]
ElasticsearchException[failed to bind service]; nested: AccessDeniedException[/usr/share/elasticsearch/data/nodes];
Likely root cause: java.nio.file.AccessDeniedException: /usr/share/elasticsearch/data/nodes
        at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:90)
        at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:106)
        at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
        at java.base/sun.nio.fs.UnixFileSystemProvider.createDirectory(UnixFileSystemProvider.java:388)
        at java.base/java.nio.file.Files.createDirectory(Files.java:694)
        at java.base/java.nio.file.Files.createAndCheckIsDirectory(Files.java:801)
        at java.base/java.nio.file.Files.createDirectories(Files.java:787)
        at org.elasticsearch.env.NodeEnvironment.lambda$new$0(NodeEnvironment.java:275)
        at org.elasticsearch.env.NodeEnvironment$NodeLock.<init>(NodeEnvironment.java:212)
        at org.elasticsearch.env.NodeEnvironment.<init>(NodeEnvironment.java:272)
        at org.elasticsearch.node.Node.<init>(Node.java:362)
        at org.elasticsearch.node.Node.<init>(Node.java:289)
        at org.elasticsearch.bootstrap.Bootstrap$5.<init>(Bootstrap.java:227)
        at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:227)
        at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:393)
        at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:170)
        at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:161)
        at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86)
        at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:127)
        at org.elasticsearch.cli.Command.main(Command.java:90)
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:126)
        at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92)
For complete error details, refer to the log at /usr/share/elasticsearch/logs/elasticsearch.log

发现上面错误,于是百度了一下,说是es挂载的本地目录权限不够。

[root@192 mall-elk]# cd /mydata/elasticsearch/
[root@192 elasticsearch]# ll
总用量 0
drwxr-xr-x. 2 root root 6 9月  22 22:36 data
drwxr-xr-x. 2 root root 6 9月  22 22:36 plugins

# 修改下权限
[root@192 elasticsearch]# chmod 777 -R *
[root@192 elasticsearch]# ll
总用量 0
drwxrwxrwx. 2 root root 6 9月  22 22:36 data
drwxrwxrwx. 2 root root 6 9月  22 22:36 plugins

# 然后重启es容器
[root@192 elasticsearch]# docker restart elasticsearch
elasticsearch

# 再次测试就正常了
[root@192 elasticsearch]# curl localhost:9200
{
  "name" : "e027176d54af",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "LvkZRniDSsOHm5qDa-rTDw",
  "version" : {
    "number" : "7.10.1",
    "build_flavor" : "default",
    "build_type" : "docker",
    "build_hash" : "1c34507e66d7db1211f66f3513706fdf548736aa",
    "build_date" : "2020-12-05T01:00:33.671820Z",
    "build_snapshot" : false,
    "lucene_version" : "8.7.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

springBoot整合logstash

引入pom(楼主springBoot的版本是2.6.5)

 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!--集成logstash-->
<dependency>
    <groupId>net.logstash.logback</groupId>
    <artifactId>logstash-logback-encoder</artifactId>
    <version>5.3</version>
</dependency>

logback.xml文件内容

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
    <include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
    <!--应用名称-->
    <property name="APP_NAME" value="mall-admin"/>
    <!--日志文件保存路径-->
    <property name="LOG_FILE_PATH" value="/var/mall/logs"/>
    <property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n"/>
    <contextName>${APP_NAME}</contextName>
    <!--每天记录日志到文件appender-->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_FILE_PATH}/${APP_NAME}-%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>30</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>${FILE_LOG_PATTERN}</pattern>
        </encoder>
    </appender>
    <!--输出到logstash的appender-->
    <appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
        <!--可以访问的logstash日志收集端口。这里的ip换上你自己的 -->
        <destination>192.168.245.133:4560</destination>
        <encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder"/>
    </appender>
    <root level="INFO">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="FILE"/>
        <appender-ref ref="LOGSTASH"/>
    </root>
</configuration>

然后启动springBoot服务

kibana配置

登录kibana。http://192.168.245.133:5601/app/dev_tools#/console

点击左侧的Dev Tools

查询索引,可以看到,我们 springboot-logstash-2022.09.22 这个索引。这个 springboot-logstash前缀的索引就是我们利用logstash收集日志的索引

GET /_cat/indices?v

现在配置kibana查询


点击 Discover -> 点击 create index pattern -> 输入 springboot-logstash* -> 点击next step -> 选择@timestamp -> 点击 create index pattern

创建好之后,重新点击左侧的Discover,就可以看到日志了

项目代码

https://github.com/liufei96/mall-learning/tree/main/mall-tiny-elk

posted @ 2022-09-22 23:01  刘翊扬  阅读(664)  评论(0编辑  收藏  举报