生产环境偶尔会有一些慢请求致使系统性能降低,吞吐量降低,下面介绍几种优化建议。java
电子商务类型网站大多都是短请求,通常响应时间都在100ms,这时能够将web容器从tomcat替换为undertow,下面介绍下步骤: 一、增长pom配置web
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> <exclusions> <exclusion> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-tomcat</artifactid> </exclusion> </exclusions> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-undertow</artifactid> </dependency>
二、增长相关配置redis
server: undertow: direct-buffers: true io-threads: 4 worker-threads: 160
从新启动能够在控制台看到容器已经切换为undertow了spring
将部分热点数据或者静态数据放到本地缓存或者redis中,若是有须要能够定时更新缓存数据缓存
在代码过程当中咱们不少代码都不须要等返回结果,也就是部分代码是能够并行执行,这个时候可使用异步,最简单的方案是使用springboot提供的@Async注解,固然也能够经过线程池来实现,下面简单介绍下异步步骤。 一、pom依赖 通常springboot引入web相关依赖就行tomcat
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency>
二、在启动类中增长@EnableAsync注解springboot
@EnableAsync @SpringBootApplication public class AppApplication { public static void main(String[] args) { SpringApplication.run(AppApplication.class, args); } }
三、须要时在指定方法中增长@Async注解,若是是须要等待返回值,则demo以下异步
@Async public Future<string> doReturn(int i){ try { // 这个方法须要调用500毫秒 Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } // 消息汇总 return new AsyncResult<>("异步调用"); }
四、若是有线程变量或者logback中的mdc,能够增长传递ide
import org.slf4j.MDC; import org.springframework.context.annotation.Configuration; import org.springframework.core.task.TaskDecorator; import org.springframework.scheduling.annotation.AsyncConfigurerSupport; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.Map; import java.util.concurrent.Executor; /** * @Description: */ @EnableAsync @Configuration public class AsyncConfig extends AsyncConfigurerSupport { @Override public Executor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setTaskDecorator(new MdcTaskDecorator()); executor.initialize(); return executor; } } class MdcTaskDecorator implements TaskDecorator { @Override public Runnable decorate(Runnable runnable) { Map<string, string> contextMap = MDC.getCopyOfContextMap(); return () -> { try { MDC.setContextMap(contextMap); runnable.run(); } finally { MDC.clear(); } }; } }
五、有时候异步须要增长阻塞spring-boot
import lombok.extern.slf4j.Slf4j; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.Executor; import java.util.concurrent.ThreadPoolExecutor; @Configuration @Slf4j public class TaskExecutorConfig { @Bean("localDbThreadPoolTaskExecutor") public Executor threadPoolTaskExecutor() { ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); taskExecutor.setCorePoolSize(5); taskExecutor.setMaxPoolSize(200); taskExecutor.setQueueCapacity(200); taskExecutor.setKeepAliveSeconds(100); taskExecutor.setThreadNamePrefix("LocalDbTaskThreadPool"); taskExecutor.setRejectedExecutionHandler((Runnable r, ThreadPoolExecutor executor) -> { if (!executor.isShutdown()) { try { Thread.sleep(300); executor.getQueue().put(r); } catch (InterruptedException e) { log.error(e.toString(), e); Thread.currentThread().interrupt(); } } } ); taskExecutor.initialize(); return taskExecutor; } }
能够将比较耗时或者不一样的业务拆分出来提供单节点的吞吐量
有不少场景对数据实时性要求不那么强的,或者对业务进行业务容错处理时能够将消息发送到kafka,而后延时消费。举个例子,根据条件查询指定用户发送推送消息,这里能够时按时、按天、按月等等,这时就 </string,></string>