日期:2019年9月24日

SpringBoot实战(十二):集成 Spring Boot Admin 监控

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:
https://zhanghan.blog.csdn.net/article/details/101273915

【前言】

程序开发完实现相应的功能只是一个部分,如何让系统在线上运行更好创造更高的价值是另外一个部分;监控是一个生产级项目避不可少重要组成部分;最近研究一下针对SpringBoot的监控项目—Spring Boot Admin,并集成项目中,在此与大家共享;

【SpringBootAdmin】

         一、SpringBootAdmin简介
1、github地址:https://github.com/codecentric/spring-boot-admin
2、重要功能列表:

         二、项目中集成SpringBootAdmin
1、搭建SpringBootAdmin服务端
(1)新建springboot项目(在此项目名定为zh-monitor)
(2)pom文件如下:



    4.0.0

    
        com.zhanghan
        zh-parent
        1.0.0-SNAPSHOT
    

    com.zhanghan
    zh-monitor
    1.0.0-SNAPSHOT
    zh-monitor
    zhanghan monitor for Spring Boot

    

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            de.codecentric
            spring-boot-admin-starter-server
        


    


    
        zh-monitor
    



(3)启动类如下(ZhMonitorApplication):

/*
 * Copyright (c) 2019. zhanghan_java@163.com All Rights Reserved.
 * 项目名称:实战SpringBoot
 * 类名称:ZhMonitorApplication.java
 * 创建人:张晗
 * 联系方式:zhanghan_java@163.com
 * 开源地址: https://github.com/dangnianchuntian/springboot
 * 博客地址: https://zhanghan.blog.csdn.net
 */

package com.zhanghan.zhmonitor;

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableAdminServer
public class ZhMonitorApplication {

    public static void main(String[] args) {
        SpringApplication.run(ZhMonitorApplication.class, args);
    }

}

(4)配置文件如下(application.properties):

server.port=8091
spring.application.name=zhMonitor

2、SpringBoot项目集成SpringBootAdmin客户端
(1)Pom中增加相关依赖



    de.codecentric
    spring-boot-admin-client

(2)启动配置文件中增加连接服务端地址

         三、部分功能效果展示:
1、详细指标

2、Log日志级别管理

3、JVM线程

4、Web中http展示

         四、项目地址:
1、地址:https://github.com/dangnianchuntian/springboot
2、代码版本:1.5.0-Release

【总结】

1、工欲善其事必先利其器,监控为系统保驾护航,让系统运行的更加稳定,发挥更大的业务价值;
2、接下来会为大家共享更多关于SpringBootAdmin的特性。

Posted by zhanghan in SpringBoot实战, 1 comment

logback 日志输出格式

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:
https://zhanghan.blog.csdn.net/article/details/101269545

【前言】

日志对一个系统的重要性不言而喻;日志通常是在排查问题时给人看,一个友好的输出样式让人看到后赏心悦目,排查效率通常也会随之提高;下面为大家共享一下通过设置logback日志输出格式,打印出令人欣喜的日志样式。

【搞一下日志格式】

        一、未指定日志格式,日志输出
              1、代码实现
(1)演示日志输出控制器

/*
 * Copyright (c) 2019. zhanghan_java@163.com All Rights Reserved.
 * 项目名称:实战SpringBoot
 * 类名称:CheckMobileController.java
 * 创建人:张晗
 * 联系方式:zhanghan_java@163.com
 * 开源地址: https://github.com/dangnianchuntian/springboot
 * 博客地址: https://zhanghan.blog.csdn.net
 */

package com.zhanghan.zhboot.controller;

import com.mysql.jdbc.StringUtils;
import com.zhanghan.zhboot.controller.request.MobileCheckRequest;
import com.zhanghan.zhboot.properties.MobilePreFixProperties;
import com.zhanghan.zhboot.util.wrapper.WrapMapper;
import com.zhanghan.zhboot.util.wrapper.Wrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@Api(value = "校验手机号控制器", tags = {"校验手机号控制器"})
public class CheckMobileController {

    private static Logger logger = LoggerFactory.getLogger(CheckMobileController.class);

    @Autowired
    private MobilePreFixProperties mobilePreFixProperties;

    @ApiOperation(value = "优雅校验手机号格式方式", tags = {"校验手机号控制器"})
    @RequestMapping(value = "/good/check/mobile", method = RequestMethod.POST)
    public Wrapper goodCheckMobile(@RequestBody @Validated MobileCheckRequest mobileCheckRequest) {

        logger.info("good check mobile param {}", mobileCheckRequest.toString());

        String countryCode = mobileCheckRequest.getCountryCode();
        String proFix = mobilePreFixProperties.getPrefixs().get(countryCode);

        if (StringUtils.isNullOrEmpty(proFix)) {
            logger.error("good check mobile param is error; param is {}, profix is {}", mobileCheckRequest.toString(), proFix);
            return WrapMapper.error("参数错误");
        }

        String mobile = mobileCheckRequest.getMobile();

        Boolean isLegal = false;
        if (mobile.startsWith(proFix)) {
            isLegal = true;
        }


        Map map = new HashMap();
        map.put("mobile", mobile);
        map.put("isLegal", isLegal);
        map.put("proFix", proFix);
        return WrapMapper.ok(map);
    }

    @ApiOperation(value = "扩展性差校验手机号格式方式", tags = {"校验手机号控制器"})
    @RequestMapping(value = "/bad/check/mobile", method = RequestMethod.POST)
    public Wrapper badCheckMobile(@RequestBody MobileCheckRequest mobileCheckRequest) {

        logger.info("bad check mobile param {}", mobileCheckRequest.toString());

        String countryCode = mobileCheckRequest.getCountryCode();

        String proFix = "";
        if (countryCode.equals("CN")) {
            proFix = "86";
        } else if (countryCode.equals("US")) {
            proFix = "1";
        } else {
            logger.error("bad check mobile param is error; param is {}, profix is {}", mobileCheckRequest.toString(), proFix);
            return WrapMapper.error("参数错误");
        }

        String mobile = mobileCheckRequest.getMobile();

        Boolean isLegal = false;
        if (mobile.startsWith(proFix)) {
            isLegal = true;
        }

        Map map = new HashMap();
        map.put("mobile", mobile);
        map.put("isLegal", isLegal);
        map.put("proFix", proFix);
        return WrapMapper.ok(map);
    }

}

              2、项目部署服务器后访问打印日志的效果

        二、指定日志格式,日志输出
              1、代码实现
(1)演示日志输出控制器(同上)
(2)在项目的resources目录下增加logback.xml设置打印格式,logback.xml内容如下:




    

    
         
    
    
         
    

    
    
    
    
        
            %d{yyyy-MM-dd HH:mm:ss} %highlight(%-5level) %green([${LOG_HOME},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}]) %magenta(${PID:-}) %white(---) %-20(%yellow([%20.20thread])) %-55(%cyan(%.32logger{30}:%L)) %highlight(- %msg%n)
            UTF-8
        
    

    
        
            ${LOG_PATH}/${appName}-log-console-%d{yyyy-MM-dd}.%i.log.zip
            ${maxSaveDays} 
            
                ${maxFileSize}
            
        
        
            %d{yyyy-MM-dd HH:mm:ss} %highlight(%-5level) %green([${LOG_HOME},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}]) %magenta(${PID:-}) %white(---) %-20(%yellow([%20.20thread])) %-55(%cyan(%.32logger{30}:%L)) %highlight(- %msg%n)
            UTF-8
        
    

    
        
            ${LOG_PATH}/${appName}-log-info-%d{yyyy-MM-dd}.%i.log.zip
            ${maxSaveDays} 
            
                ${maxFileSize}
            
        
        
            %d{"yyyy-MM-dd HH:mm:ss,SSS"}[%X{userId}|%X{sessionId}][%p][%c{0}-%M]-%m%n
            UTF-8
        
        
            ERROR
            DENY
            ACCEPT
        
    

    
        
            ${LOG_PATH}/${appName}-log-error-%d{yyyy-MM-dd}.%i.log.zip
            ${maxSaveDays} 
            
                ${maxFileSize}
            
        
        
            %d{"yyyy-MM-dd HH:mm:ss,SSS"}[%X{userId}|%X{sessionId}][%p][%c{0}-%M]-%m%n
            UTF-8
        
        
            ERROR
            ACCEPT
            DENY
        
        

    
    
    
    
    
    
    
    
    
    
    
    
        
        
        
        
    

              3、项目部署服务器后访问打印日志的效果

              4、查看日志记录文件,效果也一样,效果图:

        三、项目地址
1、地址:https://github.com/dangnianchuntian/springboot
2、代码版本:1.5.0-Release

【总结】

1、通过设定日志格式,输出的样式更加人性化,错误也更加明显;
2、这个小小的改变,使得在排查程序时更加的赏心悦目,心情上的开心将在无形中增加排错的效率;

Posted by zhanghan in 技术, 0 comments