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

  先去添加这个依赖:

		<dependency>
			<groupId>com.netflix.eureka</groupId>
			<artifactId>eureka-client-jersey3</artifactId>
		</dependency>

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

@Configuration
public class Jersey3TransportClientFactoriesConfig {

    @Bean
    public Jersey3TransportClientFactories jersey3TransportClientFactories() {
        return new Jersey3TransportClientFactories();
    }

}

  然后就可以了。

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

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

<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.

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

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

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

posted @ 2024-08-16 15:15  程序计算机人  阅读(558)  评论(0)    收藏  举报