0%

Docker-Compose-SpringBoot-Mysql-Redis

在Java的技术栈中可以使用maven插件来构建docker镜像,用起来没有docker-compose好用。

实现目标

  1. spring boot 、mysql 、redis 定义正常运行。
  2. 解决docker compose 启动顺序问题。
  3. 解决配置文件读取其它容器ip问题。

源码地址

演示效果

docker运行情况

spring boot运行情况

1
2
➜  docker-run curl http://127.0.0.1:9000/docker/
{"code":"200","data":"[information_schema, docker_app_1, mysql, performance_schema, sys]","cache":{"redis_version": "5.0.5"}}

构建项目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
.
├── Dockerfile # Dockerfile
├── docker-compose.yml # docker-compose文件
├── docker-config # docekr配置文件
│   ├── mysql
│   │   ├── init
│   │   │   └── 1_init.sql # mysql数据库初始化文件
│   │   └── my.cnf # mysql 配置文件
│   ├── pwd.txt
│   └── redis
│   └── redis.conf # redis 配置文件
├── pom.xml # java maven 项目依赖
├── src
│   ├── main
│   │   ├── java
│   │   │   └── cn
│   │   │   └── z201
│   │   │   └── docker
│   │   │   ├── AppApplication.java # spring boot 启动类
│   │   │   └── AppApplicationController.java # demo接口类
│   │   └── resources
│   │   ├── application-dev.yml # dev环境下配置文件
│   │   ├── application-test.yml # test环境下配置文件
│   │   ├── application.yml # spring boot 配置文件
│   │   └── logback.xml # 日志输出文件
│   └── test
│   └── java
└── target
├── Docker-Compose-SpringBoot-Mysql-Redis-1.0.0-SNAPSHOT.jar # mvn install 构建多产物

spring boot 项目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package cn.z201.docker;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
* @author z201.coding@gmail.com
**/
@RestController
public class AppApplicationController {

@Autowired
private JdbcTemplate jdbcTemplate;

@Autowired
private RedisTemplate redisTemplate;

@RequestMapping(value = "")
public Object index() {
List<String> dataBasesList = jdbcTemplate.queryForList("SHOW DATABASES", String.class);
Properties info = redisTemplate.getConnectionFactory().getConnection().info();
Map<String, Object> data = new HashMap<>();
data.put("code", "200");
data.put("db", dataBasesList.toString());
data.put("cache", info);
return data;
}
}


package cn.z201.docker;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

/**
* @author z201.coding@gmail.com
*/
@SpringBootApplication
public class AppApplication {

public static void main(String[] args) {
ApplicationContext applicationContext = SpringApplication.run(AppApplication.class, args);
}
}

  • 配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
spring:
application:
name: spring-boot-mysql-redis
profiles:
active: dev
mvc:
throw-exception-if-no-handler-found: true # 处理404
web:
resources:
add-mappings: false # 关闭资源映射
server:
port: 9000
servlet:
context-path: /docker

logging:
config: classpath:logback.xml


  • test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
spring:
datasource:
url: jdbc:mysql://mysql:3306/docker_app_1?useSSL=false&useUnicode=true&autoReconnect=true&failOverReadOnly=false&characterEncoding=utf-8
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
type: com.zaxxer.hikari.HikariDataSource
hikari: # https://github.com/brettwooldridge/HikariCP (uses milliseconds for all time values)
maximumPoolSize: 20 # 连接池最大连接数,默认是10
minimumIdle: 5 # 最小空闲连接数量
idleTimeout: 600000 # 空闲连接存活最大时间,默认600000(10分钟)
connectionTimeout: 30000 # 数据库连接超时时间,默认30秒,即30000
maxLifetime: 1800000 # 此属性控制池中连接的最长生命周期,值0表示无限生命周期,默认1800000即30分钟
redis:
host: redis
port: 6379
password: root
database: 0
lettuce:
pool:
max-active: 8 # 连接池最大连接数(使用负值表示没有限制)
max-wait: 2000 # 连接池最大阻塞等待时间(使用负值表示没有限制)
min-idle: 0 # 连接池中的最小空闲连接
max-idle: 8 # 连接池中的最大空闲连接
shutdown-timeout: 100 # 关闭超时时间
timeout: 60s


  • 这里需要注意在mysql和redis连接上并没有写ip而是docker容器互联的功能,在同一个网桥中使用links互通多个容器的网络。

  • dev的环境都是本地环境,可以自己搭建mysql、redis环境。

docket-compose配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
version : '3'

networks:
network-docker-app:
driver: bridge

services:
web:
container_name: cn.z201.docker-app
build:
context: .
dockerfile: .
image: cn.z201.docker-app-3
networks:
- network-docker-app
expose:
- '9000'
ports:
- '9000:9000'
depends_on: # 等待其它服务启动完成
- mysql
- redis
links:
- mysql
- redis
mysql:
image: mysql:5.7
container_name: mysql5.7-dev-3
networks:
- network-docker-app
expose:
- '3306'
ports:
- '3306:3306'
volumes:
- ./docker-config/mysql/my.cnf:/etc/mysql/my.cnf # 映射数据库配置文件
- ./docker-config/mysql/init:/docker-entrypoint-initdb.d # 初始化数据库
command: [
'--character-set-server=utf8mb4',
'--collation-server=utf8mb4_unicode_ci',
'--lower_case_table_names=1',
'--default-time-zone=+8:00']
environment:
- MYSQL_ROOT_PASSWORD=root # 设置root密码
healthcheck:
test: "/bin/netstat -anpt|grep 3306"
interval: 30s
timeout: 3s
retries: 1
redis:
image: redis:5.0.5
container_name: redis5.0.6-dev-3
networks:
- network-docker-app
expose:
- '6379'
ports:
- '6379:6379'
volumes:
- ./docker-config/redis/redis.conf:/etc/redis.conf # 映射数据库配置文件
command: redis-server /etc/redis.conf # 启动redis命令
healthcheck:
test: "/bin/netstat -anpt|grep 6379"
interval: 30s
timeout: 3s
retries: 1

END