Spring

mysql을 사용하여 Spring Boot 애플리케이션에서 "통신 링크 실패" 문제를 해결하는 방법은 무엇입니까?

기록만이살길 2022. 11. 27. 23:42
반응형

mysql을 사용하여 Spring Boot 애플리케이션에서 "통신 링크 실패" 문제를 해결하는 방법은 무엇입니까?

1. 질문(문제점):

저는 Heroku의 "Getting Started on Heroku with Java"를 기반으로 Spring Boot Application을 개발했습니다 . 그 후 mysql을 데이터베이스로 구성하기 시작했습니다. 이것은 내가 이 접근법을 통해 따랐던 예제입니다. heroku local web. _ GET 요청을 하고 데이터베이스에 저장된 데이터를 가져올 수 있습니다. POST 요청을 만들고 데이터베이스에 새 데이터를 추가할 수 있습니다. 하지만 프로덕션에 배포하면 로그에 예외가 발생합니다. 아래는 Heroku가 제공한 URL(이 경우 https://danef-dictionary-api.herokuapp.com/ )에서 실행 중인 애플리케이션을 시작할 때 표시되는 로그의 관련 부분입니다.

com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
Jan 04 02:24:27 danef-dictionary-api app/web.1 
Jan 04 02:24:27 danef-dictionary-api app/web.1 The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Jan 04 02:24:27 danef-dictionary-api app/web.1  at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.18.jar:8.0.18]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64) ~[mysql-connector-java-8.0.18.jar:8.0.18]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836) ~[mysql-connector-java-8.0.18.jar:8.0.18]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456) ~[mysql-connector-java-8.0.18.jar:8.0.18]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246) ~[mysql-connector-java-8.0.18.jar:8.0.18]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199) ~[mysql-connector-java-8.0.18.jar:8.0.18]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-3.4.1.jar:na]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:353) ~[HikariCP-3.4.1.jar:na]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:201) ~[HikariCP-3.4.1.jar:na]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:473) [HikariCP-3.4.1.jar:na]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:562) [HikariCP-3.4.1.jar:na]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115) [HikariCP-3.4.1.jar:na]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) [HikariCP-3.4.1.jar:na]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) [hibernate-core-5.4.9.Final.jar:5.4.9.Final]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:180) [hibernate-core-5.4.9.Final.jar:5.4.9.Final]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:68) [hibernate-core-5.4.9.Final.jar:5.4.9.Final]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35) [hibernate-core-5.4.9.Final.jar:5.4.9.Final]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:101) [hibernate-core-5.4.9.Final.jar:5.4.9.Final]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263) [hibernate-core-5.4.9.Final.jar:5.4.9.Final]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:237) [hibernate-core-5.4.9.Final.jar:5.4.9.Final]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214) [hibernate-core-5.4.9.Final.jar:5.4.9.Final]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.hibernate.id.factory.internal.DefaultIdentifierGeneratorFactory.injectServices(DefaultIdentifierGeneratorFactory.java:152) [hibernate-core-5.4.9.Final.jar:5.4.9.Final]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.hibernate.service.internal.AbstractServiceRegistryImpl.injectDependencies(AbstractServiceRegistryImpl.java:286) [hibernate-core-5.4.9.Final.jar:5.4.9.Final]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:243) [hibernate-core-5.4.9.Final.jar:5.4.9.Final]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214) [hibernate-core-5.4.9.Final.jar:5.4.9.Final]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.<init>(InFlightMetadataCollectorImpl.java:175) [hibernate-core-5.4.9.Final.jar:5.4.9.Final]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:118) [hibernate-core-5.4.9.Final.jar:5.4.9.Final]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1202) [hibernate-core-5.4.9.Final.jar:5.4.9.Final]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1233) [hibernate-core-5.4.9.Final.jar:5.4.9.Final]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) [spring-orm-5.2.2.RELEASE.jar:5.2.2.RELEASE]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) [spring-orm-5.2.2.RELEASE.jar:5.2.2.RELEASE]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:391) [spring-orm-5.2.2.RELEASE.jar:5.2.2.RELEASE]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:378) [spring-orm-5.2.2.RELEASE.jar:5.2.2.RELEASE]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) [spring-orm-5.2.2.RELEASE.jar:5.2.2.RELEASE]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1855) [spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1792) [spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) [spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) [spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) [spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) [spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) [spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1108) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) ~[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]
Jan 04 02:24:27 danef-dictionary-api app/web.1  at net.danef.DanefDictionaryApiApplication.main(DanefDictionaryApiApplication.java:12) ~[classes/:na]

예외에서 첫 번째 "공통 링크 실패"를 선택하여 이 질문의 제목에 사용했습니다. 나는 해결책으로 찾을 수 있는 모든 것을 시도했을 것이므로 블로그 게시물을 알고 있거나 도움이 된다고 생각하는 답변이 있으면 언제든지 저를 리디렉션하십시오.

내 application.properties 파일:

spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.generate-ddl=true
spring.datasource.url=jdbc:mysql://localhost:3306/db_words?useSSL=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=myusername
spring.datasource.password=mypassword

"myusername" 및 "mypassword"는 자리 표시자일 뿐입니다. 내 코드에 내 자격 증명을 사용하고 있습니다.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>net.danef</groupId>
    <artifactId>danef-dictionary-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>danef-dictionary-api</name>
    <description>Danef Dictionary API</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>3.0.1</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

로그의 전체 버전을 보려면 이 링크를 참조하십시오: https://paste.ubuntu.com/p/wqZPXqyYFM/

2. 해결방안:

Heroku에서 ClearDB 애드온을 설정하여 문제를 해결했습니다. 애드온을 추가한 후 원격 데이터베이스 URL을 갖게 되었습니다. 원격 URL을 datasource.url로 설정하면 문제가 해결되었습니다. 내 application.properties 파일은 이제 다음과 같습니다.

spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.generate-ddl=true
spring.datasource.url=jdbc:mysql://`remote_database_url`
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=samet
spring.datasource.password=password

백틱 없이 원격 데이터베이스 URL을 설정합니다.

반응형