Docker 是一個開源的應用容器引擎,讓開發者能夠打包他們的應用以及依賴包到一個可移植的容器中,而後發佈到任何流行的 Linux 機器上,也能夠實現虛擬化。容器是徹底使用沙箱機制,相互之間不會有任何接口。php
本篇文章引導你使用Docker Compose
在Docker
容器中運行nginx
和兩個簡單的Spring Boot
應用程序,從而實現負載均衡。關於Docker
入門請參考純潔的微笑Docker 系列文章html
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>docker-compose-springboot-nginx</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
複製代碼
package cn.merryyou.docker.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
/** * Created on 2018/3/20 0020. * * @author zlf * @email i@merryyou.cn * @since 1.0 */
@Controller
public class DockerController {
@GetMapping("/")
public String docker() {
return "docker";
}
}
複製代碼
package cn.merryyou.docker;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/** * Created on 2018/3/20 0020. * * @author zlf * @email i@merryyou.cn * @since 1.0 */
@SpringBootApplication
public class DockerApplication {
public static void main(String[] args) {
SpringApplication.run(DockerApplication.class, args);
}
}
複製代碼
<h1>Hello Spring Boot with docker-compose!</h1>
複製代碼
spring:
freemarker:
template-loader-path: classpath:/templates
request-context-attribute: .ftl
複製代碼
FROM hub.c.163.com/wuxukun/maven-aliyun:3-jdk-8
ADD ["app-1.jar", "app.jar"]
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]
複製代碼
upstream app {
server app-1:8080;
server 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;
}
}
複製代碼
version: '2'
services:
nginx:
container_name: some-nginx
image: nginx:1.13
restart: always
ports:
- 80:80
# - 443:443
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d
app-1:
restart: always
build: ./app/docker-app-1
working_dir: /app
volumes:
- ./app:/app
expose:
- "8080"
# command: mvn clean spring-boot:run
depends_on:
- nginx
app-2:
restart: always
build: ./app/docker-app-2
working_dir: /app
volumes:
- ./app:/app
expose:
- "8080"
# command: mvn clean spring-boot:run
depends_on:
- nginx
複製代碼
version: '2'
: 表示使用第二代語法來構建 docker-compose.yaml
文件;services
: 用來表示 compose
須要啓動的服務,咱們能夠看出此文件中有三個服務分別爲:nginx、app-一、app-2。container_name
:容器名稱ports
:表示對外開放的端口restart: always
:表示若是服務啓動不成功會一直嘗試。volumes
:加載本地目錄下的配置文件到容器目標地址下docker-compose-boot-nginx
目錄下執行 docker-compose up
http://localhost
docker-compose up
很慢,可在host
文件中添加127.0.0.1 localunixsocket.local
參考docker logs -f -t --tail 行數 容器名
查看容器的實時日誌從個人 github 中下載,github.com/longfeizhen…nginx
🙂🙂🙂關注微信小程序java架構師歷程 上下班的路上無聊嗎?還在看小說、新聞嗎?不知道怎樣提升本身的技術嗎?來吧這裏有你須要的java架構文章,1.5w+的java工程師都在看,你還在等什麼?git