Springcloud非同期スレッドプール、高同時要求feignソリューション
25690 ワード
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
# == 1000 ( )
hystrix.command.default.circuitBreaker.requestVolumeThreshold=1000
#
hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests=50
#
hystrix.threadpool.default.coreSize=130
#
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=100000
ribbon.ReadTimeout=120000
#
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();
//
taskExecutor.setCorePoolSize(20);
//
taskExecutor.setMaxPoolSize(100);
//
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;
/**
*
* @author Scen
* @date 2018/11/7 18:40
*/
@Component
public class DoTaskClass {
/**
* feign
*/
private final UserService userService;
@Autowired
public DoTaskClass(UserService userService) {
this.userService = userService;
}
/**
*
*
* @param uid
*/
@Async
public void dotask(String uid) {
/**
* (109 feign )
*/
{
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() + "=== " + uid + " ===");
}
}
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;
/**
*
* @author Scen
* @date 2018/11/7 18:10
*/
@RestController
public class TestController {
/**
* feign
*/
private final UserService userService;
/**
*
*/
private final DoTaskClass doTaskClass;
@Autowired
public TestController(DoTaskClass doTaskClass, UserService userService) {
this.doTaskClass = doTaskClass;
this.userService = userService;
}
/**
*
* @throws InterruptedException
*/
@RequestMapping("/test")
public void task() throws InterruptedException {
/*
1000
*/
List<User> userList = userService.findAllLite(1, 1000);
for (int i = 0; i < userList.size(); i++) {
try {
//
doTaskClass.dotask(userList.get(i).getId());
} catch (Exception e) {
/*
MaxPoolSize+QueueCapacity=110( AsyncConfig ) catch
i-- 3 ( : 109 )
*/
i--;
Thread.sleep(3000*3);
}
System.out.println(i);
}
}
}
関連スレッド・プール、タイムアウト時間などの数とサイズは、実際のビジネス構成によって異なります.