OpenFeign / feign

Feign makes writing java http clients easier
Apache License 2.0
9.49k stars 1.93k forks source link

NamedContextFactory.getContext NullPointerException #1703

Open progsq opened 2 years ago

progsq commented 2 years ago

Other service to current service is Ok,but Current service to current service will throw NullPointerException

java.lang.NullPointerException: null at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:936) ~[na:1.8.0_341] at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:964) ~[na:1.8.0_341] at org.springframework.cloud.context.named.NamedContextFactory.getContext(NamedContextFactory.java:102) ~[spring-cloud-context-3.1.1.jar:3.1.1] at org.springframework.cloud.context.named.NamedContextFactory.getProvider(NamedContextFactory.java:161) ~[spring-cloud-context-3.1.1.jar:3.1.1] at org.springframework.cloud.context.named.ClientFactoryObjectProvider.delegate(ClientFactoryObjectProvider.java:115) ~[spring-cloud-context-3.1.1.jar:3.1.1] at org.springframework.cloud.context.named.ClientFactoryObjectProvider.getIfAvailable(ClientFactoryObjectProvider.java:64) ~[spring-cloud-context-3.1.1.jar:3.1.1] at com.alibaba.cloud.nacos.loadbalancer.NacosLoadBalancer.choose(NacosLoadBalancer.java:68) ~[spring-cloud-starter-alibaba-nacos-discovery-2021.0.1.0.jar:2021.0.1.0] at com.alibaba.cloud.nacos.loadbalancer.NacosLoadBalancer.choose(NacosLoadBalancer.java:47) ~[spring-cloud-starter-alibaba-nacos-discovery-2021.0.1.0.jar:2021.0.1.0] at org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient.choose(BlockingLoadBalancerClient.java:155) ~[spring-cloud-loadbalancer-3.1.1.jar:3.1.1] at org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient.execute(FeignBlockingLoadBalancerClient.java:97) ~[spring-cloud-openfeign-core-3.1.1.jar:3.1.1] at com.alibaba.cloud.seata.feign.SeataFeignBlockingLoadBalancerClient.execute(SeataFeignBlockingLoadBalancerClient.java:44) ~[spring-cloud-starter-alibaba-seata-2021.0.1.0.jar:2021.0.1.0] at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:119) ~[feign-core-11.8.jar:na] at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:89) ~[feign-core-11.8.jar:na] at com.alibaba.cloud.sentinel.feign.SentinelInvocationHandler.invoke(SentinelInvocationHandler.java:109) ~[spring-cloud-starter-alibaba-sentinel-2021.0.1.0.jar:2021.0.1.0] at com.sun.proxy.$Proxy199.addOperaLog(Unknown Source) ~[na:na] at com.common.log.aspect.LogAspect$1.run(LogAspect.java:79) ~[classes/:na] at java.lang.Thread.run(Thread.java:750) ~[na:1.8.0_341]

whongzhuang commented 1 year ago

I also encountered this problem. I can't handle it. How did you handle it,please!

progsq commented 1 year ago

i find the problem that @ComponentScan, I use it @ComponentScan("com") ,then it will throw NullException, when i change it to @ComponentScan("com.ruoyi"),then it is ok.

progsq commented 1 year ago

异常信息如下 java.lang.NullPointerException: null at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:936) ~[na:1.8.0_341] at java.util.concurrent.ConcurrentHashMap.containsKey(ConcurrentHashMap.java:964) ~[na:1.8.0_341] at org.springframework.cloud.context.named.NamedContextFactory.getContext(NamedContextFactory.java:102) ~[spring-cloud-context-3.1.1.jar:3.1.1] at org.springframework.cloud.context.named.NamedContextFactory.getProvider(NamedContextFactory.java:161) ~[spring-cloud-context-3.1.1.jar:3.1.1] at org.springframework.cloud.context.named.ClientFactoryObjectProvider.delegate(ClientFactoryObjectProvider.java:115) ~[spring-cloud-context-3.1.1.jar:3.1.1] at org.springframework.cloud.context.named.ClientFactoryObjectProvider.getIfAvailable(ClientFactoryObjectProvider.java:64) ~[spring-cloud-context-3.1.1.jar:3.1.1] at com.alibaba.cloud.nacos.loadbalancer.NacosLoadBalancer.choose(NacosLoadBalancer.java:68) ~[spring-cloud-starter-alibaba-nacos-discovery-2021.0.1.0.jar:2021.0.1.0] at com.alibaba.cloud.nacos.loadbalancer.NacosLoadBalancer.choose(NacosLoadBalancer.java:47) ~[spring-cloud-starter-alibaba-nacos-discovery-2021.0.1.0.jar:2021.0.1.0] at org.springframework.cloud.loadbalancer.blocking.client.BlockingLoadBalancerClient.choose(BlockingLoadBalancerClient.java:155) ~[spring-cloud-loadbalancer-3.1.1.jar:3.1.1] at org.springframework.cloud.openfeign.loadbalancer.FeignBlockingLoadBalancerClient.execute(FeignBlockingLoadBalancerClient.java:97) ~[spring-cloud-openfeign-core-3.1.1.jar:3.1.1] at com.alibaba.cloud.seata.feign.SeataFeignBlockingLoadBalancerClient.execute(SeataFeignBlockingLoadBalancerClient.java:44) ~[spring-cloud-starter-alibaba-seata-2021.0.1.0.jar:2021.0.1.0] at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:119) ~[feign-core-11.8.jar:na] at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:89) ~[feign-core-11.8.jar:na] at com.alibaba.cloud.sentinel.feign.SentinelInvocationHandler.invoke(SentinelInvocationHandler.java:109) ~[spring-cloud-starter-alibaba-sentinel-2021.0.1.0.jar:2021.0.1.0] at com.sun.proxy.$Proxy187.info(Unknown Source) ~[na:na] at com.system.controller.SysUserController$1.run(SysUserController.java:57) ~[classes/:na] at java.lang.Thread.run(Thread.java:750) ~[na:1.8.0_341] 复现步骤,当前服务远程调用当前服务 原因分析: 注:情况A代表其他服务调用当前服务、情况B代表当前服务远程调用当前服务 情况A时,走的是RoundRobinLoadBalancer 而情况B时,走的是NacosLoadBalancer 深层原因分析: 在NativeMethodAccessorImpl.invoke中发现: 情况A:NacosLoadBalancerClientConfiguration 情况B:LoadBalancerClientConfiguration 使用NacosLoadBalancer时: NacosLoadBalancerClientConfiguration.nacosLoadBalancer中environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME)为空 导致NamedContextFactory.getLazyProvider为空 导致ClientFactoryObjectProvider name变量为空 导致NamedContextFactory.getContext报空指针异常 解决办法: NacosLoadBalancerClientConfiguration.nacosLoadBalancer中时 String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME) 可以通过配置文件强制指定服务名称 即: loadbalancer: client: name: 服务名称 注:scanBasePackages = "com"去除即可,怀疑扫描到了未知包