背景
auth服务本地部署,验证码正常显示。但是一旦镜像部署则会报错。
Dockerfile文件如下
FROM daocloud.io/library/java:8-jre-alpine
MAINTAINER zzjs
# 时区问题
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone
# 字体
COPY fonts/simsun.ttc /usr/share/fonts/simsun.ttc
ARG JAVA_OPTS="-Xmx128M"
ENV JAVA_OPTS=$JAVA_OPTS
ARG RUN_ARGS="--spring.profiles.active=dev"
ENV RUN_ARGS=$RUN_ARGS
ADD target/xx-auth.jar /xx-auth.jar
ENTRYPOINT ["sh","-c","java $JAVA_OPTS -jar /xx-auth.jar $RUN_ARGS"]
本地没问题,部署到服务器上图片验证码生成报错。原来是图形验证码依赖了系统组件,只需安装依赖即可解决。
报错
java.lang.NullPointerException: null
2023-07-02 22:10:49 at sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1264) ~[na:1.8.0_111-internal]
2023-07-02 22:10:49 at sun.awt.FontConfiguration.readFontConfigFile(FontConfiguration.java:219) ~[na:1.8.0_111-internal]
2023-07-02 22:10:49 at sun.awt.FontConfiguration.init(FontConfiguration.java:107) ~[na:1.8.0_111-internal]
2023-07-02 22:10:49 at sun.awt.X11FontManager.createFontConfiguration(X11FontManager.java:774) ~[na:1.8.0_111-internal]
2023-07-02 22:10:49 at sun.font.SunFontManager$2.run(SunFontManager.java:431) ~[na:1.8.0_111-internal]
2023-07-02 22:10:49 at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_111-internal]
2023-07-02 22:10:49 at sun.font.SunFontManager.<init>(SunFontManager.java:376) ~[na:1.8.0_111-internal]
2023-07-02 22:10:49 at sun.awt.FcFontManager.<init>(FcFontManager.java:35) ~[na:1.8.0_111-internal]
2023-07-02 22:10:49 at sun.awt.X11FontManager.<init>(X11FontManager.java:57) ~[na:1.8.0_111-internal]
2023-07-02 22:10:49 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_111-internal]
2023-07-02 22:10:49 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_111-internal]
2023-07-02 22:10:49 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_111-internal]
2023-07-02 22:10:49 at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_111-internal]
2023-07-02 22:10:49 at java.lang.Class.newInstance(Class.java:442) ~[na:1.8.0_111-internal]
2023-07-02 22:10:49 at sun.font.FontManagerFactory$1.run(FontManagerFactory.java:83) ~[na:1.8.0_111-internal]
2023-07-02 22:10:49 at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_111-internal]
2023-07-02 22:10:49 at sun.font.FontManagerFactory.getInstance(FontManagerFactory.java:74) ~[na:1.8.0_111-internal]
2023-07-02 22:10:49 at sun.font.SunFontManager.getInstance(SunFontManager.java:250) ~[na:1.8.0_111-internal]
2023-07-02 22:10:49 at sun.font.FontDesignMetrics.getMetrics(FontDesignMetrics.java:264) ~[na:1.8.0_111-internal]
2023-07-02 22:10:49 at sun.java2d.SunGraphics2D.getFontMetrics(SunGraphics2D.java:855) ~[na:1.8.0_111-internal]
2023-07-02 22:10:49 at com.wf.captcha.SpecCaptcha.graphicsImage(SpecCaptcha.java:76) ~[easy-captcha-1.6.2.jar!/:na]
2023-07-02 22:10:49 at com.wf.captcha.SpecCaptcha.out(SpecCaptcha.java:45) ~[easy-captcha-1.6.2.jar!/:na]
[nio-8081-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class java.util.LinkedHashMap] with preset Content-Type 'image/gif']
解决方案
1、如果服务器部署
yum install fontconfig
fc-cache --force
2、Docker部署
docker进行构建运行,如果每次都需要重新安装此组件,很耗时。
采取另一种措施:重新构建一个基础镜像(安装好依赖,用于支持验证码功能)。后续其他镜像基于此基础镜像做构建。
新建Dockerfile
FROM daocloud.io/library/java:8-jre-alpine
RUN apk add --update --no-cache ttf-dejavu fontconfig && rm -rf /var/cache/apk/*
重新构建镜像
构建:docker build -t auth-openjdk:8-jdk-alpine .
auth的Dockerfile文件修改如下:
FROM auth-openjdk:8-jdk-alpine
MAINTAINER zzjs
# 时区问题
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
RUN echo 'Asia/Shanghai' >/etc/timezone
# 字体
COPY fonts/simsun.ttc /usr/share/fonts/simsun.ttc
ARG JAVA_OPTS="-Xmx128M"
ENV JAVA_OPTS=$JAVA_OPTS
ARG RUN_ARGS="--spring.profiles.active=dev"
ENV RUN_ARGS=$RUN_ARGS
ADD target/xx-auth.jar /xx-auth.jar
ENTRYPOINT ["sh","-c","java $JAVA_OPTS -jar /xx-auth.jar $RUN_ARGS"]
部署服务器即可。