return "ip:"+visitor.getIp()+" "+visitor.getTimes()+" times.";
}
}
实体类
@Entity
public class Visitor {
@Id
@GeneratedValue
private Long id;
@Column(nullable=false)
private Long times;
@Column(nullable=false)
private String ip;
// get,set 方法略
}
Repository 层代码参考jpa 相关内容。
本地数据库打开,密码是上面配置中的,使用mvn spring-boot:run运行起来之后,可以看到ip的次数,每次统计后就自增。
dockercompose配置文件
新建docker-compose.yaml文件,如下:
version: '3'
services:
nginx:
container_name: v-nginx
image: nginx:1.13
restart: always
ports:
- 80:80
- 443:443
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d
mysql:
container_name: v-mysql
image: mysql/mysql-server:5.7
environment:
MYSQL_DATABASE: test
MYSQL_ROOT_PASSWORD: root
MYSQL_ROOT_HOST: '%'
ports:
- "3306:3306"
volumes:
- ./mysqldata:/var/lib/mysql
restart: always app:
restart: always
build: ./app
working_dir: /app
volumes:
- ./app:/app
- ~/.m2:/root/.m2
expose:
- "8080"
depends_on:
- nginx
- mysql
command: mvn clean spring-boot:run -Dspring-boot.run.profiles=docker
主要对这个配置文件进行解释,并在文件系统中增加相关的配置。
services下面有三个服务nginx,mysql,app。
images 指明使用镜像。nginx及mysql都是直接取docker仓库中已有的。
app中没有指明镜像,但用build指定了Dockerfile所在的目录。
volumes 指定了本地目录下的文件与容器目标地址的映射。
environment 配置了容器所需要的环境变量
ports 配置了本地与容器的映射的端口,本地端口在前,容器端口在后
ngixn下的volumes配置的作用:把我们写好的nginx配置文件直接覆盖到容器中默认的nginx配置文件。
mysql下的volumes配置的作用:把mysql的数据文件映射到了本地mysqldata目录下。当容器删除后,数据还在。
app下的volumes配置的作用:第一行是把代码文件映射到容器中。第二行是把maven的仓库文件映射到本地。容器删除之后,再构建,不用重新下载依赖包。
command: mvn clean spring-boot:run -Dspring-boot.run.profiles=docker命令是编译运行容器中的项目,使用docker的profiles。
所以我们要添加的文件
Dockerfile:新建文件,添加一行FROM maven:3.5-jdk-8
docker的profiles:复制application.properties为application-docker.properties,并把application-docker.properties中数据库连接地址改为jdbc:mysql://mysql:3306/test。










