首页 > 解决方案 > 我的Spring Boot微服务成功构建镜像,但测试时出现错误,因为无法连接到eureka命名服务器

问题描述

当我在 docker 中启动并运行图像时,一切都运行良好。我通常使用 构建mvn spring-boot:build-image -DskipTests,我使用了,dskiptests所以我不必看到错误。

我实际上如何以正确的方式做到这一点?(或者不包括跳过测试的方式)是不是因为我没有指定自己的测试,所以它做了一些通用测试,因为它无法到达命名服务器而失败?在构建服务之前是否需要运行命名服务器?(这不可能)任何指导都会有所帮助。

这是测试输出。我在第一个 eureka 错误处截断,因为其余的错误只是一遍又一遍。谢谢你的帮助!

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running myproject.apigateway.ApiGatewayApplicationTests
13:13:51.533 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
13:13:51.543 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
13:13:51.573 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [myproject.apigateway.ApiGatewayApplicationTests] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper]
13:13:51.582 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [myproject.apigateway.ApiGatewayApplicationTests], using SpringBootContextLoader
13:13:51.585 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [myproject.apigateway.ApiGatewayApplicationTests]: class path resource [myproject/apigateway/ApiGatewayApplicationTests-context.xml] does not exist
13:13:51.586 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [myproject.apigateway.ApiGatewayApplicationTests]: class path resource [myproject/apigateway/ApiGatewayApplicationTestsContext.groovy] does not exist
13:13:51.586 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [myproject.apigateway.ApiGatewayApplicationTests]: no resource found for suffixes {-context.xml, Context.groovy}.
13:13:51.586 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [myproject.apigateway.ApiGatewayApplicationTests]: ApiGatewayApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
13:13:51.626 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [myproject.apigateway.ApiGatewayApplicationTests]
13:13:51.673 [main] DEBUG org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider - Identified candidate component class: file [Microservices\api-gateway\target\classes\myproject\apigateway\ApiGatewayApplication.class]
13:13:51.676 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration myproject.apigateway.ApiGatewayApplication for test class myproject.apigateway.ApiGatewayApplicationTests
13:13:51.780 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - @TestExecutionListeners is not present for class [myproject.apigateway.ApiGatewayApplicationTests]: using defaults.
13:13:51.780 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.event.ApplicationEventsTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener]
13:13:51.788 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [javax/servlet/ServletContext]
13:13:51.790 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttributeSource]
13:13:51.791 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute]
13:13:51.791 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@72758afa, org.springframework.test.context.event.ApplicationEventsTestExecutionListener@fb9c7aa, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@4c398c80, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@7fc6de5b, org.springframework.test.context.support.DirtiesContextTestExecutionListener@21baa903, org.springframework.test.context.event.EventPublishingTestExecutionListener@607fbe09, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@60a2630a, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@29df4d43, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@5dd91bca, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@40cb698e, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@3382f8ae, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener@60641ec8]
13:13:51.795 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: context [DefaultTestContext@207b8649 testClass = ApiGatewayApplicationTests, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [ReactiveWebMergedContextConfiguration@65b3a85a testClass = ApiGatewayApplicationTests, locations = '{}', classes = '{class myproject.apigateway.ApiGatewayApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@51a9ad5e, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@239105a8, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@2e32ccc5, org.springframework.boot.test.web.reactive.server.WebTestClientContextCustomizer@4dd6fd0a, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@4a00d9cf, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@766653e6, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@7fa98a66], contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map[[empty]]], class annotated with @DirtiesContext [false] with mode [null].
13:13:51.807 [main] DEBUG org.springframework.test.context.support.DependencyInjectionTestExecutionListener - Performing dependency injection for test context [[DefaultTestContext@207b8649 testClass = ApiGatewayApplicationTests, testInstance = myproject.apigateway.ApiGatewayApplicationTests@a8e6492, testMethod = [null], testException = [null], mergedContextConfiguration = [ReactiveWebMergedContextConfiguration@65b3a85a testClass = ApiGatewayApplicationTests, locations = '{}', classes = '{class myproject.apigateway.ApiGatewayApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@51a9ad5e, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@239105a8, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@2e32ccc5, org.springframework.boot.test.web.reactive.server.WebTestClientContextCustomizer@4dd6fd0a, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@4a00d9cf, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@766653e6, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@7fa98a66], contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.event.ApplicationEventsTestExecutionListener.recordApplicationEvents' -> false]]].
13:13:51.830 [main] DEBUG org.springframework.test.context.support.TestPropertySourceUtils - Adding inlined properties to environment: {spring.jmx.enabled=false, org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.4)

2021-10-21 13:13:52.582  INFO 13388 --- [           main] m.n.n.e.a.ApiGatewayApplicationTests     : Starting ApiGatewayApplicationTests using Java 11.0.8 on COMPUTER with PID 13388 (started by user in Microservices\api-gateway)
2021-10-21 13:13:52.583  INFO 13388 --- [           main] m.n.n.e.a.ApiGatewayApplicationTests     : No active profile set, falling back to default profiles: default
2021-10-21 13:13:53.333  INFO 13388 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=b3d64ecf-1689-39e9-a774-4aeb91dfd40a
2021-10-21 13:13:53.400  INFO 13388 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerBeanPostProcessorAutoConfiguration' of type [org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerBeanPostProcessorAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2021-10-21 13:13:53.402  INFO 13388 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerBeanPostProcessorAutoConfiguration$ReactorDeferringLoadBalancerFilterConfig' of type [org.springframework.cloud.client.loadbalancer.reactive.LoadBalancerBeanPostProcessorAutoConfiguration$ReactorDeferringLoadBalancerFilterConfig] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2021-10-21 13:13:53.407  INFO 13388 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'reactorDeferringLoadBalancerExchangeFilterFunction' of type [org.springframework.cloud.client.loadbalancer.reactive.DeferringLoadBalancerExchangeFilterFunction] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2021-10-21 13:13:55.191  INFO 13388 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [After]
2021-10-21 13:13:55.192  INFO 13388 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Before]
2021-10-21 13:13:55.192  INFO 13388 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Between]
2021-10-21 13:13:55.192  INFO 13388 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Cookie]
2021-10-21 13:13:55.192  INFO 13388 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Header]
2021-10-21 13:13:55.192  INFO 13388 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Host]
2021-10-21 13:13:55.192  INFO 13388 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Method]
2021-10-21 13:13:55.192  INFO 13388 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Path]
2021-10-21 13:13:55.192  INFO 13388 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Query]
2021-10-21 13:13:55.192  INFO 13388 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [ReadBody]
2021-10-21 13:13:55.192  INFO 13388 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [RemoteAddr]
2021-10-21 13:13:55.192  INFO 13388 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [Weight]
2021-10-21 13:13:55.193  INFO 13388 --- [           main] o.s.c.g.r.RouteDefinitionRouteLocator    : Loaded RoutePredicateFactory [CloudFoundryRouteService]
2021-10-21 13:13:55.511  INFO 13388 --- [           main] DiscoveryClientOptionalArgsConfiguration : Eureka HTTP Client uses RestTemplate.
2021-10-21 13:13:56.648  WARN 13388 --- [           main] iguration$LoadBalancerCaffeineWarnLogger : Spring Cloud LoadBalancer is currently working with the default cache. You can switch to using Caffeine cache, by adding it and org.springframework.cache.caffeine.CaffeineCacheManager to the classpath.
2021-10-21 13:13:56.670  INFO 13388 --- [           main] o.s.c.n.eureka.InstanceInfoFactory       : Setting initial instance status as: STARTING
2021-10-21 13:13:56.771  INFO 13388 --- [           main] com.netflix.discovery.DiscoveryClient    : Initializing Eureka in region us-east-1
2021-10-21 13:13:56.777  INFO 13388 --- [           main] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration
2021-10-21 13:13:57.120  INFO 13388 --- [           main] com.netflix.discovery.DiscoveryClient    : Disable delta property : false
2021-10-21 13:13:57.120  INFO 13388 --- [           main] com.netflix.discovery.DiscoveryClient    : Single vip registry refresh property : null
2021-10-21 13:13:57.120  INFO 13388 --- [           main] com.netflix.discovery.DiscoveryClient    : Force full registry fetch : false
2021-10-21 13:13:57.120  INFO 13388 --- [           main] com.netflix.discovery.DiscoveryClient    : Application is null : false
2021-10-21 13:13:57.120  INFO 13388 --- [           main] com.netflix.discovery.DiscoveryClient    : Registered Applications size is zero : true
2021-10-21 13:13:57.120  INFO 13388 --- [           main] com.netflix.discovery.DiscoveryClient    : Application version is -1: true
2021-10-21 13:13:57.120  INFO 13388 --- [           main] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server
2021-10-21 13:14:01.493  INFO 13388 --- [           main] c.n.d.s.t.d.RedirectingEurekaHttpClient  : Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://localhost:8761/eureka/}, exception=I/O error on GET request for "http://localhost:8761/eureka/apps/": Connect to localhost:8761 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect; nested exception is org.apache.http.conn.HttpHostConnectException: Connect to localhost:8761 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect stacktrace=org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8761/eureka/apps/": Connect to localhost:8761 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect; nested exception is org.apache.http.conn.HttpHostConnectException: Connect to localhost:8761 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect

标签: javaspring-bootspring-mvcmicroservicesnetflix-eureka

解决方案


推荐阅读