SpringCloud大型企業分散式微服務雲架構原始碼之Springboot 重點知識點整理

JIAN2發表於2021-11-24
1、springboot controller 單例Spring中 controller預設是單例的,因為單例所以不是執行緒安全的。
需要框架原始碼的朋友可以看我個人簡介聯絡我。
SpringCloud大型企業分散式微服務雲架構原始碼之Springboot 重點知識點整理

所以需要注意下面幾點
1.不要在Controller中定義成員變數,可能會造成資料混亂。 2.萬一必須要定義一個非靜態成員變數時候,則通過註解@Scope(“prototype”),將其設定為多例模式。 3.在Controller中使用ThreadLocal變數解決多執行緒問題
spring bean作用域: 引用 singleton:單例模式,當spring建立applicationContext容器的時候,spring會欲初始化所有的該作用域例項,加上lazy-init就可以避免預處理; 引用 prototype:原型模式,每次通過getBean獲取該bean就會新產生一個例項,建立後spring將不再對其管理;
2、Springboot 環境變數配置 在開發的過程中不是所有的配置檔案都可以寫入yaml或者properties檔案中,可能需要動態的從容器中讀取,看下如何配置
現在看看如果我們使用環境變數的方式來配置我們的引數,如下:
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: ${DEMO_APP_JDBC_URL:jdbc:mysql://127.0.0.1/demo?serverTimezone=Asia/Shanghai}
    username: ${DEMO_APP_JDBC_USERNAME:demo}
    password: ${DEMO_APP_JDBC_PASSWORD:demo}
    hikari:
      maximum-pool-size: ${DEMO_APP_JDBC_POOL_MAX_SIZE:30}
      max-lifetime: ${DEMO_APP_JDBC_POOL_MAX_LIFE_TIME:60000}
      minimum-idle: ${DEMO_APP_JDBC_POOL_MIN_SIZE:5}
以上使用${ENV:defauleValue}的形式配置了我們應用的相關引數,如果我們的執行環境配置了上面用到的環境變數,則使用環境變數中的配置,如果沒有配置則使用預設的,比如我配置了環境變數DEMO_APP_JDBC_POOL_MAX_SIZE = 100,則應用程式中的連線池最大連線數就變成100了。
配合docker 食用更香哦
3、restful 介面 現在的應用基本上都是前後端分離的,所以後端介面基本上都是restful介面,怎麼做吶?
在controller 的class 上增加註解,就會生成restful介面
@RestController的作用 相當於+ @ResponseBody
@RestController
@RequestMapping("/api/v1/h5Adapter")
@Api(description = "server-h5-adapter")
public class BaMessageConverterController {
4、@ResponseBody 和 @RequestBody 的區別 @ResponseBody是作用在方法上的,@ResponseBody 表示該方法的返回結果直接寫入 HTTP response body 中,一般在非同步獲取資料時使用【也就是AJAX】,在使用 @RequestMapping後,返回值通常解析為跳轉路徑,但是加上 @ResponseBody 後返回結果不會被解析為跳轉路徑,而是直接寫入 HTTP response body 中。 比如非同步獲取 json 資料,加上 @ResponseBody 後,會直接返回 json 資料。@RequestBody 將 HTTP 請求正文插入方法中,使用適合的 HttpMessageConverter 將請求體寫入某個物件。
@RequestBody是作用在形參列表上,用於將前臺傳送過來固定格式的資料【xml 格式或者 json等】封裝為對應的 JavaBean 物件,封裝時使用到的一個物件是系統預設配置的 HttpMessageConverter進行解析,然後封裝到形參上。
5、@Schedule 定時任務幾乎是專案的標配了, SpringBoot內建了Sping Schedule定時框架,通過註解驅動方式新增所註解方法到定時任務,根據配置定時資訊定時執行
主要是以下兩步:
1、開啟定時器
@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
@EnableCircuitBreaker
@EnableCaching
@EnableScheduling
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
2、在方法上加上註解
//每天凌晨1點執行
  @Scheduled(cron = "0 0 1 * * ?")
  public void doTask() {
  
  }
6、async非同步呼叫 非同步呼叫主要處理一些不要求及時返回的任務,springboot提供了支援
SpringBoot中使用 async實現非同步呼叫
基於註解的使用方式包括如下三步:
啟動類加上@EnableAsync(也可以在配置類中加上)
配置類中完成非同步執行緒池的匯入(這一個可以不要,採用預設的)
需要非同步呼叫的方法加上@Async
定義執行緒池
package com.aispeech.bj.bams.msproduct.config;
import org.springframework.context.annotation.AdviceMode;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
@Configuration
@EnableAsync(mode = AdviceMode.ASPECTJ)
public class ExecutorConfig {
    /** Set the ThreadPoolExecutor's core pool size. */
    private int corePoolSize = 10;
    /** Set the ThreadPoolExecutor's maximum pool size. */
    private int maxPoolSize = 300;
    /** Set the capacity for the ThreadPoolExecutor's BlockingQueue. */
    private int queueCapacity = 10;
    @Bean
    public Executor myAsync() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.setThreadNamePrefix("MyExecutor-");
        // rejection-policy:當pool已經達到max size的時候,如何處理新任務
        // CALLER_RUNS:不在新執行緒中執行任務,而是有呼叫者所在的執行緒來執行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }
}
使用非同步 @Async 註解
@Async("myAsync")
    void notifyProductInfo(String thirdPartyId, ThirdPartyProductVO thirdPartyProductVO) {}
7、request 的獲取方式和執行緒安全 request 是來自前端的請求,怎麼才能安全的獲取request,執行緒安全的request 才能取出正確的資料,主要有下面幾種方式
1、靜態類獲取
HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
2、函式引數
Controller中獲取request物件後,如果要在其他方法中(如service方法、工具類方法等)使用request物件,需要在呼叫這些方法時將request物件作為引數傳入
此時request物件是方法引數,相當於區域性變數,毫無疑問是執行緒安全的。
@RequestMapping("/test")
    public void test(HttpServletRequest request) {
        ......
    }
3、自動注入
 @Autowired
    private HttpServletRequest request; //自動注入request
物件是執行緒區域性變數(ThreadLocal),因此request物件也是執行緒區域性變數;這就保證了request物件的執行緒安全性。
不會有執行緒安全問題的 實際上spring 注入的是一個代理類


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70006413/viewspace-2843909/,如需轉載,請註明出處,否則將追究法律責任。

相關文章