"Beginner mall" - Interface - use script to simplify deployment operation (optimization)

Keywords: Maven nexus Docker github

1. Preface

In the previous version, you mistakenly bound the build push docker image to the package execution action. As a result, the OSC service parent project will report an error that it cannot find the dependency during the install operation, and unacceptable problems such as "build the push image in advance". In order to solve these problems, this version has made some corresponding optimization.


2. Source code

Full project address: https://github.com/intomylife/osc-api

v1.4.4 label address: https://github.com/intomylife/osc-api/releases/tag/v1.4.4

v1.4.4 download address: ziptar.gz

Note: for label description "Beginners mall" - write at the front


3. Interface

Note: to view the changes: Simplify deployment operations with scripts

3.1 change binding execution action

Change from package to deploy

reason:

In the package phase, the command only completes the project compilation, unit test, and packaging functions, but not the installation and deployment to the local warehouse and remote warehouse. At this time, the package action is the "trigger point" for building the push docker image, which leads to the error result that there is no relevant dependency in the local warehouse and remote warehouse.

Can that be bound in the install phase? The answer is No. Based on the project compilation, unit test and packaging functions of package, install has one more step to install and deploy to the local warehouse, and we need to build and push the docker image after this operation.

Therefore, the final change is to bind to the deploy phase, which includes project compilation, unit test, packaging function, installation and deployment to the local warehouse, and installation and deployment to the remote warehouse.

3.2 remote private warehouse: Nexus

Note: the previously configured alicloud image warehouse address is for us to speed up download and rely on. To install and deploy to the remote warehouse, we need to deploy a Maven remote private warehouse by ourselves

3.2.1 Nexus

Nexus is a powerful Maven warehouse manager. It can not only maintain all its internal dependencies, but also act as an agent for the external image warehouse. It is easy to install, and also provides a visual operation interface.

Let's install Nexus3 in Docker

3.2.2 install Nexus3 in docker

  1. Pull Nexus image
[root@zwc ~]# systemctl start docker.service
[root@zwc ~]# docker pull sonatype/nexus3:3.17.0
  1. Configure the Nexus data directory and give the directory read / write permission
[root@zwc ~]# cd /usr/local/
[root@zwc local]# mkdir nexus-data
[root@zwc local]# chmod -R 777 nexus-data/
  1. Run Nexus container
[root@zwc ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
sonatype/nexus3     3.17.0              36b0df681a47        11 months ago       582MB
[root@zwc ~]# docker run -d --name nexus3 --restart always -p 18081:8081 -e TZ='Asia/Shanghai' -v /usr/local/nexus-data/:/nexus-data/ sonatype/nexus3:3.17.0
56153e61cba379ea74be624396285f00c07d563b52ef0414cf253ad6
[root@test ~]# 
  • -d: Background start
  • – Name: container name after startup
  • – restart: assign a fixed value always, indicating that if the container fails to start, it will always try to reconnect
  • -p: Port mapping (host port: container port)
  • -e: Environment variable, which specifies the time zone of the container
  • -v: Mapping data directory is equivalent to backing up the data in the container to the directory specified by the host (Host Directory: container directory)
  • Sonatype / nexus 3:3.17.0: image started

3.2.3 change admin password

  1. After the above steps, input ip:18081 directly in the browser
  2. After successfully entering the Nexus homepage, click the Sign in button in the upper right corner
  3. Username: admin
  4. Password: password in admin.password It's recorded in the document
[root@zwc ~]# cd /usr/local/nexus-data/
[root@zwc nexus-data]# cat admin.password
  1. After successful login, there will be a wizard to reset the admin password
  2. Click Next, enter the new password twice, and continue to Next
  3. This step prompts whether anonymous access needs to be enabled. For security reasons, continue to Next
  4. Finally, click Finish to finish

3.2.4 warehouse type

  1. Proxy: the proxy type; the proxy of the remote warehouse, when there is no internal warehouse, will go to the proxy warehouse to find
  2. hosted: host type; own internal warehouse
  3. Group: warehouse group type; configure multiple types of warehouses, and then expose a unified address to access all warehouses in this group

3.2.5 Policy policy

In the hosted warehouse, there is also the concept of Policy, which is mainly divided into the following two types:

  1. releases: online release repository
  2. snapshots: version repository for development testing

3.2.6 default warehouse

  1. Maven central: warehouse of agent type. The default warehouse address of agent is https://repo1.maven.org/maven2/
  2. Maven releases: the warehouse of the host type. The Policy is releases, which refers to the warehouse of the internal online release version
  3. Maven snapshots: the warehouse of the host type. The Policy is snapshots, which refers to the warehouse of the internal development and test version
  4. Maven public: warehouse group, currently including the above three warehouses

3.2.7 create an agent warehouse

Note: there is only one agent of Maven central warehouse in the default warehouse. Now you need to create a new warehouse that acts as Alibaba cloud image

  1. Click the icon button like settings at the top of the homepage
  2. Expand Repository on the left and select Repositories
  3. Click the Create repository button to select a maven 2 (proxy) proxy type repository
  4. Name: aliyun-proxy
  5. Proxy -> Remote storage: http://maven.aliyun.com/nexus/content/groups/public
  6. After filling in the above two items, turn to the bottom of the page and click the Create repository button

3.2.8 put the new agent warehouse into the warehouse group

  1. Click to enter Maven public warehouse group
  2. Found at group - > member repositories:
  3. There are two big boxes. On the left is the warehouse that can be added to the group, and on the right is the warehouse that has been added to the group
  4. Now add the newly created agent warehouse to the group
  5. Click aliyun proxy
  6. Click the arrow to the right (Add to Selected)
  7. At this time, aliyun proxy is at the bottom of the list. The top and bottom ranking refers to the priority of the visit
  8. You need to put aliyun proxy in front of Maven central
  9. Then click the up arrow (Move Up)
  10. Finally, click the Save button at the bottom of the page
  11. A prompt will pop up at the top right of the waiting page, indicating Success and other information

3.2.9 health examination

  1. Go back to the Repositories page
  2. It is found that the agent warehouse has a Health check item. Now let's open it
  3. Click Analyze in aliyun proxy warehouse
  4. Click Yes, only this repository to only operate the currently selected warehouse
  5. Click I Agree
  6. Operate Maven central warehouse as above

3.2.10 other settings

Deployment strategy:

  1. By default, the Maven releases warehouse is not allowed to be redeployed, which is inconvenient for self testing, so it is changed to allow redeployment here
  2. Click to enter Maven releases warehouse
  3. Find hosted - > deployment policy:
  4. Drop down to Allow redeploy
  5. Click Save to save
  6. A prompt will pop up at the top right of the waiting page, indicating Success and other information

Log error:

  1. Click the icon button like settings at the top of the homepage
  2. Expand System on the left and select Capabilities
  3. Click reach: management to enter the management capability details
  4. Click the Disable button above
  5. A prompt will pop up at the top right of the waiting page, indicating Success and other information

3.3 configure local Maven's settings.xml

3.3.1 mirror image

Note: since the address of the Alibaba cloud image warehouse has been configured in the remote private warehouse, you can add the address of the remote private warehouse just deployed after commenting the configured content

	<mirror>
        <id>nexus3</id>
        <mirrorOf>*</mirrorOf>
        <name>maven-public</name>
        <url>http://ip:18081/repository/maven-public/</url>
    </mirror>

3.3.2 server user name and password

Note: since anonymous access is not allowed when Nexus is just configured, you need to configure the authentication information of the warehouse

    <server>
      <id>nexus3</id>
      <username>admin</username>
      <password>123456</password>
    </server>
	<server>
      <id>snapshots</id>
	  <username>admin</username>
      <password>123456</password>
    </server>
	<server>
      <id>releases</id>
	  <username>admin</username>
      <password>123456</password>
    </server>
  • Configure authentication information for each remote private warehouse
  • admin is the default user to deploy Nexus. In fact, it can create roles and user information according to multiple dimensions of what operations each warehouse can have

3.4 configuration of project engineering pom.xml

3.4.1 repository

<!-- Private warehouse -->
<repositories>
    <repository>
        <id>nexus3</id>
        <url>http://10.211.55.25:18081/repository/maven-public/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>
  • Configure remote warehouse address
  • Here is the local configuration, which is the remote warehouse address specially configured for the current project
  • Priority greater than settings - > mirrors

3.4.2 distribution management deployment

<!-- Deploy to remote private warehouse -->
<distributionManagement>
    <!-- Official version -->
    <repository>
        <id>releases</id>
        <name>releases</name>
        <url>http://10.211.55.25:18081/repository/maven-releases/</url>
    </repository>
    <!-- Test version -->
    <snapshotRepository>
        <id>snapshots</id>
        <name>snapshots</name>
        <url>http://10.211.55.25:18081/repository/maven-snapshots/</url>
    </snapshotRepository>
</distributionManagement>
  • Configuration and construction of two remote warehouses
  • Where the value of id node must correspond to the id in 3.3.2 server user name password

3.5 using scripts to simplify deployment operations

3.5.1 .sh

#!/bin/bash
## $() - method, ${} - variable

## start
echo [INFO] ----------------------------- start --------------------------------------

## Get input parameter, application name
application_name=$1
## Take a look at the output
echo application_name: ${application_name}

## Parameter verification
if [ "$application_name" == "registry" ]||[ "$application_name" == "gateway" ]||[ "$application_name" == "turbine" ]||[ "$application_name" == "sleuth" ]||[ "$application_name" == "base" ]||[ "$application_name" == "log" ]||[ "$application_name" == "all" ];
then
    ## Parameter judgment
    if [ "$application_name" != "all" ];
    then

        ## entrance
        echo [INFO] ------------------------ Enter single service deployment ---------------------------------

        ## 1. Install all services
        ## commons
        cd osc-commons/
        mvn clean install -Dmaven.test.skip=true
        mvn clean deploy -Dmaven.test.skip=true
        ## service
        cd ../osc-service/
        mvn clean install -Dmaven.test.skip=true

        if [ "$application_name" == "base" ]||[ "$application_name" == "log" ];
        then
            ## 2. Install and deploy specified services
            cd osc-$application_name-service/osc-$application_name-service-api/
            mvn clean deploy -Dmaven.test.skip=true
            cd ../osc-$application_name-service-core/
            mvn clean deploy -Dmaven.test.skip=true

            ## 3. Return to root
            cd ../../../
        else
            ## 2. Install and deploy specified services
            cd osc-$application_name-service/
            mvn clean deploy -Dmaven.test.skip=true
            
            ## 3. Return to root
            cd ../../
        fi

        ## end
        echo [INFO] ----------------------------- end --------------------------------------
        echo [INFO] ------------------------------------------------------------------------

    else

        ## entrance
        echo [INFO] ------------------------ Enter the whole service deployment ---------------------------------

        ## 1. Install all services
        ## commons
        cd osc-commons/
        mvn clean install -Dmaven.test.skip=true
        mvn clean deploy -Dmaven.test.skip=true
        ## service
        cd ../osc-service/
        mvn clean install -Dmaven.test.skip=true

        ## 2. Install and deploy services one by one
        ## Registration Center
        cd osc-registry-service/
        mvn clean deploy -Dmaven.test.skip=true
        ## gateway
        cd ../osc-gateway-service/
        mvn clean deploy -Dmaven.test.skip=true
        ## Circuit breaker aggregation monitoring center
        cd ../osc-turbine-service/
        mvn clean deploy -Dmaven.test.skip=true
        ## Link tracking and monitoring center
        cd ../osc-sleuth-service/
        mvn clean deploy -Dmaven.test.skip=true
        ## Basic engineering
        cd ../osc-base-service/osc-base-service-api/
        mvn clean deploy -Dmaven.test.skip=true
        cd ../osc-base-service-core/
        mvn clean deploy -Dmaven.test.skip=true
        ## Log Engineering
        cd ../../osc-log-service/osc-log-service-api/
        mvn clean deploy -Dmaven.test.skip=true
        cd ../osc-log-service-core/
        mvn clean deploy -Dmaven.test.skip=true

        ## 3. Return to root
        cd ../../../

        ## end
        echo [INFO] -------------------------- end -------------------------------------
        echo [INFO] ------------------------------------------------------------------------

    fi
else
    ## entrance
    echo [INFO] ------------------------ parameter is incorrect ---------------------------------

    ## end
    echo [INFO] -------------------------- end -------------------------------------
    echo [INFO] ------------------------------------------------------------------------
fi

A very simple script file: judge whether the application name is involved, and if so, install and deploy it

How to use:

  1. Click Terminal entry at the bottom of IDEA
  2. Go to project root
MacBook-Pro:osc-api zouwencong$ pwd
/Users/zouwencong/JavaWork/my_osc/osc-api
MacBook-Pro:osc-api zouwencong$ ls -all
total 64
drwxr-xr-x  13 zouwencong  staff   416 Jun  5 21:50 .
drwxr-xr-x   7 zouwencong  staff   224 Jun  6 16:22 ..
-rw-r--r--@  1 zouwencong  staff  8196 Jun  5 21:50 .DS_Store
drwxr-xr-x  15 zouwencong  staff   480 Jun  5 22:21 .git
-rw-r--r--@  1 zouwencong  staff   428 Jun 13 18:47 .gitignore
drwxr-xr-x   7 zouwencong  staff   224 Jun 16 08:58 .idea
-rw-r--r--@  1 zouwencong  staff    33 Jun 13 18:47 README.md
-rw-r--r--@  1 zouwencong  staff  5476 Jun 13 18:47 deploy.bat
-rwxr-xr-x@  1 zouwencong  staff  3744 Jun 13 18:47 deploy.sh
-rw-r--r--@  1 zouwencong  staff  1622 Jun 13 18:47 growth-version.bat
-rwxr-xr-x@  1 zouwencong  staff  1649 Jun 13 18:47 growth-version.sh
-rw-r--r--@  1 zouwencong  staff   122 Jun 13 18:47 old-version.bat
drwxr-xr-x@  4 zouwencong  staff   128 Jun 13 18:47 osc-commons
drwxr-xr-x@  9 zouwencong  staff   288 Jun 13 18:47 osc-service
MacBook-Pro:osc-api zouwencong$ 
  1. Enter the command/ deploy.sh All: at this time, all projects will be compiled, unit tested, packaged, installed and deployed to the local warehouse, and installed and deployed to the remote warehouse
  2. If you only want to deploy the specified service separately, the command is/ deploy.sh Registry: deploy registry service project

-------------------Update on June 13, 2020: Categorizing script files -------------------

  1. The directory is now like this
MacBook-Pro:osc-api zouwencong$ ls -all
total 40
drwxr-xr-x  11 zouwencong  staff    352 Jun 13 22:27 .
drwxr-xr-x   7 zouwencong  staff    224 Jun  6 16:22 ..
-rw-r--r--@  1 zouwencong  staff  10244 Jun 13 23:03 .DS_Store
drwxr-xr-x  15 zouwencong  staff    480 Jun 13 23:01 .git
-rw-r--r--   1 zouwencong  staff    428 Jun  4 11:47 .gitignore
drwxr-xr-x  11 zouwencong  staff    352 Jun 13 23:07 .idea
-rw-r--r--@  1 zouwencong  staff     33 Nov 15  2019 README.md
drwxr-xr-x   4 zouwencong  staff    128 Jan 10 11:28 logs
drwxr-xr-x  10 zouwencong  staff    320 Jun 13 18:34 osc-commons
drwxr-xr-x  14 zouwencong  staff    448 Jun 11 09:27 osc-service
drwxr-xr-x   5 zouwencong  staff    160 Jun 13 22:27 script
MacBook-Pro:osc-api zouwencong$
  1. The script files are placed in the script directory, and the command changes to. / script / deploy/ deploy.sh All

Core command:

  1. mvn clean install -Dmaven.test.skip=true : executing this command will clean the target directory first, then compile the project, unit test, package the function, install and deploy to the local warehouse; plus- Dmaven.test.skip=true Parameter will skip the test
  2. mvn clean deploy -Dmaven.test.skip=true : executing this command will clean the target directory first, then compile the project, unit test, package the function, install and deploy to the local warehouse, install and deploy to the remote warehouse; plus- Dmaven.test.skip=true Parameter will skip the test

3.5.2 .bat

@echo off

REM ## start
echo [INFO] ----------------------------- start --------------------------------------

REM ## Get input parameter, application name
set application_name=%1%
REM ## Take a look at the output
echo application_name: %application_name%

REM ## Whether the parameters conform to
REM set "is_true=false"
REM ## Take a look at the output
REM echo is_true: %is_true%

REM ## Parameter verification
if "%application_name%" == "all" (

    REM ## entrance
    echo [INFO] ------------------------ Enter the whole service deployment ---------------------------------

    REM ## 1. Install all services
    REM ## commons
    cd osc-commons/
    mvn clean install -Dmaven.test.skip=true
    mvn clean deploy -Dmaven.test.skip=true
    REM ## service
    cd ../osc-service/
    mvn clean install -Dmaven.test.skip=true

    REM ## 2. Install and deploy services one by one
    REM ## Registration Center
    cd osc-registry-service/
    mvn clean deploy -Dmaven.test.skip=true
    REM ## gateway
    cd ../osc-gateway-service/
    mvn clean deploy -Dmaven.test.skip=true
    REM ## Circuit breaker aggregation monitoring center
    cd ../osc-turbine-service/
    mvn clean deploy -Dmaven.test.skip=true
    REM ## Link tracking and monitoring center
    cd ../osc-sleuth-service/
    mvn clean deploy -Dmaven.test.skip=true
    REM ## Basic engineering
    cd ../osc-base-service/osc-base-service-api/
    mvn clean deploy -Dmaven.test.skip=true
    cd ../osc-base-service-core/
    mvn clean deploy -Dmaven.test.skip=true
    REM ## Log Engineering
    cd ../../osc-log-service/osc-log-service-api/
    mvn clean deploy -Dmaven.test.skip=true
    cd ../osc-log-service-core/
    mvn clean deploy -Dmaven.test.skip=true

    REM ## 3. Return to root
    cd ../../../

    REM ## end
    echo [INFO] -------------------------- end -------------------------------------
    echo [INFO] ------------------------------------------------------------------------

) else (

    REM ## entrance
    echo [INFO] ------------------------ Enter single service deployment ---------------------------------

    REM ## 1. Install all services
    REM ## commons
    cd osc-commons/
    mvn clean install -Dmaven.test.skip=true
    mvn clean deploy -Dmaven.test.skip=true
    REM ## service
    cd ../osc-service/
    mvn clean install -Dmaven.test.skip=true

    REM ## Parameter verification
    if "%application_name%" == "registry" (
        
        REM ## 2. Install and deploy specified services
        cd osc-%application_name%-service/
        mvn clean deploy -Dmaven.test.skip=true
        
        REM ## 3. Return to root
        cd ../../

    ) else (

        if "%application_name%" == "gateway" (
            
            REM ## 2. Install and deploy specified services
            cd osc-%application_name%-service/
            mvn clean deploy -Dmaven.test.skip=true
            
            REM ## 3. Return to root
            cd ../../

        ) else (

            if "%application_name%" == "turbine" (
    
                REM ## 2. Install and deploy specified services
                cd osc-%application_name%-service/
                mvn clean deploy -Dmaven.test.skip=true
                
                REM ## 3. Return to root
                cd ../../

            ) else (

                if "%application_name%" == "sleuth" (
                    
                    REM ## 2. Install and deploy specified services
                    cd osc-%application_name%-service/
                    mvn clean deploy -Dmaven.test.skip=true
                    
                    REM ## 3. Return to root
                    cd ../../

                ) else (

                    if "%application_name%" == "base" (
                        
                        REM ## 2. Install and deploy specified services
                        cd osc-%application_name%-service/osc-%application_name%-service-api/
                        mvn clean deploy -Dmaven.test.skip=true
                        cd ../osc-%application_name%-service-core/
                        mvn clean deploy -Dmaven.test.skip=true

                        REM ## 3. Return to root
                        cd ../../../

                    ) else (

                        if "%application_name%" == "log" (
                            
                            REM ## 2. Install and deploy specified services
                            cd osc-%application_name%-service/osc-%application_name%-service-api/
                            mvn clean deploy -Dmaven.test.skip=true
                            cd ../osc-%application_name%-service-core/
                            mvn clean deploy -Dmaven.test.skip=true

                            REM ## 3. Return to root
                            cd ../../../

                        ) else (

                            REM ## Return to root
                            cd ../
                            
                            REM ## entrance
                            echo [INFO] ------------------------ parameter is incorrect ---------------------------------

                            REM ## end
                            echo [INFO] -------------------------- end -------------------------------------
                            echo [INFO] ------------------------------------------------------------------------
                            
                        )

                    )

                )

            )

        )

    )

)

It is consistent with the effect and purpose of. sh, so briefly explain the differences:

What are the differences:

  1. Grammatical differences
  2. The difference of execution mode. Bat batch script execution command: deploy.bat All

-------------------Update on June 13, 2020: Categorizing script files -------------------

  1. . bat batch script execution command changed to: script \ deploy\ deploy.bat All

4. Verification

4.1 interface

  1. Download the code of v1.4.4 tag or change the code of v1.4.3 tag according to this blog
  2. If it is a Mac OS system, you also need to set the executable permission of the. Sh script file: chmod +x deploy.sh
  3. Click Terminal entry at the bottom of IDEA
  4. Go to project root
  5. According to different systems, use different commands:. / script / deploy/ deploy.sh All or script \ deploy\ deploy.bat All

5. Conclusion

Write repetitive operations into scripts, improve efficiency, save time, Bashi.


I hope I can help you

over




Posted by abolmeer on Mon, 15 Jun 2020 21:51:39 -0700