读《Spring Cloud微服务全栈技术与案例解析》的笔记三--线程池配置的自定义和随机端口

感谢读者

非常感谢微信读者,意想不到的收获大约20+阅读量,虽然是个小小的进步,但还是控制不住内心的激动,这种小小的成就感让我精神起来了。您的支持就是我们进步最大的动力,还请读者朋友们多多关注,给我们提出更好的建设性的意见,让我们一起做更好的自己。

在实际应用中,我们可能需要根据实际需求来调整自己线程池的配置,因此我们通过配置文件来调整线程池,会更灵活方便

配置对象类

配置对象类代码:

package com.yeegee.demo.configuration;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConfigurationProperties("spring.task.pool")
public class ThreadPoolConfig {

    //核心线程数
    private int corePoolSize=5;

    public int getMaxPoolSize() {
        return maxPoolSize;
    }

    public void setMaxPoolSize(int maxPoolSize) {
        this.maxPoolSize = maxPoolSize;
    }

    //最大线程数
    private int maxPoolSize=5;

    public int getKeepAliveSeconds() {
        return keepAliveSeconds;
    }

    public void setKeepAliveSeconds(int keepAliveSeconds) {
        this.keepAliveSeconds = keepAliveSeconds;
    }

    //线程池维护线程所允许的空闲时间
    private int keepAliveSeconds=60;

    public int getQueueCapacity() {
        return queueCapacity;
    }

    public void setQueueCapacity(int queueCapacity) {
        this.queueCapacity = queueCapacity;
    }

    //队列长度
    private int queueCapacity=10000;

    public String getThreadNamePrefix() {
        return threadNamePrefix;
    }

    public void setThreadNamePrefix(String threadNamePrefix) {
        this.threadNamePrefix = threadNamePrefix;
    }

    //线程池名称前缀
    private String threadNamePrefix="Yeegee-AsyncTask-";

    public int getCorePoolSize() {
        return corePoolSize;
    }

    public void setCorePoolSize(int corePoolSize) {
        this.corePoolSize = corePoolSize;
    }


}

线程池使用配置

线程池使用配置的代码:

package com.yeegee.demo.configuration;

import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.lang.reflect.Method;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class AsyncTaskExecutePoolConfiguration implements AsyncConfigurer {

    @Autowired
    private ThreadPoolConfig threadPoolConfig;

    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setCorePoolSize(threadPoolConfig.getCorePoolSize());
        threadPoolTaskExecutor.setMaxPoolSize(threadPoolConfig.getMaxPoolSize());
        threadPoolTaskExecutor.setQueueCapacity(threadPoolConfig.getQueueCapacity());
        threadPoolTaskExecutor.setKeepAliveSeconds(threadPoolConfig.getKeepAliveSeconds());
        threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        threadPoolTaskExecutor.initialize();
        return threadPoolTaskExecutor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {

        //异步任务中的异常处理
        return new AsyncUncaughtExceptionHandler() {
            @Override
            public void handleUncaughtException(Throwable throwable, Method method, Object... objects) {
                System.out.println("==========="+throwable.getMessage()+"======="+throwable.getStackTrace()+"\n\r");
                System.out.println("=========== exception method:"+method.getName()+"=======");
            }
        };
    }
}

ThreadPoolExecutor.CallerRunsPolicy 是线程池运行的一种策略,它是由主线程直接执行该任务,执行完成后尝试添加下一个任务到线程池中,这样可以有效的降低向线程池内添加任务的速度。 同时也能保证任务执行不会被丢弃

随机端口

一般项目中也很少用到随机端口,这部分感兴趣的可以看看书上内容,我这里一笔带过了。

我们只要知道可以进行随机配置即可

server.port = ${random.int(2000,8000)}

通过上述配置可以生成一个数字在2000-8000之间的端口号

关于启动参数设置类,可以考虑设计用于JVM优化参数配置等性能调优方面,如果仅仅为了个端口去写个这种类,感觉有点大材小用了。

免费领书

关于《Spring Cloud微服务全栈技术与案例解析》 这本书,一共有300多页的内容,从单体应用的基础到微服务的各个组件,从负载均衡到分布式事务都有非常详细的讲解,如果您对此书感兴趣,可以加我微信(yeegee2024)免费领书

这本书是我第一次系统化的梳理知识的书,只要时间允许,我还会去啃更多的书,用系统化的方式,去总结每一个知识点,希望大家都能跟上节奏,一起好好消化,掌握更多专业技能,收获更多的人生价值。