问题描述
商城首页有许多商品组件、优惠券组件信息需要同步更新。比如:当时配置优惠券时,由于没有用户信息,默认都是未领取状态。当一个登录用户访问首页时,这时候首页配置的优惠券状态就要根据当前用户信息进行更新:已领取/未领取/已领完等。
由于组件更新都需要进行远程服务调用,商品最新价格需要远程调用商品服务,优惠券针对用户状态需要远程调用优惠券服务,如果首页配置的组件非常多,同步串行调用将非常耗时,首页加载速度非常慢,影响用户体验。
基于上述情况,为了提高首页加载速度,采用了并行请求模式。具体代码如下:
// 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]》
具体解决方案:将并行改为串行即可。