Spring Boot 2.0 (6): Deploying Spring Boot Open Source Software Cloud Collection with Docker

It only takes three steps to deploy open source project cloud collections and create an exclusive personal collection system. It's that simple!

The cloud collection project has been open source for more than 2 years. As a training project that just started learning Spring Boot, it used a lot of new technologies at the time. Now it seems that many new technologies are unnecessary to use, but as a learning case A great Spring Boot practice indeed.

From open source to now, I have written some tutorials to introduce how to deploy cloud collections and how to run cloud collections in IDE, but there are still many friends who don’t know how to use and deploy them? Questions like "please provide a cloud collection data structure" have been answered at least a hundred times, and nearly ten similar questions have been closed on github.

It can also be seen from another aspect that deploying cloud collection projects is relatively complicated for some friends. Now with Docker, we can solve this problem happily. It only takes three steps to deploy cloud collection projects and create exclusive personal collections. system.

cloud collection

Some friends may not know about cloud collection , so I will give you a brief introduction:

Cloud Collection is an open source website built with Spring Boot, which allows users to collect a website online anytime, anywhere, and categorize the collected websites or articles on the website, which can be used as a temporary storage for later reading. As an open and open source software, it allows users to import favorites from the browser to the cloud collection, and also supports exporting the articles collected by the cloud collection for backup at any time.

Product Home

http://favorites.ren

Project homepage

https://github.com/cloudfavorites/favorites-web

Product screenshot

Core function points:

  • Collection, classification and retrieval of articles
  • Export, export (package live from browser)
  • Like, share, discuss
  • Register, log in, personal account
  • Temporary Favorites, View Others Favorites
  • other...

Technology used in the project:

  • View
  • Bootstrap
  • jQuery
  • Thymeleaf
  • Spring Data Jpa
  • Spring Boot Mail
  • WebJars
  • Mysql
  • Tomcat
  • Redis

Redis was removed later due to limited server resources and troublesome deployment

Project renovation

dependent environment

To prepare a server with a system of Centos 7 or above, the system needs to install Docker and Docker Compos environment. For the installation method, please refer to the previous two articles:

Dockerized transformation

The structure diagram of the project after the renovation is as follows:

Because the previous article Spring Boot 2.0 (5): Docker Compose + Spring Boot + Nginx + Mysql practice has already introduced the structure and content of such projects, so the new content is mainly described here.

docker-compose.yamldocument

Let's first look at the docker-compose.yamlfile:

version: '3'
services:
  nginx:
   container_name: favorites-nginx
   image: nginx:1.13
   restart: always
   ports:
   - 80:80
   - 443:443
   volumes:
     - ./nginx/conf.d:/etc/nginx/conf.d
     - /tmp/logs:/var/log/nginx
     
    
  mysql:
   build: ./mysql
   environment:
     MYSQL_DATABASE: favorites
     MYSQL_ROOT_PASSWORD: root
     MYSQL_ROOT_HOST: '%'
     TZ: Asia/Shanghai
   ports:
   - "3306:3306"
   volumes:
     - ./mysql_data:/var/lib/mysql
   restart: always
      
  app:
    restart: always
    build: ./app
    working_dir: /app
    volumes:
      - ./app:/app
      - ~/.m2:/root/.m2
      - /tmp/logs:/usr/local/logs
    expose:
      - "8080"
    command: mvn clean spring-boot:run -Drun.profiles=docker
    depends_on:
      - nginx
      - mysql

Compared with the previous content, this docker-compose.yamldocument mainly adds two new parts:

  • 1. Map the logs of Nginx and app to the host, so that we can view the logs
  • 2. Map the data storage of Mysql to the host. The advantage of this is that the data will not be lost after the cluster is turned off.

docker-compose.yamlIn the file, the log section:

version: '3'
services:
  nginx:
   volumes:
     - /tmp/logs:/var/log/nginx
  app:
   volumes:
     - /tmp/logs:/usr/local/logs

Map the Nginx and cloud collection project logs to the host respectively /tmp/logs, so that we can view the project logs.

Customize mysql initialization information

docker-compose.yamlIn the file, Mysql changes:

version: '3'
services:
  mysql:
   build: ./mysql
   environment:
     TZ: Asia/Shanghai
   volumes:
     - ./mysql_data:/var/lib/mysql

I picked out all the changed content, mysql added the TZ environment variable to point the time zone to Shanghai, and we brought out the Mysql image content and put it in the mysql directory of the project and built it separately. There are two files in the mysql directory, one is the Dockerfile to define the Mysql image, and the other is the my.cnf file to define the Mysql encoding and other information.

my.cnf file content

#省略一部分
...
character_set_server=utf8
character_set_filesystem=utf8
collation-server=utf8_general_ci
init-connect='SET NAMES utf8'
init_connect='SET collation_connection = utf8_general_ci'
skip-character-set-client-handshake

The main function of this file is to allow Mysql to support UTF-8.

Dockerfile file content

FROM mysql/mysql-server:5.7
COPY my.cnf /etc/my.cnf

Use Mysql5.7 version, and copy my.cnf in the same directory to the /etc/my.cnfserver

In this way, the relevant information of Mysql is defined.

other

The other contents have not changed much. The Nginx configuration files are stored in the nginx directory. The new application-docker.propertiesfiles in the project can be modified by modifying the database connection part.

After the transformation is completed, we only need to copy the project to the deployment server and execute: docker-compose upit can be started.

deploy

I have submitted the transformation content of the project to github, so that when you deploy, you can successfully deploy the cloud collection project in only three steps.

1. Download the source code and decompress it

Download the latest released version

wget https://github.com/cloudfavorites/favorites-web/archive/favorites-1.1.1.zip

decompress

unzip favorites-1.1.1.zip

enter the directory

cd favorites-web-favorites-1.1.1/

2. Modify the configuration file

Modify the fileapplication-docker.properties

vi app/src/main/resources/application-docker.properties

Modifications are as follows

favorites.base.path=http://xx.xxx.xx.xx/ 

The address is the address of the deployment server

3. Start the project

After the configuration is complete, the background starts

[root@~]# docker-compose up -d
Creating network "favoriteswebfavorites111_default" with the default driver
Creating favorites-nginx                  ... done
Creating favoriteswebfavorites111_mysql_1 ... done
Creating favoriteswebfavorites111_app_1   ... done

After the startup is complete, the browser accesses the above configuration address: http://xx.xxx.xx.xx/, and you can see the home page of the cloud collection.

Secondary content

After startup, if you want to view the running status of the service in a container, you can use the following command to enter:

Use to docker psview Docker containers running on the host

[root@VM_73_217_centos ~]# docker ps
CONTAINER ID        IMAGE                            COMMAND                  CREATED             STATUS                  PORTS                                      NAMES
a466ce6e58a5        favoriteswebfavorites111_app     "/usr/local/bin/mv..."   16 hours ago        Up 16 hours             8080/tcp                                   favoriteswebfavorites111_app_1
1b4f1b912de0        nginx:1.13                       "nginx -g 'daemon ..."   16 hours ago        Up 16 hours             0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp   favorites-nginx
65b481bb7741        favoriteswebfavorites111_mysql   "/entrypoint.sh my..."   16 hours ago        Up 16 hours (healthy)   0.0.0.0:3306->3306/tcp, 33060/tcp          favoriteswebfavorites111_mysql_1

According to the Docker container ID information queried above, execute the following command

docker exec -ti CONTAINER_ID  bash
#比如进入项目容器中
[root@VM_73_217_centos ~]# docker exec -ti a466ce6e58a5 bash
root@a466ce6e58a5:/app# ps -ef|grep java
...

To exit the container execute the following command:

root@a466ce6e58a5:/app# exit
exit
[root@VM_73_217_centos ~]# 

In this way, if we want to deploy the cloud collection project in the future, it will be very simple. It only takes three steps to build our own collection system happily, and the friends quickly get started.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325155204&siteId=291194637