2024-08-16 15:15阅读: 228评论: 0推荐: 0

eureka-client注册exception——Error creating bean with name ‘scopedTarget.eurekaClient‘ defined in class……或者Could'not find com.netflix.discovery.shared.transport.jersey.TransportClientFactories<?>

  错误没有截图,不过去看idea内置的日志可以看到“Error creating bean with name ‘scopedTarget.eurekaClient‘ defined in class……”或者“Error starting ApplicationContext. To display the conditions report re-run ...”或者Error processing condition on org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientConfigurations$RestClientConfiguration又或者

Caused by: java.lang.NullPointerException: Cannot invoke "org.springframework.cloud.netflix.eureka.CloudEurekaClient.getApplications()" because the return value of "org.springframework.cloud.netflix.eureka.serviceregistry.EurekaRegistration.getEurekaClient()" is null。诸如此类的描述。日志往下翻都能看到好几个"cause by",最让我印象深刻的还是第一个错误(或者说你有mongo数据库没有设置好的错误的话是第二个错误)——Could'not find com.netflix.discovery.shared.transport.jersey.TransportClientFactories<?>,下面的解决方案主要是针对这个错误的(其他错误我看了一下我的error日记发现要么是依赖冲突,spring和eureka版本冲突,依赖版本冲突,要么是依赖缺失,如果还不能解决那就看一下下面的解决方案,因为这一段时间我出现的bug都是eureka-client的bug)

  查了一下终于让我找到这个bug的相关解决办法了。参考链接:https://github.com/spring-cloud/spring-cloud-netflix/issues/4185

  先去添加这个依赖:

1
2
3
4
<dependency>
    <groupId>com.netflix.eureka</groupId>
    <artifactId>eureka-client-jersey3</artifactId>
</dependency>

  然后去client模块那里新加一个config配置类。

1
2
3
4
5
6
7
8
9
@Configuration
public class Jersey3TransportClientFactoriesConfig {
 
    @Bean
    public Jersey3TransportClientFactories jersey3TransportClientFactories() {
        return new Jersey3TransportClientFactories();
    }
 
}

  然后就可以了。

  这种解决办法其实是自己设置一个transportClient的bean。

  一开始我在找相关资料的时候了解到是eureka和spring的http客户端用的不是同一个东西,eureka用的是jersey,spring用的是restTemplate,然后我就将子pom改成了这样:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<dependencies>
<!--     error:没有某个bean的相关记载,我看了一下是说没有引入依赖web,但是父pom那里又确实引了,-->
<!--     -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <!-- 排除Jersey,用SpringMVC Rest方式,但是据说这个派出了后有另一个问题-->
            <exclusions>
                <exclusion>
                    <groupId>com.sun.jersey</groupId>
                    <artifactId>jersey-client</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jersey</groupId>
                    <artifactId>jersey-core</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jersey.contribs</groupId>
                    <artifactId>jersey-apache-client4</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

  (不用管注释)

  再去client模块(也就是子模块)的application.yml那里新加了一项client.transport.rest-template-enabled : true.

1
2
3
4
5
6
7
8
client:
  registerWithEureka: false #不向自身注册。由于该应用为单个注册中心,所以设置为false,代表不向注册中心注册自己
  fetchRegistry: false #不从自身拉取注册信息。由于单个注册中心,不拉取自身信息
  enabled: true
  service-url:
    defaultZone: http://localhost:8108/eureka/
  transport:
    rest-template-enabled: true

  但是没有什么用,该爆这个错还是该爆这个错。

  然后最后就找到了新加一个bean的做法……

本文作者:程序计算机人

本文链接:https://www.cnblogs.com/clina/p/18362912

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   程序计算机人  阅读(228)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 Lions Pride Audio artist
  2. 2 Salty Sailor David Arkenstone
  3. 3 Stonefire David Arkenstone
Stonefire - David Arkenstone
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

Not available