通常我们拿到一组干净的linux服务器,需要初始化安装一些基础软件,这里一站式介绍部署jdk+tomcat+rocketmq+redis-cluster
前言:如果要在多台服务器上安装,在一台服务器上上传并解压安装包后,使用scp命令进行服务器间的拷贝
scp -r /usr/local/temp/ root@host:/usr/local
yum install lrzsz ----开启文件上传下载
1、安装jdk
在网上自行下载tar.gz的jdk安装包,本示例使用的是jdk-8u144-linux-x64.tar.gz
# mkdir usr/local/java //创建java安装目录
# cd usr/local/java
# rz -be //上传jdk tar.gz包
解压安装jdk
# tar -xvf jdk-8u144-linux-x64.tar.gz
# rm -rf jdk-8u144-linux-x64.tar.gz
(如果需要多台部署) # scp -r ../java/ root@[host]:/usr/local/java/
配置全局java环境变量
# vim /etc/profile
文件最后面添加
export JAVA_HOME=/usr/local/java/jdk1.8.0_144
export JRE_HOME=$JAVA_HOME/jre
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=./:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
使profile生效
# source /etc/profile
配置用户java环境变量
# vim ~/.bashrc
export JAVA_HOME=/usr/local/java/jdk1.8.0_144
export JRE_HOME=$JAVA_HOME/jre
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=./:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
使bashrc配置生效
# source ~/.bashrc
检查一下环境变量
# echo $JAVA_HOME
# echo $CLASSPATH
# echo $PATH
# java -version
2、安装tomcat
自行下载tomcat.tar.gz包安装,本示例使用的是apache-tomcat-8.5.23.tar.gz
# mkdir /usr/local/tomcat
# cd /usr/local/tomcat
# rz -be //上传tomcat的tar.gz安装包
# tar -zxvf apache-tomcat-8.5.23.tar.gz // 解压压缩包
# rm -rf apache-tomcat-8.5.23.tar.gz // 删除压缩包
(如果需要多台服务器部署) # scp -r /usr/local/tomcat/apache-tomcat-8.5.23/ root@[hostIp]:/usr/local/tomcat/
启动Tomcat8
# /usr/local/tomcat/apache-tomcat-8.5.23/bin/startup.sh //启动tomcat,屏幕应该打印如下信息:
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/java/jdk1.8.0_144/jre
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
因为tomcat肯定是要让外部访问的,确保防火墙不会阻止tomcat端口
查看防火墙状态
# service iptables status
停止防火墙
# service iptables stop
启动防火墙
# service iptables start
重启防火墙
# service iptables restart
永久关闭防火墙
# chkconfig iptables off
永久关闭后启用
# chkconfig iptables on
如果防火墙当前是运行状态,确保tomcat使用端口没被拦截,执行以下操作:
# vim /etc/sysconfig/iptables
增加以下代码
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
重启防火墙
# service iptables restart
检验Tomcat8安装运行
在浏览器中输入http://[hostip]:8080/ 查看tomcat是否运行正常:
看到tomcat系统界面,说明安装成功!
3、安装rocketMq
下载rocketMQ.tar.gz(例子使用3.5.8版本 alibaba-rockecmq-3.5.8.tar.gz)
# mkdir /usr/local/rocketmq
# tar -zxvf alibaba-rockecmq-3.5.8.tar.gz // 解压
# rm -rf alibaba-rockecmq-3.5.8.tar.gz
# chmod -R 777 alibaba-rockecmq-3.5.8/bin
配置profile
# vim/etc/profile
在最后添加
export ROCKETMQ_HOME=/usr/local/rocketmq/alibaba-rockecmq-3.5.8
export PATH=$JAVA_HOME/bin:$ROCKETMQ_HOME/bin:$PATH
启动nameserver :
# nohup sh mqnamesrv &
如果遇到java.net.UnknownHostException: DS-TEST-202: Name or service not known异常,说明DS-TEST-202无法解析(也可能是其他名字),解决办法:
# vim /etc/hosts 添加如下配置127.0.0.1 localhost.localdomain localhost DS-TEST-202
启动broker(主从)
# nohup sh mqbroker -n [nameserverip]:9876 -c /usr/local/rocketmq/alibaba-rockecmq-3.5.8/conf/2m-2s-sync/broker-a.properties &
如果是在单机安装主从mq,需要修改从mqbroker配置
# vim/usr/local/rocketmq/alibaba-rockecmq-3.5.8/conf/2m-2s-sync/broker-a-s.properties
增加或修改
listenPort=10921 // 因为默认是10911,主不修改采用默认值就是10911,而listenPort+1是haService的默认端口,listenPort-2是fastRemotingServer服务的端口,所以这里增加默认端口号10,避免冲突.
# nohup sh mqbroker -n [nameserverip]:9876 -c /usr/local/rocketmq/alibaba-rockecmq-3.5.8/conf/2m-2s-sync/broker-a-s.properties &
若启动broker出现如下错误日志:
Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000740000000, 2147483648, 0) failed; error='Cannot allocate memory' (errno=12)
修改/usr/local/rocketmq/alibaba-rockecmq-3.5.8/bin/下的服务启动脚本 runserver.sh 、runbroker.sh 中对于内存的限制,JAVA_OPT
# vim /usr/local/rocketmq/alibaba-rockecmq-3.5.8/bin/runserver.sh
# vim /usr/local/rocketmq/alibaba-rockecmq-3.5.8/bin/runbroker.sh
关闭nameserver:
# sh mqshutdown namesrv
关闭mqbroker
#sh mqshutdown broker
# ps aux | grep rocketmq查询是否成功
# jps -l查看进程
4、安装redis集群
下载redis.tar.gz(例子redis-3.2.9.tar.gz)
首先安装单机redis
# mkdir /usr/local/redis
# cd /usr/local/redis
# rz -be //上传tar包
# tar -zxvf redis-3.2.9.tar.gz //解压
# rm -rf redis-3.2.9.tar.gz // 删除tar包
# cd redis-3.2.9
# make // 编译
# cd src
# sudo make install // 安装
在profile中添加配置
# vim /etc/profile
export REDIS_HOME=/usr/local/redis/redis-3.2.9
export PATH=$PATH:$REDIS_HOME/src
使配置生效
# source /etc/profile
启动redis看是否安装正确
# redis-server /usr/local/redis/redis-3.2.9/redis.conf
下面是集群redis配置
创建集群目录
# mkdir /usr/local/redis/cluster/
# cd /usr/local/redis/cluster/
# mkdir 8501 8502 8503 8504 8505 8506
拷贝配置文件到其中一个目录
# cp /usr/local/redis/redis-3.2.9/redis.conf /usr/local/redis/cluster/8501/
修改配置
# vim /usr/local/redis/cluster/8501/redis.conf
tips: vim中搜索字符串使用/+字符串
cluster-enabled yes
cluster-config-file nodes-8501.conf
cluster-node-timeout 15000
appendonly yes
# Redis使用后台模式
daemonize yes
# 关闭保护模式
protected-mode no
# 注释以下内容开启远程访问
# bind 127.0.0.1
# 修改启动端口为6379
port 8501
# 修改pidfile指向路径
pidfile /usr/local/redis/cluster/8501/redis_8501.pid
将修改好的配置文件拷贝到其他目录
# cp /usr/local/redis/cluster/8501/redis.conf /usr/local/redis/cluster/8502/
# cp /usr/local/redis/cluster/8501/redis.conf /usr/local/redis/cluster/8503/
# cp /usr/local/redis/cluster/8501/redis.conf /usr/local/redis/cluster/8504/
# cp /usr/local/redis/cluster/8504/redis.conf /usr/local/redis/cluster/8505/
# cp /usr/local/redis/cluster/8504/redis.conf /usr/local/redis/cluster/8506/
依次修改端口号port、pidfile、cluster-config-file
# vim /usr/local/redis/cluster/8502/redis.conf //port --> 8502
# vim /usr/local/redis/cluster/8503/redis.conf //port --> 8503
# vim /usr/local/redis/cluster/8504/redis.conf //port --> 8504
# vim /usr/local/redis/cluster/8505/redis.conf //port --> 8505 slaveof -8502
# vim /usr/local/redis/cluster/8506/redis.conf //port --> 8506 slaveof -8503
分别启动配置文件
# redis-server /usr/local/redis/cluster/8501/redis.conf
# redis-server /usr/local/redis/cluster/8502/redis.conf
# redis-server /usr/local/redis/cluster/8503/redis.conf
# redis-server /usr/local/redis/cluster/8504/redis.conf
# redis-server /usr/local/redis/cluster/8505/redis.conf
# redis-server /usr/local/redis/cluster/8506/redis.conf
检查启动状态,应有如下输出
# ps aux | grep redis
root 13550 0.0 0.1 135588 9596 ? Ssl 16:04 0:00 redis-server *:8503 [cluster]
root 13567 0.0 0.0 135588 7564 ? Ssl 16:11 0:00 redis-server *:8504 [cluster]
root 13575 0.0 0.0 135588 7568 ? Ssl 16:11 0:00 redis-server *:8505 [cluster]
root 13583 0.0 0.0 135588 7564 ? Ssl 16:11 0:00 redis-server *:8506 [cluster]
root 13611 0.0 0.0 135588 7564 ? Ssl 16:14 0:00 redis-server *:8501 [cluster]
root 13619 0.0 0.0 135588 7568 ? Ssl 16:14 0:00 redis-server *:8502 [cluster]
启动集群
# redis-trib.rb create --replicas 1 127.0.0.1:8501 127.0.0.1:8502 127.0.0.1:8503 127.0.0.1:8504 127.0.0.1:8505 127.0.0.1:8506 // replicas 后紧跟的1代表为每一个master节点创建一个从节点
如果出现如下异常,需要安装ruby
/usr/bin/env: ruby: No such file or directory
# yum -y install ruby
# yum -y install ruby-rdoc
# yum install rubygems
# gem install redis --version 3.0.0 |
重新启动集群,启动成功
# redis-trib.rb create --replicas 1 127.0.0.1:8501 127.0.0.1:8502 127.0.0.1:8503 127.0.0.1:8504 127.0.0.1:8505 127.0.0.1:8506
检查Node状态
# redis-trib.rb check 127.0.0.1:8501
为集群配置密码,集群启动后要在各个节点下设置(两个config都要执行):
# redis-cli -c -p 8501~8506
主:config set masterauth Dsjrxd
从:config set requirepass Dsjrxd
设置密码后:
# redis-trib.rb check 127.0.0.1:8501,则会报错ERR] Sorry, can’t connect to node 127.0.0.1:7000
解决办法:
vim /usr/lib/ruby/gems/1.8/gems/redis-3.0.0/lib/redis/client.rb
将Defaults节点下内容修改如下:
DEFAULTS = {
:url => lambda { ENV["REDIS_URL"] },
:scheme => "redis",
:host => "127.0.0.1",
:port => 6379,
:path => nil,
:timeout => 5.0,
:password => "Dsjrxd",
:db => 0,
:driver => nil,
:id => nil,
:tcp_keepalive => 0,
:reconnect_attempts => 1,
:inherit_socket => false
}
集群重启
# ps aux | grep redis
# kill -9 [redisPid]
重新启动各个节点
# redis-server /usr/local/redis/cluster/8501/redis.conf
# redis-server /usr/local/redis/cluster/8502/redis.conf
# redis-server /usr/local/redis/cluster/8503/redis.conf
# redis-server /usr/local/redis/cluster/8504/redis.conf
# redis-server /usr/local/redis/cluster/8505/redis.conf
# redis-server /usr/local/redis/cluster/8506/redis.conf
重新为集群每一个服务设置密码,然后,重新启动集群
# redis-trib.rb create --replicas 1 127.0.0.1:8501 127.0.0.1:8502 127.0.0.1:8503 127.0.0.1:8504 127.0.0.1:8505 127.0.0.1:8506