style(Dockerfile):Dockerfile添加日志

This commit is contained in:
有来技术 2021-09-22 00:01:10 +08:00
parent 5bcd64fd18
commit d7ba9b0657
7 changed files with 98 additions and 143 deletions

View File

@ -1,6 +1,24 @@
FROM openjdk:8-jdk-alpine # 基础镜像
VOLUME /tmp FROM openjdk:8-jre
ADD target/pms-boot.jar mall-pms.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/mall-pms.jar"] # 维护者信息
EXPOSE 8602 MAINTAINER youlai <youlaitech@163.com>
# 设置容器时区为当前时区
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \&& echo 'Asia/Shanghai' >/etc/timezone RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \&& echo 'Asia/Shanghai' >/etc/timezone
# /tmp 目录作为容器数据卷目录SpringBoot内嵌Tomcat容器默认使用/tmp作为工作目录任何向 /tmp 中写入的信息不会记录进容器存储层,从而保证容器存储层的无状态化
# 在宿主机的/var/lib/docker目录下创建一个临时文件并把它链接到容器中的/tmp目录
VOLUME /tmp
# 复制主机文件至镜像内,复制的目录需放置在 Dockerfile 文件同级目录下
ADD target/pms-boot.jar app.jar
# 容器启动执行命令
ENTRYPOINT ["java", "-Xms128M -Xmx128M", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app.jar"]
# 声明运行时容器提供服务端口,仅仅是声明
EXPOSE 8802

View File

@ -1,6 +1,18 @@
FROM openjdk:8-jdk-alpine FROM openjdk:8-jre
VOLUME /tmp MAINTAINER youlai youlaitech@163.com
ADD target/sms-boot.jar mall-sms.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/mall-sms.jar"]
EXPOSE 8604
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \&& echo 'Asia/Shanghai' >/etc/timezone RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \&& echo 'Asia/Shanghai' >/etc/timezone
# /tmp 目录作为容器数据卷目录SpringBoot内嵌Tomcat容器默认使用/tmp作为工作目录任何向 /tmp 中写入的信息不会记录进容器存储层,从而保证容器存储层的无状态化
# 在宿主机的/var/lib/docker目录下创建一个临时文件并把它链接到容器中的/tmp目录
VOLUME /tmp
# 复制jar到镜像
ADD target/sms-boot.jar app.jar
ENTRYPOINT ["java", "-Xmx128m", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app.jar"]
EXPOSE 8804

View File

@ -1,6 +1,24 @@
FROM openjdk:8-jdk-alpine # 基础镜像
VOLUME /tmp FROM openjdk:8-jre
ADD target/admin-boot.jar youlai-admin.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/youlai-admin.jar"] # 维护者信息
EXPOSE 8100 MAINTAINER youlai <youlaitech@163.com>
# 设置容器时区为当前时区
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \&& echo 'Asia/Shanghai' >/etc/timezone RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \&& echo 'Asia/Shanghai' >/etc/timezone
# /tmp 目录作为容器数据卷目录SpringBoot内嵌Tomcat容器默认使用/tmp作为工作目录任何向 /tmp 中写入的信息不会记录进容器存储层,从而保证容器存储层的无状态化
# 在宿主机的/var/lib/docker目录下创建一个临时文件并把它链接到容器中的/tmp目录
VOLUME /tmp
# 复制主机文件至镜像内,复制的目录需放置在 Dockerfile 文件同级目录下
ADD target/admin-boot.jar app.jar
# 容器启动执行命令
ENTRYPOINT ["java", "-Xms128M -Xmx128M", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app.jar"]
# 声明容器提供服务端口
EXPOSE 8802

View File

@ -1,6 +1,18 @@
FROM openjdk:8-jdk-alpine FROM openjdk:8-jre
VOLUME /tmp MAINTAINER youlai youlaitech@163.com
ADD target/youlai-auth.jar youlai-auth.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/youlai-auth.jar"]
EXPOSE 8000
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \&& echo 'Asia/Shanghai' >/etc/timezone RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \&& echo 'Asia/Shanghai' >/etc/timezone
# /tmp 目录作为容器数据卷目录SpringBoot内嵌Tomcat容器默认使用/tmp作为工作目录任何向 /tmp 中写入的信息不会记录进容器存储层,从而保证容器存储层的无状态化
# 在宿主机的/var/lib/docker目录下创建一个临时文件并把它链接到容器中的/tmp目录
VOLUME /tmp
# 复制jar到镜像
ADD target/youlai-auth.jar app.jar
ENTRYPOINT ["java", "-Xmx128m", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app.jar"]
EXPOSE 8000

View File

@ -1,83 +0,0 @@
package com.youlai.common.web.aspect;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.youlai.common.constant.AuthConstants;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.MDC;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* @author hxr
* @date 2021-03-01
*/
/*@Aspect
@Component
@AllArgsConstructor
@Slf4j
@ConditionalOnProperty(value = "spring.application.name", havingValue = "youlai-auth")*/
public class LoginLogAspect {
@Pointcut("execution(public * com.youlai.auth.controller.AuthController.postAccessToken(..))")
public void Log() {
}
@Around("Log()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
LocalDateTime startTime = LocalDateTime.now();
Object result = joinPoint.proceed();
// 获取请求信息
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// 刷新token不记录
String grantType=request.getParameter(AuthConstants.GRANT_TYPE_KEY);
if(grantType.equals(AuthConstants.REFRESH_TOKEN)){
return result;
}
// 时间统计
LocalDateTime endTime = LocalDateTime.now();
long elapsedTime = Duration.between(startTime, endTime).toMillis(); // 请求耗时毫秒
// 获取接口描述信息
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
String description = signature.getMethod().getAnnotation(ApiOperation.class).value();// 方法描述
String username = request.getParameter(AuthConstants.USER_NAME_KEY); // 登录用户名
String date = startTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")); // 索引名需要因为默认生成索引的date时区不一致
// 获取token
String token = Strings.EMPTY;
if (request != null) {
JSONObject jsonObject = JSONUtil.parseObj(result);
token = jsonObject.getStr("value");
}
// MDC 扩展logback字段具体请看logback-spring.xml的自定义日志输出格式
MDC.put("elapsedTime", StrUtil.toString(elapsedTime));
/* MDC.put("description", description);
MDC.put("region", region);
MDC.put("username", username);
MDC.put("date", date);
MDC.put("token", token);
MDC.put("clientIP", clientIP);*/
//log.info("{} 登录,耗费时间 {} 毫秒", username, elapsedTime); // 收集日志这里必须打印一条日志内容随便吧记录在message字段具体看logback-spring.xml文件
return result;
}
}

View File

@ -1,35 +0,0 @@
package com.youlai.common.web.listener;
import org.slf4j.MDC;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.SmartApplicationListener;
import org.springframework.core.Ordered;
/**
* @author <a href="mailto:xianrui0365@163.com">xianrui</a>
* @date 2021/8/26
*/
public class LoggingListener implements SmartApplicationListener,Ordered {
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
return ApplicationEnvironmentPreparedEvent.class.isAssignableFrom(eventType) || ApplicationPreparedEvent.class.isAssignableFrom(eventType);
}
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationEnvironmentPreparedEvent) {
String appName = ((ApplicationEnvironmentPreparedEvent) event).getEnvironment().getProperty("spring.application.name");
MDC.put("appName",appName);
}
if (event instanceof ApplicationPreparedEvent) {
}
}
@Override
public int getOrder() {
// 写在加载配置文件之后
return Ordered.HIGHEST_PRECEDENCE -1;
}
}

View File

@ -1,6 +1,19 @@
FROM openjdk:8-jdk-alpine FROM openjdk:8-jre
VOLUME /tmp MAINTAINER youlai youlaitech@163.com
ADD target/youlai-gateway.jar youlai-gateway.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/youlai-gateway.jar"]
EXPOSE 9999
RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \&& echo 'Asia/Shanghai' >/etc/timezone RUN /bin/cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \&& echo 'Asia/Shanghai' >/etc/timezone
# /tmp 目录作为容器数据卷目录SpringBoot内嵌Tomcat容器默认使用/tmp作为工作目录任何向 /tmp 中写入的信息不会记录进容器存储层,从而保证容器存储层的无状态化
# 在宿主机的/var/lib/docker目录下创建一个临时文件并把它链接到容器中的/tmp目录
VOLUME /tmp
# 复制jar至镜像
ADD target/youlai-gateway.jar app.jar
ENTRYPOINT ["java", "-Xmx128m", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app.jar"]
EXPOSE 9999