Docker Springboot 集成 Graylog

Posted seasonszx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker Springboot 集成 Graylog相关的知识,希望对你有一定的参考价值。

1,拉取镜像

docker pull mongo:4.2

docker pull elasitcsearch:7.8.0

docker pull graylog/graylog:4.2

2,启动容器

version: '3'
services:
  mongo:
    image: mongo:4.2
  volumes:
      - /usr/local/mapping/mongo/data:/data/db
      - /usr/local/mapping/mongo/conf:/data/configdb
    networks:
      - graylog
  elasticsearch:
    image: elasticsearch:7.8.0
  volumes:
      - /usr/local/mapping/elasticsearch/data:/usr/share/elasticsearch/data
      - /usr/local/mapping/elasticsearch/plugins:/usr/share/elasticsearch/plugins
    environment:
      - http.host=0.0.0.0
      - transport.host=localhost
      - network.host=0.0.0.0
      - ES_JAVA_OPTS=-Dlog4j2.formatMsgNoLookups=true -Xms512m -Xmx512m
    ulimits:
      memlock:
        soft: -1
        hard: -1
    deploy:
      resources:
        limits:
          memory: 1g
    networks:
      - graylog
  graylog:
    image: graylog/graylog:4.2
    environment:
      - GRAYLOG_PASSWORD_SECRET=somepasswordpepper
      # Password: 9Ji315Rx4yUvro8H
      - GRAYLOG_ROOT_PASSWORD_SHA2=cc090167df736bc65c0bd69551f405aa52fbb5ae9c791c3a9e42742af6074d66 #默认密码:9Ji315Rx4yUvro8H
      - GRAYLOG_HTTP_EXTERNAL_URI=http://127.0.0.1:9000/
      - GRAYLOG_ROOT_TIMEZONE=Asia/Shanghai         #设置时区
      - GRAYLOG_ALLOW_HIGHLIGHTING=true             #搜索高亮
    entrypoint: /usr/bin/tini -- wait-for-it elasticsearch:9200 --  /docker-entrypoint.sh
    networks:
      - graylog
    restart: always
    depends_on:
      - mongo
      - elasticsearch
    ports:
      - 9000:9000
      - 1514:1514
      - 1514:1514/udp
      - 12201:12201
      - 12201:12201/udp
networks:
  graylog:
    driver: bridge

 docker-compose up -d 启动

 配置建议:

 1,密码建议修改 

 2,mongo和elasticsearch 建议都挂接本地磁盘,否则down之后 数据内容全部丢失了

3,设置Graylog

登录graylog, http://公网IP:9000

帐号admin 密码9Ji315Rx4yUvro8H

 

4,Springboot集成Graylog

pom.xml增加

<dependency>
			<groupId>de.siegmar</groupId>
			<artifactId>logback-gelf</artifactId>
			<version>3.0.0</version>
		</dependency>

logback.xml中增加

<appender name="GELF" class="de.siegmar.logbackgelf.GelfUdpAppender">
        <!-- Graylog服务的地址 -->
        <graylogHost>服务器公网IP</graylogHost>
        <!-- UDP Input端口 -->
        <graylogPort>12201</graylogPort>
        <!-- 最大GELF数据块大小(单位:字节),508为建议最小值,最大值为65467 -->
        <maxChunkSize>508</maxChunkSize>
        <!-- 是否使用压缩 -->
        <useCompression>true</useCompression>
        <encoder class="de.siegmar.logbackgelf.GelfEncoder">
            <!-- 是否发送原生的日志信息 -->
            <includeRawMessage>false</includeRawMessage>
            <includeMarker>true</includeMarker>
            <includeMdcData>true</includeMdcData>
            <includeCallerData>false</includeCallerData>
            <includeRootCauseData>false</includeRootCauseData>
            <!-- 是否发送日志级别的名称,否则默认以数字代表日志级别 -->
            <includeLevelName>true</includeLevelName>
            <shortPatternLayout class="ch.qos.logback.classic.PatternLayout">
                <pattern>%m%nopex</pattern>
            </shortPatternLayout>
            <fullPatternLayout class="ch.qos.logback.classic.PatternLayout">
                <pattern>%d - [%thread] %-5level %logger35 - %msg%n</pattern>
            </fullPatternLayout>

            <!-- 配置应用名称(服务名称),通过staticField标签可以自定义一些固定的日志字段 -->
            <staticField>app_name:ops</staticField>
        </encoder>
    </appender>

    <root level="debug">
        <appender-ref ref="GELF"/>
    </root>

这里如果用的云服务器,开端口的时候,切记开放UDP端口,

一般默认开放TCP端口,死活收不到消息,在这里坑了我整整1天。

 5,结果

springboot 控制台展示

graylog 展示 

 这里是最基础的搭建配置,

 而在实际微服务或者分布式应用中,配置比这个复杂的多,需要的得做更深度研究

以上是关于Docker Springboot 集成 Graylog的主要内容,如果未能解决你的问题,请参考以下文章

Docker Springboot 集成 Graylog

Springboot集成solr

在Docker上实战ElasticSearch以及与SpringBoot集成

转简易的微服务持续集成方案,SpringBoot+Docker+Jenkins+Gitlab

OpenOffice4: 软件包安装, Docker安装,集成SpringBoot应用

SpringBoot 集成测试 Sybase 和 Testcontainers