Docker之docker-compose
2019-03-12 本文已影响0人
任嘉平生愿
docker-compose 是一个把 docker 自动化的工具。可以把所有繁复的 docker 操作全都一条命令,自动化的完成。就好比每一个docker容器是一个生产车的机器,但是机器不能自动生产车,需要一系列有序的顺序控制才能完整的生产车,这就是docker-compose。包括从容器的构建到容器间的相互配合使用。
docker+springboot+nginx+mysql实现负载均衡
代码:https://github.com/jiajia154569836/dockercompose-springboot-mysql-nginx
项目目录结构
1.java代码 记录服务器IP和客户端访问IP
@RestController
public class VisitorController {
@Autowired
private VisitorRepository repository;
@RequestMapping("/")
public String index(HttpServletRequest request) throws UnknownHostException {
String ip=request.getRemoteAddr();
Visitor visitor=repository.findByIp(ip);
if(visitor==null){
visitor=new Visitor();
visitor.setIp(ip);
visitor.setTimes(1);
}else {
visitor.setTimes(visitor.getTimes()+1);
}
repository.save(visitor);
InetAddress address = InetAddress.getLocalHost();
//System.out.println(address.getHostName());//主机名
return "当前服务器名:"+address.getHostName()+"当前服务器ip"+address.getHostAddress()+"I have been seen ip "+visitor.getIp()+" "+visitor.getTimes()+" times.";
}
}
2.application-docker.properties
spring.datasource.url=jdbc:mysql://mysql:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
注意:这里的mysql:3306中mysql在docker中是一个服务
3.与app同级目录下新建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: 123456
MYSQL_ROOT_HOST: '%'
ports:
- "3306:3306"
restart: always
app-1:
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
app-2:
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
4.与app同级目录下新建nginx目录结构如下
image.pngapp.conf目录
upstream app {
server dockercomposespringbootmysqlnginx_app-1:8080;
server dockercomposespringbootmysqlnginx_app-2:8080;
}
server {
listen 80;
charset utf-8;
access_log off;
location / {
proxy_pass http://app;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /static {
access_log off;
expires 30d;
alias /app/static;
}
}
注意这里的server dockercomposespringbootmysqlnginx_app-1:8080;
这需要先输入命令:docker ps查看起的springboot项目的名字
当nginx报错的时候就是这里的问题:是项目名字错误因为重新部署的过程中项目名会根据版本在后面_1
5.用winscp工具上传文件
image.png6.这里有个技巧就是先运行一下项目然后将项目需要的jar包下载下来然后将这些jar也复制到对应的目录中去,这样可以节省下在的时间
image.png7.进入dockercompose-springboot-mysql-nginx
cd dockercompose-springboot-mysql-nginx
docker-compose up
然后下面的信息时
image.png
8.在页面输入http://192.168.229.134/ 并多次切换
image.png当发现两个服务器IP不同则实现负载
关闭服务
docker-compose down
至此docker+springboot+nginx+mysql实现负载均衡完成
代码:https://github.com/jiajia154569836/dockercompose-springboot-mysql-nginx