生產環境偶爾會有一些慢請求致使系統性能降低,吞吐量降低,下面介紹幾種優化建議。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>