如何搭建SpringcloudAlibaba基础环境(一)

  |   0 评论   |   0 浏览

本文详解如何搭建SpringcloudAlibaba基础环境:Mysql、Nacos、Sentinal、RocketMQ

整体概况

本系列文章主要是详解搭建SpringcloudAlibaba的一系列环境。主要包括2部分:基础环境准备、项目搭建。

基础环境主要包括:Mysql、Nacos、Sentinal、RocketMQ、Docker。

项目搭建包括3个服务:网关、订单服务、商品服务、营销服务。

本篇主要详解第一部分:基础环境准备。

基础环境

基础环境准备:Docker、Mysql、Nacos、Sentinal、RocketMQ

大部分组件直接使用docker安装,方便快捷。如果有对docker的不了解的,需要提前准备下。本文不做详解。

安装Docker

Docker的安装此处不多赘述,笔者使用的是Mac,直接在Docker官网下载安装即可。

Docker部署Mysql、Nacos、Sentinal

考虑到3个组件单独用docker部署比较麻烦,所以此处笔者直接利用docker-compose,将3个部件合并部署。

nacos:1.3.2

sentinal:1.8

mysql:5.7

1、在某个文件夹下编写名为docker-compose.yaml的yaml文件,安装Mysql、安装Nacos、安装Sentinal

version: "3"
services:
  mysql:
    container_name: mysql
    image: mysql:5.7
    environment:
      - MYSQL_ROOT_PASSWORD=123456
    volumes:
      - /Users/yclxiao/Program/volume/mysql/data:/var/lib/mysql
    ports:
      - "3306:3306"
    restart: always

  nacos:
    image: nacos/nacos-server:1.3.2
    container_name: nacos
    environment:
      - PREFER_HOST_MODE=hostname
      - MODE=standalone
      - MYSQL_DATABASE_NUM=1
      - MYSQL_SERVICE_HOST=127.0.0.1
      - MYSQL_SERVICE_DB_NAME=nacos_config
      - MYSQL_SERVICE_PORT=3306
      - MYSQL_SERVICE_USER=root
      - MYSQL_SERVICE_PASSWORD=123456
    volumes:
      - /Users/yclxiao/logs/nacos:/home/nacos/logs
    ports:
      - "8848:8848"
    depends_on:
      - mysql
    restart: always

  sentinel:
    image: bladex/sentinel-dashboard:latest
    container_name: sentinel
    ports:
      - "8858:8858"
    restart: always

2、在yaml所在文件夹下运行docker-compose up -d即可。

3、docker ps -a查询容器

image-20210823230003369

验证Mysql、Nacos、Sentinal部署结果

1、验证Mysql:直接使用DataGrip连接本地Mysql服务器,用户名root,密码123456

image-20210823230003369

2、验证Nacos:http://localhost:8848/nacos/index.html,用户名nacos,密码nacos

image-20210825152233517

3、验证Sentinal:http://localhost:8858/#/dashboard,用户名sentinel,密码sentinel

image-20210823223432716

安装RocketMQ

之前尝试用docker安装RocketMQ多个版本一直没成功,要不就是成功之后页面无法打开,要不就是容器启动之后,Mac机器直接CPU飙升,所以放弃了docker部署,直接采用原始方式安装,安装包的下载地址:https://archive.apache.org/dist/rocketmq/

RocketMQ:4.4.0

(一)安装nameserver、broker

安装步骤:

1、建立自己的rocketmq安装目录

2、进入https://archive.apache.org/dist/rocketmq/下载安装包,选择4.4.0

3、解压安装包

4、建立rocketmq的数据文件夹rocketmqdata

5、编辑./conf/broker.conf

5.1、增加brokerIp1 = 127.0.0.1

5.2、增加文件存储位置:

#存储路径
storePathRootDir=/Users/yclxiao/Project/middleware/rocketmq/rocketmq-native/rocketmqdata/store
#commitLog 存储路径
storePathCommitLog=/Users/yclxiao/Project/middleware/rocketmq/rocketmq-native/rocketmqdata/store/commitlog
#消费队列存储路径存储路径
storePathConsumeQueue=/Users/yclxiao/Project/middleware/rocketmq/rocketmq-native/rocketmqdata/store/consumequeue
#消息索引存储路径
storePathIndex=/Users/yclxiao/Project/middleware/rocketmq/rocketmq-native/rocketmqdata/store/index
#checkpoint 文件存储路径
storeCheckpoint=/Users/yclxiao/Project/middleware/rocketmq/rocketmq-native/rocketmqdata/store/checkpoint
#abort 文件存储路径
abortFile=/Users/yclxiao/Project/middleware/rocketmq/rocketmq-native/rocketmqdata/store/abort

6、编写启动脚本,内容如下,文件名startrmq.sh,授予执行权限chmod +x startrmq.sh

#!/bin/sh
nohup sh ./rocketmq-all-4.4.0-bin-release/bin/mqnamesrv >mqnamesrv.log 2>&1 &
sleep 5
echo "namesrv started"
nohup sh ./rocketmq-all-4.4.0-bin-release/bin/mqbroker -n localhost:9876 -c ./rocketmq-all-4.4.0-bin-release/conf/broker.conf >mqbroker.log 2>&1 &
echo "broker starting"
nohup java -jar -Drocketmq.config.namesrvAddr=localhost:9876 -Drocketmq.config.isVIPChannel=false -Drocketmq.config.dataPath=./rocketmqdata -Dserver.port=8180 ./rocketmq-dashboard-2.0.0.jar >dashboard.log 2>&1 &
echo "dashboard starting"

7、启动:

启动namesrv:nohup sh ./rocketmq-all-4.4.0-bin-release/bin/mqnamesrv >mqnamesrv.log 2>&1 &

启动broker:nohup sh ./rocketmq-all-4.4.0-bin-release/bin/mqbroker -n localhost:9876 -c ./rocketmq-all-4.4.0-bin-release/conf/broker.conf >mqbroker.log 2>&1 &

(二)安装dashboard

由于rocketmq-console已经被rocketmq-dashboard取代了,所以直接安装dashboard

1、拷贝项目:https://github.com/apache/rocketmq-dashboard

2、将项目打成jar包:mvn clean package -Dmaven.test.skip=true

3、将jar拷贝到外层目录

4、启动:nohup java -jar -Drocketmq.config.namesrvAddr=localhost:9876 -Drocketmq.config.isVIPChannel=false -Drocketmq.config.dataPath=./rocketmqdata -Dserver.port=8180 ./rocketmq-dashboard-2.0.0.jar >dashboard.log 2>&1 &

参考文件目录如下:

image-20210825152233517

(三)rocketmq一键启停

rocketmq的启动涉及到3个部件,比较麻烦,所以制作了简易的shell脚本,方便启动。

启动脚本:

#!/bin/sh
nohup sh ./rocketmq-all-4.4.0-bin-release/bin/mqnamesrv >mqnamesrv.log 2>&1 &
sleep 5
echo "namesrv started"
nohup sh ./rocketmq-all-4.4.0-bin-release/bin/mqbroker -n localhost:9876 -c ./rocketmq-all-4.4.0-bin-release/conf/broker.conf >mqbroker.log 2>&1 &
echo "broker starting"
nohup java -jar -Drocketmq.config.namesrvAddr=localhost:9876 -Drocketmq.config.isVIPChannel=false -Drocketmq.config.dataPath=./rocketmqdata -Dserver.port=8180 ./rocketmq-dashboard-2.0.0.jar >dashboard.log 2>&1 &
echo "dashboard starting"

停止脚本:

#!/bin/sh
sh ./rocketmq-all-4.4.0-bin-release/bin/mqshutdown namesrv
sh ./rocketmq-all-4.4.0-bin-release/bin/mqshutdown broker

pid=`ps ax | grep rocketmq-dashboard | grep java | grep -v grep | awk '{print $1}'`
if [ -z "$pid" ] ; then
        echo "No rocketmq-dashboard running."
        exit -1;
fi

echo "The rocketmq-dashboard(${pid}) is running..."
kill ${pid}
echo "Send shutdown request to rocketmq-dashboard(${pid}) OK"

一键启动:./startrmq.sh

一键停止:./stoprmq.sh

(四)验证RocketMQ部署结果

image-20210825152233517

总结

总体来说,整个安装过程还算简单清晰,照着步骤安装肯定会成功,如果在安装过程中有啥疑问,也可与我交流,谢谢你的阅读,我们下期见。

文件中的/Users/yclxiao/Project/middleware/rocketmq/rocketmq-native目录改成自己的目录地址


标题:如何搭建SpringcloudAlibaba基础环境(一)
作者:程序员半支烟
地址:http://mangod.top/articles/2023/03/15/1678873083956.html