1.引入Hystrix
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2.在启动类上开启Hystrix功能
@EnableHystrix
3.请求合并实现代码
import com.bzcst.bop.oms.orm.model.po.Product;
import com.bzcst.bop.oms.orm.service.ProductService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.Future;
/**
* @author 向振华
* @date 2023/10/26 14:37
*/
@Slf4j
@Service
public class BatchService {
@Resource
private ProductService productService;
@HystrixCollapser(
batchMethod = "getByIds",
scope = com.netflix.hystrix.HystrixCollapser.Scope.GLOBAL,
collapserProperties = {
@HystrixProperty(name = HystrixPropertiesManager.MAX_REQUESTS_IN_BATCH, value = "10"),
@HystrixProperty(name = HystrixPropertiesManager.TIMER_DELAY_IN_MILLISECONDS, value = "20")
}
)
public Future<Product> getById(Long id) {
return null;
}
@HystrixCommand
public List<Product> getByIds(List<Long> ids) {
log.info("批查询 ---> " + ids);
return productService.listByIds(ids);
}
}
其中getById(Long id)方法并不会进入。
其中ProductService 是基于mybatisplus的查询接口。
import com.bzcst.bop.oms.orm.model.po.Product;
imp