成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

性能優化之Hystrix請求合并&自實現簡化版本

開發 前端
在業務開發過程中,存在這樣的場景:程序接收到數據后,調用其他接口再將數據轉發出去;如果接收一條轉發一條,效率是比較低的,所以一個思路是先將數據緩存起來,緩存到一定數量后一次性轉發出去。

背景介紹

在業務開發過程中,存在這樣的場景:程序接收到數據后,調用其他接口再將數據轉發出去;如果接收一條轉發一條,效率是比較低的,所以一個思路是先將數據緩存起來,緩存到一定數量后一次性轉發出去。

有優點就有缺點,需要根據業務場景進行考量:

  • 在QPS較小的情況下,達到閾值的等待時間較長,造成數據延遲較大
  • 在應用發布的時候,緩存的數據存在丟失的可能性
  • 在應用非正常down掉的情況下,緩存的數據存在丟失的可能性

下面內容是對Hystrix請求合并及根據Hystrix請求合并原理自定義實現的簡化版本。

Hystrix請求合并

什么是請求合并

Without Collapsing

without collapsing

With Collapsing

with collapsing

請求合并設計思路

design

Hystrix使用示例

示例采用Spring-Boot編寫,下面代碼拷貝到工程中可以直接運行。

添加依賴

下面是spring與hystrix集成的依賴pom。

pom

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>

啟動類

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;

@EnableHystrix
@SpringBootApplication
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.class, args);
}
}

使用示例

HystrixController

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@RestController
public class HystrixController {
@Resource
private HystrixService hystrixService;

@RequestMapping("/byid")
public Long byId(Long id) throws InterruptedException, ExecutionException {
Future<Long> future = hystrixService.byId(id);
return future.get();
}
}

HystrixService

import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.Future;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;

@Service
public class HystrixService {
@HystrixCollapser(batchMethod="byIds",scope= com.netflix.hystrix.HystrixCollapser.Scope.GLOBAL,
collapserProperties={
@HystrixProperty(name="maxRequestsInBatch",value="10"),
@HystrixProperty(name="timerDelayInMilliseconds",value="1000")
})
public Future<Long> byId(Long id){
return null;
}

@HystrixCommand
public List<Long> byIds(List<Long> ids){
System.out.println(ids);
return ids;
}
}

測試類

發送請求進行驗證。

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class HystrixTest {

public static void main(String[] args) throws Exception{
CloseableHttpClient httpClient = HttpClients.custom().setMaxConnPerRoute(100).build();
String url = "http://localhost:8086/byid?id=";
ExecutorService executorService = Executors.newFixedThreadPool(20);

int requestCount = 20;
for(int i = 0;i < requestCount;i++){
final int id = i;
executorService.execute(new Runnable() {
@Override
public void run(){
try{
HttpGet httpGet = new HttpGet(url+ id);
HttpResponse response = httpClient.execute(httpGet);
System.out.println(response);
}catch (Exception e){
e.printStackTrace();
}
}
});
}

Thread.sleep(1000*10);
executorService.shutdown();
httpClient.close();
}
}

簡化版本實現

由于Hystrix已不再維護,同時考慮到Hystrix使用RxJava的學習門檻,根據HystrixCollapser設計思路及常見業務功能需求實現了一個簡化版本。

實現

RequestCollapserFactory

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class RequestCollapserFactory {
private static RequestCollapserFactory factory;
private static final int MAXBATCHSIZE = 20;
private static final long DELAY = 1000L;
private static final long PERIOD = 500L;
private ConcurrentHashMap<String,RequestCollapser> collapsers;
private ScheduledExecutorService executor;

private RequestCollapserFactory(){
collapsers = new ConcurrentHashMap<>();
ThreadFactory threadFactory = new ThreadFactory() {
final AtomicInteger counter = new AtomicInteger();
@Override
public Thread newThread(Runnable r){
Thread thread = new Thread(r, "RequestCollapserTimer-" + counter.incrementAndGet());
thread.setDaemon(true);
return thread;
}
};
executor = new ScheduledThreadPoolExecutor(Runtime.getRuntime().availableProcessors(), threadFactory);
}

public static RequestCollapserFactory getInstance(){
if(factory != null){
return factory;
}
synchronized (RequestCollapserFactory.class){
if(factory != null){
return factory;
}
factory = new RequestCollapserFactory();
}
return factory;
}

public RequestCollapser getRequestCollapser(String key,RequestBatch requestBatch){
return getRequestCollapser(key,requestBatch,MAXBATCHSIZE,DELAY,PERIOD);
}
public RequestCollapser getRequestCollapser(String key,RequestBatch requestBatch,int maxBatchSize){
return getRequestCollapser(key,requestBatch,maxBatchSize,DELAY,PERIOD);
}
public RequestCollapser getRequestCollapser(String key,RequestBatch requestBatch,int maxBatchSize,long delay, long period){
RequestCollapser collapser = collapsers.get(key);
if(collapser != null){
return collapser;
}

synchronized (collapsers){
collapser = collapsers.get(key);
if(collapser != null){
return collapser;
}
collapser = new RequestCollapser(requestBatch,maxBatchSize,delay,period,executor);
collapsers.put(key,collapser);
return collapser;
}
}
}

RequestCollapser

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

public class RequestCollapser {
private int maxBatchSize;
private long delay;
private long period;
private LinkedBlockingQueue<Object> queue = new LinkedBlockingQueue();
private ScheduledExecutorService executor;
private RequestBatch requestBatch;
private AtomicBoolean timerscheduled = new AtomicBoolean();
protected RequestCollapser(RequestBatch requestBatch,int maxBatchSize,long delay,long period,ScheduledExecutorService executor){
if(requestBatch == null){
throw new IllegalArgumentException("requestBatch can not be null");
}
this.maxBatchSize = maxBatchSize;
this.delay = delay;
this.period = period;
this.executor = executor;
this.requestBatch = requestBatch;
}

public List<Object> submitRequest(Object obj,boolean ifFullThenBatchExecute){
if(timerscheduled.compareAndSet(false,true)){
this.startSchedule();
}
List<Object> objectList = null;
synchronized (queue){
if(obj instanceof Collection){
queue.addAll((Collection)obj);
}else {
queue.offer(obj);
}

if(queue.size() >= this.maxBatchSize){
objectList = new LinkedList<>();
queue.drainTo(objectList);
}
}

if(!ifFullThenBatchExecute){
return objectList;
}

this.doBatch(objectList);
return null;
}

private boolean doBatch(List<Object> objectList){
if(objectList == null){
return true;
}
try{
return requestBatch.batch(objectList);
}catch (Throwable t){
t.printStackTrace();
}
return false;
}

private void startSchedule(){
Runnable r = new Runnable() {
@Override
public void run(){
List<Object> objectList = null;
synchronized (queue){
if(queue.size() > 0) {
objectList = new LinkedList<>();
queue.drainTo(objectList);
}
}
doBatch(objectList);
}
};

this.executor.scheduleAtFixedRate(r,this.delay,this.period, TimeUnit.MILLISECONDS);
}
}

RequestBatch

import java.util.List;

public interface RequestBatch {
boolean batch(List<Object> objectList);
}

驗證測試

RequestCollapserTest

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class RequestCollapserTest {
private static AtomicInteger counter = new AtomicInteger();
private static long delay = 1;
private static long period = 1;
private static int maxBatchSize = 20;
private static int requestCount = 50000;
private static RequestCollapserFactory factory = RequestCollapserFactory.getInstance();
private static RequestBatch requestBatch = new RequestBatch() {
@Override
public boolean batch(List<Object> objectList){
int size = objectList.size();
counter.addAndGet(size);
System.out.println(counter + ":::::" + size + ":::::" + objectList);
return true;
}
};

public static void main(String[] args) throws Exception{
//sync();
async();
}

public static void async() throws Exception{
ExecutorService executorService = Executors.newFixedThreadPool(20);
CountDownLatch countDownLatch = new CountDownLatch(requestCount);
for(int i = 0;i < requestCount;i++){
final int id = i;
executorService.execute(new Runnable() {
@Override
public void run(){
try{
RequestCollapser requestCollapser =
factory.getRequestCollapser("1",requestBatch,maxBatchSize,delay,period);
requestCollapser.submitRequest(id,true);
}catch (Exception e){
e.printStackTrace();
}
countDownLatch.countDown();
}
});
}

executorService.shutdown();
countDownLatch.await();
Thread.sleep(1000);
System.out.println(counter.get());
}

public static void sync() throws Exception{
for(int i = 0;i < requestCount;i++){
final int id = i;
RequestCollapser requestCollapser =
factory.getRequestCollapser("1",requestBatch,maxBatchSize,delay,period);
requestCollapser.submitRequest(id,true);
}
Thread.sleep(1000);
System.out.println(counter.get());
}
}


責任編輯:武曉燕 來源: 今日頭條
相關推薦

2023-02-03 15:16:42

SpringHystrix

2017-12-01 08:54:18

SpringCloudHystrix

2023-11-28 12:49:01

AI訓練

2017-04-03 21:52:30

隔離線程池分布式

2010-05-17 15:50:06

2021-07-29 14:20:34

網絡優化移動互聯網數據存儲

2022-02-16 14:10:51

服務器性能優化Linux

2021-11-29 11:13:45

服務器網絡性能

2011-02-13 09:37:55

ASP.NET

2009-12-17 15:59:44

VS2010簡化版

2009-06-30 11:23:02

性能優化

2018-01-09 16:56:32

數據庫OracleSQL優化

2019-12-13 10:25:08

Android性能優化啟動優化

2009-06-01 09:04:15

Windows 7微軟操作系統

2015-03-16 14:09:33

GoogleUbuntuDocker

2025-01-20 09:09:59

2013-02-20 14:32:37

Android開發性能

2011-07-11 15:26:49

性能優化算法

2023-07-19 12:24:48

C++constexpr?語句

2010-08-04 13:30:07

Visual Stud
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 中文天堂在线一区 | 久久久久久国产精品免费免费男同 | 日韩久久精品 | 免费视频久久久久 | 亚洲aⅴ一区二区 | 亚洲精品在线免费播放 | 亚洲欧洲在线视频 | 操久久 | 日韩国产一区二区三区 | 国产男女视频网站 | 少妇一级淫片免费播放 | 婷婷国产一区二区三区 | 日本不卡一区 | 亚洲精品日韩综合观看成人91 | 日韩和的一区二区 | 天堂一区二区三区 | 日本不卡一区 | www.天天干.com | 人人干人人看 | 久久久免费电影 | 久久国产精品视频 | 亚洲一区精品在线 | 2022国产精品 | 粉嫩一区二区三区国产精品 | 色偷偷888欧美精品久久久 | 在线国产中文字幕 | 亚洲一区二区三区免费在线 | 中文字幕在线观看成人 | 日本一区二区高清不卡 | 色久影院| 精品乱码一区二区三四区视频 | 欧美一级片在线 | 精品一区二区久久久久久久网站 | 久久视频一区 | 一区二区三区中文字幕 | 国产一级片91 | 国产伦精品一区二区三区精品视频 | 精品视频一区在线 | 国产成人高清成人av片在线看 | 一级片免费视频 | 国产东北一级毛片 |