preface

The Spring Cloud project is a microservice project, that is, a project consisting of multiple Sping Boot modules

The Nuxt.js project is part of the front-end Vue based server-side rendering project

A project based on Spring Cloud + server-side rendering technology nuxt.js has recently been launched on the server deployment, which is noted here

Deploy the backend

1, packaging,

Steps:

  • Add packaging dependencies to POM.xml
  • In IDEA, click Clean and select Install to package as a JAR
  • You can see the packaged JAR packages in the Target folder

Note: If multiple jars are present in the target folder,.jar. Original is a plain JAR package that does not contain dependencies, and.jar is an executable jar package that contains all the dependencies in pum.xml and can be executed directly with the Java-jar command.

Package every module in the Spring Cloud project with a package dependency

For example, in the Gateway module

Add the following code to pom.xml

<build>
        <finalName>service-gateway</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
</build>
Copy the code

Then click the Maven plugin in IDEA

How to package interdependent modules?

For example, if module A depends on module B, compile must be added to the dependencies of module A referencing module B. Otherwise, an error will be displayed when module A is packaged

The pom.xml file in module A

<dependency>
            <groupId>com.zfz</groupId>
            <artifactId>common-util</artifactId>
            <version>0.0.1 - the SNAPSHOT</version>
            <scope>compile</scope>
</dependency>
Copy the code

Then click Clean and Install in IDEA to package the JAR package

2. Upload the JAR package to the server

Ensure that the required JAR package is in the same directory as the Dockerfile and docker-comemage. yml files

3. Build an image

Create a Dockerfile file, for example, gateway module

FROM java:8
MAINTAINER 
ADD service-gateway.jar app.jar
EXPOSE 80
ENTRYPOINT ["java"."-jar"."app.jar"]    
Copy the code

Enter the following command in the XShell command line tool to build the image

docker build -t service-gateway .
Copy the code

And so on, build all the images you want to build using the command above

Finally, type Docker Images to view the build image

4. Run the container

Create the docker-comemage. yml file

version: '3.1'
services: 
  service-gateway:
    image: service-gateway
    ports:
      - "80:80" 
    restart: "always"
    container_name: service-gateway
    volumes:  
      - /root/service-gateway.jar:/root/cloud/service-gateway.jar
    entrypoint: java -jar /root/cloud/service-gateway.jar
  The service name:
    image: Name of an existing mirror
    ports:
      - Port mapping 
    restart: "always"
    container_name: Container name
    volumes:  
      - Mount the path
    entrypoint: After building the container, run the command
   .
Copy the code

Enter the following command in the XShell command line tool to deploy the JAR package in one click

docker-compose up -d
Copy the code

If this command is not recognized, docker-compose may not be installed

Installation tutorial:

#The installationThe curl - L "https://get.daocloud.io/docker/compose/releases/download/1.27.3/docker-compose-$(uname - s) - $(uname -m)" - o /usr/local/bin/docker-compose
#Granting Administrator Rights
chmod +x /usr/local/bin/docker-compose

#Restart the docker
service docker restart

#Viewing version Information
docker-compose --version
Copy the code

Finally, type docker PS to see the jar package in action

Deploy the front end

1. Upload front-end files to the server

2. Build an image

Create a Dockerfile file

# Specify node environment
FROM node:14.16.0
# the author
MAINTAINER
The node environment is production
ENV NODE_ENV=production
Allow all IP access
ENV HOST 0.0.0.0
RUN mkdir -p /app
COPY . /app
WORKDIR /app
# Exposed port
EXPOSE 3000
# Use Taobao Mirror
RUN npm config set registry https://registry.npm.taobao.org
# Download dependency
RUN npm install
RUN npm run build
CMD ["npm"."start"]

Copy the code

Enter the /root/app directory in the XShell command line and enter the following command to build the image. Wait for the result as shown in the figure, it indicates that the image is successful

docker build -t nuxt .
Copy the code

Finally, enter the command docker images to view the build image

3. Run the container

Create the container and run it

docker run -d --restart=always --name nuxt -p 3000:3000 nuxt
Copy the code

Finally, enter the command docker ps to view the container that is running

After finishing, remember in ali cloud security group, open port 3000, run access

Access the NUxT project on the public network through http:// domain name :3000/