concurrent-aggregator - Spring 无关联并发聚合器


未知
跨平台
Java

软件简介

背景与目的

在开发后台接口时, 为了开发效率, 我们往往习惯编写串行执行的代码, 去调用不同的接口,

即使这些接口之间并无依赖, 这使得最后开发的接口性能低下, 且数据不方便复用

此框架目的旨在保持开发效率的同时, 很方便地支持并发和数据复用

原理

  1. Spring + CountDownLatch + Future + 反射 + 动态代理
  2. 通过启动类的注解去加载需要代理的接口,
  3. 用 Factorybean 将代理的bean注册到spring 容器
  4. 代理接口使用注解配置使用 spring 的 bean 来完成操作
  5. 线程池执行具体的操作,CountDownLatch做同步
  6. 目前自动识别代理方法的类型和参数比较弱鸡,后续需要继续完善
  7. 工程需要自己下载下来 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;