Spring Boot Event 觀察者模式,輕鬆實現業務解耦!

大家好,我是不才陳某~

陳某的 《Spring Cloud Alibaba 實戰項目》 視頻教程已經錄完了,涉及到 Alibaba 的各種中間件、OAuth2 微服務認證鑑權全鏈路灰度發佈分佈式事務實戰,戳這裏 --->Spring Cloud Alibaba 實戰 視頻專欄 開放訂閱~

實際業務開發過程中,業務邏輯可能非常複雜,核心業務 + N 個子業務。如果都放到一塊兒去做,代碼可能會很長,耦合度不斷攀升,維護起來也麻煩,甚至頭疼。還有一些業務場景不需要在一次請求中同步完成,比如郵件發送、短信發送等。

MQ 確實可以解決這個問題,但 MQ 重啊,非必要不提升架構複雜度。

針對這些問題,我們瞭解一下 Spring Event。

Spring Event 同步使用

Spring Event(Application Event)其實就是一個觀察者設計模式,一個 Bean 處理完成任務後希望通知其它 Bean 或者說一個 Bean 想觀察監聽另一個 Bean 的行爲。

Spring Event 用來解耦業務真的賊好用!

1. 自定義事件

定義事件,繼承 ApplicationEvent 的類成爲一個事件類

/**
 * @author Strive
 * @date 2022/4/22 18:00
 * @description
 */
@Data
@ToString
public class OrderProductEvent extends ApplicationEvent {

  /** 該類型事件攜帶的信息 */
  private String orderId;

  public OrderProductEvent(Object source, String orderId) {
    super(source);
    this.orderId = orderId;
  }
}

2. 定義監聽器

監聽並處理事件,實現 ApplicationListener 接口或者使用 @EventListener 註解;關注公衆號:“碼猿技術專欄”,回覆關鍵詞:“3333”,獲取阿里內部《Spring Cloud Alibaba 實戰》手冊

/**
 * 實現 ApplicationListener 接口,並指定監聽的事件類型
 *
 * @author Strive
 * @date 2022/4/24 09:09
 * @description
 */
@Slf4j
@Component
public class OrderProductListener implements ApplicationListener<OrderProductEvent> {

  /** 使用 onApplicationEvent 方法對消息進行接收處理 */
  @SneakyThrows
  @Override
  public void onApplicationEvent(OrderProductEvent event) {
    String orderId = event.getOrderId();
    long start = System.currentTimeMillis();
    Thread.sleep(2000);
    long end = System.currentTimeMillis();
    log.info("{}:校驗訂單商品價格耗時:({})毫秒", orderId, (end - start));
  }
}

3. 定義發佈者

發佈事件,通過 ApplicationEventPublisher 發佈事件;

推薦個人知識總結網站:www.java-family.cn

/**
 * @author Strive
 * @date 2022/4/24 09:25
 * @description
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class OrderService {

  /** 注入ApplicationContext用來發布事件 */
  private final ApplicationContext applicationContext;

  /**
   * 下單
   *
   * @param orderId 訂單ID
   */
  public String buyOrder(String orderId) {
    long start = System.currentTimeMillis();
    // 1.查詢訂單詳情

    // 2.檢驗訂單價格 (同步處理)
    applicationContext.publishEvent(new OrderProductEvent(this, orderId));

    // 3.短信通知(異步處理)

    long end = System.currentTimeMillis();
    log.info("任務全部完成,總耗時:({})毫秒", end - start);
    return "購買成功";
  }
}

4. 單測執行

@SpringBootTest
public class OrderServiceTest {
  @Autowired private OrderService orderService;

  @Test
  public void buyOrderTest() {
    orderService.buyOrder("732171109");
  }
}

執行結果如下:

2022-04-24 10:13:17.535  INFO 44272 --- [           main] c.c.m.e.listener.OrderProductListener    : 732171109:校驗訂單商品價格耗時:(2008)毫秒
2022-04-24 10:13:17.536  INFO 44272 --- [           main] c.c.mingyue.event.service.OrderService   : 任務全部完成,總耗時:(2009)毫秒

Spring Event 異步使用

有些業務場景不需要在一次請求中同步完成,比如郵件發送、短信發送等。

1. 自定義事件

@Data
@AllArgsConstructor
public class MsgEvent {

  /** 該類型事件攜帶的信息 */
  public String orderId;
}

2. 定義監聽器

推薦使用 @EventListener 註解

@Slf4j
@Component
public class MsgListener {

  @SneakyThrows
  @EventListener(MsgEvent.class)
  public void sendMsg(MsgEvent event) {
    String orderId = event.getOrderId();
    long start = System.currentTimeMillis();
    log.info("開發發送短信");
    log.info("開發發送郵件");
    Thread.sleep(4000);
    long end = System.currentTimeMillis();
    log.info("{}:發送短信、郵件耗時:({})毫秒", orderId, (end - start));
  }
}

3. 定義發佈者

/**
  * 下單
  * @param orderId 訂單ID
  */
public String buyOrder(String orderId) {
    long start = System.currentTimeMillis();
    // 1.查詢訂單詳情

    // 2.檢驗訂單價格 (同步處理)
    applicationContext.publishEvent(new OrderProductEvent(this, orderId));

    // 3.短信通知(異步處理)
    applicationContext.publishEvent(new MsgEvent(orderId));

    long end = System.currentTimeMillis();
    log.info("任務全部完成,總耗時:({})毫秒", end - start);
    return "購買成功";
}

4. 單測執行(同步)

@Test
public void buyOrderTest() {
    orderService.buyOrder("732171109");
}

執行結果如下:

2022-04-24 10:24:13.905  INFO 54848 --- [           main] c.c.m.e.listener.OrderProductListener    : 732171109:校驗訂單商品價格耗時:(2004)毫秒
2022-04-24 10:24:13.906  INFO 54848 --- [           main] c.c.mingyue.event.listener.MsgListener   : 開發發送短信
2022-04-24 10:24:13.907  INFO 54848 --- [           main] c.c.mingyue.event.listener.MsgListener   : 開發發送郵件
2022-04-24 10:24:17.908  INFO 54848 --- [           main] c.c.mingyue.event.listener.MsgListener   : 732171109:發送短信、郵件耗時:(4002)毫秒
2022-04-24 10:24:17.908  INFO 54848 --- [           main] c.c.mingyue.event.service.OrderService   : 任務全部完成,總耗時:(6008)毫秒

5. 開啓異步

啓動類增加 @EnableAsync 註解

@EnableAsync
@SpringBootApplication
public class MingYueSpringbootEventApplication {

  public static void main(String[] args) {
    SpringApplication.run(MingYueSpringbootEventApplication.class, args);
  }
}

Listener 類需要開啓異步的方法增加 @Async 註解

@Async
@SneakyThrows
@EventListener(MsgEvent.class)
public void sendMsg(MsgEvent event) {
    String orderId = event.getOrderId();
    long start = System.currentTimeMillis();
    log.info("開發發送短信");
    log.info("開發發送郵件");
    Thread.sleep(4000);
    long end = System.currentTimeMillis();
    log.info("{}:發送短信、郵件耗時:({})毫秒", orderId, (end - start));
}

6. 單測執行(異步)

發送短信的線程顯示 task-1,主線程結束後(總耗時:(2017) 毫秒)控制檯停止打印了

2022-04-24 10:30:59.002  INFO 59448 --- [           main] c.c.m.e.listener.OrderProductListener    : 732171109:校驗訂單商品價格耗時:(2009)毫秒
2022-04-24 10:30:59.009  INFO 59448 --- [           main] c.c.mingyue.event.service.OrderService   : 任務全部完成,總耗時:(2017)毫秒
2022-04-24 10:30:59.028  INFO 59448 --- [         task-1] c.c.mingyue.event.listener.MsgListener   : 開發發送短信
2022-04-24 10:30:59.028  INFO 59448 --- [         task-1] c.c.mingyue.event.listener.MsgListener   : 開發發送郵件

Java 後端面試官 Java 後端全棧面試題精選,數據庫、消息隊列、架構..... 你想要知道的都在這裏!

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/p6x2JcTukj6nXBPO0MOPYA