一、环境:
2个提供者服务程序(java,dubbo内部启动方式),3台虚拟机分别安装java1.8,tomcat8,zookeeper3.4.11。
二、目标:
zookeeper集群搭建,dubbo服务注册,dubbo-admin监控集群服务。
三、环境搭建
1、准备3台虚拟机分别安装了centos7(安装教程略,很简单)
2、给虚拟机配置net
(1)在虚拟机打开虚拟网络编辑器,此处的网关IP是GATEWAY的值
3、配置静态IP。
(1) 配置静态IP:cd /etc/sysconfig/network-scripts,vi ifcfg-ens33编辑配置
TYPE=Ethernet |
(2) 配置主机名
vi /etc/hosts vi /etc/hostname |
4、关闭防火墙或者开放端口(这里是关闭了防火墙)
关闭防火墙:systemctl stop firewalld.service 关闭开机启动:systemctl disable firewalld.service |
5、安装jdk1.8
官网下载 jdk-8u161-linux-x64.tar.gz
新建开发路径:usr/local/software/
新建java路径:usr/local/software/java
将jdk压缩包传至此路径下,解压 tar -zxvf usr/local/software/java/jdk-8u161-linux-x64.tar.gz
配置java环境变量:
vi /etc/profile 在最后添加
#javapath
exportJAVA_HOME=/usr/local/software/java/jdk1.8.0_161
exportJRE_HOME=/$JAVA_HOME/jre
exportCLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
exportPATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
6、tomcat安装(略,上传,解压,进入bin路径,./startup.sh启动即可,懒,脚本没写)
7、安装zookeeper(单机)
路径:/usr/local/software/zookeeper/zookeeper-3.4.11,上传解压
配置:复制一份 zoo_sample.cfg 文件,命名为zoo.cfg
cp /usr/local/software/zookeeper/zookeeper-3.4.11/conf/zoo_sample.cfg /usr/local/software/zookeeper/zookeeper-3.4.11/conf/zoo.cfg
启动:
进入目录/usr/local/software/zookeeper/zookeeper-3.4.11/bin
执行:./zkServer.sh start
设置开机启动,在init.d目录下新建脚本文件(安装成服务)
cd/etc/rc.d/init.d
新建一个名为zookeeper的文件,命令是: touch zookeeper
文件内容:脚本
#!/bin/bash
#chkconfig: 2345 10 90
#description: zookeeper
exportJAVA_HOME=/usr/local/software/java/jdk1.8.0_161
exportZOO_LOG_DIR=/usr/local/software/zookeeper/dataLog/log
ZOOKEEPER_PATH=/usr/local/software/zookeeper/zookeeper-3.4.11/bin
case"$1" in
start)
echo"start zookeeper service..."
sh${ZOOKEEPER_PATH}/zkServer.sh start
;;
stop)
echo"stop zookeeper service..."
sh${ZOOKEEPER_PATH}/zkServer.sh stop
;;
restart)
echo"restart zookeeper service..."
sh${ZOOKEEPER_PATH}/zkServer.sh restart
;;
status)
echo"zookeeper status"
sh${ZOOKEEPER_PATH}/zkServer.sh status
;;
*)
echo"require start|stop|status|restart"
exit1
;;
esac
为新建的/etc/rc.d/init.d/zookeeper文件添加可执行权限
chmod +x /etc/rc.d/init.d/zookeeper
把zookeeper这个脚本添加到开机启动项里面
chkconfig --add zookeeper
reboot重启,之后查看,service zookeeper status状态,确认是否成功
6、克隆虚拟机
(1)、克隆(略,很简单,直接在vm客户端克隆),克隆两台
(2)、完事之后,设置静态路由(只改IP,把UUID去掉就行,3台:127,128,129)主机名更改为
(zookeeper01,zookeeper02,zookeeper03)
以上步骤上文均有说明
7、zookeeper集群设置
(1)、3台虚拟机均创建data、dataLog文件夹(在/usr/local/software/zookeeper下)
(2)、在data文件夹里创建myid文件,内容只是编号
IP 主机名 myid内容
192.168.112.127 zookeeper01 1
192.168.112.128 zookeeper02 2
192.168.112.129 zookeeper03 3
(3)、修改配置文件/usr/local/software/zookeeper/zookeeper-3.4.11/conf/zoo.cfg(3台都是一样的)
尾部添加:
server.1=zookeeper01:2888:3888
server.2=zookeeper02:2888:3888
server.3=zookeeper03:2888:3888
(4)、修改/etc/hosts文件集群模式
192.168.112.127zookeeper01
192.168.112.128zookeeper02
192.168.112.129zookeeper03
(5)、reboot或者service zookeeper restart(3台都是)
(6)、查看zookeeper状态 service zookeeper status(如果是leader,或者flower表示成功)
8、项目部署:
服务端:dubbo配置文件
dubbo-admin端:dubbo.properties
dubbo.registry.address=zookeeper://192.168.112.127:2181?backup=192.168.112.128:2181,192.168.112.129:2181
dubbo-admin项目随便在哪里运行都可以(一定要注意防火墙要么关闭,要不么开放端口)
最后dubbo-admin可以监控到这3台虚拟机的服务。(由于有些dubbo服务要与其他服务分开部署或者防止夯机,性能等,集群是必须的)