欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于springcloud異步線程池、高并發(fā)請(qǐng)求feign的解決方案

 更新時(shí)間:2021年02月25日 10:38:55   作者:Scenoob  
這篇文章主要介紹了基于springcloud異步線程池、高并發(fā)請(qǐng)求feign的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

ScenTaskTestApplication.java

package com.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author scen
* @version 2018年9月27日 上午11:51:04
*/
@EnableFeignClients
@SpringBootApplication
public class ScenTaskTestApplication {
 public static void main(String[] args) {
  SpringApplication.run(ScenTaskTestApplication.class, args);
 }
}

application.properties

spring.application.name=scen-task-test
server.port=9009
feign.hystrix.enabled=true
#熔斷器失敗的個(gè)數(shù)==進(jìn)入熔斷器的請(qǐng)求達(dá)到1000時(shí)服務(wù)降級(jí)(之后的請(qǐng)求直接進(jìn)入熔斷器)
hystrix.command.default.circuitBreaker.requestVolumeThreshold=1000
#回退最大線程數(shù)
hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests=50
#核心線程池?cái)?shù)量
hystrix.threadpool.default.coreSize=130
#請(qǐng)求處理的超時(shí)時(shí)間
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=100000
ribbon.ReadTimeout=120000
#請(qǐng)求連接的超時(shí)時(shí)間
ribbon.ConnectTimeout=130000
eureka.instance.instance-id=${spring.application.name}:${spring.application.instance_id:${server.port}}
eureka.instance.preferIpAddress=true
eureka.client.service-url.defaultZone=http://127.0.0.1:9000/eureka
logging.level.com.test.user.service=debug
logging.level.org.springframework.boot=debug
logging.level.custom=info

AsyncConfig.java

package com.test;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
/**
 * springboot異步線程池配置
 * @author Scen
 * @date 2018/11/7 18:28
 */
@Configuration
@EnableAsync
public class AsyncConfig implements AsyncConfigurer {
 
 
 @Override
 public Executor getAsyncExecutor() {
  //定義線程池
  ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
  //核心線程數(shù)
  taskExecutor.setCorePoolSize(20);
  //線程池最大線程數(shù)
  taskExecutor.setMaxPoolSize(100);
  //線程隊(duì)列最大線程數(shù)
  taskExecutor.setQueueCapacity(10);
  //初始化
  taskExecutor.initialize();
  return taskExecutor;
 }
}

DoTaskClass.java

package com.test;
import com.test.pojo.User;
import com.test.pojo.UserEducation;
import com.test.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.util.List;
/**
 * 任務(wù)類 定義異步工作任務(wù)
 * @author Scen
 * @date 2018/11/7 18:40
 */
@Component
public class DoTaskClass { 
 /**
  * 一個(gè)feign的客戶端
  */
 private final UserService userService;
 
 @Autowired
 public DoTaskClass(UserService userService) {
  this.userService = userService;
 }
 
 /**
  * 核心任務(wù)
  *
  * @param uid
  */
 @Async
 public void dotask(String uid) {
  /**
   * 模擬復(fù)雜工作業(yè)務(wù)(109個(gè)線程同時(shí)通過(guò)feign請(qǐng)求微服務(wù)提供者)
   */
  {
   List<UserEducation> userEducationByUid = userService.findUserEducationByUid(uid);
   List<String> blackList = userService.getBlackList();
   String userSkilled = userService.getUserSkilled(uid);
   String userFollow = userService.getUserFollow(uid);
   User userById = userService.getUserById(uid);
   List<String> followList = userService.getFollowList(uid);
   int userActivityScore = userService.getUserActivityScore(uid);
  }
//  打印線程名稱分辨是否為多線程操作
  System.out.println(Thread.currentThread().getName() + "===任務(wù)" + uid + "執(zhí)行完成===");
 }
}

TestController.java

package com.test;
import com.test.pojo.User;
import com.test.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
 * 測(cè)試案例
 * @author Scen
 * @date 2018/11/7 18:10
 */
@RestController
public class TestController {
 
 /**
  * 此處僅用此feign客戶端請(qǐng)求微服務(wù)獲取核心工作所需參數(shù)
  */
 private final UserService userService;
 
 /**
  * 核心工作異步算法
  */
 private final DoTaskClass doTaskClass;
 
 @Autowired
 public TestController(DoTaskClass doTaskClass, UserService userService) {
  this.doTaskClass = doTaskClass;
  this.userService = userService;
 } 
 
 /**
  * 手動(dòng)觸發(fā)工作
  * @throws InterruptedException
  */
 @RequestMapping("/test")
 public void task() throws InterruptedException {
  /*
   取到1000個(gè)要執(zhí)行任務(wù)的必備參數(shù)
   */
  List<User> userList = userService.findAllLite(1, 1000);
  for (int i = 0; i < userList.size(); i++) {
   try {
//    異步線程開(kāi)始工作
    doTaskClass.dotask(userList.get(i).getId());
   } catch (Exception e) {
    /*
     若并發(fā)線程數(shù)達(dá)到MaxPoolSize+QueueCapacity=110(參考AsyncConfig配置)會(huì)進(jìn)入catch代碼塊
     i--休眠3秒后重試(嘗試進(jìn)入線程隊(duì)列:當(dāng)且僅當(dāng)109個(gè)線程有一個(gè)或多個(gè)線程完成異步任務(wù)時(shí)重試成功)
     */
    i--;
    Thread.sleep(3000*3);
   }
   System.out.println(i);
  }
 } 
}

相關(guān)線程池、超時(shí)時(shí)間等數(shù)量和大小按實(shí)際業(yè)務(wù)配置

補(bǔ)充:SpringCloud關(guān)于@FeignClient和Hystrix集成對(duì)http線程池監(jiān)控問(wèn)題

@FeignClient可以作為Http代理訪問(wèn)其他微服務(wù)節(jié)點(diǎn),可以用apache的httpclient替換@FeignClient原生的URLConnection請(qǐng)求方式,以達(dá)到讓http請(qǐng)求走Http線程池的目的。

而@FeignClient和hystrix集成之后,在hystrix dashboard上可以監(jiān)控到 @FeignClient 中接口調(diào)用情況和 @FeignClient 中httpclient中線程池使用狀況。

下面是demo的示例:

1、@FeignClient的接口代碼如下:

@FeignClient(value="service-A", fallback=ServiceClientHystrix.class)
public interface ServiceClient { 
 @RequestMapping(method = RequestMethod.GET, value = "/add/{id}")
 String add(@PathVariable("id") Integer id);
}

2、ServiceClientHystrix.java

@Component
public class ServiceClientHystrix implements ServiceClient{
 @Override
 public String add(Integer id) {
  return "add value from ServiceClientHystrix";
 }
}

3、關(guān)于@FeignClient和hystrix

集成后,Http線程池配置如下:

hystrix.threadpool.服務(wù)實(shí)例ID.參數(shù)

例如設(shè)置httpclient的線程池最大線程數(shù)量

hystrix.threadpool.service-A.coreSize=20//默認(rèn)是hystrix.threadpool.default.coreSize = 10
hystrix.threadpool.service-A.maximumSize=20//默認(rèn)是hystrix.threadpool.default.maximumSize = 10

啟動(dòng)服務(wù)后用測(cè)試用例連續(xù)調(diào)用接口測(cè)試,用hystrix dashboard

監(jiān)控得到下圖監(jiān)控效果:

去掉hystrix.threadpool.服務(wù)實(shí)例ID.參數(shù)配置后,再次用測(cè)試用例調(diào)用接口得到監(jiān)控如下圖:

PoolSize的大小取決于hystrix.threadpool.服務(wù)實(shí)例ID.coreSize大小設(shè)置

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

  • java對(duì)接Modbus協(xié)議代碼示例

    java對(duì)接Modbus協(xié)議代碼示例

    Modbus是一種串行通信協(xié)議,Modbus已經(jīng)成為工業(yè)領(lǐng)域通信協(xié)議的業(yè)界標(biāo)準(zhǔn),并且現(xiàn)在是工業(yè)電子設(shè)備之間常用的連接方式,這篇文章主要給大家介紹了關(guān)于java對(duì)接Modbus協(xié)議的相關(guān)資料,需要的朋友可以參考下
    2024-03-03
  • Java+Redis撤銷重做功能實(shí)現(xiàn)

    Java+Redis撤銷重做功能實(shí)現(xiàn)

    這篇文章主要介紹了Java+Redis實(shí)現(xiàn)撤銷重做功能,需要考慮撤銷的最大步數(shù),撤銷之后穿插著其他操作則不能再重做,所以引入分布式鎖Redisson進(jìn)行加鎖處理,防止對(duì)圖表的操作有并發(fā)請(qǐng)求導(dǎo)致處理撤銷邏輯混亂,感興趣的朋友跟隨小編一起看看吧
    2023-05-05
  • Java通過(guò)XPath獲取XML文件中符合特定條件的節(jié)點(diǎn)

    Java通過(guò)XPath獲取XML文件中符合特定條件的節(jié)點(diǎn)

    今天小編就為大家分享一篇關(guān)于Java通過(guò)XPath獲取XML文件中符合特定條件的節(jié)點(diǎn),小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • java 使用idea將工程打成jar并創(chuàng)建成exe文件類型執(zhí)行的方法詳解

    java 使用idea將工程打成jar并創(chuàng)建成exe文件類型執(zhí)行的方法詳解

    這篇文章主要介紹了java 使用idea將工程打成jar并創(chuàng)建成exe文件類型執(zhí)行,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2020-09-09
  • Java實(shí)現(xiàn)簡(jiǎn)易的分詞器功能

    Java實(shí)現(xiàn)簡(jiǎn)易的分詞器功能

    搜索功能是具備數(shù)據(jù)庫(kù)功能的系統(tǒng)的一大重要特性和功能,生活中常見(jiàn)的搜索功能基本上都具備了分詞搜索功能.然而ES功能固然強(qiáng)大,但對(duì)于學(xué)生或小項(xiàng)目而言整合起來(lái)太費(fèi)人力物力,若是寫個(gè)分詞器就會(huì)使項(xiàng)目錦上添花,使其不僅僅是只能單關(guān)鍵詞搜索的系統(tǒng),需要的朋友可以參考下
    2021-06-06
  • Springboot異常錯(cuò)誤處理解決方案詳解

    Springboot異常錯(cuò)誤處理解決方案詳解

    這篇文章主要介紹了Springboot異常錯(cuò)誤處理解決方案詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • spring框架下websocket的搭建

    spring框架下websocket的搭建

    本篇文章主要介紹了spring框架下websocket的搭建,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。
    2017-03-03
  • Java實(shí)現(xiàn)圖片上傳至FastDFS入門教程

    Java實(shí)現(xiàn)圖片上傳至FastDFS入門教程

    這篇文章主要介紹了Java實(shí)現(xiàn)圖片上傳至FastDFS入門教程,通過(guò)前端ajax提交圖片到后端,java處理服務(wù)器文件上傳至FastDFS文件服務(wù)器系統(tǒng),以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • Java SpringMVC的@RequestMapping注解使用及說(shuō)明

    Java SpringMVC的@RequestMapping注解使用及說(shuō)明

    這篇文章主要介紹了Java SpringMVC的@RequestMapping注解使用及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Java Kafka分區(qū)發(fā)送及消費(fèi)實(shí)戰(zhàn)

    Java Kafka分區(qū)發(fā)送及消費(fèi)實(shí)戰(zhàn)

    本文主要介紹了Kafka分區(qū)發(fā)送及消費(fèi)實(shí)戰(zhàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07

最新評(píng)論