聊聊分布式服務下的八種異步實現(xiàn)方式
一、異步的八種實現(xiàn)方式
1、線程Thread
2、Future
3、異步框架CompletableFuture
4、Spring注解@Async
5、Spring ApplicationEvent事件
6、消息隊列
7、第三方異步框架,比如Hutool的ThreadUtil
8、Guava異步
二、什么是異步?
首先我們先看一個常見的用戶下單的場景:
什么是異步?
在同步操作中,我們執(zhí)行到 發(fā)送短信 的時候,我們必須等待這個方法徹底執(zhí)行完才能執(zhí)行 贈送積分 這個操作,如果 贈送積分 這個動作執(zhí)行時間較長,發(fā)送短信需要等待,這就是典型的同步場景。
實際上,發(fā)送短信和贈送積分沒有任何的依賴關系,通過異步,我們可以實現(xiàn)贈送積分和發(fā)送短信這兩個操作能夠同時進行,比如:
異步
這就是所謂的異步,是不是非常簡單,下面就說說異步的幾種實現(xiàn)方式吧。
三、異步編程
1、線程異步
public class AsyncThread extends Thread {
@Override
public void run() {
System.out.println("Current thread name:" + Thread.currentThread().getName() + " Send email success!");
}
public static void main(String[] args) {
AsyncThread asyncThread = new AsyncThread();
asyncThread.run();
}
}
當然如果每次都創(chuàng)建一個Thread線程,頻繁的創(chuàng)建、銷毀,浪費系統(tǒng)資源,我們可以采用線程池:
private ExecutorService executorService = Executors.newCachedThreadPool();
public void fun() {
executorService.submit(new Runnable() {
@Override
public void run() {
log.info("執(zhí)行業(yè)務邏輯...");
}
});
}
可以將業(yè)務邏輯封裝到Runnable或Callable中,交由線程池來執(zhí)行。
2、 Future異步
@Slf4j
public class FutureManager {
public String execute() throws Exception {
ExecutorService executor = Executors.newFixedThreadPool(1);
Future<String> future = executor.submit(new Callable<String>() {
@Override
public String call() throws Exception {
System.out.println(" --- task start --- ");
Thread.sleep(3000);
System.out.println(" --- task finish ---");
return "this is future execute final result!!!";
}
});
//這里需要返回值時會阻塞主線程
String result = future.get();
log.info("Future get result: {}", result);
return result;
}
@SneakyThrows
public static void main(String[] args) {
FutureManager manager = new FutureManager();
manager.execute();
}
}
輸出結果:
--- task start ---
--- task finish ---
Future get result: this is future execute final result!!!
(1) Future的不足之處
Future的不足之處的包括以下幾點:
- 無法被動接收異步任務的計算結果:雖然我們可以主動將異步任務提交給線程池中的線程來執(zhí)行,但是待異步任務執(zhí)行結束之后,主線程無法得到任務完成與否的通知,它需要通過get方法主動獲取任務執(zhí)行的結果。
- Future件彼此孤立:有時某一個耗時很長的異步任務執(zhí)行結束之后,你想利用它返回的結果再做進一步的運算,該運算也會是一個異步任務,兩者之間的關系需要程序開發(fā)人員手動進行綁定賦予,F(xiàn)uture并不能將其形成一個任務流(pipeline),每一個Future都是彼此之間都是孤立的,所以才有了后面的CompletableFuture,CompletableFuture就可以將多個Future串聯(lián)起來形成任務流。
- Futrue沒有很好的錯誤處理機制:截止目前,如果某個異步任務在執(zhí)行發(fā)的過程中發(fā)生了異常,調用者無法被動感知,必須通過捕獲get方法的異常才知曉異步任務執(zhí)行是否出現(xiàn)了錯誤,從而在做進一步的判斷處理。
3、CompletableFuture實現(xiàn)異步
public class CompletableFutureCompose {
/**
* thenAccept子任務和父任務公用同一個線程
*/
@SneakyThrows
public static void thenRunAsync() {
CompletableFuture<Integer> cf1 = CompletableFuture.supplyAsync(() -> {
System.out.println(Thread.currentThread() + " cf1 do something....");
return 1;
});
CompletableFuture<Void> cf2 = cf1.thenRunAsync(() -> {
System.out.println(Thread.currentThread() + " cf2 do something...");
});
//等待任務1執(zhí)行完成
System.out.println("cf1結果->" + cf1.get());
//等待任務2執(zhí)行完成
System.out.println("cf2結果->" + cf2.get());
}
public static void main(String[] args) {
thenRunAsync();
}
}
我們不需要顯式使用ExecutorService,CompletableFuture 內部使用了ForkJoinPool來處理異步任務,如果在某些業(yè)務場景我們想自定義自己的異步線程池也是可以的。
4、Spring的@Async異步
(1)自定義異步線程池
/**
* 線程池參數(shù)配置,多個線程池實現(xiàn)線程池隔離,@Async注解,默認使用系統(tǒng)自定義線程池,可在項目中設置多個線程池,在異步調用的時候,指明需要調用的線程池名稱,比如:@Async("taskName")
@EnableAsync
@Configuration
public class TaskPoolConfig {
/**
* 自定義線程池
*
**/
@Bean("taskExecutor")
public Executor taskExecutor() {
//返回可用處理器的Java虛擬機的數(shù)量 12
int i = Runtime.getRuntime().availableProcessors();
System.out.println("系統(tǒng)最大線程數(shù) :" + i);
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//核心線程池大小
executor.setCorePoolSize(16);
//最大線程數(shù)
executor.setMaxPoolSize(20);
//配置隊列容量,默認值為Integer.MAX_VALUE
executor.setQueueCapacity(99999);
//活躍時間
executor.setKeepAliveSeconds(60);
//線程名字前綴
executor.setThreadNamePrefix("asyncServiceExecutor -");
//設置此執(zhí)行程序應該在關閉時阻止的最大秒數(shù),以便在容器的其余部分繼續(xù)關閉之前等待剩余的任務完成他們的執(zhí)行
executor.setAwaitTerminationSeconds(60);
//等待所有的任務結束后再關閉線程池
executor.setWaitForTasksToCompleteOnShutdown(true);
return executor;
}
}
(2) AsyncService
public interface AsyncService {
MessageResult sendSms(String callPrefix, String mobile, String actionType, String content);
MessageResult sendEmail(String email, String subject, String content);
}
@Slf4j
@Service
public class AsyncServiceImpl implements AsyncService {
@Autowired
private IMessageHandler mesageHandler;
@Override
@Async("taskExecutor")
public MessageResult sendSms(String callPrefix, String mobile, String actionType, String content) {
try {
Thread.sleep(1000);
mesageHandler.sendSms(callPrefix, mobile, actionType, content);
} catch (Exception e) {
log.error("發(fā)送短信異常 -> ", e)
}
}
@Override
@Async("taskExecutor")
public sendEmail(String email, String subject, String content) {
try {
Thread.sleep(1000);
mesageHandler.sendsendEmail(email, subject, content);
} catch (Exception e) {
log.error("發(fā)送email異常 -> ", e)
}
}
}
在實際項目中, 使用@Async調用線程池,推薦等方式是是使用自定義線程池的模式,不推薦直接使用@Async直接實現(xiàn)異步。
5、Spring ApplicationEvent事件實現(xiàn)異步
(1)定義事件
public class AsyncSendEmailEvent extends ApplicationEvent {
/**
* 郵箱
**/
private String email;
/**
* 主題
**/
private String subject;
/**
* 內容
**/
private String content;
/**
* 接收者
**/
private String targetUserId;
}
(2)定義事件處理器
@Slf4j
@Component
public class AsyncSendEmailEventHandler implements ApplicationListener<AsyncSendEmailEvent> {
@Autowired
private IMessageHandler mesageHandler;
@Async("taskExecutor")
@Override
public void onApplicationEvent(AsyncSendEmailEvent event) {
if (event == null) {
return;
}
String email = event.getEmail();
String subject = event.getSubject();
String content = event.getContent();
String targetUserId = event.getTargetUserId();
mesageHandler.sendsendEmailSms(email, subject, content, targerUserId);
}
}
另外,可能有些時候采用ApplicationEvent實現(xiàn)異步的使用,當程序出現(xiàn)異常錯誤的時候,需要考慮補償機制,那么這時候可以結合Spring Retry重試來幫助我們避免這種異常造成數(shù)據(jù)不一致問題。
6、消息隊列
(1)回調事件消息生產者
@Slf4j
@Component
public class CallbackProducer {
@Autowired
AmqpTemplate amqpTemplate;
public void sendCallbackMessage(CallbackDTO allbackDTO, final long delayTimes) {
log.info("生產者發(fā)送消息,callbackDTO,{}", callbackDTO);
amqpTemplate.convertAndSend(CallbackQueueEnum.QUEUE_GENSEE_CALLBACK.getExchange(), CallbackQueueEnum.QUEUE_GENSEE_CALLBACK.getRoutingKey(), JsonMapper.getInstance().toJson(genseeCallbackDTO), new MessagePostProcessor() {
@Override
public Message postProcessMessage(Message message) throws AmqpException {
//給消息設置延遲毫秒值,通過給消息設置x-delay頭來設置消息從交換機發(fā)送到隊列的延遲時間
message.getMessageProperties().setHeader("x-delay", delayTimes);
message.getMessageProperties().setCorrelationId(callbackDTO.getSdkId());
return message;
}
});
}
}
(2)回調事件消息消費者
@Slf4j
@Component
@RabbitListener(queues = "message.callback", containerFactory = "rabbitListenerContainerFactory")
public class CallbackConsumer {
@Autowired
private IGlobalUserService globalUserService;
@RabbitHandler
public void handle(String json, Channel channel, @Headers Map<String, Object> map) throws Exception {
if (map.get("error") != null) {
//否認消息
channel.basicNack((Long) map.get(AmqpHeaders.DELIVERY_TAG), false, true);
return;
}
try {
CallbackDTO callbackDTO = JsonMapper.getInstance().fromJson(json, CallbackDTO.class);
//執(zhí)行業(yè)務邏輯
globalUserService.execute(callbackDTO);
//消息消息成功手動確認,對應消息確認模式acknowledge-mode: manual
channel.basicAck((Long) map.get(AmqpHeaders.DELIVERY_TAG), false);
} catch (Exception e) {
log.error("回調失敗 -> {}", e);
}
}
}
7、ThreadUtil異步工具類
@Slf4j
public class ThreadUtils {
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
ThreadUtil.execAsync(() -> {
ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();
int number = threadLocalRandom.nextInt(20) + 1;
System.out.println(number);
});
log.info("當前第:" + i + "個線程");
}
log.info("task finish!");
}
}
8、Guava異步
Guava的ListenableFuture顧名思義就是可以監(jiān)聽的Future,是對java原生Future的擴展增強。我們知道Future表示一個異步計算任務,當任務完成時可以得到計算結果。如果我們希望一旦計算完成就拿到結果展示給用戶或者做另外的計算,就必須使用另一個線程不斷的查詢計算狀態(tài)。這樣做,代碼復雜,而且效率低下。使用「Guava ListenableFuture」可以幫我們檢測Future是否完成了,不需要再通過get()方法苦苦等待異步的計算結果,如果完成就自動調用回調函數(shù),這樣可以減少并發(fā)程序的復雜度。
ListenableFuture是一個接口,它從jdk的Future接口繼承,添加了void addListener(Runnable listener, Executor executor)方法。
我們看下如何使用ListenableFuture。首先需要定義ListenableFuture的實例:
ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
final ListenableFuture<Integer> listenableFuture = executorService.submit(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
log.info("callable execute...")
TimeUnit.SECONDS.sleep(1);
return 1;
}
});
首先通過MoreExecutors類的靜態(tài)方法listeningDecorator方法初始化一個ListeningExecutorService的方法,然后使用此實例的submit方法即可初始化ListenableFuture對象。
ListenableFuture要做的工作,在Callable接口的實現(xiàn)類中定義,這里只是休眠了1秒鐘然后返回一個數(shù)字1,有了ListenableFuture實例,可以執(zhí)行此Future并執(zhí)行Future完成之后的回調函數(shù)。
Futures.addCallback(listenableFuture, new FutureCallback<Integer>() {
@Override
public void onSuccess(Integer result) {
//成功執(zhí)行...
System.out.println("Get listenable future's result with callback " + result);
}
@Override
public void onFailure(Throwable t) {
//異常情況處理...
t.printStackTrace();
}
});
那么,以上就是本期介紹的分布式服務下實現(xiàn)異步的八種方式了,供您參考。