偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

SpringCloud Hystrix高并發(fā)下實(shí)現(xiàn)請(qǐng)求合并

開發(fā) 架構(gòu)
Spring Cloud通過(guò)Hystrix實(shí)現(xiàn)請(qǐng)求合并,減輕高并發(fā)時(shí)的請(qǐng)求線程消耗、降低請(qǐng)求響應(yīng)時(shí)間的效果。今天就來(lái)聊一聊Hystrix請(qǐng)求合并的實(shí)現(xiàn)方式。

前言

在高并發(fā)的場(chǎng)景下,前端會(huì)有大量的訪問(wèn)請(qǐng)求。如果一個(gè)請(qǐng)求就需要打開一個(gè)數(shù)據(jù)庫(kù)連接,操作完數(shù)據(jù)庫(kù)后再進(jìn)行關(guān)閉,無(wú)形中對(duì)數(shù)據(jù)造成很大的開銷。請(qǐng)求合并是將多個(gè)單個(gè)請(qǐng)求合并成一個(gè)請(qǐng)求,去調(diào)用服務(wù)提供者提供的服務(wù)接口,再遍歷合并的結(jié)果為每個(gè)合并前的單個(gè)請(qǐng)求設(shè)置返回結(jié)果。Spring Cloud通過(guò)Hystrix實(shí)現(xiàn)請(qǐng)求合并,減輕高并發(fā)時(shí)的請(qǐng)求線程消耗、降低請(qǐng)求響應(yīng)時(shí)間的效果。今天就來(lái)聊一聊Hystrix請(qǐng)求合并的實(shí)現(xiàn)方式。

實(shí)現(xiàn)方式

由于是高并發(fā)場(chǎng)景,因此準(zhǔn)備了SpringCloud微服務(wù)框架。準(zhǔn)備了注冊(cè)中心、網(wǎng)關(guān)、服務(wù)提供者、服務(wù)消費(fèi)者等組件。

導(dǎo)入依賴

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

啟動(dòng)類上增加注解

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

實(shí)現(xiàn)請(qǐng)求合并,Service中代碼如下:

//請(qǐng)求合并的方法  合并5s內(nèi)的請(qǐng)求
@HystrixCollapser(batchMethod = "mergeGet", scope = com.netflix.hystrix.HystrixCollapser.Scope.GLOBAL, collapserProperties = {@HystrixProperty(name = "timerDelayInMilliseconds", value = "5000")})
public Future<Item> get(String id) {
log.info("======執(zhí)行了get方法========" + id);
return null;
}

//合并請(qǐng)求之后調(diào)用的方法
@HystrixCommand
public List<Item> mergeGet(List<String> ids) {
log.info("===合并開始===");
List<Item> items = ids.stream().map(
x -> {
Item item = new Item();
item.setId(Integer.valueOf(x));
item.setName("商品 :" + x);
return item;
}

).collect(Collectors.toList());
log.info("===合并結(jié)束,合并 {} 條 請(qǐng)求====", ids.size());
return items;
}

?說(shuō)明:調(diào)用get方法,如果5s內(nèi)get有多次調(diào)用,則合并后mergeGet方法。

controller調(diào)用代碼如下:

@RequestMapping(value = "/find/{id}")
public Item find(@PathVariable String id) throws InterruptedException, ExecutionException {
HystrixRequestContext context = HystrixRequestContext.initializeContext();
Future<Item> items = itemService.get(id);
System.out.println(items.get());
context.close();
return items.get();
}

執(zhí)行

執(zhí)行127.0.0.1:8080/find/11,同時(shí)執(zhí)行127.0.0.1:8080/find/22,保證兩個(gè)請(qǐng)求在5s內(nèi)發(fā)出。

返回結(jié)果

說(shuō)明:

  • scope = com.netflix.hystrix.HystrixCollapser.Scope.GLOBAL:將所有線程中多次服務(wù)調(diào)用進(jìn)行合并
  • scope = com.netflix.hystrix.HystrixCollapser.Scope.REQUEST:對(duì)一次請(qǐng)求的多次服務(wù)調(diào)用進(jìn)行合并
責(zé)任編輯:姜華 來(lái)源: 今日頭條
相關(guān)推薦

2017-12-01 08:54:18

SpringCloudHystrix

2014-08-08 13:30:44

Nginx

2013-01-30 10:12:24

NginxNginx優(yōu)化高并發(fā)

2022-06-12 06:45:26

高并發(fā)防重

2025-02-20 00:01:00

2022-12-08 08:27:18

HystrixQPS數(shù)據(jù)

2019-10-30 16:54:08

golangredis數(shù)據(jù)庫(kù)

2024-10-31 09:04:20

Spring高并發(fā)

2025-05-27 03:33:00

Spring高并發(fā)接口

2020-07-15 08:14:12

高并發(fā)

2017-11-27 08:50:29

架構(gòu)數(shù)據(jù)存儲(chǔ)

2020-09-23 22:36:27

分布式架構(gòu)系統(tǒng)

2025-02-26 08:20:18

2021-10-28 09:36:12

高并發(fā)數(shù)據(jù)實(shí)踐

2019-11-08 08:40:29

Java高并發(fā)流量

2025-03-21 06:20:00

連接池系統(tǒng)數(shù)據(jù)庫(kù)

2024-11-26 07:29:57

高并發(fā)線程安全

2021-03-28 09:45:05

冪等性接口數(shù)據(jù)

2021-07-01 06:58:12

高并發(fā)訂單號(hào)SCM

2023-08-25 08:06:20

CPUMySQL線程
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)