concurrent-aggregator Spring 无关联并发聚合器 项目简介
背景与目的在开发后台接口时, 为了开发效率, 我们往往习惯编写串行执行的代码, 去调用不同的接口,即使这些接口之间并无依赖, 这使得最后开发的接口性能低下, 且数据不方便复用此框架目的旨在保持开发效率的同时, 很方便地支持并发和数据复用原理Spring + CountDownLatch + Future + 反射 + 动态代理 通过启动类的注解去加载需要代理的接口, 用 Factorybean 将代理的bean注册到spring 容器 代理接口使用注解配置使用 spring 的 bean 来完成操作 线程池执行具体的操作,CountDownLatch做同步 目前自动识别代理方法的类型和参数比较弱鸡,后续需要继续完善 工程需要自己下载下来 install注解介绍@EnableConcurrentAggregate 定义数据提供者 @ConcurrentAggregate 动态代理的接口上需要添加注解 @AggregateProviders 并行处理的提供者注解 @AggregateProvider 定单个并行处理的方法的注解 @AggregateConsumer 最后聚合的方注解 @AggregateParameterName 参数名使用方法pom.xml<dependency>
<groupId>com.wolfking</groupId>
<artifactId>concurrent-aggregator</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>启动类添加注解 @EnableConcurrentAggregate("com.xxx.service.xxx.aggregator")示例开发h5首页接口,需要从七八个地方组织数据,并且数据没有关联,7个接口串联调用需要3秒IndexService.java
IndexService.java
1. IndexVo assemblyExperience(IndexVo indexVo, Integer tenantId)
2. IndexVo assemblyTheme(IndexVo indexVo, Integer tenantId)
3. IndexVo assemblyFounder(IndexVo indexVo, Integer tenantId, Integer customerId)
4. IndexVo assemblyRaffleActivity(IndexVo indexVo, Integer tenantId)
5. IndexVo assemblyAgreeActivity(IndexVo indexVo, Integer tenantId, Integer customerId, Integer userId)
6. IndexVo assemblyBargin(IndexVo indexVo, Integer tenantId, Integer customerId)
7. IndexVo assemblyGroup(IndexVo indexVo, Integer tenantId, Integer customerId)
8. IndexVo assemblyGroup(IndexVo indexVo, Integer tenantId, Integer customerId)
9. IndexVo assemblyReduction(IndexVo indexVo, Integer tenantId, Integer customerId)
10.void summary() ---汇聚接口定义并发代理的接口最后汇聚AggregateConsumer输入参数是AggregateProvider每个的返回值,discardResult除外,汇聚的可以没有@ConcurrentAggregate
public interface IndexAggregatorProxy {
@AggregateProviders(ignoreException = true, timeout = 5000, value = {
@AggregateProvider(beanClass = IndexService.class, method = "assemblyExperience",
parameters = {@AggregateParameter("index"), @AggregateParameter("tenantId")}),
@AggregateProvider(beanClass = IndexService.class, method = "assemblyTheme",
parameters = {@AggregateParameter("index"), @AggregateParameter("tenantId")}),
@AggregateProvider(beanClass = IndexService.class, method = "assemblyFounder",
parameters = {@AggregateParameter("index"), @AggregateParameter("tenantId"), @AggregateParameter("customerId")}),
@AggregateProvider(beanClass = IndexService.class, method = "assemblyRaffleActivity",
parameters = {@AggregateParameter("index"), @AggregateParameter("tenantId")}),
@AggregateProvider(beanClass = IndexService.class, method = "assemblyAgreeActivity",
parameters = {@AggregateParameter("index"), @AggregateParameter("tenantId"), @AggregateParameter("customerId"), @AggregateParameter("userId")}),
@AggregateProvider(beanClass = IndexService.class, method = "assemblyBargin",
parameters = {@AggregateParameter("index"), @AggregateParameter("tenantId"), @AggregateParameter("customerId")}),
@AggregateProvider(beanClass = IndexService.class, method = "assemblyGroup",
parameters = {@AggregateParameter("index"), @AggregateParameter("tenantId"), @AggregateParameter("customerId")}),
@AggregateProvider(beanClass = IndexService.class, method = "assemblyInviteFriend",
parameters = {@AggregateParameter("index"), @AggregateParameter("tenantId")}),
@AggregateProvider(beanClass = IndexService.class, method = "assemblyReduction",
parameters = {@AggregateParameter("index"), @AggregateParameter("tenantId"), @AggregateParameter("customerId")})})
@AggregateConsumer(beanClass = IndexService.class,method = "summary")
void index(@AggregateParameterName("index") IndexVo indexVo,
@AggregateParameterName("tenantId") Integer tenantId,
@AggregateParameterName("customerId") Integer customerId,
@AggregateParameterName("userId") Integer userId);
}在spring的service中使用并发代理的接口,直接注入就行 @Autowired
@SuppressWarnings("all")
private IndexAggregatorProxy indexAggregatorProxy;
<groupId>com.wolfking</groupId>
<artifactId>concurrent-aggregator</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>启动类添加注解 @EnableConcurrentAggregate("com.xxx.service.xxx.aggregator")示例开发h5首页接口,需要从七八个地方组织数据,并且数据没有关联,7个接口串联调用需要3秒IndexService.java
IndexService.java
1. IndexVo assemblyExperience(IndexVo indexVo, Integer tenantId)
2. IndexVo assemblyTheme(IndexVo indexVo, Integer tenantId)
3. IndexVo assemblyFounder(IndexVo indexVo, Integer tenantId, Integer customerId)
4. IndexVo assemblyRaffleActivity(IndexVo indexVo, Integer tenantId)
5. IndexVo assemblyAgreeActivity(IndexVo indexVo, Integer tenantId, Integer customerId, Integer userId)
6. IndexVo assemblyBargin(IndexVo indexVo, Integer tenantId, Integer customerId)
7. IndexVo assemblyGroup(IndexVo indexVo, Integer tenantId, Integer customerId)
8. IndexVo assemblyGroup(IndexVo indexVo, Integer tenantId, Integer customerId)
9. IndexVo assemblyReduction(IndexVo indexVo, Integer tenantId, Integer customerId)
10.void summary() ---汇聚接口定义并发代理的接口最后汇聚AggregateConsumer输入参数是AggregateProvider每个的返回值,discardResult除外,汇聚的可以没有@ConcurrentAggregate
public interface IndexAggregatorProxy {
@AggregateProviders(ignoreException = true, timeout = 5000, value = {
@AggregateProvider(beanClass = IndexService.class, method = "assemblyExperience",
parameters = {@AggregateParameter("index"), @AggregateParameter("tenantId")}),
@AggregateProvider(beanClass = IndexService.class, method = "assemblyTheme",
parameters = {@AggregateParameter("index"), @AggregateParameter("tenantId")}),
@AggregateProvider(beanClass = IndexService.class, method = "assemblyFounder",
parameters = {@AggregateParameter("index"), @AggregateParameter("tenantId"), @AggregateParameter("customerId")}),
@AggregateProvider(beanClass = IndexService.class, method = "assemblyRaffleActivity",
parameters = {@AggregateParameter("index"), @AggregateParameter("tenantId")}),
@AggregateProvider(beanClass = IndexService.class, method = "assemblyAgreeActivity",
parameters = {@AggregateParameter("index"), @AggregateParameter("tenantId"), @AggregateParameter("customerId"), @AggregateParameter("userId")}),
@AggregateProvider(beanClass = IndexService.class, method = "assemblyBargin",
parameters = {@AggregateParameter("index"), @AggregateParameter("tenantId"), @AggregateParameter("customerId")}),
@AggregateProvider(beanClass = IndexService.class, method = "assemblyGroup",
parameters = {@AggregateParameter("index"), @AggregateParameter("tenantId"), @AggregateParameter("customerId")}),
@AggregateProvider(beanClass = IndexService.class, method = "assemblyInviteFriend",
parameters = {@AggregateParameter("index"), @AggregateParameter("tenantId")}),
@AggregateProvider(beanClass = IndexService.class, method = "assemblyReduction",
parameters = {@AggregateParameter("index"), @AggregateParameter("tenantId"), @AggregateParameter("customerId")})})
@AggregateConsumer(beanClass = IndexService.class,method = "summary")
void index(@AggregateParameterName("index") IndexVo indexVo,
@AggregateParameterName("tenantId") Integer tenantId,
@AggregateParameterName("customerId") Integer customerId,
@AggregateParameterName("userId") Integer userId);
}在spring的service中使用并发代理的接口,直接注入就行 @Autowired
@SuppressWarnings("all")
private IndexAggregatorProxy indexAggregatorProxy;