【问题标题】:WebFlux + MongoDB + docker compose = errorWebFlux + MongoDB + docker compose = 错误
【发布时间】:2021-08-05 14:34:45
【问题描述】:

我遇到了这个集成的问题。我使用基于 docker 的 MongoDB 没有问题,但是当我创建 Docker Compose 时,Spring Boot WebFlux 停止查找 Mongo。我正在寻找问题,但我不知道如何解决。

服务日志显示了这个问题:

2021-08-05 14:04:45.184  INFO 1 --- [127.0.0.1:27017] org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server 127.0.0.1:27017 2021-08-05T14:04:45.203968373Z 

我的 Spring Mongo 配置是:

@Configuration
@RequiredArgsConstructor
@EnableReactiveMongoRepositories(value = "br.com.person.repositories")
public class MongoReactiveConfig extends AbstractReactiveMongoConfiguration {

    @Value("${spring.data.mongodb.host}")
    private String host;

    @Value("${spring.data.mongodb.database}")
    private String database;

    @Value("${spring.data.mongodb.port}")
    private String port;


    @Bean
    public MongoClient mongoClient() {
        final String connectionString = String.format("mongodb://%s:%s/%s", host, port, database);
        return MongoClients.create(connectionString);
    }

    @Override
    protected String getDatabaseName() {
        return this.database;
    }

    @Bean
    public ReactiveMongoTemplate reactiveMongoTemplate() {
        return new ReactiveMongoTemplate(reactiveMongoClient(), getDatabaseName());
    }
}

这是我的 docker-compose.ymlspri

version: "3.8"
  services:
  #####################
  ##### DATABASES #####
  #####################
  person_db:
    image: mongo:latest
    container_name: person_db
    ports:
      - 27017:27017
    environment:
      - MONGO_INITDB_DATABASE=person_database
    networks:
      - backend_network

  ######################
  #####   DevOps   #####
  ######################
  eureka_service:
    container_name: eureka_service
    image: eureka:0.0.1-SNAPSHOT
    ports: 
      - 8761:8761
    environment: 
      - SPRING_PROFILES_ACTIVE=prod
    networks:
      - backend_network

  gateway_service:
    container_name: gateway_service
    image: gateway:0.0.1-SNAPSHOT
    depends_on: 
      - eureka_service
    ports: 
      - 8080:8080
    environment: 
      - SPRING_PROFILES_ACTIVE=prod
    networks:
      - backend_network

  ######################
  #####  SERVICES  #####
  ######################
  person_service:
    container_name: person_service
    image: person:0.0.1-SNAPSHOT
    depends_on:
      - eureka_service
      - gateway_service
      - person_db
    ports:
      - 8180:8180
    environment:
      - SPRING_PROFILES_ACTIVE=prod
      - SPRING_DATA_MONGODB_HOST=person_db
      - SPRING_DATA_MONGODB_PORT=27017
      - SPRING_DATA_MONGODB_DATABASE=person_database
    networks:
      - backend_network

networks:
  backend_network:
  driver: bridge

当我运行 docker compose 时,个人服务会显示此日志

2021-08-05T14:04:31.598842495Z Setting Active Processor Count to 4
2021-08-05T14:04:31.819407236Z Calculating JVM memory based on 949096K available memory
2021-08-05T14:04:31.819861212Z Calculated JVM Memory Configuration: -XX:MaxDirectMemorySize=10M -Xmx513855K -XX:MaxMetaspaceSize=128040K -XX:ReservedCodeCacheSize=240M -Xss1M (Total Memory: 949096K, Thread Count: 50, Loaded Class Count: 20192, Headroom: 0%)
2021-08-05T14:04:31.848459508Z Adding 129 container CA certificates to JVM truststore
2021-08-05T14:04:31.904569794Z Spring Cloud Bindings Enabled
2021-08-05T14:04:31.912394489Z Picked up JAVA_TOOL_OPTIONS: -Djava.security.properties=/layers/paketo-buildpacks_bellsoft-liberica/java-security-properties/java-security.properties -agentpath:/layers/paketo-buildpacks_bellsoft-liberica/jvmkill/jvmkill-1.16.0-RELEASE.so=printHeapHistogram=1 -XX:ActiveProcessorCount=4 -XX:MaxDirectMemorySize=10M -Xmx513855K -XX:MaxMetaspaceSize=128040K -XX:ReservedCodeCacheSize=240M -Xss1M -Dorg.springframework.cloud.bindings.boot.enable=true
2021-08-05T14:04:35.413180717Z 
2021-08-05T14:04:35.413211708Z   .   ____          _            __ _ _
2021-08-05T14:04:35.413216318Z  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
2021-08-05T14:04:35.413320843Z ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
2021-08-05T14:04:35.413354057Z  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
2021-08-05T14:04:35.413765333Z   '  |____| .__|_| |_|_| |_\__, | / / / /
2021-08-05T14:04:35.413782827Z  =========|_|==============|___/=/_/_/_/
2021-08-05T14:04:35.417621291Z  :: Spring Boot ::                (v2.5.3)
2021-08-05T14:04:35.417654257Z 
2021-08-05T14:04:36.002780729Z     2021-08-05 14:04:35.999  INFO 1 --- [           main] br.com.fechaki.person.PersonApplication  : Starting PersonApplication using Java 11.0.12 on 58e28b490a76 with PID 1 (/workspace/BOOT-INF/classes started by cnb in /workspace)
2021-08-05T14:04:36.011092592Z     2021-08-05 14:04:36.010  INFO 1 --- [           main] br.com.fechaki.person.PersonApplication  : The following profiles are active: prod
2021-08-05T14:04:40.365284181Z     2021-08-05 14:04:40.364  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Reactive MongoDB repositories in DEFAULT mode.
2021-08-05T14:04:40.930071250Z     2021-08-05 14:04:40.929  INFO 1 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 546 ms. Found 4 Reactive MongoDB repository interfaces.
2021-08-05T14:04:42.970647290Z     2021-08-05 14:04:42.969  INFO 1 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=0802a2fe-6be5-304b-a881-d9075ba4ecbd
2021-08-05T14:04:43.452183684Z     2021-08-05 14:04:43.451  INFO 1 --- [           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-08-05T14:04:43.454250069Z     2021-08-05 14:04:43.453  INFO 1 --- [           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-08-05T14:04:43.459156593Z     2021-08-05 14:04:43.458  INFO 1 --- [           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-08-05T14:04:44.896692596Z     2021-08-05 14:04:44.896  INFO 1 --- [           main] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[127.0.0.1:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'}
2021-08-05T14:04:45.203933051Z     2021-08-05 14:04:45.184  INFO 1 --- [127.0.0.1:27017] org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server 127.0.0.1:27017
2021-08-05T14:04:45.203968373Z 
2021-08-05T14:04:45.203973180Z com.mongodb.MongoSocketOpenException: Exception opening socket
2021-08-05T14:04:45.203976425Z  at com.mongodb.internal.connection.AsynchronousSocketChannelStream$OpenCompletionHandler.failed(AsynchronousSocketChannelStream.java:124) ~[mongodb-driver-core-4.2.3.jar:na]
2021-08-05T14:04:45.203979943Z  at java.base/sun.nio.ch.Invoker.invokeUnchecked(Unknown Source) ~[na:na]
2021-08-05T14:04:45.203982885Z  at java.base/sun.nio.ch.Invoker$2.run(Unknown Source) ~[na:na]
2021-08-05T14:04:45.203985770Z  at java.base/sun.nio.ch.AsynchronousChannelGroupImpl$1.run(Unknown Source) ~[na:na]
2021-08-05T14:04:45.203988692Z  at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) ~[na:na]
2021-08-05T14:04:45.203991515Z  at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) ~[na:na]
2021-08-05T14:04:45.203994830Z  at java.base/java.lang.Thread.run(Unknown Source) ~[na:na]
2021-08-05T14:04:45.203997655Z Caused by: java.net.ConnectException: Connection refused
2021-08-05T14:04:45.204000381Z  at java.base/sun.nio.ch.UnixAsynchronousSocketChannelImpl.checkConnect(Native Method) ~[na:na]
2021-08-05T14:04:45.204003321Z  at java.base/sun.nio.ch.UnixAsynchronousSocketChannelImpl.finishConnect(Unknown Source) ~[na:na]
2021-08-05T14:04:45.204006214Z  at java.base/sun.nio.ch.UnixAsynchronousSocketChannelImpl.finish(Unknown Source) ~[na:na]
2021-08-05T14:04:45.204009012Z  at java.base/sun.nio.ch.UnixAsynchronousSocketChannelImpl.onEvent(Unknown Source) ~[na:na]
2021-08-05T14:04:45.204011815Z  at java.base/sun.nio.ch.EPollPort$EventHandlerTask.run(Unknown Source) ~[na:na]
2021-08-05T14:04:45.204014689Z  ... 1 common frames omitted
2021-08-05T14:04:45.204017452Z 
2021-08-05T14:04:45.545976961Z     2021-08-05 14:04:45.545  WARN 1 --- [           main] o.s.data.convert.CustomConversions       : Registering converter from class java.time.LocalDateTime to class org.joda.time.LocalDateTime as reading converter although it doesn't convert from a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-08-05T14:04:45.703706515Z     2021-08-05 14:04:45.702  WARN 1 --- [           main] o.s.data.convert.CustomConversions       : Registering converter from class java.time.LocalDateTime to class org.joda.time.LocalDateTime as reading converter although it doesn't convert from a store-supported type! You might want to check your annotation setup at the converter implementation.
2021-08-05T14:04:47.551565146Z     2021-08-05 14:04:47.550  INFO 1 --- [           main] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[person-db:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'}
2021-08-05T14:04:47.840405051Z     2021-08-05 14:04:47.839  INFO 1 --- [person-db:27017] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:4, serverValue:2}] to person-db:27017
2021-08-05T14:04:47.843604229Z     2021-08-05 14:04:47.839  INFO 1 --- [person-db:27017] org.mongodb.driver.connection            : Opened connection [connectionId{localValue:3, serverValue:1}] to person-db:27017
2021-08-05T14:04:47.850063426Z     2021-08-05 14:04:47.849  INFO 1 --- [person-db:27017] org.mongodb.driver.cluster               : Monitor thread successfully connected to server with description ServerDescription{address=person-db:27017, type=STANDALONE, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=13, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=233945852}
2021-08-05T14:04:50.033785035Z     2021-08-05 14:04:50.033  INFO 1 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 1 endpoint(s) beneath base path '/actuator'
2021-08-05T14:04:50.506380131Z     2021-08-05 14:04:50.505  INFO 1 --- [           main] DiscoveryClientOptionalArgsConfiguration : Eureka HTTP Client uses RestTemplate.
2021-08-05T14:04:50.613620505Z     2021-08-05 14:04:50.613  WARN 1 --- [           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-08-05T14:04:50.659847813Z     2021-08-05 14:04:50.659  INFO 1 --- [           main] o.s.c.n.eureka.InstanceInfoFactory       : Setting initial instance status as: STARTING
2021-08-05T14:04:50.786660930Z     2021-08-05 14:04:50.783  INFO 1 --- [           main] com.netflix.discovery.DiscoveryClient    : Initializing Eureka in region us-east-1
2021-08-05T14:04:50.795473894Z     2021-08-05 14:04:50.795  INFO 1 --- [           main] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration
2021-08-05T14:04:50.836725459Z     2021-08-05 14:04:50.836  INFO 1 --- [           main] com.netflix.discovery.DiscoveryClient    : Disable delta property : false
2021-08-05T14:04:50.836793911Z     2021-08-05 14:04:50.836  INFO 1 --- [           main] com.netflix.discovery.DiscoveryClient    : Single vip registry refresh property : null
2021-08-05T14:04:50.836957193Z     2021-08-05 14:04:50.836  INFO 1 --- [           main] com.netflix.discovery.DiscoveryClient    : Force full registry fetch : false
2021-08-05T14:04:50.837273453Z     2021-08-05 14:04:50.836  INFO 1 --- [           main] com.netflix.discovery.DiscoveryClient    : Application is null : false
2021-08-05T14:04:50.837776663Z     2021-08-05 14:04:50.837  INFO 1 --- [           main] com.netflix.discovery.DiscoveryClient    : Registered Applications size is zero : true
2021-08-05T14:04:50.837921597Z     2021-08-05 14:04:50.837  INFO 1 --- [           main] com.netflix.discovery.DiscoveryClient    : Application version is -1: true
2021-08-05T14:04:50.838032297Z     2021-08-05 14:04:50.837  INFO 1 --- [           main] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server
2021-08-05T14:04:51.845984898Z     2021-08-05 14:04:51.845  INFO 1 --- [           main] com.netflix.discovery.DiscoveryClient    : The response status is 200
2021-08-05T14:04:51.850243611Z     2021-08-05 14:04:51.849  INFO 1 --- [           main] com.netflix.discovery.DiscoveryClient    : Starting heartbeat executor: renew interval is: 30
2021-08-05T14:04:51.855368679Z     2021-08-05 14:04:51.854  INFO 1 --- [           main] c.n.discovery.InstanceInfoReplicator     : InstanceInfoReplicator onDemand update allowed rate per min is 4
2021-08-05T14:04:51.865201633Z     2021-08-05 14:04:51.864  INFO 1 --- [           main] com.netflix.discovery.DiscoveryClient    : Discovery Client initialized at timestamp 1628172291862 with initial instances count: 0
2021-08-05T14:04:51.867569693Z     2021-08-05 14:04:51.867  INFO 1 --- [           main] o.s.c.n.e.s.EurekaServiceRegistry        : Registering application PERSON-SERVICE with eureka with status UP
2021-08-05T14:04:51.869260578Z     2021-08-05 14:04:51.868  INFO 1 --- [           main] com.netflix.discovery.DiscoveryClient    : Saw local status change event StatusChangeEvent [timestamp=1628172291868, current=UP, previous=STARTING]
2021-08-05T14:04:51.887066149Z     2021-08-05 14:04:51.886  INFO 1 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_PERSON-SERVICE/58e28b490a76:person-service:8180: registering service...
2021-08-05T14:04:52.174876518Z     2021-08-05 14:04:52.174  INFO 1 --- [           main] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port 8180
2021-08-05T14:04:52.195632232Z     2021-08-05 14:04:52.195  INFO 1 --- [TaskExecutor-14] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8180
2021-08-05T14:04:52.243191280Z     2021-08-05 14:04:52.242  INFO 1 --- [           main] br.com.fechaki.person.PersonApplication  : Started PersonApplication in 18.647 seconds (JVM running for 20.352)
2021-08-05T14:04:52.314744592Z     2021-08-05 14:04:52.314  INFO 1 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_PERSON-SERVICE/58e28b490a76:person-service:8180 - registration status: 204
2021-08-05T14:05:21.830865756Z     2021-08-05 14:05:21.830  INFO 1 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Disable delta property : false
2021-08-05T14:05:21.830998294Z     2021-08-05 14:05:21.830  INFO 1 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Single vip registry refresh property : null
2021-08-05T14:05:21.831012581Z     2021-08-05 14:05:21.830  INFO 1 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Force full registry fetch : false
2021-08-05T14:05:21.831109915Z     2021-08-05 14:05:21.830  INFO 1 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Application is null : false
2021-08-05T14:05:21.831234786Z     2021-08-05 14:05:21.830  INFO 1 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Registered Applications size is zero : true
2021-08-05T14:05:21.831492323Z     2021-08-05 14:05:21.831  INFO 1 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Application version is -1: false
2021-08-05T14:05:21.831507822Z     2021-08-05 14:05:21.831  INFO 1 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Getting all instance registry info from the eureka server
2021-08-05T14:05:21.854773386Z     2021-08-05 14:05:21.854  INFO 1 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : The response status is 200

【问题讨论】:

    标签: spring-boot docker-compose spring-webflux


    【解决方案1】:

    那么这里的问题到底是什么?在不了解该应用程序的情况下,我无法真正说出它为什么首先尝试 localhost,但根据这些日志,它似乎能够连接到在 person-db 容器上运行的 mongo:

    2021-08-05T14:04:47.551565146Z 2021-08-05 14:04:47.550 INFO 1 --- [main] org.mongodb.driver.cluster:使用设置 {hosts=[person-db:27017] 创建的集群, 模式=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'} 2021-08-05T14:04:47.840405051Z 2021-08-05 14:04:47.839 信息 1 --- [person-db:27017] org.mongodb.driver.connection:打开的连接 [connectionId{localValue:4, serverValue :2}] 到 person-db:27017 2021-08-05T14:04:47.843604229Z 2021-08-05 14:04:47.839 信息 1 --- [person-db:27017] org.mongodb.driver.connection:打开的连接 [connectionId{localValue:3, serverValue :1}] 到 person-db:27017 2021-08-05T14:04:47.850063426Z 2021-08-05 14:04:47.849 INFO 1 --- [person-db:27017] org.mongodb.driver.cluster:监控线程已成功连接到服务器,描述为 ServerDescription{地址=person-db:27017,类型=独立,状态=连接,ok=true,minWireVersion=0,maxWireVersion=13,maxDocumentSize=16777216,logicalSessionTimeoutMinutes=30,roundTripTimeNanos=233945852}

    【讨论】:

    • 问题是当我运行应用程序时,它正在寻找 [127.0.0.1:27017] org.mongodb.driver.cluster 而不是 docker compose 上的 person-db 主机。它试图运行一个集群,我没有定义一个
    【解决方案2】:

    经过几天的测试和再次测试,我发现我的 MongoReactiveConfig 类存在一个问题。我以为我做错了什么。阅读 Spring 文档,我发现了这个注释:

    为了让它工作,我删除了我的 MongoReactiveConfig 类并更改了我的 docker-compose.yml 配置以将数据库、主机、端口弹簧参数更改为 URI。

    之前:

      ######################
      #####  Services  #####
      ######################
      person-service:
        container_name: person-service
        image: person:0.0.1-SNAPSHOT
        depends_on:
          - eureka-service
          - gateway-service
        links:
          - person-db
        ports:
          - 8180:8180
        environment:
          - SPRING_PROFILES_ACTIVE=prod
          - SPRING_DATA_MONGODB_HOST=person-db
          - SPRING_DATA_MONGODB_PORT=27017
          - SPRING_DATA_MONGODB_DATABASE=person_database
    

    之后:

      ######################
      #####  Services  #####
      ######################
      person-service:
        container_name: person-service
        image: person:0.0.1-SNAPSHOT
        depends_on:
          - eureka-service
          - gateway-service
        links:
          - person-db
        ports:
          - 8180:8180
        environment:
          - SPRING_PROFILES_ACTIVE=prod
          - SPRING_DATA_MONGODB_URI=mongodb://person-db:27017/person_database
    

    我真的不知道为什么它没有显示真正的错误并尝试使用 localhost 解决它。也许在 Spring 上,它应该在尝试解决之前检查 Mongo 版本

    【讨论】:

      猜你喜欢
      • 2019-07-22
      • 2017-05-03
      • 2021-11-24
      • 2017-08-18
      • 2021-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多