java常用相关配置以及问题

目录

一、centos7使用nohup启动java服务以及logback日志问题

二、将java服务加入开机自启

三、java接口通用返回Bean

四、全局异常处理

五、mybatis配置xml头


一、centos7使用nohup启动java服务以及logback日志问题

在linux系统centos7中,正常启动java服务器使用:java -jar xxx.jar & 命令进行后台运行的方式启动,但是这种方式启动后,关闭窗口时,会中断程序运行。

解决办法1:使用nohup java -jar xxx.jar & 命令启动java jar,它将所有日志信息输出到nohup.out文件中,时间久了nohup.out会越来越大,项目中集成logback日志框架,例如按照天、大小等方式切割文件对nohup.out不生效。

解决办法2:使用nohup java -jar xxx.jar >/dev/null 2>&1 &命令启动服务。

>/dev/null  意思是将启动服务的所有正常日志不进行记录。

2>&1  意思是将服务的所有错误日志重定向到正常日志中,也就是最终到/dev/null。

结合以上两点,所有的正常日志、错误日志都指向到/dev/null  所以都不进行记录,也就不会产生nohup.out文件。正常根据logback配置,找对应的日志即可。

二、将java服务加入开机自启

#新建/lib/systemd/system/xxx.service文件
[root@bogon opt]# vi /lib/systemd/system/webserver.service
#----------加入以下内容------------
[Unit]
Description=redis
After=redis.service mysql.service    #在redis、mysql服务启动后,在启动该服务

[Service]
Type=simple
ExecStart=/usr/bin/nohup /opt/jdk1.8/bin/java -jar /opt/webserver.jar >/dev/null 2>&1
ExecReload=/usr/bin/kill -9 $MAINPID
ExecStop=/usr/bin/kill -9 $MAINPID
WorkingDirectory=/opt
PrivateTmp=true

[Install]  
WantedBy=multi-user.target
#----------:wq进行保存--------
[root@bogon opt]# systemctl daemon-reload
[root@bogon opt]# systemctl enable webserver.service     #加入开机自启
[root@bogon opt]# systemctl [status|start|restart|stop] webserver    #启停

三、java通用接口返回对象

package com.springboot.common.utils;

import lombok.Data;

/**
 * 通用返回接口
 */
@Data
public class ResultVO<T> {
    private Integer code;
    private String msg;
    private T data;

    ResultVO(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static ResultVO success() {
        return success(null);
    }

    public static <T> ResultVO<T> success(T data) {
        return new ResultVO(200, null, data);
    }

    public static ResultVO error() {
        return error(500);
    }

    public static ResultVO error(Integer code) {
        return error(code, null);
    }

    public static ResultVO error(String msg) {
        return error(500, msg);
    }

    public static ResultVO error(Integer code, String msg) {
        return new ResultVO(code, msg, null);
    }

    @Override
    public String toString() {
        return "{'code':" + code + ",'msg':'" + msg + "','data':" + data + "}";
    }
}

四、java自定义异常处理

1.新建BusinessException类

package com.springboot.common.exception;

import lombok.Getter;

/**
 * 自定义业务异常
 * 在业务中使用:throw new BusinessException("")
 */
@Getter
public class BusinessException extends RuntimeException{

    private String msg;

    public BusinessException(String msg) {
        this.msg = msg;
    }
}

2.新建BaseExceptionHandler

package com.springboot.common.exception;

import com.springboot.common.utils.ResultVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 自定义异常
 */
@RestControllerAdvice
public class BaseExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(BaseExceptionHandler.class);

    @ExceptionHandler({BusinessException.class})
    public ResultVO businessExceptionHandler(BusinessException e){
        return ResultVO.error(500, e.getMsg());
    }

    @ExceptionHandler(value=Exception.class)
    public ResultVO handleException(Exception e) throws Exception {
        logger.error("出现异常:",e);
        return ResultVO.error(e.getMessage());
    }
}

五、mybatis配置xml头

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="xxx.xxx.xxx.xxxxx">
</mapper>

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天道酬勤-L

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值