Докеризация SpringBoot-Микросервис, + Angular + MySQL + RabbitMQ - PullRequest
0 голосов
/ 07 ноября 2019

Я учу докер. При развертывании моего приложения (Angular, SpringBoot, RabbitMQ, MySQL) я могу получить доступ к пользовательскому интерфейсу, но службы перестают работать из-за ошибки соединения между службами и MySQL и RabbitMq. Но по отдельности я могу получить доступ как к MySql, так и к RabbitMq с помощью команды docker exec, а таблицы и очереди генерируются, пока 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

Отдельный файл Docker работает нормально.

Ошибка:

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. Пробовал менять пружину. datasource.url = jdbc: mysql: // mysql: 3306 / stackroute? verifyServerCertificate = false & useSSL = false & requireSSL = false & allowPublicKeyRetrieval = true Получена следующая ошибка: вызвано: java.net.UnknownHostException: mysql

-RR*

2. Удален сетевой_мод: хост получил следующую ошибку

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

Ответы [ 2 ]

1 голос
/ 07 ноября 2019

Вы не можете использовать 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 голос
/ 07 ноября 2019

Я думаю, это потому, что вы используете network_mode = host

Это означает, что вы хотите "повторно использовать" пространство имен из базовой сети хоста.

Попробуйте использовать мост по умолчанию"режим так, что docker создаст" виртуальную сеть "между контейнерами.

Этот режим моста является режимом по умолчанию, поэтому просто удалите строку network_mode: "host" из файла компоновки Docker.

Здесь - это хороший учебник, который описывает поддержку сети Docker. на высоком уровне.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...