使用 openFeign 报初始化 空指针的问题:at org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer.<init>
2021/9/17 1:04:59
本文主要是介绍使用 openFeign 报初始化 空指针的问题:at org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer.<init>,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
这个问题整整搞了老子三天多,万恶的百度,全都是 cv 大法帖,没有一个说到点的,而且不知道是不是没有人遇到这个问题,相关的帖子也很少,
本来也决定放弃了,因为重新新建工程写 demo 又跑通了,但是看到整合在旧项目里面的代码,就是跑不起来,又不甘心,还是较真了起来,最后在第三天的晚上,终于解决了,
好了,说了一堆废话,下面看具体什么原因:
本人是使用 springboot 整合 openFeign 的,相关 maven 和版本号如下:
<!-- 使用 openfeign 做 rpc 远程调用(默认是 2.1.0.RELEASE ) --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
<!-- nacos 版本号要和下面的 springCloud 版本号对应 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-nacos-discovery</artifactId> <version>2.1.1.RELEASE</version> </dependency>
<!-- ↓↓↓↓ springCloud 部分 ↓↓↓↓ --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.1.1.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency>
1、yml 文件里面的服务命,要和 @FeignClient(value = "xxx") 里面的 xxx 一样,切记别弄错
2、启动类 main 方法,要在所有模块最外层,保证能扫描到你的 feign 和 service 层的服务
3、IClientConfig 类,重点来了,就是这个类,如果不自己定义(openFeign 是可以自定义这个类的,然后自己初始化),那么就千万不要自己去创建一个 bean 出来,然后自己加上注解定义成配置类如下:
@Configuration public class IClientConfig { @Bean public DefaultClientConfigImpl iClientConfig(){ return new DefaultClientConfigImpl(); } } 这玩意千万不要在程序里自己创建出来,可能很多初学者不是很懂,一开始有配置了这个,结果又只是单纯的 return 了一个没有任何属性的 DefaultClientConfigImpl 对象,然后 openFeign 就会使用你创建的这个对象,结果 去初始化的时候,就会在源码里面报空指针异常,把这玩意去掉,基本就可以了,如果要自己定义,那记得把里面该有的属性都要初始化值。
这篇关于使用 openFeign 报初始化 空指针的问题:at org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer.<init>的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求
- 2024-10-01使用 golang 将ETH账户的资产平均分散到其他账户
- 2024-10-01JWT用户校验课程:从入门到实践
- 2024-10-01Server Component课程入门指南
- 2024-09-30Dnd-Kit学习:新手快速入门指南