微服务架构图
1635229126009.png
环境准备
[root@localhost simple-microservice]# vi /etc/profile
JAVA_HOME=/usr/local/jdk
PATH=$JAVA_HOME/bin:$PATH:/usr/local/maven/bin
export JAVA_HOME PATH
[root@localhost simple-microservice]# source /etc/profile
[root@localhost simple-microservice]# java -version
[root@localhost simple-microservice]# mvn -version
编译微服务项目
[root@gitlab simple-microservice]# mvn clean package -Dmaven.test.skip=true
eureka集群配置
defaultZone: http://192.168.153.17:${server.port}/eureka/,http://192.168.153.18:${server.port}/eureka/,http://192.168.153.19:${server.port}/eureka/
eureka集群服务
001 分发jar包
[root@localhost simple-microservice]# mkdir /data/ms -p
[root@localhost simple-microservice]# cp -r eureka-service/target/eureka-service.jar /data/ms
[root@localhost ms]# sshpass -p"root" scp -r /data/ms/eureka/eureka-service.jar root@192.168.153.18:/data/ms/eureka/
[root@localhost ms]# sshpass -p"root" scp -r /data/ms/eureka/eureka-service.jar root@192.168.153.19:/data/ms/eureka/
002 编写eureka系统服务
[root@localhost system]# cd /usr/lib/systemd/system
[root@localhost system]# vi eureka.service
---------------------------------配置文件----------------------------------
[Unit]
Description=Eureka
Documentation=eureka
[Service]
ExecStart=/usr/local/jdk/bin/java -jar /data/ms/eureka/eureka-service.jar
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
[Install]
WantedBy=multi-user.target
---------------------------------------------------------------------------
[root@localhost system]# systemctl daemon-reload
003 启动eureka系统服务
[root@localhost system]# systemctl start eureka
[root@localhost system]# jps -l
1940 /data/ms/eureka-service.jar
1963 sun.tools.jps.Jps
[root@localhost system]# ss -antp|grep 8888
004 查看eureka集群
http://192.168.153.17:8888/
1635232273913.png
docker安装mysql5.7
#拉起MySQL镜像(:5.7 表示5.7版本)
docker pull mysql:5.7
#运行MySQL容器
docker run -d -p 3306:3306 --privileged=true -v /docker/mysql/conf/my.cnf:/etc/my.cnf -v /docker/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123 --name mysql mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci
#参数说明:
run run 是运行一个容器
-d 表示后台运行
-p 表示容器内部端口和服务器端口映射关联
--privileged=true 设值MySQL 的root用户权限, 否则外部不能使用root用户登陆
-v /docker/mysql/conf/my.cnf:/etc/my.cnf 将服务器中的my.cnf配置映射到docker中的/docker/mysql/conf/my.cnf配置
-v /docker/mysql/data:/var/lib/mysql 同上,映射数据库的数据目录, 避免以后docker删除重新运行MySQL容器时数据丢失
-e MYSQL_ROOT_PASSWORD=123456 设置MySQL数据库root用户的密码
--name mysql 设值容器名称为mysql
mysql:5.7 表示从docker镜像mysql:5.7中启动一个容器
--character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci 设值数据库默认编码
#赋予远程登录权限
[root@xdclass ~]# docker exec -it mysql bash
root@ce7e026432b3:/# mysql -u root -p
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> FLUSH PRIVILEGES;
部署业务程序
001 创建工作目录
for ip in 192.168.153.17 192.168.153.18 192.168.153.19; do
for name in product order stock; do
sshpass -p"root" ssh root@$ip "mkdir /data/ms/$name -p"
done
done
002 修改配置文件连接的数据库和eureka地址
#数据库配置
url: jdbc:mysql://192.168.153.17:3306/{tb_order,tb_product}?characterEncoding=utf-8
username: root
password: 123
#eureka
http://192.168.153.17:8888/eureka/,http://192.168.153.18:8888/eureka/,http://192.168.153.19:8888/eureka/
003 拷贝构建文件到所有服务器
#编译构建
[root@gitlab simple-microservice]# mvn clean package -Dmaven.test.skip=true
[root@bogon simple-microservice]# for ip in 192.168.153.17 192.168.153.18 192.168.153.19; do
for name in product order stock; do
sshpass -p"root" scp $name-service/$name-service-biz/target/$name-service-biz.jar root@$ip:/data/ms/$name
done
done
004 systemd管理服务
#三台服务器分别执行shell
[root@bogon ~]# for name in product order stock; do
cat > /usr/lib/systemd/system/$name.service <<EOF
[Unit]
Description=$name
Documentation=$name
[Service]
ExecStart=/usr/local/jdk/bin/java -jar /data/ms/$name/$name-service-biz.jar
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
done
----------------------------------------------------------------------------------
#修改文件配置,重新加载
for ip in 192.168.153.17 192.168.153.18 192.168.153.19; do
sshpass -p"root" ssh root@$ip "systemctl daemon-reload"
done
005 启动服务
[root@bogon ~]# for ip in 192.168.153.17 192.168.153.18 192.168.153.19; do
for name in eureka; do
sshpass -p"root" ssh root@$ip "systemctl start $name"
done
done
[root@bogon ~]# for ip in 192.168.153.17 192.168.153.18 192.168.153.19; do
for name in product order stock; do
sshpass -p"root" ssh root@$ip "systemctl start $name"
done
done
006 查看服务
[root@devops2 ~]# journalctl -u product -f
1635258176122.png
部署网关和前端程序
001 创建工作目录
for ip in 192.168.153.17 192.168.153.18 192.168.153.19; do
for name in gateway portal; do
sshpass -p"root" ssh root@$ip "mkdir /data/ms/$name -p"
done
done
002 修改配置文件eureka地址
#eureka
http://192.168.153.17:8888/eureka/,http://192.168.153.18:8888/eureka/,http://192.168.153.19:8888/eureka/
003 拷贝构建文件到所有服务器
#编译构建
[root@gitlab simple-microservice]# mvn clean package -Dmaven.test.skip=true
[root@bogon simple-microservice]# for ip in 192.168.153.17 192.168.153.18 192.168.153.19; do
for name in gateway portal; do
sshpass -p"root" scp $name-service/target/$name-service.jar root@$ip:/data/ms/$name
done
done
004 systemd管理服务
#三台服务器分别执行shell
[root@bogon ~]# for name in gateway portal; do
cat > /usr/lib/systemd/system/$name.service <<EOF
[Unit]
Description=$name
Documentation=$name
[Service]
ExecStart=/usr/local/jdk/bin/java -jar /data/ms/$name/$name-service.jar
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
done
---------------------------------------------------------------------------
#修改文件配置,重新加载
for ip in 192.168.153.17 192.168.153.18 192.168.153.19; do
sshpass -p"root" ssh root@$ip "systemctl daemon-reload"
done
005 启动服务
#启动mysql
[root@devops1 system]# docker start mysql
[root@bogon ~]# for ip in 192.168.153.17 192.168.153.18 192.168.153.19; do
for name in eureka; do
sshpass -p"root" ssh root@$ip "systemctl start $name"
done
done
[root@bogon ~]# for ip in 192.168.153.17 192.168.153.19; do
for name in product order stock; do
sshpass -p"root" ssh root@$ip "systemctl start $name"
done
done
[root@bogon ~]# for ip in 192.168.153.17; do
for name in gateway portal; do
sshpass -p"root" ssh root@$ip "systemctl start $name"
done
done
for ip in 192.168.153.17; do
for name in gateway portal; do
sshpass -p"root" ssh root@$ip "systemctl stop $name"
done
done
006 查看注册服务
1635287881715.png
007 查看网关服务
http://192.168.153.17:9999/product/queryAllProduct
{"status":200,"msg":"success","result":[{"id":1,"productName":"测试商品1","price":99.99,"stock":99},{"id":2,"productName":"美女","price":999.0,"stock":88},{"id":3,"productName":"Q币","price":100.0,"stock":77},{"id":4,"productName":"貂皮大衣很厚很厚的那种","price":9999.0,"stock":66}]}
http://192.168.153.17:9999/order/queryAllOrder
{"status":200,"msg":"success","result":[]}
对外提供访问
网站架构图
1635290391960.png
网关映射路径
1635290706014.png
修改nginx配置文件
[root@devops1 conf.d]# vi /etc/nginx/conf.d/site.conf
---------------------------配置文件------------------------------
upstream gateway {
server 192.168.153.17:9999;
}
server {
listen 80;
server_name gateway.ctnrs.com;
access_log /var/log/nginx/gateway-access.log main;
location / {
proxy_pass http://gateway;
}
}
upstream portal {
server 192.168.153.17:8080;
}
server {
listen 80;
server_name portal.ctnrs.com;
access_log /var/log/nginx/portal-access.log main;
location / {
proxy_pass http://portal;
}
}
-----------------------------------------------------------------------
#启动nginx
[root@devops1 conf.d]# systemctl start nginx
修改hosts文件
192.168.153.17 gateway.ctnrs.com portal.ctnrs.com
展示效果
1635290774243.png
1635290803041.png
1635290872619.png
网友评论