今天在公司为springCloud框架下的项目上线前做打包准备,在准备的过程中发现一个比较有意思的现象特地记下来,为“基友”们提供一下参考,也为使自己在后续碰到同样的问题时能够快速解决。

现象:开发工具内(idea)运行正常的微服务因为改了东西后莫名无法正常启动,报错信息如下

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.netflix.discovery.EurekaClient]: Factory method 'eurekaClient' threw exception; nested exception is java.lang.RuntimeException: Failed to initialize DiscoveryClient!
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:583)
        ... 47 common frames omitted
Caused by: java.lang.RuntimeException: Failed to initialize DiscoveryClient!
        at com.netflix.discovery.DiscoveryClient.<init>(DiscoveryClient.java:411)
        at com.netflix.discovery.DiscoveryClient.<init>(DiscoveryClient.java:269)
        at org.springframework.cloud.netflix.eureka.CloudEurekaClient.<init>(CloudEurekaClient.java:63)
        at org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration$RefreshableEurekaClientConfiguration.eurekaClient(EurekaClientAutoConfiguration.java:290)
        at org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration$RefreshableEurekaClientConfiguration$$EnhancerBySpringCGLIB$$7f5dfdbe.CGLIB$eurekaClient$0(<generated>)
        at org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration$RefreshableEurekaClientConfiguration$$EnhancerBySpringCGLIB$$7f5dfdbe$$FastClassBySpringCGLIB$$4fdb82e1.invoke(<generated>)
        at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
        at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:365)
        at org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration$RefreshableEurekaClientConfiguration$$EnhancerBySpringCGLIB$$7f5dfdbe.eurekaClient(<generated>)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
        ... 48 common frames omitted
Caused by: java.lang.RuntimeException: Cannot create Jersey client
        at com.netflix.discovery.shared.transport.jersey.EurekaJerseyClientImpl$EurekaJerseyClientBuilder.build(EurekaJerseyClientImpl.java:182)
        at com.netflix.discovery.shared.transport.jersey.JerseyEurekaHttpClientFactory$JerseyEurekaHttpClientFactoryBuilder.buildLegacy(JerseyEurekaHttpClientFactory.java:230)
        at com.netflix.discovery.shared.transport.jersey.JerseyEurekaHttpClientFactory$JerseyEurekaHttpClientFactoryBuilder.build(JerseyEurekaHttpClientFactory.java:204)
        at com.netflix.discovery.shared.transport.jersey.JerseyEurekaHttpClientFactory.create(JerseyEurekaHttpClientFactory.java:161)
        at com.netflix.discovery.shared.transport.jersey.Jersey1TransportClientFactories.newTransportClientFactory(Jersey1TransportClientFactories.java:59)
        at com.netflix.discovery.DiscoveryClient.scheduleServerEndpointTask(DiscoveryClient.java:485)
        at com.netflix.discovery.DiscoveryClient.<init>(DiscoveryClient.java:398)
        ... 61 common frames omitted
Caused by: java.lang.RuntimeException: Cannot create Jersey client
        at com.netflix.discovery.shared.transport.jersey.EurekaJerseyClientImpl.<init>(EurekaJerseyClientImpl.java:61)
        at com.netflix.discovery.shared.transport.jersey.EurekaJerseyClientImpl$EurekaJerseyClientBuilder.build(EurekaJerseyClientImpl.java:180)
        ... 67 common frames omitted
Caused by: java.lang.ExceptionInInitializerError: null
        at com.sun.jersey.core.spi.factory.MessageBodyFactory.initReaders(MessageBodyFactory.java:182)
        at com.sun.jersey.core.spi.factory.MessageBodyFactory.initReaders(MessageBodyFactory.java:175)
        at com.sun.jersey.core.spi.factory.MessageBodyFactory.init(MessageBodyFactory.java:162)
        at com.sun.jersey.api.client.Client.init(Client.java:343)
        at com.sun.jersey.api.client.Client.access$000(Client.java:119)
        at com.sun.jersey.api.client.Client$1.f(Client.java:192)
        at com.sun.jersey.api.client.Client$1.f(Client.java:188)
        at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)
        at com.sun.jersey.api.client.Client.<init>(Client.java:188)
        at com.sun.jersey.client.apache4.ApacheHttpClient4.<init>(ApacheHttpClient4.java:151)
        at com.sun.jersey.client.apache4.ApacheHttpClient4.<init>(ApacheHttpClient4.java:137)
        at com.sun.jersey.client.apache4.ApacheHttpClient4.create(ApacheHttpClient4.java:181)
        at com.netflix.discovery.shared.transport.jersey.EurekaJerseyClientImpl.<init>(EurekaJerseyClientImpl.java:53)
        ... 68 common frames omitted
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: org.jboss.resteasy.spi.ResteasyProviderFactory
        at javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:152)
        at javax.ws.rs.ext.RuntimeDelegate.getInstance(RuntimeDelegate.java:120)
        at javax.ws.rs.core.MediaType.valueOf(MediaType.java:179)
        at com.sun.jersey.core.header.MediaTypes.<clinit>(MediaTypes.java:65)
        ... 81 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.jboss.resteasy.spi.ResteasyProviderFactory
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:93)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:264)
        at javax.ws.rs.ext.FactoryFinder.newInstance(FactoryFinder.java:114)
        at javax.ws.rs.ext.FactoryFinder.find(FactoryFinder.java:207)
        at javax.ws.rs.ext.RuntimeDelegate.findDelegate(RuntimeDelegate.java:135)
        ... 84 common frames omitted

 凭借多年工作经验一眼就判定为包依赖有问题,于是果断把刚刚注掉的自认为没用的依赖包都给放回来,但是仍然不行于是和另一个项目进行了对比,然后没有发现任何可疑的地方,无耐之下只好找到历史版本给回退了一下,OK,一切正常。于是就顺手把一个看着不符合自己习惯的地方给改了一下(处女座,你懂得),结果再次启动同样的异常再次重现,果断重复试验了好几次(屡试不爽)竟然全部复现^_^!,具体改动地方如下:

 

各位“基友”看出区别了吗?就因为两个jar包依赖的顺序不一样,结果却完全不同。

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐