SpringBoot 2.x 集成 之 微服务异常

1、Quartz异常失败信息

1.1异常信息

java.util.concurrent.ExecutionException: java.lang.RuntimeException: 执行定时任务失败
	at java.util.concurrent.FutureTask.report(FutureTask.java:122) ~[na:1.8.0_201]
	at java.util.concurrent.FutureTask.get(FutureTask.java:192) ~[na:1.8.0_201]
	at com.job.server.job.utils.ScheduleJob.executeInternal(ScheduleJob.java:70) ~[classes!/:na]
	at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:75) [spring-context-support-5.0.10.RELEASE.jar!/:5.0.10.RELEASE]
	at org.quartz.core.JobRunShell.run(JobRunShell.java:202) [quartz-2.3.0.jar!/:na]
	at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) [quartz-2.3.0.jar!/:na]
Caused by: java.lang.RuntimeException: 执行定时任务失败
	at com.job.server.job.utils.ScheduleRunnable.run(ScheduleRunnable.java:56) ~[classes!/:na]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[na:1.8.0_201]
	at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[na:1.8.0_201]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_201]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_201]
	at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_201]
Caused by: java.lang.reflect.InvocationTargetException: null
	at sun.reflect.GeneratedMethodAccessor177.invoke(Unknown Source) ~[na:na]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_201]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_201]
	at com.job.server.job.utils.ScheduleRunnable.run(ScheduleRunnable.java:51) ~[classes!/:na]
	... 5 common frames omitted
Caused by: org.springframework.web.client.HttpServerErrorException: 500 null
	at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:97) ~[spring-web-5.0.10.RELEASE.jar!/:5.0.10.RELEASE]
	at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:79) ~[spring-web-5.0.10.RELEASE.jar!/:5.0.10.RELEASE]
	at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-5.0.10.RELEASE.jar!/:5.0.10.RELEASE]
	at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:730) ~[spring-web-5.0.10.RELEASE.jar!/:5.0.10.RELEASE]
	at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:688) ~[spring-web-5.0.10.RELEASE.jar!/:5.0.10.RELEASE]
	at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:644) ~[spring-web-5.0.10.RELEASE.jar!/:5.0.10.RELEASE]
	at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:296) ~[spring-web-5.0.10.RELEASE.jar!/:5.0.10.RELEASE]
	at com.job.server.job.task.Crontab.feignFaBack(Crontab.java:39) ~[classes!/:na]
	... 9 common frames omitted

1.2 解决方案一(建议)

  1. 手动创建线程池
private ExecutorService service = newSingleThreadExecutor();

public static ExecutorService newSingleThreadExecutor() {
		return new ThreadPoolExecutor(1, 1,
						0L, TimeUnit.MILLISECONDS,
						new LinkedBlockingQueue<Runnable>());
	}
  1. 添加注解
@DisallowConcurrentExecution
@PersistJobDataAfterExecution

2、Swagger 404

2.1 添加 配置文件SwaggerConfig(与启动类放在同级目录)

@EnableSwagger2
@Configuration
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xxx.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("xxx")
                .description("关注:xxxx")
                .termsOfServiceUrl("http://localhost:9001")
                .contact("sanduo")
                .version("v1.0")
                .build();
    }

}

2.2 添加config

 	@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
    /**
     * 解决 swagger-ui404问题
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}

3、解决前台返回Long类型精度损失的问题

3.1 添加config

/**
 * 解决前台返回Long类型精度损失的问题
 * @author 三多
 * @Time 2019/3/25
 */
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
    /**
     * 解决前端解析Long丢精度的问题
     * @param converters
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        ObjectMapper objectMapper = new ObjectMapper();
        /**
         * 序列换成json时,将所有的long变成string
         * 因为js中得数字类型不能包含所有的java long值
         */
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);
        jackson2HttpMessageConverter.setObjectMapper(objectMapper);
        converters.add(jackson2HttpMessageConverter);
    }
}

4、微服务注册启动配置重试异常

4.1 创建配置文件

public class CoreApplitionConfig {
    @Bean
    @ConditionalOnMissingBean(name = "configServerRetryInterceptor")
    public RetryOperationsInterceptor configServerRetryInterceptor() {

        return RetryInterceptorBuilder
                .stateless()
                .backOffOptions(1000, 1.2, 5000)
                .maxAttempts(10)
                .build();
    }
}

配置注入交给spring管理,创建spring.factories

创建文件
添加:

org.springframework.cloud.bootstrap.BootstrapConfiguration=com.job.server.config.CoreApplitionConfig

5、 文件上传 超限制

5.1 异常信息

  1. 项目使用的是Spring Boot + Spring Cloud,上传附件报超出自带tomacat限制大小(默认1M)
"Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: 
The field file exceeds its maximum permitted size of 1048576 bytes

5.2 解决方案

  1. 解决方案
    (1)在配置文件(application.properties)加入如下代码
spring:
  servlet:
    multipart:
      max-file-size: 10MB # 单个数据大小 
      max-request-size: 100MB #是总数据大小

(2)把如下代码放在启动类上,并在类上加入@Configuration


    /**
     * 文件上传配置
     * 
     * @return
     */
    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //  单个数据大小
        factory.setMaxFileSize("10MB"); // KB,MB
        /// 总上传数据大小
        factory.setMaxRequestSize("100MB");
        return factory.createMultipartConfig();
    }

PS:若是有网关之类的中转,需在网关中也加入如上配置。

参考

  1. springboot附件上传超限
  2. 多实例注册解决方案
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值