目录
1.启动错误
1.1 错误: 找不到或无法加载主类
解决方法:
mvn clean package
1.2 找不到主清单
在CCE容器部署时候发现日志一直打印这个,然后容器无法正常启动
no main manifest attribute, in delivery-ocpx-1.0.0.jar
问题原因:注意repackage
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.6.6</version>
<configuration>
<fork>true</fork> <!-- 如果没有该配置,devtools不会生效 -->
</configuration>
<!-- 加上这个就ok了 -->
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
2.找不到类
2.1 java -jar 启动失败
问题描述:IDEA可以正常启动,打包后运行出现java.lang.ClassNotFoundException
详细:
为什么我能搞出这么大的BUG
问题原因:
解决方法:
3.定时任务
问题描述:
1.通过@Scheduler启动的定时任务在本地执行,服务器部署也执行,但是在docker容器中不执行
问题原因:
1.这个定时任务是单线程,需要配置任务线程池
@Bean
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(50);
return taskScheduler;
}
2.时区问题,本地时区和容器的时区不一样
@Scheduled(cron = "0 0/1 * * * ?", zone = "Asia/Shanghai")
时区问题在使用@Scheduled注解时保持和docker容器里面的时区一致
然后问题解决
4.AOP和JetCache同时使用出现的问题
java.lang.IllegalStateException: No MethodInvocation found: Check that an AOP invocation is in progress and that the ExposeInvocationInterceptor is upfront in the interceptor chain. Specifically, note that advices with order HIGHEST_PRECEDENCE will execute before ExposeInvocationInterceptor! In addition, ExposeInvocationInterceptor and ExposeInvocationInterceptor.currentInvocation() must be invoked from the same thread
解决办法:加入 @Order注解
gson对象转换
说明:在使用redis做消息发布订阅时候,B服务消费A服务发送的消息,进行序列化发现B服务实体对象中存在Date类型的createTime和updateTime字段,正常使用的转换器无法成功序列化
自己使用的序列化工具类
package com.hhmt.delivery.utils;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hhmt.delivery.adapter.GsonTypeAdapter;
import com.hhmt.delivery.constant.DateParams;
import com.hhmt.delivery.continer.ErrorCode;
import com.hhmt.delivery.core.domain.ResultVo;
import com.hhmt.delivery.core.page.TableDataInfo;
import com.hhmt.delivery.utils.exception.ServiceCustomerException;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
/**
* @author huachun
* @version 1.0
* @description: TODO
* @email huachun_w@163.com
* @date 2022-08-23 11:16
*/
@Log4j2
@Component
public class TypeCovertUtils {
/**
* @param data 数据
* @param clazz 目标类型
* @return java.util.List<T>
* @description: 转换List集合类型
* 示例: typeCovertUtils.covertListType(baiduAdxVo.getResponse(), HhAdxAdvertiserInfoVo.class);
* @author huachun
* @email huachun_w@163.com
* @date 2023/1/5 18:58
*/
public <T, K> List<T> covertListType(K data, Class<T> clazz) {
Gson gson = new GsonBuilder().setDateFormat(DateFormatContant.YYYY_MM_DD_HH_MM_SS).registerTypeAdapter(new TypeToken<List<T>>() {
}.getType(), new GsonTypeAdapter()).create();
String jsonStr = gson.toJson(data);
return gson.fromJson(jsonStr, new TypeToken<List<T>>() {
}.getType());
}
}
通过 Gson gson = new GsonBuilder().setDateFormat(DateFormatContant.YYYY_MM_DD_HH_MM_SS) 方式设置Date属性格式,完美解决问题
原文参考:https://wenku.youkuaiyun.com/answer/056d2ad1b8444c83a0d4949eec180b76