Spring Boot 2 - Docker (二):Docker Compose + Spring Boot + Nginx + Mysql 實踐

Spring Boot + Nginx + Mysql 是實際工作中最常用的一個組合,最前端使用 Nginx 代理請求轉(zhuǎn)發(fā)到后端 Spring Boot 內(nèi)嵌的 Tomcat 服務(wù),Mysql 負責業(yè)務(wù)中數(shù)據(jù)相關(guān)的交互,那么在沒有 docker 之前,我們是如何來搞定這些環(huán)境的呢?

  • 1、安裝 Nginx,配置 Nginx 相關(guān)信息,重啟。
  • 2、安裝 Mysql ,配置字符集時區(qū)等信息,重啟,最后初始化腳本。
  • 3、啟動 Spring Boot 項目,整體進行聯(lián)調(diào)測試。

大家看我只寫了三行,但其實搭建這些環(huán)境的時候還挺費事的,但這還不是結(jié)局,在用了一段時間時候需要遷移到另外一個環(huán)境,怎么辦又需要重新搞一次?正常情況下,測試環(huán)境、SIT 環(huán)境、UAT 環(huán)境、生產(chǎn)環(huán)境!我們需要重復搭建四次。有人說不就是搭建四次嗎?也沒什么大不了的,那么我想告訴你,Too yong ,Too Simple 。

image

讓我們看看以下幾個因素:

第一,這只是一個最簡單的案例,如果項目涉及到 MongoDB、Redis、ES … 一些列的環(huán)境呢? 第二,如果你經(jīng)常搭建環(huán)境或者調(diào)試程序,你就會知道什么是環(huán)境問題?有的時候明明是一模一樣的配置,但是到了另外一個環(huán)境就是跑不起來。于是你花費很多時間來查找,最后才發(fā)現(xiàn)是少了一個參數(shù)或者逗號的問題,或者是系統(tǒng)內(nèi)核版本不一致、或者你最后也沒搞懂是為什么!只能再換另外一臺服務(wù)器,那么使用 Docker 呢就可以完美的避開這些坑。

好了,廢話不多說我們就開始吧!

Spring Boot 案例

首先我們先準備一個 Spring Boot 使用 Mysql 的小場景,我們做這樣一個示例,使用 Spring Boot 做一個 Web 應用,提供一個按照 IP 地址統(tǒng)計訪問次數(shù)的方法,每次請求時將統(tǒng)計數(shù)據(jù)存入 Mysql 并展示到頁面中。

配置信息

依賴包

<dependencies>
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

主要添加了 Spring Boot Web 支持,使用 Jpa 操作數(shù)據(jù)庫、添加 Myql 驅(qū)動包等。

配置文件

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true

配置了數(shù)據(jù)庫的鏈接信息,以及 Jpa 更新表模式、方言和是否顯示Sql

核心代碼

核心代碼很簡單,每過來一個請求,判斷是否已經(jīng)統(tǒng)計過,如果沒有統(tǒng)計新增數(shù)據(jù),如果有統(tǒng)計數(shù)據(jù)更新數(shù)據(jù)。

@RestController
public class VisitorController {

    @Autowired
    private VisitorRepository repository;

    @RequestMapping("/")
    public String index(HttpServletRequest request) {
        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);
        return "I have been seen ip "+visitor.getIp()+" "+visitor.getTimes()+" times.";
    }
}

實體類和 Repository 層代碼比較簡單,這里就不貼出來了,大家感興趣可以下載源碼查看。

以上內(nèi)容都完成后,啟動項目,訪問:http://localhost:8080/ 我們就可以看到這樣的返回結(jié)果:

I have been seen ip 0:0:0:0:0:0:0:1 1 times.

再訪問一次會變成

I have been seen ip 0:0:0:0:0:0:0:1 2 times.

多次訪問一直疊加,說明演示項目開發(fā)完成。

Docker 化改造

首先我們將目錄改造成這樣一個結(jié)構(gòu)

image

我們先從最外層說起:

  • docker-compose.yaml:docker-compose 的核心文件,描述如何構(gòu)建整個服務(wù)
  • nginx:有關(guān) nginx 的配置
  • app:Spring Boot 項目地址

如果我們需要對 Mysql 有特殊的定制,也可以在最外層創(chuàng)建 mysql 文件夾,在此目錄下進行配置。

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"
   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
  • version: '3': 表示使用第三代語法來構(gòu)建 docker-compose.yaml 文件。
  • services: 用來表示 compose 需要啟動的服務(wù),我們可以看出此文件中有三個服務(wù)分別為:nginx、mysql、app。
  • container_name: 容器名稱
  • environment: 此節(jié)點下的信息會當作環(huán)境變量傳入容器,此示例中 mysql 服務(wù)配置了數(shù)據(jù)庫、密碼和權(quán)限信息。
  • ports: 表示對外開放的端口
  • restart: always 表示如果服務(wù)啟動不成功會一直嘗試。
  • volumes: 加載本地目錄下的配置文件到容器目標地址下
  • depends_on:可以配置依賴服務(wù),表示需要先啟動 depends_on 下面的服務(wù)后,再啟動本服務(wù)。
  • command: mvn clean spring-boot:run -Dspring-boot.run.profiles=docker: 表示以這個命令來啟動項目,-Dspring-boot.run.profiles=docker表示使用 application-docker.properties文件配置信息進行啟動。

Nginx 文件解讀

nginx 在目錄下有一個文件 app.conf,主要配置了服務(wù)轉(zhuǎn)發(fā)信息

server {
    listen 80;
    charset utf-8;
    access_log off;

    location / {
        proxy_pass http://app:8080;
        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;
    }
}

這塊內(nèi)容比較簡單,配置請求轉(zhuǎn)發(fā),將80端口的請求轉(zhuǎn)發(fā)到服務(wù) app 的8080端口。其中proxy_pass http://app:8080這塊的配置信息需要解釋一下,這里使用是app而不是localhost,是因為他們沒有在一個容器中,在一組 compose 的服務(wù)通訊需要使用 services 的名稱進行訪問。

Spring Boot 項目改造

app目錄下也就是和pom.xm文件同級添加Dockerfile文件,文件內(nèi)容如下:

FROM maven:3.5-jdk-8

只有一句,依賴于基礎(chǔ)鏡像maven3.5jdk 1.8。因為在docker-compose.yaml文件設(shè)置了項目啟動命令,這里不需要再添加啟動命令。

在項目的resources目錄下創(chuàng)建application-dev.propertiesapplication-docker.properties文件

  • application-dev.properties 中的配置信息和上面一致
  • application-docker.properties 中的配置信息做稍微的改造,將數(shù)據(jù)庫的連接信息由jdbc:mysql://localhost:3306/test改為jdbc:mysql://mysql:3306/test 。

這樣我們所有的配置都已經(jīng)完成。

部署

我們將項目拷貝到服務(wù)器中進行測試,服務(wù)器需要先安裝 Docker 和 Docker Compos 環(huán)境

將項目拷貝到服務(wù)器中,進入目錄cd dockercompose-springboot-mysql-nginx

啟動服務(wù):docker-compose up

[root@VM_73_217_centos dockercompose-springboot-mysql-nginx]# docker-compose up
Creating network "dockercomposespringbootmysqlnginx_default" with the default driver
Creating v-nginx ... done
Creating v-mysql ... done
Creating dockercomposespringbootmysqlnginx_app_1 ... done
Attaching to v-nginx, v-mysql, dockercomposespringbootmysqlnginx_app_1
v-mysql  | [Entrypoint] MySQL Docker Image 5.7.21-1.1.4
v-mysql  | [Entrypoint] Initializing database
app_1    | [INFO] Scanning for projects...
... 
app_1    | 2018-03-26 02:54:55.658  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
app_1    | 2018-03-26 02:54:55.660  INFO 1 --- [           main] com.neo.ComposeApplication               : Started ComposeApplication in 14.869 seconds (JVM running for 30.202)

看到信息Tomcat started on port(s): 8080表示服務(wù)啟動成功。也可以使用docker-compose up -d后臺啟動

訪問服務(wù)器地址;http://58.87.69.230/,返回:I have been seen ip 172.19.0.2 1 times. 表示整體服務(wù)啟動成功

使用docker-compose ps查看項目中目前的所有容器

[root@VM_73_217_centos dockercompose-springboot-mysql-nginx]# docker-compose ps
                 Name                                Command                  State                        Ports                  
----------------------------------------------------------------------------------------------------------------------------------
dockercomposespringbootmysqlnginx_app_1   /usr/local/bin/mvn-entrypo ...   Up             8080/tcp                                
v-mysql                                   /entrypoint.sh mysqld            Up (healthy)   0.0.0.0:3306->3306/tcp, 33060/tcp       
v-nginx                                   nginx -g daemon off;             Up             0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp

可以看到項目中服務(wù)的狀態(tài)、命令、端口等信息。

關(guān)閉服務(wù)docker-compose down

[root@VM_73_217_centos dockercompose-springboot-mysql-nginx]# docker-compose down
Stopping dockercomposespringbootmysqlnginx_app_1 ... done
Stopping visitor-nginx                           ... done
Stopping visitor-mysql                           ... done
Removing dockercomposespringbootmysqlnginx_app_1 ... done
Removing visitor-nginx                           ... done
Removing visitor-mysql                           ... done

docker-compose 順序

在使用 docker-compose 啟動的時候經(jīng)常會出現(xiàn)項目報 Mysql 連接異常,跟蹤了一天終于發(fā)現(xiàn)了問題。 docker-compose 雖然可以通過depends_on 來定義服務(wù)啟動的順序,但是無法確定服務(wù)是否啟動完成,因此會出現(xiàn)這樣一個現(xiàn)象,Mysql 服務(wù)啟動比較慢,當 Spring Boot 項目已經(jīng)啟動起來,但是 Mysql 還沒有初始化好,這樣當項目連接 Mysql 數(shù)據(jù)庫的時候,就會出現(xiàn)連接數(shù)據(jù)庫的異常。

針對這樣的問題,有兩種解決方案:

1、足夠的容錯和重試機制,比如連接數(shù)據(jù)庫,在初次連接不上的時候,服務(wù)消費者可以不斷重試,直到連接上服務(wù)。也就是在服務(wù)中定義: restart: always

2、同步等待,使用wait-for-it.sh或者其他shell腳本將當前服務(wù)啟動阻塞,直到被依賴的服務(wù)加載完畢。這種方案后期可以嘗試使用。

總結(jié)

沒有對比就沒有傷害,在沒有使用 Docker 之前,我們需要搭建這樣一個環(huán)境的話,需要安裝 Nginx、Mysql ,再進行一系列的配置調(diào)試,還要擔心各種環(huán)境問題;使用 Docker 之后簡單兩個命令就完成服務(wù)的上線、下線。

docker-compose up
docker-compose down

其實容器技術(shù)對部署運維的優(yōu)化還有很多,這只是剛剛開始,后面使用了 Swarm 才會真正感受到它的便利和強大。

示例代碼-github

示例代碼-碼云

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容