微服务项目部署[开源代码实战]
项目后端: macrozheng/mall-swarm
项目前端: macrozheng/mall-admin-web
容器化方式部署 云计算-Tigerfive:微服务项目容器化部署[开源代码实战]
容器化部署文章已更新
架构图
业务架构
业务流程图之登录
开发技术选型
后端技术
前端技术
环境搭建
开发环境
mysql部署
下载并安装mysql5.7版本,下载地址:https://dev.mysql.com/downloads/installer/
设置数据库帐号密码:root root
下载并安装客户端连接工具Navicat,下载地址:http://www.formysql.com/xiazai.html
mysql yum源下载地址
https://dev.mysql.com/downloads/repo/yum/
打开5.7的安装源 关闭8.0的源
vim /etc/yum.repos.d/mysql-community.repo
# Enable to use MySQL 5.7
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/ [替换]
enabled=1 [修改]
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql-cluster-8.0-community]
name=MySQL Cluster 8.0 Community
baseurl=http://repo.mysql.com/yum/mysql-cluster-8.0-community/el/6/$basearch/
enabled=0 [修改]
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
重新加载缓存
yum clean all && yum makecache fast
yum -y install mysql-community-server
systemctl start mysqld && systemctl enable mysqld
使用弱密码并且改密码
mysql> set global validate_password_policy=0;
mysql> set global validate_password_length=4;
mysql> alter user user() identified by "root"; 【密码自己定】
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
mysql> create database mall;
创建数据库mall
导入mall-swarm/document/sql下的mall.sql文件
mysql -uroot -proot mall < document/sql/mall.sql
Redis
-
下载Redis,下载地址:
https://
download.redis.io/relea
ses/redis-6.0.9.tar.gz
bind: 0.0.0.0 [修改redis.conf 允许远程访问]
启动
vim /etc/redis.conf
bind 0.0.0.0
systemctl restart redis
JDK & maven部署
所有部署微服务的机器都要部署
tar xvzf jdk-8u271-linux-x64.tar.gz -C /usr/local/
ls /usr/local/jdk1.8.0_271/
vim /etc/profile.d/java.sh
JAVA_HOME=/usr/local/jdk1.8.0_271
PATH=$PATH:$JAVA_HOME/bin
export JAVA_HOME PATH
source /etc/profile.d/java.sh
java -version
maven下载地址:
https://maven.apache.org/download.cgi
下载bin包地址:
wget https://mirrors.bfsu.edu.cn/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.zip
解压压缩包:
unzip -d /usr/local/ apache-maven-3.6.3-bin.zip
配置环境变量:
vim /etc/profile.d/mvn.sh
MVN_HOME=/usr/local/apache-maven-3.6.3
PATH=$PATH:$MVN_HOME/bin
export PATH MVN_HOME
source /etc/profile
mvn -version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /usr/local/apache-maven-3.6.3
Java version: 1.8.0_271, vendor: Oracle Corporation, runtime: /usr/local/java8/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-957.el7.x86_64", arch: "amd64", family: "unix"
配置mvn加速
vim /usr/local/apache-maven-3.6.3/conf/settings.xml
在 <mirrors> </mirrors>模块中添加下列配置
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
Elasticsearch
[改机器需要先部署JDK]
下载Elasticsearch7.6.2的zip包,并解压到指定目录, [https://elasticsearch.cn/download/]
https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.6.2-linux-x86_64.tar.gz
安装中文分词插件,在elasticsearch-7.6.2\bin目录下执行以下命令: [分词器因依赖jdk版本不同,暂不安装]
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.6.2/elasticsearch-analysis-ik-7.6.2.zip
运行bin目录下的elasticsearch.sh启动Elasticsearch服务。
--------
具体部署过程:
tar xvzf elasticsearch-7.6.2-linux-x86_64.tar.gz -C /usr/local
useradd ela
passwd ela
chown -R ela.ela /usr/local/elasticsearch-7.6.2/
su - ela
vim /usr/local/elasticsearch-7.6.2/config/elasticsearch.yml
cluster.name: tiger
node.name: elk01
cluster.initial_master_nodes: ["elk01"] #单节点增加该项目
node.master: true
node.data: true
path.data: /data/elasticsearch/data
path.logs: /data/elasticsearch/logs
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
network.host: 0.0.0.0
http.port: 9200
#discovery.zen.ping.unicast.hosts: ["172.16.244.26", "172.16.244.27"]
#discovery.zen.minimum_master_nodes: 2
#discovery.zen.ping_timeout: 150s
#discovery.zen.fd.ping_retries: 10
#client.transport.ping_timeout: 60s
http.cors.enabled: true
http.cors.allow-origin: "*"
sed -i 's/-Xms1g/-Xms4g/' /usr/local/elasticsearch-7.6.2/config/jvm.options
sed -i 's/-Xmx1g/-Xmx4g/' /usr/local/elasticsearch-7.6.2/config/jvm.options
创建ES数据及日志存储目录并授权
mkdir -p /data/elasticsearch/data
mkdir -p /data/elasticsearch/logs
chown -R ela.ela /data/elasticsearch/
chown -R ela.ela /usr/local/elasticsearch-7.6.2/
增加最大文件打开数
vim /etc/security/limits.conf
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
* soft memlock unlimited
* hard memlock unlimited
增加最大内存映射数【root身份执行】
echo "vm.max_map_count=262144" >> /etc/sysctl.conf
sysctl -p
cd /usr/local/elasticsearch-7.6.2/
$ nohup bin/elasticsearch &
--------------
docker 部署 es+kibana
mkdir -p /data/elasticsearch
docker run -dit --name elk \
-p 5601:5601 \
-p 9200:9200 \
-p 5044:5044 \
-v /data/elasticsearch:/var/lib/elasticsearch \
-v /etc/localtime:/etc/localtime \
sebp/elk:762
Kibana
下载Kibana,作为访问Elasticsearch的客户端,请下载7.6.2版本的zip包,并解压到指定目录,
https://artifacts.elastic.co/downloads/kibana/kibana-7.6.2-linux-x86_64.tar.gz
logstash
下载Logstash,用于收集日志,请下载7.6.2版本的zip包,并解压到指定目录,
docker 部署 ELK [不用部署]
84 yum install -y yum-utils device-mapper-persistent-data lvm2
85 yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
86 yum makecache fast
87 yum -y install docker-ce
88 mkdir -p /etc/docker
89 vim /etc/docker/daemon.json [在网上自己找]
90 systemctl restart docker
增加最大文件打开数
vim /etc/security/limits.conf
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
* soft memlock unlimited
* hard memlock unlimited
增加最大内存映射数【root身份执行】
echo "vm.max_map_count=262144" >> /etc/sysctl.conf
sysctl -p
cd /usr/local/elasticsearch-7.6.2/
$ nohup bin/elasticsearch &
docker 部署ELK
mkdir -p /data/elasticsearch
docker run -dit --name elk \
-p 5601:5601 \
-p 9200:9200 \
-p 5044:5044 \
-v /data/elasticsearch:/var/lib/elasticsearch \
-v /etc/localtime:/etc/localtime \
sebp/elk:762
Mongodb
参考: https://www.runoob.com/mongodb/mongodb-linux-install.html
https://www.mongodb.com/try/download/community
https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.2.11.tgz
4.2.11
tar xvf mongodb-linux-x86_64-rhel70-4.2.11.tgz -C /usr/local/
cat /etc/profile.d/mongo.sh
MONGO_HOME=/usr/local/mongodb-linux-x86_64-rhel70-4.2.11
PATH=$PATH:$MONGO_HOME/bin
export PATH MONGO_HOME
source /etc/profile.d/mongo.sh
创建数据库目录
默认情况下 MongoDB 启动后会初始化以下两个目录:
数据存储目录:/var/lib/mongo
日志文件目录:/var/log/mongodb
我们在启动前可以先创建这两个目录并设置当前用户有读写权限:
mkdir -p /var/lib/mongo
mkdir -p /var/log/mongodb
chown `whoami` /var/lib/mongo # 设置权限
chown `whoami` /var/log/mongodb # 设置权限
接下来启动 Mongodb 服务:
mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --fork
增加 --bind_ip=0.0.0.0允许远程访问
mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --bind_ip=0.0.0.0 --fork
打开 /var/log/mongodb/mongod.log 文件看到以下信息,说明启动成功。
# tail -100f /var/log/mongodb/mongod.log
2020-12-04T20:23:16.723+0800 I STORAGE [LogicalSessionCacheRefresh] createCollection: config.system.sessions with provided UUID: f243ffd7-156e-4b6a-bb0f-b82cee825eb6 and options: { uuid: UUID("f243ffd7-156e-4b6a-bb0f-b82cee825eb6") }
2020-12-04T20:23:16.724+0800 I NETWORK [listener] Listening on /tmp/mongodb-27017.sock
2020-12-04T20:23:16.724+0800 I NETWORK [listener] Listening on 0.0.0.0
2020-12-04T20:23:16.724+0800 I NETWORK [listener] waiting for connections on port 27017
MongoDB 后台管理 Shell
如果你需要进入 mongodb 后台管理,你需要先打开 mongodb 装目录的下的 bin 目录,然后执行 mongo 命令文件。
MongoDB Shell 是 MongoDB 自带的交互式 Javascript shell,用来对 MongoDB 进行操作和管理的交互式环境。
当你进入 mongoDB 后台后,它默认会链接到 test 文档(数据库):
--------------------
docker部署
docker pull mongo:4.2.11
docker run --name mongodb -v /data/mongo:/data/db -p 27017:27017 -d mongo:4.2.11
docker exec -it mongodb bash
vim /etc/mongod.conf.orig
注释掉# bindIp: 127.0.0.1
或者改成bindIp: 0.0.0.0
即可开启远程连接
rabbitmq部署
erlang和rabbitmq版本依赖说明
https://www.rabbitmq.com/which-erlang.html
推荐编译安装:
参考:https://blog.csdn.net/qq_36737803/article/details/100537593
erlang 推荐下载地址: erlang的所有版本下载[http://erlang.org/download/]
http://erlang.org/download/otp_src_21.3.tar.gz
https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.7.16/rabbitmq-server-generic-unix-3.7.16.tar.xz
------------------------------------
yum部署方式
安装 erlang
添加yum支持
cd /usr/local/src/
mkdir rabbitmq
cd rabbitmq
wget http://packages.erlang-solutions.com/erlang-solutions-1.0-1.noarch.rpm
rpm -ivh erlang-solutions-1.0-1.noarch.rpm
rpm --import http://packages.erlang-solutions.com/rpm/erlang_solutions.asc
yum -y install erlang
2、安装RabbitMQ
2.1、用 yum 安装 RabbitMQ
```bash
rpm --import https://github.com/rabbitmq/signing-keys/releases/download/2.0/rabbitmq-release-signing-key.asc
# this example assumes the CentOS 7 version of the package
yum install rabbitmq-server-3.7.13-1.el7.noarch.rpm
```bash
rpm --import https://www.rabbitmq.com/rabbitmq-release-signing-key.asc
# this example assumes the CentOS 7 version of the package
yum install rabbitmq-server-3.7.13-1.el7.noarch.rpm
------------------
docker 方式安装 rabbitmq
mkdir /data/rabbitmq
docker pull rabbitmq:latest
docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 -v /data/rabbitmq:/var/lib/rabbitmq --hostname myRabbit -e RABBITMQ_DEFAULT_VHOST=my_vhost -e RABBITMQ_DEFAULT_USER=admin -e RABBITMQ_DEFAULT_PASS=admin rabbitmq
添加用户 mall,权限为administrator
创建一个新的虚拟host为:/mall
点击mall用户进入用户配置页面 mall用户配置该虚拟host的权限
OSS[可以不配置]
开通OSS服务
- 登录阿里云官网;
- 将鼠标移至产品标签页,单击对象存储 OSS,打开OSS 产品详情页面;
- 在OSS产品详情页,单击立即开通。
创建存储空间
- 点击网页右上角控制台按钮进入控制台;
- 选择我的云产品中的对象存储OSS;
- 点击左侧存储空间的加号新建存储空间;
- 新建存储空间并设置读写权限为公共读。
跨域资源共享(CORS)的设置
- 选择一个存储空间,打开其基础设置;
- 点击跨越设置的设置按钮;
- 点击创建规则;
- 进行跨域规则设置;
Endpoint(地域节点) | Bucket 域名 |
---|
nacos 注册中心搭建
mysql> create database nacos;
下载软件包 https://github.com/alibaba/nacos/releases [1.4.0版本]
https://github.com/alibaba/nacos/releases/download/1.4.0/nacos-server-1.4.0.tar.gz
tar xzvf nacos-server-1.4.0.tar.gz -C /usr/local
cd /usr/local/nacos/conf
\#nacos-mysql.sql 导入到Mysql
mysql -uroot -p'root' nacos < nacos-mysql.sql
\#导入成功后,修改 application.properties 配置文件 [1.4前三行不用配置]
server.contextPath=/nacos
server.servlet.contextPath=/nacos
server.port=8848
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://mysql_ip:3306/nacos? characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true [改IP]
db.user=root
db.password=root
# cd ../bin/
# sh startup.sh -m standalone
# 查看日志 /usr/local/nacos/logs/start.out
# 启动后访问
http://192.168.190.128:8848/nacos/index.html
打包项目启动
修改配置文件的中的 [比如mall-portal]
application.yml bootstrap-dev.yml 中的对应服务IP和认证配置
以及config目录下各个服务的ip
在IDE中用 maven工具对 root目录 执行install,把私有包加载到私有库或者本地库中 [mvn install]
[ linux上配置maven用 mvn package -Dskip.test=true打包]
依次部署项目
启动前台服务mall-portal
启动网关服务mall-gateway
启动认证中心mall-auth
启动后台管理服务mall-admin
启动搜索服务mall-search
启动监控中心mall-monitor
find ./ -name '*jar' |xargs -I {} cp {} /app
nohup java -jar mall-portal-1.0-SNAPSHOT.jar > /logs/mall-portal.log &
nohup java -jar mall-gateway-1.0-SNAPSHOT.jar > /logs/mall-gateway.log &
nohup java -jar mall-auth-1.0-SNAPSHOT.jar > /logs/mall-auth.log &
nohup java -jar mall-admin-1.0-SNAPSHOT.jar > /logs/mall-admin.log &
nohup java -jar mall-search-1.0-SNAPSHOT.jar > /logs/mall-search.log &
nohup java -jar mall-monitor-1.0-SNAPSHOT.jar > /logs/mall-monitor.log &
运行完成后可以通过监控中心查看监控信息,账号密码为 macro:123456:http://localhost:8101
运行完成后可以直接通过如下地址访问API文档:http://localhost:8201/doc.html
http:// localhost:8201/doc.html
-
如何访问需要登录的接口,先调用认证中心接口获取token,后台管理
client_id
和client_secret
为admin-app:123456
,前台系统为portal-app:123456
;
- 然后将token添加到请求头中,即可访问需要权限的接口了。
效果展示
- 注册中心服务信息,访问地址: http:// localhost:8848/nacos/
监控中心服务概览信息,访问地址: http://localhost:8101
- 日志收集系统信息,访问地址: http://localhost:5601
前端项目打包部署
node环境
wget https://npm.taobao.org/mirrors/node/v14.15.1/node-v14.15.1-linux-x64.tar.xz
tar xvf node-v14.15.1-linux-x64.tar.xz -C /usr/local/
cat /etc/profile.d/npm.sh
NODE_HOME=/usr/local/node-v14.15.1-linux-x64
PATH=$NODE_HOME/bin:$PATH
export NODE_HOME PATH
source /etc/profile.d/npm.sh
node --version #检查node版本号
npm --version
--------------------------
安装webpackage
npm install webpack -g --registry=https://registry.npm.taobao.org
npm install -g cnpm --registry=https://registry.npm.taobao.org
cnpm install -g @vue/cli-service
-------------------
项目打包部署
git clone https://gitee.com/macrozheng/mall-admin-web.git
修改 mall-admin-web/config 下dev.env.js 和 prod.env.js中
module.exports = {
NODE_ENV: '"production"',
BASE_API: '"http://192.168.0.106/mall-admin"' //修改改IP为Nginx IP
配置node 和 npm环境打包项目
cnpm install //下载项目依赖
cnpm run build //打包dev环境的包
Nginx跨域
docker 直接启动
mkdir -p /app/nginx/{www,logs,conf}
docker run -d -p 80:80 --name n2 -v /app/nginx/www:/usr/share/nginx/html -v /app/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /app/nginx/logs:/var/log/nginx nginx
yum或者编译安装nginx 也行
------------------------
复制前端项目到nginx 目录
vim conf/nginx.conf
user root;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
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;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name 192.168.0.106;
location /
root /usr/share/nginx/html;
index index.html index.htm;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT, X-Mx-ReqToken, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Authorization';
if ($request_method = 'OPTIONS') {
return 204;
location /mall-admin {
proxy_pass http://192.168.0.106:8201/mall-admin; # 网关的IP
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Forward-For $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
add_header Access-Control-Allow-Methods *;
add_header Access-Control-Max-Age 3600;
add_header Access-Control-Allow-Credentials true;
add_header Access-Control-Allow-Origin $http_origin;
add_header Access-Control-Allow-Headers $http_access_control_request_headers;
if ($request_method = OPTIONS ) {
return 200;