SpringBoot-微服务的Docker化,+ Angular + MySQL + RabbitMQ

问题描述 投票:0回答:2

我正在学习docker。在部署我的应用程序(Angular,SpringBoot,RabbitMQ,MySQL)时,我能够访问UI,但是由于服务与MySQL和RabbitMq之间的连接错误而导致服务失败。但是单独地,我能够通过docker exec命令访问MySql和RabbitMq,同时在docker-compose期间生成表和队列。但是在测试时,它不起作用

docker-compose.yml

version: '3.1'
services:
  rabbitmq:
    image: rabbitmq:management
    ports:
      - 5672:5672
      - 15672:15672

  mysql:
    image: mysql:8.0.17
    ports:
      - 3306:3306
    environment:
      MYSQL_DATABASE: stackroute
      MYSQL_ROOT_PASSWORD: password
      MYSQL_USER: cmatches
      MYSQL_PASSWORD: cmatches

  user-app:
    image: userservice
    build: userservice/
    ports:
      - 9100:9100
    depends_on:
      - mysql
    network_mode: "host"

  fav-app:
    image: favouriteservice
    build: favouriteservice/
    ports:
      - 9200:9200
    depends_on:
      - mysql
      - rabbitmq
    network_mode: "host"

  rec-app:
    image: matchrecommendationservice
    build: matchrecommendationservice/
    ports:
      - 9300:9300
    depends_on:
      - mysql
      - rabbitmq
    network_mode: "host"

  cmatches-ui:
    build: CMatchesUI/
    container_name: cmatchesUi
    ports:
      - 4200:4200

单个dockerfile运行正常。

错误:

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

我需要在application.properties中进行一些更改

server.port=9200

spring.datasource.url=jdbc:mysql://localhost:3306/stackroute?verifyServerCertificate=false&useSSL=false&requireSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=password

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

spring.jpa.hibernate.ddl-auto = update

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

spring.cloud.stream.bindings.recommendedMatchesChannel.destination=recommendedMatches
spring.cloud.stream.default.contentType=application/json

================================================ ========================更新:1。尝试改变spring.datasource.url = jdbc:mysql:// mysql:3306 / stackroute?verifyServerCertificate = false&useSSL = false&requireSSL = false&allowPublicKeyRetrieval = true得到以下错误:造成原因:java.net.UnknownHostException:mysql

-还原-

2。删除network_mode:主机出现以下错误

com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
user-app_1     | 
user-app_1     | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
user-app_1     |    at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.17.jar!/:8.0.17]
user-app_1     |    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.17.jar!/:8.0.17]
user-app_1     |    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:827) ~[mysql-connector-java-8.0.17.jar!/:8.0.17]
user-app_1     |    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:447) ~[mysql-connector-java-8.0.17.jar!/:8.0.17]
user-app_1     |    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:237) ~[mysql-connector-java-8.0.17.jar!/:8.0.17]
user-app_1     |    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199) ~[mysql-connector-java-8.0.17.jar!/:8.0.17]
user-app_1     |    at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136) ~[HikariCP-3.2.0.jar!/:na]
user-app_1     |    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369) ~[HikariCP-3.2.0.jar!/:na]
user-app_1     |    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198) ~[HikariCP-3.2.0.jar!/:na]
user-app_1     |    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467) [HikariCP-3.2.0.jar!/:na]
user-app_1     |    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:541) [HikariCP-3.2.0.jar!/:na]
user-app_1     |    at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) [HikariCP-3.2.0.jar!/:na]
user-app_1     |    at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) [HikariCP-3.2.0.jar!/:na]
user-app_1     |    at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) [hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
user-app_1     |    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180) [hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
user-app_1     |    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:68) [hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
user-app_1     |    at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35) [hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
user-app_1     |    at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:94) [hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
user-app_1     |    at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263) [hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
user-app_1     |    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237) [hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
user-app_1     |    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214) [hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
user-app_1     |    at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.injectServices(DefaultIdentifierGeneratorFactory.java:152) [hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
user-app_1     |    at org.hibernate.service.internal.AbstractServiceRegistryImpl.injectDependencies(AbstractServiceRegistryImpl.java:286) [hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
user-app_1     |    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:243) [hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
user-app_1     |    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214) [hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
user-app_1     |    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.<init>(InFlightMetadataCollectorImpl.java:179) [hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
user-app_1     |    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:119) [hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
user-app_1     |    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:904) [hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
user-app_1     |    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:935) [hibernate-core-5.3.7.Final.jar!/:5.3.7.Final]
user-app_1     |    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:57) [spring-orm-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
user-app_1     |    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) [spring-orm-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
user-app_1     |    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:390) [spring-orm-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
user-app_1     |    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:377) [spring-orm-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
user-app_1     |    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) [spring-orm-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
user-app_1     |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1804) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
user-app_1     |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1741) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
user-app_1     |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
user-app_1     |    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
user-app_1     |    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
user-app_1     |    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
user-app_1     |    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
user-app_1     |    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) [spring-beans-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
user-app_1     |    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1083) ~[spring-context-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
user-app_1     |    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:853) ~[spring-context-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
user-app_1     |    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
user-app_1     |    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
user-app_1     |    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
user-app_1     |    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
user-app_1     |    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
user-app_1     |    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
user-app_1     |    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
user-app_1     |    at com.stackroute.userservice.UserServiceApplication.main(UserServiceApplication.java:10) ~[classes!/:1.0]
user-app_1     |    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_111]
user-app_1     |    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_111]
user-app_1     |    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_111]
user-app_1     |    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_111]
user-app_1     |    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) ~[userservice-1.0.jar:1.0]
user-app_1     |    at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) ~[userservice-1.0.jar:1.0]
user-app_1     |    at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) ~[userservice-1.0.jar:1.0]
user-app_1     |    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) ~[userservice-1.0.jar:1.0]
user-app_1     | Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
user-app_1     | 
user-app_1     | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
user-app_1     |    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_111]
user-app_1     |    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_111]
user-app_1     |    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_111]
user-app_1     |    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_111]
user-app_1     |    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.17.jar!/:8.0.17]
user-app_1     |    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.17.jar!/:8.0.17]
user-app_1     |    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.17.jar!/:8.0.17]
user-app_1     |    at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.17.jar!/:8.0.17]
user-app_1     |    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91) ~[mysql-connector-java-8.0.17.jar!/:8.0.17]
user-app_1     |    at com.mysql.cj.NativeSession.connect(NativeSession.java:150) ~[mysql-connector-java-8.0.17.jar!/:8.0.17]
user-app_1     |    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:947) ~[mysql-connector-java-8.0.17.jar!/:8.0.17]
user-app_1     |    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:817) ~[mysql-connector-java-8.0.17.jar!/:8.0.17]
user-app_1     |    ... 57 common frames omitted
user-app_1     | Caused by: java.net.ConnectException: Connection refused (Connection refused)
user-app_1     |    at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_111]
user-app_1     |    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_111]
user-app_1     |    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_111]
user-app_1     |    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_111]
user-app_1     |    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_111]
user-app_1     |    at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_111]
user-app_1     |    at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155) ~[mysql-connector-java-8.0.17.jar!/:8.0.17]
user-app_1     |    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65) ~[mysql-connector-java-8.0.17.jar!/:8.0.17]
user-app_1     |    ... 60 common frames omitted
spring spring-boot docker docker-compose rabbitmq
2个回答
1
投票

由于应用程序在docker容器中运行,因此您不能使用localhost作为主机名。另外,为了彼此通信,所有容器都应该在同一个docker网络中;

version: '3.1'
services:
  rabbitmq:
    image: rabbitmq:management
    container_name: rabbitmq
    ports:
      - 5672:5672
      - 15672:15672
    networks:
      - my-net

  mysql:
    image: mysql:8.0.17
    container_name: mysql
    ports:
      - 3306:3306
    environment:
      MYSQL_DATABASE: stackroute
      MYSQL_ROOT_PASSWORD: password
      MYSQL_USER: cmatches
      MYSQL_PASSWORD: cmatches
    networks:
      - my-net

  user-app:
    image: userservice
    build: userservice/
    container_name: userapp
    ports:
      - 9100:9100
    depends_on:
      - mysql
    networks:
      - my-net

  fav-app:
    image: favouriteservice
    build: favouriteservice/
    container_name: favapp
    ports:
      - 9200:9200
    depends_on:
      - mysql
      - rabbitmq
    networks:
      - my-net

  rec-app:
    image: matchrecommendationservice
    build: matchrecommendationservice/
    container_name: recapp
    ports:
      - 9300:9300
    depends_on:
      - mysql
      - rabbitmq
    networks:
      - my-net

  cmatches-ui:
    build: CMatchesUI/
    container_name: cmatchesUi
    ports:
      - 4200:4200
    networks:
      - my-net

networks:
    my-net:

将主机名在所有位置替换为它们各自的容器名称;

server.port=9200

spring.datasource.url=jdbc:mysql://mysql:3306/stackroute?verifyServerCertificate=false&useSSL=false&requireSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=password

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

spring.jpa.hibernate.ddl-auto = update

spring.rabbitmq.host=rabbitmq
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

spring.cloud.stream.bindings.recommendedMatchesChannel.destination=recommendedMatches
spring.cloud.stream.default.contentType=application/json

1
投票

我认为这是因为您使用了network_mode = host

这意味着您想“重用”底层主机网络中的名称空间。

尝试使用默认的“桥接”模式,以便docker在容器之间创建一个“虚拟网络”。

此桥接模式是默认模式,因此只需从docker compose文件中删除network_mode: "host"行。

Here是一个很好的教程,从较高的角度描述了Docker网络的支持。

© www.soinside.com 2019 - 2024. All rights reserved.