使用docker compose的spring boot mysql应用程序问题

00jrzges  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(498)

操作系统详细信息

Mint version 19, 
Code name : Tara,
PackageBase : Ubuntu Bionic
Cinnamon (64-bit)

我按照这个url安装了MySQL5.7和workbench 6.3
我可以检查mysql服务是否正在运行

xxxxxxxxx:~$ sudo netstat -nlpt | grep 3306
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      1547/mysqld

我还检查了目录:/etc/mysql/mysql.conf.d下文件mysqld.cnf中的绑定地址/

bind-address = 127.0.0.1

我用mysql编写了一个简单的spring应用程序,这里所有的类都是用mysql编写的
SpringBootDataJPaExample应用程序

@EnableJpaRepositories(basePackages = "com.springbootdev.examples.repository")
@SpringBootApplication
public class SpringBootDataJpaExampleApplication
{

    public static void main(String[] args) {
        SpringApplication.run(SpringBootDataJpaExampleApplication.class, args);
    }
}

用户控制器

@RestController
@RequestMapping("/api")
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @GetMapping("/create")
    public List<User> users() {
        User users = new User();
        users.setId(new Long(1));
        users.setName("Sam");
        users.setCountry("Development");

        userRepository.save(users);

        return userRepository.findAll();
    }

    @GetMapping("/users")
    public List<User> findAll()
    {
        return userRepository.findAll();
    }
}

用户存储库

public interface UserRepository extends JpaRepository<User, Long>
{

}

用户

@Entity
@Table(name = "user")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    private String country;
    //getters and setters
}

部分pom.xml文件。请看我使用的是SpringBoot1.5.9和JDK1.8

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

应用程序属性

spring.datasource.url = jdbc:mysql://mysql-standalone:3306/test
spring.datasource.username = testuser
spring.datasource.password = testpassword
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

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

spring.jpa.hibernate.ddl-auto = create

dockerfile文件

FROM openjdk:8
VOLUME /tmp
EXPOSE 8080
ADD target/spring-boot-app.jar spring-boot-app.jar
ENTRYPOINT ["java","-jar","spring-boot-app.jar"]

当我使用ide在本地运行此应用程序时,它工作正常,我可以访问:http://localhost:8080/api/users/和http://localhost:8080/api/创建/
我使用命令从应用程序中构建映像

docker build . -t spring-boot-app

我可以看到图像正在建立。

xxxxxxxxxx:$ docker images
REPOSITORY                        TAG                 IMAGE ID            CREATED             SIZE
spring-boot-app                   latest              069e53a7c389        27 minutes ago      652MB
mysql                             5.7                 1b30b36ae96a        8 days ago          372MB

现在我运行命令来运行mysql容器

docker run --name mysql-standalone -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=test -e MYSQL_USER=testuser -e MYSQL_PASSWORD=testpassword -d mysql:5.7

然后我从我的应用程序(spring-boot-app)连接到mysql独立容器(引用这个)

docker run --name spring-boot-app-container --link mysql-standalone:mysql -d spring-boot-app

有了它,我可以看到我的应用程序在docker中使用http://(docker container ip):8080/api/users/和http://(docker container ip):8080/api/create运行良好/
在这里,我想让docker compose做同样的事情。引用此安装docker compose。

xxxxxxxxxx:~$ docker-compose --version
docker-compose version 1.22.0, build f46880fe

然后我在我的项目目录中创建了docker-compose.yml文件。

version: '3'

services:
  mysql-docker-container:
    image: mysql:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=adminpassword
      - MYSQL_DATABASE=test
      - MYSQL_USER=testuser
      - MYSQL_PASSWORD=testpassword
    volumes:
      - /data/mysql
  spring-boot-app-container:
    image: spring-boot-app
    build:
      context: .
      dockerfile: Dockerfile
    depends_on:
      - mysql-docker-container
    ports:
      - 8087:8080
    volumes:
      - /data/spring-boot-app

更改了my application.properties中的一行,并对前面的数据源url进行了注解,如下所示。

spring.datasource.url = jdbc:mysql://mysql-docker-container:3306/test

# spring.datasource.url = jdbc:mysql://mysql-standalone:3306/test

没有进行干净的安装以便创建新的目标jar。我还从docker中删除了图像和容器。
然后运行以下命令:

docker-compose up

这是我在日志中看到的

Creating spring-boot-data-jpa-mysql-docker-no-composer-master_mysql-docker-container_1 ... done
Creating spring-boot-data-jpa-mysql-docker-no-composer-master_spring-boot-app-container_1 ... done
Attaching to spring-boot-data-jpa-mysql-docker-no-composer-master_mysql-docker-container_1, spring-boot-data-jpa-mysql-docker-no-composer-master_spring-boot-app-container_1
spring-boot-app-container_1  | 
spring-boot-app-container_1  |   .   ____          _            __ _ _
spring-boot-app-container_1  |  /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
spring-boot-app-container_1  | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
spring-boot-app-container_1  |  \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
spring-boot-app-container_1  |   '  |____| .__|_| |_|_| |_\__, | / / / /
spring-boot-app-container_1  |  =========|_|==============|___/=/_/_/_/
spring-boot-app-container_1  |  :: Spring Boot ::        (v1.5.9.RELEASE)
spring-boot-app-container_1  | 
spring-boot-app-container_1  | 2018-10-26 01:24:48.748  INFO 1 --- [           main] .s.e.SpringBootDataJpaExampleApplication : Starting SpringBootDataJpaExampleApplication v0.0.1-SNAPSHOT on 582c035536e4 with PID 1 (/spring-boot-app.jar started by root in /)
spring-boot-app-container_1  | 2018-10-26 01:24:48.752  INFO 1 --- [           main] .s.e.SpringBootDataJpaExampleApplication : No active profile set, falling back to default profiles: default
spring-boot-app-container_1  | 2018-10-26 01:24:48.875  INFO 1 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@28c97a5: startup date [Fri Oct 26 01:24:48 UTC 2018]; root of context hierarchy
spring-boot-app-container_1  | 2018-10-26 01:24:51.022  INFO 1 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
spring-boot-app-container_1  | 2018-10-26 01:24:51.063  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
spring-boot-app-container_1  | 2018-10-26 01:24:51.065  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.23
spring-boot-app-container_1  | 2018-10-26 01:24:51.218  INFO 1 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
spring-boot-app-container_1  | 2018-10-26 01:24:51.218  INFO 1 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2399 ms
spring-boot-app-container_1  | 2018-10-26 01:24:51.335  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
spring-boot-app-container_1  | 2018-10-26 01:24:51.340  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
spring-boot-app-container_1  | 2018-10-26 01:24:51.341  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
spring-boot-app-container_1  | 2018-10-26 01:24:51.341  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
spring-boot-app-container_1  | 2018-10-26 01:24:51.341  INFO 1 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
spring-boot-app-container_1  | 2018-10-26 01:24:51.895 ERROR 1 --- [           main] o.a.tomcat.jdbc.pool.ConnectionPool      : Unable to create initial connections of pool.
spring-boot-app-container_1  | 
spring-boot-app-container_1  | com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
spring-boot-app-container_1  | 
spring-boot-app-container_1  | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
spring-boot-app-container_1  |  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_181]

如何修复此错误:com.mysql.jdbc.exceptions.jdbc4.communicationsexception:communications link failure
我可以在这个论坛上看到很多类似的错误信息的答案。尝试了一堆选项/答案,但都没用。没有答案谈论这种组合(linux+spring-boot+mysql+docker-compose)
注意:这在不使用docker compose的情况下运行良好。在上述描述中已经提到了相同的内容。docker compose文件或应用程序属性文件中是否有错误?

3z6pesqy

3z6pesqy1#

如果您使用的是<2.0的spring引导版本,那么我确实看到很多人在pom.xml中添加hikari依赖项

<!-- Spring Boot Data 2.0 includes HikariCP by default -->
  <!-- <dependency>
         <groupId>com.zaxxer</groupId>
         <artifactId>HikariCP</artifactId>
         <version>3.1.0</version>
  </dependency> -->

因此,我考虑使用相同的应用程序,但对pom.xml做了如下更改

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

然后,我按照问题描述中提到的完全相同的事情。我看到了更明显的错误,如下所示:

spring-boot-app-container_1  | 2018-10-27 18:51:47.259  INFO 1 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
spring-boot-app-container_1  | 2018-10-27 18:51:48.464 ERROR 1 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization.
spring-boot-app-container_1  | 
spring-boot-app-container_1  | com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
spring-boot-app-container_1  | 
spring-boot-app-container_1  | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

这证实了springboot2.0使用hikari数据源默认值。
现在回到我是如何解决问题的。我在application.properties中更改了连接字符串,如下所示:

spring.datasource.url = jdbc:mysql://mysql-docker-container:3306/test?autoReconnect=true&failOverReadOnly=false&maxReconnects=10&useSSL=false

代替先前使用的:

spring.datasource.url = jdbc:mysql://mysql-docker-container:3306/test

答案很简单。下面的更改在springboot2.0和springboot1.5.9中对我起了作用:(将此添加到连接字符串中)
?autoreconnect=真&failoverreadonly=假&maxreconnects=10
一些方便的命令:
容器启动后,可以使用以下命令检查容器的ip地址:

docker inspect -f '{{.Name}} - {{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $(docker ps -aq)

========================================
更新:一些额外的便利信息。。。
以下内容已弃用

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

这需要用

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

若要修复hikaricp池初始化问题/异常,请将hikaricp的initializationfailtimeout属性设置为0(零)或负数。


# HikaryCP Properties

spring.datasource.hikari.initialization-fail-timeout=0

此属性控制如果无法成功使用初始连接对池进行种子设定,则池是否会“快速失败”。任何正数都被认为是尝试获取初始连接的毫秒数;在此期间,应用程序线程将被阻止。如果在此超时发生之前无法获取连接,将引发异常。此超时在connectiontimeout时段之后应用。如果该值为零(0),hikaricp将尝试获取并验证连接。如果获得了连接,但验证失败,则会引发异常,并且池不会启动。但是,如果无法获取连接,则池将启动,但稍后获取连接的努力可能会失败。小于零的值将绕过任何初始连接尝试,并且池将在尝试在后台获取连接时立即启动。因此,以后获得连接的努力可能会失败。默认值:1

相关问题