参考官方后端开发手册启动api-gateway测试时连接mysql失败

  • Choerodon平台版本:0.18.0,但是api-gateway的镜像是:registry.cn-shanghai.aliyuncs.com/choerodon/api-gateway:0.17.1

  • 运行环境(如localhost或k8s):本地Linux虚拟机

  • 遇到问题时的前置条件:使用docker-compose.yaml文件up -d启动,启动时所需文件都在执行的目录下

  • 问题描述:
    启动服务命令:docker-compose up -d
    docker-compose.yaml文件如下:

version: "3"
services:
  mysql:
    container_name: mysql
    hostname: mysql
    image: registry.cn-hangzhou.aliyuncs.com/choerodon-tools/mysql:5.7.17
    ports:
    - "3306:3306"
    environment:
      MYSQL_ROOT_PASSWORD: root
    volumes:
    - ./mysql/mysql_data:/var/lib/mysql
    - ./mysql/mysql_db.cnf:/etc/mysql/conf.d/mysql_db.cnf
    - ./mysql/init_user.sql:/docker-entrypoint-initdb.d/init_user.sql
    expose:
    - "3306"
    networks:
    - "c7nNetwork"
  redis:
    container_name: redis
    hostname: redis
    image: redis:4.0.11
    ports:
    - "6379:6379"
    expose:
    - "6379"
    networks:
    - "c7nNetwork"
  eureka-server:
    container_name: eureka-server
    hostname: eureka-server
    image: registry.choerodon.com.cn/choerodon-framework/eureka-server:0.9.0
    ports:
    - "8000:8000"
    environment:
    - EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://eureka-server:8000/eureka/
    - EUREKA_CLIENT_REGISTER_WITH_EUREKA=false
    - EUREKA_CLIENT_FETCH_REGISTRY=false
    - LOGGING_LEVEL_ROOT=WARN
    - JAVA_OPTS=-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -Xms256M -Xmx512M
    expose:
    - "8000"
    networks:
    - "c7nNetwork"
  api-gateway:
    container_name: api-gateway
    hostname: api-gateway
    image: registry.cn-shanghai.aliyuncs.com/choerodon/api-gateway:0.17.1
    links: 
    - eureka-server
    depends_on:
    - eureka-server
    ports:
    - "8080:8080"
    environment:
    - SPRING_CLOUD_CONFIG_ENABLED=false
    - HYSTRIX_STREAM_QUEUE_ENABLED=false
    - SPRING_CLOUD_BUS_ENABLED=false
    - SPRING_SLEUTH_STREAM_ENABLED=false
    - LOGGING_LEVEL_ROOT=WARN
    - EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://eureka-server:8000/eureka/
    - ZUUL_ADDHOSTHEADER=true
    - SPRING_APPLICATION_JSON={"zuul":{"routes":{"dev":{"path":"/todo/**", "serviceId":"choerodon-todo-service"}}}}
    - JAVA_OPTS=-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -Xms512M -Xmx768M
    expose:
    - "8080"
    networks:
    - "c7nNetwork"
  iam-service:
    container_name: iam-service
    image: registry.cn-shanghai.aliyuncs.com/choerodon/iam-service:0.17.1
    depends_on:
    - eureka-server
    - mysql
    links: 
    - eureka-server
    - mysql
    ports:
    - "8030:8030"
    environment:
    - SPRING_CLOUD_CONFIG_ENABLED=false
    - HYSTRIX_STREAM_QUEUE_ENABLED=false
    - SPRING_CLOUD_BUS_ENABLED=false
    - SPRING_SLEUTH_STREAM_ENABLED=false
    - LOGGING_LEVEL_ROOT=WARN
    - EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://eureka-server:8000/eureka/
    - SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/iam_service?useUnicode=true&characterEncoding=utf-8&useSSL=false
    - SPRING_DATASOURCE_USERNAME=choerodon
    - SPRING_DATASOURCE_PASSWORD=123456
    - CHOERODON_SAGA_CONSUMER_ENABLED=false
    - CHOERODON_SCHEDULE_CONSUMER_ENABLED=false
    - JAVA_OPTS=-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -Xms512M -Xmx768M
    networks:
    - "c7nNetwork"
  manager-service:
    container_name: manager-service
    image: registry.cn-shanghai.aliyuncs.com/choerodon/manager-service:0.17.1
    depends_on:
    - eureka-server
    - mysql
    links: 
    - eureka-server
    - mysql
    ports:
    - "8963:8963"
    environment:
    - SPRING_CLOUD_CONFIG_ENABLED=false
    - HYSTRIX_STREAM_QUEUE_ENABLED=false
    - SPRING_CLOUD_BUS_ENABLED=false
    - SPRING_SLEUTH_STREAM_ENABLED=false
    - LOGGING_LEVEL_ROOT=WARN
    - EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://eureka-server:8000/eureka/
    - SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/manager_service?useUnicode=true&characterEncoding=utf-8&useSSL=false
    - SPRING_DATASOURCE_USERNAME=choerodon
    - SPRING_DATASOURCE_PASSWORD=123456
    - CHOERODON_SWAGGER_CLIENT=client
    - CHOERODON_SWAGGER_OAUTH_URL=http://api-gateway:8080/oauth/oauth/authorize
    - CHOERODON_GATEWAY_DOMAIN=api-gateway:8080
    - JAVA_OPTS=-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -Xms512M -Xmx768M
    networks:
    - "c7nNetwork"
  oauth-server:
    container_name: oauth-server
    image: registry.cn-shanghai.aliyuncs.com/choerodon/oauth-server:0.17.1
    depends_on:
    - eureka-server
    - redis
    - mysql
    links: 
    - eureka-server
    - redis
    - mysql
    ports:
    - "8020:8020"
    environment:
    - SPRING_CLOUD_CONFIG_ENABLED=false
    - HYSTRIX_STREAM_QUEUE_ENABLED=false
    - SPRING_CLOUD_BUS_ENABLED=false
    - SPRING_SLEUTH_STREAM_ENABLED=false
    - LOGGING_LEVEL_ROOT=WARN
    - EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://eureka-server:8000/eureka/
    - SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/iam_service?useUnicode=true&characterEncoding=utf-8&useSSL=false
    - SPRING_DATASOURCE_USERNAME=choerodon
    - SPRING_DATASOURCE_PASSWORD=123456
    - SPRINT_REDIS_HOST=redis
    - SPRING_REDIS_PORT=6379
    - SPRING_REDIS_DATABASE=4
    - CHOERODON_OAUTH_LOGIN_SSL=false
    - CHOERODON_OAUTH_LOGIN_PATH=/login
    - CHOERODON_DEFAULT_REDIRECT_URL=http://localhost:9000
    - JAVA_OPTS=-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -Xms512M -Xmx768M
    networks:
    - "c7nNetwork"
networks:
  c7nNetwork:
    driver: bridge
  • 报错信息(请尽量使用代码块的形式展现):
    api-gateway容器日志:
    at io.undertow.servlet.core.DeploymentManagerImpl$1.call(DeploymentManagerImpl.java:191) ~[undertow-servlet-1.4.26.Final.jar!/:1.4.26.Final]
     at io.undertow.servlet.core.DeploymentManagerImpl$1.call(DeploymentManagerImpl.java:173) ~[undertow-servlet-1.4.26.Final.jar!/:1.4.26.Final]
     at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:42) ~[undertow-servlet-1.4.26.Final.jar!/:1.4.26.Final]
     at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43) ~[undertow-servlet-1.4.26.Final.jar!/:1.4.26.Final]
     at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:238) ~[undertow-servlet-1.4.26.Final.jar!/:1.4.26.Final]
     at org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory.createDeploymentManager(UndertowServletWebServerFactory.java:284) ~[spring-boot-2.0.6.RELEASE.jar!/:2.0.6.RELEASE]
     at org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory.getWebServer(UndertowServletWebServerFactory.java:208) ~[spring-boot-2.0.6.RELEASE.jar!/:2.0.6.RELEASE]
     at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:179) ~[spring-boot-2.0.6.RELEASE.jar!/:2.0.6.RELEASE]
     at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:152) ~[spring-boot-2.0.6.RELEASE.jar!/:2.0.6.RELEASE]
     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-5.0.10.RELEASE.jar!/:5.0.10.RELEASE]
     at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.6.RELEASE.jar!/:2.0.6.RELEASE]
     at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:754) ~[spring-boot-2.0.6.RELEASE.jar!/:2.0.6.RELEASE]
     at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386) ~[spring-boot-2.0.6.RELEASE.jar!/:2.0.6.RELEASE]
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) ~[spring-boot-2.0.6.RELEASE.jar!/:2.0.6.RELEASE]
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1242) ~[spring-boot-2.0.6.RELEASE.jar!/:2.0.6.RELEASE]
     at org.springframework.boot.SpringApplication.run(SpringApplication.java:1230) ~[spring-boot-2.0.6.RELEASE.jar!/:2.0.6.RELEASE]
     at io.choerodon.gateway.GatewayApplication.main(GatewayApplication.java:26) ~[classes!/:0.17.1]
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_202]
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_202]
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_202]
     at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_202]
     at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) ~[api-gateway.jar:0.17.1]
     at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) ~[api-gateway.jar:0.17.1]
     at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) ~[api-gateway.jar:0.17.1]
     at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) ~[api-gateway.jar:0.17.1]
    

Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_202]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_202]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_202]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_202]
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_202]
at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_202]
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211) ~[mysql-connector-java-5.1.47.jar!/:5.1.47]
at com.mysql.jdbc.MysqlIO.(MysqlIO.java:301) ~[mysql-connector-java-5.1.47.jar!/:5.1.47]
… 165 common frames omitted

2019-10-28 16:13:13.551 ERROR 7 — [ main] o.a.i.mapping.VendorDatabaseIdProvider : Could not get a databaseId from dataSource

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_202]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_202]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_202]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_202]

* 原因分析:
> mysql确定没问题,因为之前单独启动后端单体服务的时候是可以正常连接的。

* 疑问:
> mysql是再同一个docker-comose文件中定义的,为什么会连接不上?