在开发后台接口时, 为了开发效率, 我们往往习惯编写串行执行的代码, 去调用不同的接口,
即使这些接口之间并无依赖, 这使得最后开发的接口性能低下, 且数据不方便复用
此框架目的旨在保持开发效率的同时, 很方便地支持并发和数据复用
@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;