没长正的技术专栏 勤动手、多思考

SpringBoot常见问题

2018-06-30

阅读:


一、SpringBoot

介绍SpringBoot常见问题总结,便于快速分析。

1. 注解

1.1 @Valid注解

//时间格式:
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
private LocalDateTime createTime;

@NotEmpty(message="")
@NotNull(message="")
@Length(Min=1,Max=6)

//正则表达式 [2020-05-20]
@Pattern(regexp = "^[a-z0-9_]+$", message = "数字、26个小写英文字母和下划线")

//嵌套实体校验
@Valid

/**
 * 开启嵌套实体校验
 */
@Valid
@ApiModelProperty("模板字段集合")
private List<TemplateFields> templateFieldsList;

2. 事务

//类级别事务
@Transactional(rollbackFor = Exception.class)
public class TemplateServiceImpl implements ITemplateService {
    @Override
    public PageEntity<Template> findMyTemplateList(TemplateParam templateParam) {
    
}

 //方法级别
@Transactional(propagation = Propagation.NESTED, 
isolation = Isolation.DEFAULT, rollbackFor = DataCollectionException.class)
public class TemplateServiceImpl implements ITemplateService {
    @Override
    public PageEntity<Template> findMyTemplateList(TemplateParam templateParam) {
    
}


// 特别注意:如果逻辑存在明显逻辑/先后关系,注意使用事务处理。(保证顺序)
// 或者获取上一个语句的返回值,再进行下一条语句。
@Override
@Transactional(rollbackFor = Exception.class)
public String getOrder(GetOrderParam param) {
    try {
        //... 略
        workorderLockInfoMapper.update(build);

        SysRelationParam relationBuild = SysRelationParam.builder().orderIds(Arrays.asList(param.getOrderId())).userCode(xUser.getCode()).build();
        addSysRelation(relationBuild);
        return uuid12;
    } catch (WlhBusinessException e) {
        log.error("领取工单失败:-> {}", e.getMessage());
        throw new WlhBusinessException(e.getMessage());
    }
 }

3. 异步服务

原服务类方法写异步注解,不生效。

/**
 * 异步配置类
 */
@EnableAsync
@Configuration
@Slf4j
public class AsyncConfig implements AsyncConfigurer {
    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        int optimum = Runtime.getRuntime().availableProcessors();
        executor.setCorePoolSize(optimum * 2);
        executor.setMaxPoolSize(optimum * 4);
        executor.setQueueCapacity(1000);
        executor.setThreadNamePrefix("data-schedule-asyncExecutor-");
        executor.initialize();
        return executor;
    }
    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return (Throwable ex, Method method, Object... params) -> {
            log.error("Unexpected error occurred invoking async method: " + method, ex);
        };
    }
}

/**
 * 异步服务  需要使用 @EnableAsync 注解
 */
@Component
public class AsyncService {

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

    @Resource
    private IUserService userService;

    @Async
    public void getUserName() {

        logger.info(userService.getUserName());
    }
}

4. OOM

4.1 springboot 内存溢出如何重启

参考


欢迎拍砖,多多交流,转载请注明出处:[没长正的技术专栏](http://blog.meizhangzheng.com) 如涉及侵权问题,请发送邮件到xsj34567@163.com,如情况属实本人将会尽快删除。


下一篇 初识微服务

Comments

Content