安装JDK

1. 在Linux操作系统目录下创建soft目录

  1. [root@VM-24-10-centos /]# mkdir soft

2. 删除系统默认的JDK(如果有的话)

  • 查找已经安装的JDK

    1. yum list installed | grep jdk
  • 删除安装的JDK

    1. yum remove JDK的名称

    3. 把下载好的JDK通过命令或文件上传工具上传到Linux系统的soft目录下,这里使用的Cyberduck工具

    Snipaste_2022-02-05_17-22-02.png

    4. 解压上传的jdk-8u161-linux-x64.tar.gz文件

    1. tar -zxvf jdk-8u161-linux-x64.tar.gz

    5. 将解压的JDK拷贝到/usr/local目录下,并重新命名为jdk1.8

    1. mv /soft/jdk1.8.0_161 /usr/local/jdk1.8

    6. 配置环境变量JAVA_HOME

    1. vim /etc/profile

    配置内容如下:

    1. JAVE_HOME=/usr/local/jdk1.8
    2. PATH=$JAVE_HOME/bin:$PATH
    3. export JAVE_HOME PATH

    重新加载文件,使刚刚配置的内容生效

    1. source /etc/profile

    image.png

    7. 验证是否生效

    1. java -version

    image.png

安装zookeeper

安装

下载地址https://zookeeper.apache.org/releases.html
Snipaste_2022-02-05_19-02-54.png
下载最新稳定版3.6.3二进制版就可以了
将下载后的apache-zookeeper-3.6.3-bin.tar.gz上传到Linux服务器上,这里使用Cyberduck工具上传
image.png
解压

  1. tar -zxvf apache-zookeeper-3.6.3-bin.tar.gz

移动到/usr/local/目录下

  1. mv apache-zookeeper-3.6.3-bin /usr/local/

cd到如下目录

  1. cd /usr/local/apache-zookeeper-3.6.3-bin/bin

启动Server

  1. ./zkServer.sh

报如下错误

  1. [root@VM-24-10-centos bin]# ./zkServer
  2. -bash: ./zkServer: 没有那个文件或目录
  3. [root@VM-24-10-centos bin]# ./zkServer.sh
  4. /usr/local/jdk1.8/bin/java
  5. ZooKeeper JMX enabled by default
  6. Using config: /usr/local/apache-zookeeper-3.6.3-bin/bin/../conf/zoo.cfg
  7. grep: /usr/local/apache-zookeeper-3.6.3-bin/bin/../conf/zoo.cfg: 没有那个文件或目录
  8. grep: /usr/local/apache-zookeeper-3.6.3-bin/bin/../conf/zoo.cfg: 没有那个文件或目录
  9. mkdir: 无法创建目录"": 没有那个文件或目录
  10. Usage: ./zkServer.sh [--config <conf-dir>] {start|start-foreground|stop|version|restart|status|print-cmd}

需要一个zoo.cfg配置文件

  1. cd /usr/local/apache-zookeeper-3.6.3-bin/conf
  1. cp zoo_sample.cfg zoo.cfg

并在zoo.cfg文件中添加如下内容并修改dataDir数据存放的目录,为方便起见在一台服务器上部署3个zookeeper,需要提前配置好集群中都有哪些服务。

  1. server.1=120.53.119.107:2887:3887
  2. server.2=120.53.119.107:2888:3888
  3. server.3=120.53.119.107:2889:3889

service.N =YYY: A:B
N:代表服务器编号(也就是myid里面的值),用于快速的选出leader,其实想在200ms中快速选出leader,大家都互相谦让,拿出这个编号,让最大的做leader(可以先这么理解)
YYY:服务器地址
A:表示 Flower 跟 Leader的通信端口,简称服务端内部通信的端口(默认2888)
B:表示选举端口,如果leader挂掉或者刚开始启动选举leader的时候,使用该端口进行通信选举(默认是3888)
Snipaste_2022-02-05_21-10-41.png
重新命名一台服务

  1. mv apache-zookeeper-3.6.3-bin zookeeper-server-01

这里方便起见数据存放目录,放在了如下位置

  1. /usr/local/zookeeper-server-01/data

zoo.cfg文件有如下内容

  1. # The number of milliseconds of each tick
  2. # 心跳时间2s
  3. tickTime=2000
  4. # The number of ticks that the initial
  5. # synchronization phase can take
  6. # 集群启动时间限制在10 * 2000 = 20s
  7. initLimit=10
  8. # The number of ticks that can pass between
  9. # sending a request and getting an acknowledgement
  10. # 同步数据的时候5 * 2000 = 10s,可以理解leader写完数据同步到其它节点的时间限制
  11. syncLimit=5
  12. # the directory where the snapshot is stored.
  13. # do not use /tmp for storage, /tmp here is just
  14. # example sakes.
  15. # 数据存放的目录
  16. dataDir=/usr/local/zookeeper-server-01/data
  17. # the port at which the clients will connect
  18. clientPort=2181
  19. # the maximum number of client connections.
  20. # increase this if you need to handle more clients
  21. #maxClientCnxns=60
  22. #
  23. # Be sure to read the maintenance section of the
  24. # administrator guide before turning on autopurge.
  25. #
  26. # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
  27. #
  28. # The number of snapshots to retain in dataDir
  29. #autopurge.snapRetainCount=3
  30. # Purge task interval in hours
  31. # Set to "0" to disable auto purge feature
  32. #autopurge.purgeInterval=1
  33. ## Metrics Providers
  34. #
  35. # https://prometheus.io Metrics Exporter
  36. #metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
  37. #metricsProvider.httpPort=7000
  38. #metricsProvider.exportJvmInfo=true
  39. #集群中的服务列表,1、2、3代表的是服务的id,需要和myid文件(规定myid文件必须在dataDir目录下)中的内容一致。
  40. #2887:数据同步端口,比如Leader写完数据,通过哪一个端口同步给其它节点。
  41. #3887:选举端口,比如Leader挂了通过哪一个端口选举
  42. server.1=120.53.119.107:2887:3887
  43. server.2=120.53.119.107:2888:3888
  44. server.3=120.53.119.107:2889:3889

由于规定myid文件必须在dataDir目录下,切换目录到如下位置

  1. /usr/local/apache-zookeeper-3.6.3-bin/data

创建myid文件,内容写1

  1. echo 1 > myid

需要部署3台,我们做如下操作

  1. cp -R zookeeper-server-01/ zookeeper-server-02
  2. cp -R zookeeper-server-01/ zookeeper-server-03

启动

  1. ./zkServer.sh start

如果报如下错误,看一下防火墙对应的端口是否没有放开

  1. Client port found: 2181. Client address: localhost. Client SSL: false.
  2. Error contacting service. It is probably not running.

如果报如下问题

  1. 2022-02-05 21:34:26,875 [myid:1] - ERROR [ListenerHandler-/120.53.119.107:3887:QuorumCnxManager$Listener$ListenerHandler@1093] - Exception while listening
  2. java.net.BindException: 无法指定被请求的地址 (Bind failed)
  3. at java.net.PlainSocketImpl.socketBind(Native Method)
  4. at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:387)
  5. at java.net.ServerSocket.bind(ServerSocket.java:375)
  6. at java.net.ServerSocket.bind(ServerSocket.java:329)
  7. at org.apache.zookeeper.server.quorum.QuorumCnxManager$Listener$ListenerHandler.createNewServerSocket(QuorumCnxManager.java:1135)
  8. at org.apache.zookeeper.server.quorum.QuorumCnxManager$Listener$ListenerHandler.acceptConnections(QuorumCnxManager.java:1064)
  9. at org.apache.zookeeper.server.quorum.QuorumCnxManager$Listener$ListenerHandler.run(QuorumCnxManager.java:1033)
  10. at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
  11. at java.util.concurrent.FutureTask.run(FutureTask.java:266)
  12. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
  13. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
  14. at java.lang.Thread.run(Thread.java:748)

改配置文件,添加如下配置

  1. quorumListenOnAllIPs=true

查看zookeeper的运行状态,报如下错误

  1. [root@VM-24-10-centos bin]# ./zkServer.sh status
  2. /usr/local/jdk1.8/bin/java
  3. ZooKeeper JMX enabled by default
  4. Using config: /usr/local/zookeeper-server-02/bin/../conf/zoo.cfg
  5. Client port found: 2182. Client address: localhost. Client SSL: false.
  6. Error contacting service. It is probably not running.

查看进程是否在

  1. netstat -ntlp

发现进程都在

  1. tcp6 0 0 :::2181 :::* LISTEN 3668/java
  2. tcp6 0 0 :::2182 :::* LISTEN 3992/java
  3. tcp6 0 0 :::2183 :::* LISTEN 4254/java

查看错误日志/usr/local/zookeeper-server-02/logs/zookeeper-root-server-VM-24-10-centos.out

  1. tail -100f zookeeper-root-server-VM-24-10-centos.out
  1. 2022-02-06 09:00:45,505 [myid:2] - WARN [QuorumConnectionThread-[myid=2]-678:QuorumCnxManager@400] - Cannot open channel to 1 at election address /120.53.119.107:3887
  2. java.net.SocketTimeoutException: connect timed out
  3. at java.net.PlainSocketImpl.socketConnect(Native Method)
  4. at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
  5. at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
  6. at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
  7. at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
  8. at java.net.Socket.connect(Socket.java:589)
  9. at org.apache.zookeeper.server.quorum.QuorumCnxManager.initiateConnection(QuorumCnxManager.java:383)
  10. at org.apache.zookeeper.server.quorum.QuorumCnxManager$QuorumConnectionReqThread.run(QuorumCnxManager.java:457)
  11. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
  12. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
  13. at java.lang.Thread.run(Thread.java:748)

查看集群状态

  1. [root@VM-24-10-centos local]# zookeeper-server-01/bin/zkServer.sh status
  2. /usr/local/jdk1.8/bin/java
  3. ZooKeeper JMX enabled by default
  4. Using config: /usr/local/zookeeper-server-01/bin/../conf/zoo.cfg
  5. Client port found: 2181. Client address: localhost. Client SSL: false.
  6. Mode: leader
  7. [root@VM-24-10-centos local]# zookeeper-server-02/bin/zkServer.sh status
  8. /usr/local/jdk1.8/bin/java
  9. ZooKeeper JMX enabled by default
  10. Using config: /usr/local/zookeeper-server-02/bin/../conf/zoo.cfg
  11. Client port found: 2182. Client address: localhost. Client SSL: false.
  12. Mode: follower
  13. [root@VM-24-10-centos local]# zookeeper-server-03/bin/zkServer.sh status
  14. /usr/local/jdk1.8/bin/java
  15. ZooKeeper JMX enabled by default
  16. Using config: /usr/local/zookeeper-server-03/bin/../conf/zoo.cfg
  17. Client port found: 2183. Client address: localhost. Client SSL: false.
  18. Mode: follower

可以通过jps(JDK自带的命令)查看Java的进程

  1. [root@VM-24-10-centos ~]# jps
  2. 2292 QuorumPeerMain
  3. 1381 QuorumPeerMain
  4. 1847 QuorumPeerMain
  5. 20687 Jps

安装Redis

Redis英文网址:https://redis.io/
Snipaste_2022-02-10_21-42-21.png
右击复制链接

  1. https://download.redis.io/releases/redis-6.2.6.tar.gz
  1. Linux安装wget命令

    1. yum install wget
  2. cd到自己喜欢的目录,这里是/soft。执行如下命令

    1. cd /soft
    2. wget https://download.redis.io/releases/redis-6.2.6.tar.gz
  3. 解压

    1. tar -zxvf redis-6.2.6.tar.gz
  4. 解压后有个redis-6.2.6目录,cd进去ls一下

    1. [root@VM-24-10-centos redis-6.2.6]# ls
    2. 00-RELEASENOTES COPYING MANIFESTO runtest-cluster src
    3. BUGS deps README.md runtest-moduleapi tests
    4. CONDUCT INSTALL redis.conf runtest-sentinel TLS.md
    5. CONTRIBUTING Makefile runtest sentinel.conf utils

    Redis是C写的,任何软件的安装应该先看README.md 文件,里面详细有安装步骤。按照README.md 文件的内容执行。

  5. 执行make命令

    1. make
  6. 如果报如下错误,需要安装GCC

报错信息

  1. cc: command not found

安装GCC

  1. yum install gcc

安装完成,再清理一下

  1. make distclean

再次执行make命令

  1. cd src/目录下就能看到很多可执行程序了,执行 ./redis-server就能把Redis跑起来了

    1. ./redis-server
  2. 这样启动还是太麻烦而且在前台启动,我们希望能变成一个服务软件在后台启动,变成一个服务,安装到系统里面。

  3. 先安装到一个目录下,比如/usr/local/redis

    1. [root@VM-24-10-centos redis-6.2.6]# make install PREFIX=/usr/local/redis
  4. cd /usr/local/redis目录下,ls一下,会有很多编译好的安装命令,这样不会和源码里面的混合在一起

    1. redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server
  5. 我们期望使用service redis start启动起来变成一个服务,但现在还不支持。现需要修改/etc/profile配置文件,把上面的/usr/local/redis路径加到PATH环境变量里面。

    1. export REDISE_HOME=/usr/local/redis
    2. export PATH=$PATH:$REDISE_HOME/bin
    1. source /etc/profile
    2. echo $PATH
    3. /usr/local/jdk1.8/bin:/usr/local/jdk1.8/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/redis/bin

    这个时候就可以直接使用/usr/local/redis/bin里面的命令了,比如:redis-server

  6. cd到Redis源码目录/soft/redis-6.2.6/utils。ls一下,执行install_server.sh 脚本

    1. build-static-symbols.tcl hyperloglog speed-regression.tcl
    2. cluster_fail_time.tcl install_server.sh srandmember
    3. corrupt_rdb.c lru systemd-redis_multiple_servers@.service
    4. create-cluster redis-copy.rb systemd-redis_server.service
    5. generate-command-help.rb redis_init_script tracking_collisions.c
    6. gen-test-certs.sh redis_init_script.tpl whatisdoing.sh
    7. graphs redis-sha1.rb
    8. hashtable releasetools

    发现报如下错误. ```java [root@VM-24-10-centos utils]# ./install_server.sh Welcome to the redis service installer This script will help you easily set up a running redis server

This systems seems to use systemd. Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!

  1. 解决方案:
  2. ```java
  3. vi ./install_server.sh

注释如下代码

  1. #bail if this system is managed by systemd
  2. #_pid_1_exe="$(readlink -f /proc/1/exe)"
  3. #if [ "${_pid_1_exe##*/}" = systemd ]
  4. #then
  5. # echo "This systems seems to use systemd."
  6. # echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!"
  7. # exit 1
  8. #fi

重新执行./install_server.sh脚本

  1. [root@VM-24-10-centos utils]# ./install_server.sh
  2. Welcome to the redis service installer
  3. This script will help you easily set up a running redis server
  4. This systems seems to use systemd.
  5. Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!
  6. Please select the redis port for this instance: [6379]

需要填一个端口号,一个物理机器可以安装多个Redis,靠端口号来区分,使用默认[6379]即可,直接回车。

  1. Please select the redis config file name [/etc/redis/6379.conf]

提示如下信息,需要配置文件的位置和名称,配置文件随着端口号的变化而变化,都会为每个Redis实例按照端口号生成相应的配置文件,直接一路回车使用默认即可

  1. Please select the redis config file name [/etc/redis/6379.conf]
  2. Selected default - /etc/redis/6379.conf
  3. Please select the redis log file name [/var/log/redis_6379.log]
  4. Selected default - /var/log/redis_6379.log
  5. Please select the data directory for this instance [/var/lib/redis/6379]

上面需要选一个数据存放目录,Redis虽然是基于内存的,但内存数据掉电易失数据,所以需要持久化。回车使用默认的

  1. Please select the redis executable path [/usr/local/redis/bin/redis-server]

需要填写可执行程序的路径,回车

  1. Please select the redis executable path [/usr/local/redis/bin/redis-server]
  2. Selected config:
  3. Port : 6379
  4. Config file : /etc/redis/6379.conf
  5. Log file : /var/log/redis_6379.log
  6. Data dir : /var/lib/redis/6379
  7. Executable : /usr/local/redis/bin/redis-server
  8. Cli Executable : /usr/local/redis/bin/redis-cli
  9. Is this ok? Then press ENTER to go on or Ctrl-C to abort.

按回车

  1. Is this ok? Then press ENTER to go on or Ctrl-C to abort.
  2. Copied /tmp/6379.conf => /etc/init.d/redis_6379
  3. Installing service...
  4. Successfully added to chkconfig!
  5. Successfully added to runlevels 345!
  6. Starting Redis server...
  7. Installation successful!

会在/etc/init.d/目录放一个redis_6379脚本,查看一下Redis的状态,redis_6379为脚本的名称

  1. service redis_6379 status

可以通过./install_server.sh在一台机器上安装多个Redis服务。cd到Redis源码安装目录的utils下,/soft/redis-6.2.6/utils

  1. cd /soft/redis-6.2.6/utils
  2. ./install_server.sh

提示如下信息

  1. [root@VM-24-10-centos utils]# ./install_server.sh
  2. Welcome to the redis service installer
  3. This script will help you easily set up a running redis server
  4. This systems seems to use systemd.
  5. Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!
  6. Please select the redis port for this instance: [6379] 6380

6379已经安装了,给个6380,一路回车即可

  1. Please select the redis port for this instance: [6379] 6380
  2. Please select the redis config file name [/etc/redis/6380.conf]
  3. Selected default - /etc/redis/6380.conf
  4. Please select the redis log file name [/var/log/redis_6380.log]
  5. Selected default - /var/log/redis_6380.log
  6. Please select the data directory for this instance [/var/lib/redis/6380]
  7. Selected default - /var/lib/redis/6380
  8. Please select the redis executable path [/usr/local/redis/bin/redis-server]
  9. Selected config:
  10. Port : 6380
  11. Config file : /etc/redis/6380.conf
  12. Log file : /var/log/redis_6380.log
  13. Data dir : /var/lib/redis/6380
  14. Executable : /usr/local/redis/bin/redis-server
  15. Cli Executable : /usr/local/redis/bin/redis-cli
  16. Is this ok? Then press ENTER to go on or Ctrl-C to abort.
  17. Copied /tmp/6380.conf => /etc/init.d/redis_6380
  18. Installing service...
  19. Successfully added to chkconfig!
  20. Successfully added to runlevels 345!
  21. Starting Redis server...
  22. Installation successful!
  1. [root@VM-24-10-centos utils]# ps -fe | grep redis
  2. root 25896 1 0 22:56 ? 00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:6379
  3. root 27579 1 0 23:08 ? 00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:6380
  4. root 27850 25047 0 23:09 pts/3 00:00:00 grep --color=auto redis

可以通过如下命令启动、关闭、查看状态。redis_6379为具体的Redis服务

  1. service redis_6379 start/stop/status