# 安装文档(中间件集群)
- 若使用的是云平台,不建议使用云服务器搭建集群,建议直接使用云产品。
- 当使用私有机房,才考虑搭建集群,这样能提供系统的稳定性和数据的安全性。
# MongoDB-7.0集群
名称 | IP | 说明 |
---|---|---|
node1 | 192.168.1.101:27017 | 主节点 |
node2 | 192.168.1.102:27017 | 从节点 |
node3 | 192.168.1.103:27017 | 仲裁节点 |
使用副本集方案,需要3个节点
# 1. 安装
# all node
cat >> /etc/yum.repos.d/mongodb-org-7.0.repo << 'EOF'
[mngodb-org-7.0]
name=MongoDB Repository
baseurl=http://mirrors.aliyun.com/mongodb/yum/redhat/8/mongodb-org/7.0/x86_64/
gpgcheck=0
enabled=1
EOF
# all node
yum -y install mongodb-org
# 2. 配置
# node1
sed -i 's/bindIp: 127.0.0.1/bindIp: 192.168.1.101/' /etc/mongod.conf
sed -i 's/#replication/replication/' /etc/mongod.conf
sed -i '/replication/a\ replSetName: rs0 ' /etc/mongod.conf
# node2
sed -i 's/bindIp: 127.0.0.1/bindIp: 192.168.1.102/' /etc/mongod.conf
sed -i 's/#replication/replication/' /etc/mongod.conf
sed -i '/replication/a\ replSetName: rs0 ' /etc/mongod.conf
# node3
sed -i 's/bindIp: 127.0.0.1/bindIp: 192.168.1.103/' /etc/mongod.conf
sed -i 's/#replication/replication/' /etc/mongod.conf
sed -i '/replication/a\ replSetName: rs0 ' /etc/mongod.conf
# 3. 启动
# all node
systemctl start mongod
# 任一节点执行即可,进行集群初始化
rs.initiate({_id:"rs0",members:[{_id:0,host:"192.168.1.101:27017", priority:10},{_id:1,host:"192.168.1.102:27017", priority:5},{_id:2,host:"192.168.1.103:27017", arbiterOnly:true}]})
# 4. 验证
# 检验
rs.status()
# 配置
spring.data.mongodb.uri=mongodb://192.168.1.101:27017,192.168.1.102:27017,192.168.1.103:27017/education?replicaSet=rs0
# Redis-6.2.9集群
名称 | IP | 说明 |
---|---|---|
node1 | 192.168.1.101:6379,192.168.1.101:26379 | 主节点+哨兵 |
node2 | 192.168.1.102:6379,192.168.1.102:26379 | 从节点+哨兵 |
node3 | 192.168.1.103:6379,192.168.1.103:26379 | 从节点+哨兵 |
使用哨兵模式方案,需要3个节点
# 1. 安装
# all node
yum install -y gcc tcl
cd /opt/tools
wget https://download.redis.io/releases/redis-6.2.9.tar.gz
tar zxvf redis-6.2.9.tar.gz && cd redis-6.2.9
make install PREFIX=/opt/redis
# 添加配置文件
cp -r /opt/tools/redis-6.2.9/redis.conf /opt/redis/redis.conf
cp -r /opt/tools/redis-6.2.9/sentinel.conf /opt/redis/sentinel.conf
# 创建redis内部用户
useradd -r redis
chown -R redis:redis /opt/redis
# 2. 配置
# node1
sed -i 's/bind 127.0.0.1/bind 192.168.1.101/' /opt/redis/redis.conf
sed -i 's/protected-mode yes/protected-mode no/' /opt/redis/redis.conf
sed -i 's/# requirepass foobared/requirepass mypassword/' /opt/redis/redis.conf
sed -i 's/daemonize no/daemonize yes/' /opt/redis/redis.conf
sed -i 's/top-writes-on-bgsave-error yes/top-writes-on-bgsave-error no/' /opt/redis/redis.conf
sed -i 's|# bind 127.0.0.1 192.168.1.1|bind 192.168.1.101|' /opt/redis/sentinel.conf
sed -i 's|sentinel monitor mymaster 127.0.0.1 6379 2|sentinel monitor mymaster 192.168.1.101 6379 2|' /opt/redis/sentinel.conf
sed -i 's|daemonize no|daemonize yes|' /opt/redis/sentinel.conf
echo 'sentinel auth-pass mymaster mypassword' >> /opt/redis/sentinel.conf
# node2
sed -i 's/bind 127.0.0.1/bind 192.168.1.102/' /opt/redis/redis.conf
sed -i 's/protected-mode yes/protected-mode no/' /opt/redis/redis.conf
sed -i 's/# requirepass foobared/requirepass mypassword/' /opt/redis/redis.conf
sed -i 's/daemonize no/daemonize yes/' /opt/redis/redis.conf
sed -i 's/top-writes-on-bgsave-error yes/top-writes-on-bgsave-error no/' /opt/redis/redis.conf
sed -i 's|# replicaof <masterip> <masterport>|replicaof 192.168.1.101 6379|' /opt/redis/redis.conf
sed -i 's|# masterauth <master-password>|masterauth mypassword|' /opt/redis/redis.conf
sed -i 's|# bind 127.0.0.1 192.168.1.1|bind 192.168.1.102|' /opt/redis/sentinel.conf
sed -i 's|sentinel monitor mymaster 127.0.0.1 6379 2|sentinel monitor mymaster 192.168.1.101 6379 2|' /opt/redis/sentinel.conf
sed -i 's|daemonize no|daemonize yes|' /opt/redis/sentinel.conf
echo 'sentinel auth-pass mymaster mypassword' >> /opt/redis/sentinel.conf
# node3
sed -i 's/bind 127.0.0.1/bind 192.168.1.103/' /opt/redis/redis.conf
sed -i 's/protected-mode yes/protected-mode no/' /opt/redis/redis.conf
sed -i 's/# requirepass foobared/requirepass mypassword/' /opt/redis/redis.conf
sed -i 's/daemonize no/daemonize yes/' /opt/redis/redis.conf
sed -i 's/top-writes-on-bgsave-error yes/top-writes-on-bgsave-error no/' /opt/redis/redis.conf
sed -i 's|# replicaof <masterip> <masterport>|replicaof 192.168.1.101 6379|' /opt/redis/redis.conf
sed -i 's|# masterauth <master-password>|masterauth mypassword|' /opt/redis/redis.conf
sed -i 's|# bind 127.0.0.1 192.168.1.1|bind 192.168.1.103|' /opt/redis/sentinel.conf
sed -i 's|sentinel monitor mymaster 127.0.0.1 6379 2|sentinel monitor mymaster 192.168.1.101 6379 2|' /opt/redis/sentinel.conf
sed -i 's|daemonize no|daemonize yes|' /opt/redis/sentinel.conf
echo 'sentinel auth-pass mymaster mypassword' >> /opt/redis/sentinel.conf
# 3. 启动
# all node
cat >> /usr/lib/systemd/system/redis.service << 'EOF'
[Unit]
Description=Redis
After=network.target
[Service]
Type=forking
User=redis
LimitNOFILE=65536
LimitNPROC=65536
PIDFILE=/var/run/redis.pid
ExecStart=/opt/redis/bin/redis-server /opt/redis/redis.conf
ExecRepload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
# 设置开机启动
systemct enable redis
# 启动
systemct start redis
cat >> /usr/lib/systemd/system/redis-sentinel.service << 'EOF'
[Unit]
Description=Redis Sentinel
After=network.target
[Service]
Type=forking
User=redis
LimitNOFILE=65536
LimitNPROC=65536
PIDFILE=/var/run/redis-sentinel.pid
ExecStart=/opt/redis/bin/redis-sentinel /opt/redis/sentinel.conf
ExecRepload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
# 设置开机启动
systemct enable redis-sentinel
# 启动
systemct start redis-sentinel
# 4. 验证
# 任一节点执行即可
redis-cli -h 192.168.1.101 -p 26379
> info sentinel
# 返回
------
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.1.101:6379,slaves=2,sentinels=3
------
# 配置
spring.redis.sentinel.nodes=192.168.1.101:26379,192.168.1.102:26379,192.168.1.103:26379
spring.redis.sentinel.password=mypassword
spring.redis.sentinel.master=mymaster
# Elasticsearch-8.11.4集群
名称 | IP | 说明 |
---|---|---|
node1 | 192.168.1.101:9200 | 节点1 |
node2 | 192.168.1.102:9200 | 节点2 |
node3 | 192.168.1.103:9200 | 节点3 |
# 1. 安装
# all node
tar -zxvf elasticsearch-8.11.4-linux-x86_64.tar.gz
mv elasticsearch-8.11.4 /opt/elasticsearch
useradd -r elastic
chown -R elastic:elastic /opt/elasticsearch
# 2. 配置
# node1
echo 'cluster.name: education' >> /opt/elasticsearch/config/elasticsearch.yml
echo 'node.name: node-1' >> /opt/elasticsearch/config/elasticsearch.yml
echo 'network.host: 192.168.1.101' >> /opt/elasticsearch/config/elasticsearch.yml
echo 'discovery.seed_hosts: ["192.168.1.101", "192.168.1.102", "192.168.1.103"]' >> /opt/elasticsearch/config/elasticsearch.yml
echo 'cluster.initial_master_nodes: ["node-1", "node-2", "node-3"]' >> /opt/elasticsearch/config/elasticsearch.yml
# node2
echo 'cluster.name: education' >> /opt/elasticsearch/config/elasticsearch.yml
echo 'node.name: node-2' >> /opt/elasticsearch/config/elasticsearch.yml
echo 'network.host: 192.168.1.102' >> /opt/elasticsearch/config/elasticsearch.yml
echo 'discovery.seed_hosts: ["192.168.1.101", "192.168.1.102", "192.168.1.103"]' >> /opt/elasticsearch/config/elasticsearch.yml
echo 'cluster.initial_master_nodes: ["node-1", "node-2", "node-3"]' >> /opt/elasticsearch/config/elasticsearch.yml
# node3
echo 'cluster.name: education' >> /opt/elasticsearch/config/elasticsearch.yml
echo 'node.name: node-3' >> /opt/elasticsearch/config/elasticsearch.yml
echo 'network.host: 192.168.1.103' >> /opt/elasticsearch/config/elasticsearch.yml
echo 'discovery.seed_hosts: ["192.168.1.101", "192.168.1.102", "192.168.1.103"]' >> /opt/elasticsearch/config/elasticsearch.yml
echo 'cluster.initial_master_nodes: ["node-1", "node-2", "node-3"]' >> /opt/elasticsearch/config/elasticsearch.yml
# 3. 启动
# all node
cat >> /usr/lib/systemd/system/elasticsearch.service << 'EOF'
[Unit]
Description=Elasticsearch
[Service]
User=elastic
LimitNOFILE=65536
LimitNPROC=65536
ExecStart=/opt/elasticsearch/bin/elasticsearch
[Install]
WantedBy=multi-user.target
EOF
# all node
sudo systemctl start elasticsearch
# 4. 验证
# 任一节点执行即可,验证
curl -XGET http://192.168.1.101:9200/_cluster/health?pretty
# 返回
------
{
"cluster_name" : "education-application",
"status" : "green", // green代表成功
"timed_out" : false,
"number_of_nodes" : 3, // 节点数
"number_of_data_nodes" : 3,
"active_primary_shards" : 1,
"active_shards" : 2,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0
}
------
# 配置
spring.elasticsearch.uris=192.168.1.101:9200,192.168.1.102:9200,192.168.1.103:9200
# Nacos-2.3.2集群
名称 | IP | 说明 |
---|---|---|
node1 | 192.168.1.101:8848 | 节点1 |
node2 | 192.168.1.102:8848 | 节点2 |
node3 | 192.168.1.103:8848 | 节点3 |
集群模式使用:域名 + VIP模式,可读性好,而且换ip方便。
# 1. 安装
# all node
tar zxvf nacos-server-2.3.2.tar.gz
mv nacos /opt/nacos
chown -R roncoo:roncoo /opt/nacos
# all node
ln -s /opt/java /usr/java
mkdir -p /work/Tomcat/localhost/nacos
chown -R roncoo:roncoo /work
# 2. 配置
# all node
sed -i 's/server.tomcat.accesslog.enabled=true/server.tomcat.accesslog.enabled=false/' /opt/nacos/conf/application.properties
sed -i 's/nacos.core.auth.enabled=false/nacos.core.auth.enabled=true/' /opt/nacos/conf/application.properties
sed -i 's/nacos.core.auth.server.identity.key=/nacos.core.auth.server.identity.key=roncoo/' /opt/nacos/conf/application.properties
sed -i 's/nacos.core.auth.server.identity.value=/nacos.core.auth.server.identity.value=roncoo/' /opt/nacos/conf/application.properties
sed -i 's/nacos.core.auth.plugin.nacos.token.secret.key=/nacos.core.auth.plugin.nacos.token.secret.key=SecretKey012345678901234567890123456789012345678901234567890123456789/' /opt/nacos/conf/application.properties
echo "spring.datasource.platform=mysql" >> /opt/nacos/conf/application.properties
echo "db.num=1" >> /opt/nacos/conf/application.properties
echo "db.url.0=jdbc:mysql://127.0.0.1:3306/education_nacos?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&autoReconnect=true" >> /opt/nacos/conf/application.properties
echo "db.user.0=roncoo" >> /opt/nacos/conf/application.properties
echo "db.password.0=RonCoo.123" >> /opt/nacos/conf/application.properties
# all node
cat >> /opt/nacos/conf/cluster.conf << 'EOF'
192.168.1.101:8848
192.168.1.102:8848
192.168.1.103:8848
EOF
# 3. 启动
# all node
cat >> /usr/lib/systemd/system/nacos.service << 'EOF'
[Unit]
Description=Nacos
After=network.target
[Service]
User=roncoo
Type=forking
ExecStart=/opt/nacos/bin/startup.sh
ExecStop=/opt/nacos/bin/shutdown.sh
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
# all node
sudo systemctl start nacos
# 方法1
spring.cloud.nacos.server-addr=192.168.1.101:8848,192.168.1.102:8848,192.168.1.103:8848
# 方法2:通过负载均衡ip
spring.cloud.nacos.server-addr=[负载均衡IP]:8848
# 方法3:通过nginx负载转发
spring.cloud.nacos.server-addr=localhost:8848