Java教程

使用 openFeign 报初始化 空指针的问题:at org.springframework.cloud.openfeign.ribbon.FeignLoadBalancer.<init>

本文主要是介绍使用 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>的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!