parallelStream()并发远程调用服务导致异常

问题描述

商城首页有许多商品组件、优惠券组件信息需要同步更新。比如:当时配置优惠券时,由于没有用户信息,默认都是未领取状态。当一个登录用户访问首页时,这时候首页配置的优惠券状态就要根据当前用户信息进行更新:已领取/未领取/已领完等。

由于组件更新都需要进行远程服务调用,商品最新价格需要远程调用商品服务,优惠券针对用户状态需要远程调用优惠券服务,如果首页配置的组件非常多,同步串行调用将非常耗时,首页加载速度非常慢,影响用户体验。

基于上述情况,为了提高首页加载速度,采用了并行请求模式。具体代码如下:

// itemList是组件对象列表,包括:商品组件、优惠券组件等
itemList.parallelStream().forEach(item -> {
	JSONObject listItem = (JSONObject) item;

	ParallelParam parallelParam = new ParallelParam(listItem, Objects.nonNull(user) ? user.getId() : null);
	itemConsumer.accept(parallelParam);
});

Consumer<ParallelParam> itemConsumer = parallelParam -> {
	// 如果是商品组件,更新商品信息
	if(goodsTypes.contains(parallelParam.item.getString("type"))) {
		goodsItemConsumer.accept(parallelParam);
	}
	
	// 如果是优惠券组件,则获取最新的个人-优惠券状态
	if("coupon".equals(parallelParam.item.getString("type"))) {
		couponItemConsumer.accept(parallelParam);
	}
};

// 在上述goodsItemConsumer和couponItemConsumer中均会远程调用对应的服务

这里顺便提一句咱们的部署方式,采用K8s+Docker容器的方式部署服务。服务部署完成后,在实际运行中出现异常:Could not find class [org.springframework.boot.autoconfigure.condition.OnPropertyCondition],具体报错内容如下:

2024-03-19 11:23:13.293  WARN 6 --- [onPool-worker-3] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.cc.framwork.common.gray.rule.GrayLoadBalancerClientConfiguration]; nested exception is java.lang.IllegalArgumentException: Could not find class [org.springframework.boot.autoconfigure.condition.OnPropertyCondition]
2024-03-19 11:23:13.293 ERROR 6 --- [onPool-worker-3] .f.f.RemoteCouponsServiceFallbackFactory : 远程调用优惠券服务异常:Failed to parse configuration class [com.cc.framwork.common.gray.rule.GrayLoadBalancerClientConfiguration]; nested exception is java.lang.IllegalArgumentException: Could not find class [org.springframework.boot.autoconfigure.condition.OnPropertyCondition]

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.cc.framwork.common.gray.rule.GrayLoadBalancerClientConfiguration]; nested exception is java.lang.IllegalArgumentException: Could not find class [org.springframework.boot.autoconfigure.condition.OnPropertyCondition]
        at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:188)
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:331)
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:247)
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:311)
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:112)
        at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:746)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:564)
        at org.springframework.cloud.context.named.NamedContextFactory.createContext(NamedContextFactory.java:155)
        at org.springframework.cloud.context.named.NamedContextFactory.getContext(NamedContextFactory.java:108)
        at org.springframework.cloud.context.named.NamedContextFactory.getInstances(NamedContextFactory.java:203)
        at org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient.execute(FeignBlockingLoadBalancerClient.java:94)
        at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:121)
        at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:91)
        at com.cc.framework.common.sentinel.feign.PigxSentinelInvocationHandler.invoke(PigxSentinelInvocationHandler.java:104)
        at org.springframework.cloud.openfeign.FeignCachingInvocationHandlerFactory$1.proceed(FeignCachingInvocationHandlerFactory.java:66)
        at org.springframework.cache.interceptor.CacheInterceptor.lambda$invoke$0(CacheInterceptor.java:54)
        at org.springframework.cache.interceptor.CacheAspectSupport.execute(CacheAspectSupport.java:351)
        at org.springframework.cache.interceptor.CacheInterceptor.invoke(CacheInterceptor.java:64)
        at org.springframework.cloud.openfeign.FeignCachingInvocationHandlerFactory.lambda$create$1(FeignCachingInvocationHandlerFactory.java:53)

问题解决方案

针对该问题,在网上找到了一篇文章,描述的非常清晰仔细,这里不赘述,大家可以直接阅读《Could not find class [org.springframework.boot.autoconfigure.condition.OnPropertyCondition]》

具体解决方案:将并行改为串行即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

萧十一郎君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值