In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the "front-end deployment method tutorial based on Spring Cloud and Vue.js". In daily operation, I believe that many people have doubts about the front-end and back-end deployment method tutorial based on Spring Cloud and Vue.js. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "front-end deployment method tutorial based on Spring Cloud and Vue.js". Next, please follow the editor to study!
Preparation in advance
The system version is CentOS 7.664-bit.
Connect to the server
To operate on a remote server, you must first connect to the server. Open the command line tool, enter the following command, and then enter the password of the server instance to connect to the server remotely.
Ssh root@xxx.xx.xx.xx
@ is followed by the public network IP of the server. If the server IP address is 120.456.78.123, then the command to connect to the server is:
Ssh root@120.456.78.123
After hitting enter, you will be asked to enter the password and enter the correct password.
Install Docker
Not to mention the advantages of Docker containerized deployment, directly on the practical information on how to install Docker.
Just copy the commands in the code box to the command line for execution.
Remove the old version first (if any):
Sudo yum remove docker\ docker-client\ docker-client-latest\ docker-common\ docker-latest\ docker-latest-logrotate\ docker-logrotate\ docker-selinux\ docker-engine-selinux\ docker-engine
Install some necessary tools:
Sudo yum install-y yum-utils device-mapper-persistent-data lvm2
Add software source information:
Sudo yum-config-manager-- add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Update the yum cache:
Sudo yum makecache fast
Install Docker-ce:
Sudo yum-y install docker-ce
Start the Docker backend service:
Sudo systemctl start docker
Test run hello-world (with or without this step to verify that the Docker installation was successful, and if successful, the Hello World will be printed out):
Docker run hello-world install MySQL
Pull the image of MySQL version 5.7:
Docker pull mysql:5.7
Run MySQL:
Docker run-p 3306 PWD/conf:/etc/mysql/conf.d 3306-- name mysql-v $PWD/conf:/etc/mysql/conf.d-v $PWD/logs:/logs-v $PWD/data:/var/lib/mysql-e MYSQL_ROOT_PASSWORD=123456-d mysql:5.7
Command description:
-p 3306virtual 3306: map port 3306 of the container to port 3306 of the host.
-v $PWD/conf:/etc/mysql/conf.d: Mount the conf/my.cnf under the current directory of the host to the / etc/mysql/my.cnf of the container.
-v $PWD/logs:/logs: Mount the logs directory under the current directory of the host to the / logs of the container.
-v $PWD/data:/var/lib/mysql: Mount the data directory under the current directory of the host to the / var/lib/mysql of the container.
-e MYSQL_ROOT_PASSWORD=123456: initializes the password of the root user.
Be careful to change to the appropriate directory before running, because the directory that the MySQL container maps to the native is relative to the mapped current directory. For example, if the current directory is / root/abc, after executing the above command, the directory and files mounted by MySQL will be created under / root/abc.
Install Redis
You can also execute the run command directly, and if the system detects that the image is not installed, it will pull the installation and run it again.
Download and run redis:4.0.8:
Docker run-p 6379RabbitMQ 6379-t-dit redis:4.0.8 installation RabbitMQ
As above, execute the run command directly to install and run RabbitMQ:3.7.7:
Docker run-d-- hostname my-rabbit-p 5672 rabbitmq:3.7.7-management 5672 rabbitmq:3.7.7-management
The pre-environment preparation tools for deploying the micro-service back-end project here have been completed. If you want to deploy the front end, you also need to install Nginx, which will be discussed in the chapter on front end deployment.
Back-end deployment
There are two things that need to be done to deploy the backend, one is to modify the configuration file application.yml of each micro-service module, and the other is to write Dockerfile.
Let's first look at the directory structure:
There are five modules, of which common is pure Java code for extracting the common code of each module, and each of the remaining four modules is an independent micro-service module, so we need to deploy four modules: eureka, user, education and gateway, that is, we will finally run four independent Docker containers.
The specific business logic will not be explained too much, and this article will only talk about deployment.
Profile application.yml
In order that local debugging and server deployment do not affect each other, we split the original application.yml into three files:
Application.yml: general configuration, specifying which of the following configurations should be used
Application-dev.yml: development environment configuration
Application-pro.yml: build environment configuration
In addition, for convenience, put Dockerfile in the same level directory. As shown in the figure:
Here is the code for the three configuration files:
Spring: profiles: active: pro eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ instance: prefer-ip-address: true server: port: 8899 spring: application: name: education datasource: driver-class-name: com.mysql.jdbc.Driver username: root password: 123456 url: jdbc:mysql://127.0.0.1/edu?characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai jpa: show-sql: true # if the field value is null, jackson is not returned: Default-property-inclusion: non_null rabbitmq: host: localhost port: 5672 username: guest password: guest redis: port: 6379 database: 0 host: 127.0.0.1 password: jedis: pool: 8 max-wait:-1ms max-idle: 8 min-idle: 0 timeout: 5000ms eureka: client: service-url: defaultZone: ${SPRING-CLOUD-EUREKA-ZONE} instance: prefer-ip-address: true server: port: 8899 spring: application: Name: education datasource: driver-class-name: com.mysql.jdbc.Driver username: root password: 123456 url: jdbc:mysql://$ {SPRING-CLOUD-MYSQL-HOST} / ${SPRING-CLOUD-DB-NAME}? characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai jpa: show-sql: true # if the field value is null, do not return jackson: default-property-inclusion: non_null rabbitmq: host: ${SPRING-CLOUD-RABBIT-MQ-HOST} port: 5672 username: guest password: guest redis : port: 6379 database: 0 host: ${SPRING-CLOUD-REDIS-HOST} password: jedis: pool: max-active: 8 max-wait:-1ms max-idle: 8 min-idle: 0 timeout: 5000ms
This project is fully configured, including Redis, RabbitMQ, MySQL and JPA.
The configuration of dev is similar to that of pro, except that the local addresses of localhost and 127.0.0.1 in dev are replaced with variables such as ${SPRING-CLOUD-EUREKA-ZONE} and ${SPRING-CLOUD-RABBIT-MQ-HOST}.
So where are these variables set? When you write Dockerfile later, you will see that these variables will be set as environment variables in Dockerfile. When you start the Docker container, the program will read the values set in Dockerfile and apply them to the project.
Write Dockerfile
The following is the Dockerfile of the education module:
FROM java:8 VOLUME / tmp ADD education.jar app.jar RUN bash-c 'touch / app.jar' ENV SPRING-CLOUD-EUREKA-ZONE http://123.456.789.10:8761/eureka/ ENV SPRING-CLOUD-MYSQL-HOST 123.456.789.10 ENV SPRING-CLOUD-DB-NAME edu ENV SPRING-CLOUD-RABBIT-MQ-HOST 123.456.789.10 ENV SPRING-CLOUD-REDIS-HOST 123.456.789.10 ENTRYPOINT ["java", "- Djava.security.egd=file:/dev/./urandom" "- jar", "/ app.jar"] EXPOSE 8899
Simply explain the function of each sentence:
FROM java:8: specify the basic image, which must be the first command
VOLUME / tmp: used to specify the persistence directory
ADD education.jar app.jar: add the local file education.jar to the container and name it app.jar. Note that the education.jar here should be replaced with the name of the jar package packaged by your own project. if the name of the package you are packing is abc.jar, it should be written like this: ADD abc.jar app.jar.
RUN bash-c 'touch / app.jar': command executed in the mirror container to run the jar package.
ENV: these lines start with ENV to set environment variables. Remember the ${} variables in the application-pro.yml file above? This is where it was set. Change all the places to fill in the address with the public network IP address of this server. This configuration assumes that the address of your server is 123.456.789.10. When you use it, change this address to your own server address.
ENTRYPOINT: configure the container to make it executable.
EXPOSE 8899: Port 8899 is exposed to the public. This port should be consistent with the port set by server.port in the configuration file of the project.
To sum up, there are only three things you need to change in the Dockerfile file:
The third line education.jar is changed to the name of the jar package you packaged.
The environment variable at the beginning of ENV is set according to the configuration of application-pro.yml.
Change the last line EXPOSE 8899 to the port number of your project
Post another module's Dockerfile for comparison:
FROM java:8 VOLUME / tmp ADD gateway.jar app.jar RUN bash-c 'touch / app.jar' ENV SPRING-CLOUD-EUREKA-ZONE http://123.456.789.10:8761/eureka/ ENV SPRING-CLOUD-REDIS-HOST 123.456.789.10 ENTRYPOINT ["java", "- Djava.security.egd=file:/dev/./urandom", "- jar", "/ app.jar"] EXPOSE 8888
In this way, write the Dockerfile for each micro-service project that needs to be packaged.
Packing
After configuring the three applicaiton.yml and Dockerfile (actually no Dockerfile is needed for packaging), execute the following command under the root directory of the project to package:
Mvn clean package-Dmaven.test.skip=true
If you see the output SUCCESS of the console, the package is successful:
The finished package is in the project directory / target, as shown in the figure:
Check the size of the jar package, if it is dozens of M, there is no problem, if it is hundreds of K, then there is a problem with the packaging configuration, check whether the label configuration of the pom.xml file is correct.
Attached configuration:
Org.springframework.boot spring-boot-maven-plugin repackage org.springframework.boot spring-boot-maven-plugin education Writing Automation script
After writing the Dockerfile file, you have to execute the command to package it into an image image, and you also need to run the container. Although there are not many lines of code, it is still troublesome to write each time. Encapsulate it into a sh script, saving time and effort every time you run the script directly.
Create a new education_deploy.sh file and put two lines of code in it:
Docker build-t education. Docker run-p 8899 8899-t-dit-- restart=always education
Notice that there is a dot at the end of the first line. If you want to print the image called abc and the exposed port is 6666, you should write:
Docker build-t abc. Docker run-p 6666 restart=always abc 6666-t-restart=always abc
Upload to the server
Create a new folder and a new folder for each microservice module to store jar packages, Dockerfile and automation deployment files, as shown in the figure:
Compress the back_end folder as a whole, and the compression command is:
Tar-cvf back_end.tar. / back_end
After execution, you will see the back_end.tar zip file in the directory.
Log in to the server using the ssh command, create a new edu folder under the root directory, enter this folder, view the current directory, and remember this directory:
/ / create a new folder edu mkdir edu / / go to the edu folder cd edu / / View the current directory and output: / root/edu pwd
Remember the location you want to send to the server: / root/edu, then go back to the directory where the local machine just compressed the back_end.tar file, and execute the following command to send back_end.tar to the server's / root/edu directory:
Scp back_end.tar root@123.456.789.10:/root/edu
After uploading, extract it from the server:
Tar-xvf back_end.tar
Then go to the folders of each module in turn and execute the sh script:
Sh. / education_deploy.sh
After all running, the back-end microservice is deployed!
Common Docker commands
To verify whether the deployment is correct after deployment, start with Docker, and finally enter the interface address in the browser to see if it can be connected.
The following commands are executed on the CentOS server.
Check the currently running container to see if MySQL, Redis, RabbitMQ, and your own project are running:
Docker ps
View the log
To enter the container to view the log:
Docker logs Container ID
For example, if you want to view the container log whose container ID is 378af204f7bc, you should execute:
Docker logs 378af204f7bc
If the container runs for a long time, it will generate a lot of logs. Using docker logs directly will print out all the logs. Can you print only the last number of lines or logs after a certain time? Yes, of course.
View the log after the specified time and display only the last 100 lines:
Docker logs-f-t-since= "2019-10-24"-- tail=100 CONTAINER_ID
Print only the last 50 lines of the log:
Docker logs-tail=50 Container ID
View the log for the last 30 minutes:
Docker logs-- since 30m container ID
View the log after a certain time:
Docker logs-t-since= "2019-10-24T13:23:37" CONTAINER_ID
View the log for a certain period of time:
Docker logs-t-since= "2019-10-24T13:23:37"-- until "2019-10-25T12:23:37" CONTAINER_ID
Stop and start
Stop the container:
Docker stop Container ID
View all containers (including those that have been stopped):
Docker ps-a
Restart the container (the container has been stopped but has not been deleted):
Docker start Container ID
Delete containers and images
If there are any changes to the project, it is best to delete the previous containers and images, and then run the new container.
Stop the container first:
Docker stop Container ID
Then delete the container:
Docker rm Container ID
View the image image:
Docker images
Delete the mirror:
Docker rmi image ID front-end deployment
First package the front-end project, compress it into a tar file, send it to the server, and decompress it on the server.
To use Nginx as an agent, you need to install Nginx first.
Install Nginx
Download and install the basic library first:
Yum-y install gcc gcc-c++ autoconf pcre pcre-devel make automake yum-y install wget httpd-tools vim
Install Nginx:
Sudo yum install nginx
Configure Nginx
Configuration file directory for Nginx:
Nginx main profile:
Nginx main configuration file: / etc/nginx/nginx.conf / etc/nginx/ etc/nginx/conf.d/ etc/nginx/conf.d/default.conf
Let's take a look at the default configuration of nginx.conf:
# For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes auto; error_log / var/log/nginx/error.log; pid / run/nginx.pid; # Load dynamic modules. See / usr/share/doc/nginx/README.dynamic. Include / usr/share/nginx/modules/*.conf; events {worker_connections 1024;} http {log_format main'$remote_addr-$remote_user [$time_local] "$request"'$status $body_bytes_sent "$http_referer"'"$http_user_agent"$http_x_forwarded_for"; access_log / var/log/nginx/access.log main; sendfile on Tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include / etc/nginx/mime.types; default_type application/octet-stream; # Load modular configuration files from the / etc/nginx/conf.d directory. # See http://nginx.org/en/docs/ngx_core_module.html#include # for more information. Include / etc/nginx/conf.d/*.conf; server {listen 80 default_server; listen [:]: 80 default_server; server_name _; root / usr/share/nginx/html; # Load configuration files for the default server block. Include / etc/nginx/default.d/*.conf; location / {} error_page 404 / 404.html; location = / 40x.html {} error_page 500502503504 / 50x.hml; location = / 50x.html {}} # Settings for a TLS enabled server. # # server {# listen 443 ssl http2 default_server; # listen [:]: 443 ssl http2 default_server; # server_name _; # root / usr/share/nginx/html; # # ssl_certificate "/ etc/pki/nginx/server.crt"; # ssl_certificate_key "/ etc/pki/nginx/private/server.key" # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 10m; # ssl_ciphers Higgl Load configuration files for the default server block; # ssl_prefer_server_ciphers on; # Load configuration files for the default server block. # include / etc/nginx/default.d/*.conf; # # location / {#} # error_page 404 / 404.html; # location = / 40x.html {#} # # error_page 500502 503 504 / 50x.hml; # location = / 50x.html {#} #}
Modify the Server node of nginx.conf, we will still listen to port 80, change server_name to your domain name, and then modify location,root to the directory where the front-end file is located, and index as the entry file:
Location / {root/ root/edu/front_end/; index index.html index.htm;}
You can only change these two places, and don't move anywhere else:
Server {listen 80 default_server; listen [::]: 80 default_server; server_name www.abc.cn abc.cn; root / usr/share/nginx/html; # Load configuration files for the default server block. Include / etc/nginx/default.d/*.conf; location / {root/ root/edu/front_end/; index index.html index.htm;} error_page 404 / 404.html; location = / 40x.html {} error_page 500502503504 / 50x.html; location = / 50x.html {}
After changing and saving successfully, check the Nginx configuration. Successful shows that there is no syntax error in the configuration file:
Nginx-t-c / etc/nginx/nginx.conf
Reload the configuration:
Nginx-s reload-c / etc/nginx/nginx.conf
Nginx error reporting
[error] open () "/ var/run/nginx.pid" failed
Sometimes an error occurs when reloading the configuration: nginx: [error] open () "/ var/run/nginx.pid" failed (2: No such file or directory).
The solution is to execute the following two lines of code in turn:
Sudo nginx-c / etc/nginx/nginx.conf nginx-s reload
Browser access report 403
Use the browser to access the domain name, report 403 errors, to see exactly what went wrong, you can check the nginx error log, where is the error log? the nginx.conf file indicates: error_log / var/log/nginx/error.log;.
View the contents of the file with the cat command:
Cat / var/log/nginx/error.log
If you sign up for Permission denied, there is a good chance that the currently logged-in user does not match the user declared on the first line of the nginx.conf file.
Connect () to 127.0.0.1 failed 8000 failed (13: Permission denied)....
Changing user nginx; to user root; can generally be solved by reloading the configuration again.
At this point, the study on the "front-end deployment method tutorial based on Spring Cloud and Vue.js" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.