Mac

Mac安装配置管理Memcached和Zookeeper

零 背景说明

近期进入公司内部一个Java项目组,该项目中需要用到Memcached和Zookeeper。之前,我都是在Linux服务器上配置安装好这两个软件,然后从Git服务器上clone代码,然后本地开发环境的代码中,将它们的连接和配置信息,指向Linux服务器上的配置。本地机器上并不安装这2个软件,现在想着还是在本地机器上安装配置好这2个软件,然后开发环境的代码中不做变动和配置更改,clone项目代码到本地开发机器上,直接debug启动测试,进入开发。

一 安装配置管理Zookeeper

1 下载解压Zookeeper软件

Zookeeper官网地址:https://zookeeper.apache.org/

下载地址:https://zookeeper.apache.org/releases.html

下载对应OS版本的软件,并解压。下载到/Users/asher/Downloads路径下,通过tar -zxvf 命令,解压到/Users/asher/Documents路径下:

$ ls -lrt /Users/asher/Downloads/apache-zookeeper-3.7.0-bin.tar.gz 
-rw-r--r--@ 1 asher  staff  12387614 Jul  8 14:09 /Users/asher/Downloads/apache-zookeeper-3.7.0-bin.tar.gz

asher at MacBook-Air-3 in ~
$ ls -lrt /Users/asher/Documents/apache-zookeeper-3.7.0-bin/
total 48
drwxr-xr-x@ 17 asher  staff    544 Mar 17 17:45 bin
-rw-r--r--@  1 asher  staff   3570 Mar 17 17:45 README_packaging.md
-rw-r--r--@  1 asher  staff   2214 Mar 17 17:45 README.md
-rw-r--r--@  1 asher  staff    432 Mar 17 17:45 NOTICE.txt
-rw-r--r--@  1 asher  staff  11358 Mar 17 17:45 LICENSE.txt
drwxr-xr-x@ 25 asher  staff    800 Mar 17 17:45 docs
drwxr-xr-x@ 60 asher  staff   1920 Jul  8 14:09 lib
drwxr-xr-x   4 asher  staff    128 Jul  8 14:11 logs
drwxr-xr-x@  6 asher  staff    192 Jul  8 14:11 conf

asher at MacBook-Air-3 in ~
$ 
2 编辑修改配置文件

默认情况下,在解压出来的路径下,apache-zookeeper-3.7.0-bin/conf/有一个名为系统自带的 zoo_sample.cfg配置文件,我们将该文件重命名为 zoo.cfg,或者利用该文件copy生成一个新的名为zoo.cfg的配置文件 。

为啥这么做呢?因为,默认启动Zookeeper的时候,会自动读取解压路径下的conf路径下的名为zoo.cfg配置文件。另外,生成新的zoo.cfg配置文件之后,暂时不需要对该文件做其它配置。有需要的时候,再做调整和修改。

否则的话,你在启动Zookeeper的时候,可能会遇到下述错误:

$ ./zkServer.sh start &
[1] 80924

asher at MacBook-Air-3 in ~/Documents/apache-zookeeper-3.7.0-bin/bin
$ ZooKeeper JMX enabled by default
Using config: /Users/asher/Documents/apache-zookeeper-3.7.0-bin/bin/../conf/zoo.cfg
grep: /Users/asher/Documents/apache-zookeeper-3.7.0-bin/bin/../conf/zoo.cfg: No such file or directory
grep: /Users/asher/Documents/apache-zookeeper-3.7.0-bin/bin/../conf/zoo.cfg: No such file or directory
mkdir: : No such file or directory
Starting zookeeper ... ./zkServer.sh: line 175: /zookeeper_server.pid: Permission denied
FAILED TO WRITE PID

[1]+  Exit 1                  ./zkServer.sh start

asher at MacBook-Air-3 in ~/Documents/apache-zookeeper-3.7.0-bin/bin
$
3 启动Zookeeper

进入/Users/asher/Documents/apache-zookeeper-3.7.0-bin/bin路径下,通过执行zkServer.sh脚本文件来启动和管理Zookeeper。

$ ./zkServer.sh 
ZooKeeper JMX enabled by default
Using config: /Users/asher/Documents/apache-zookeeper-3.7.0-bin/bin/../conf/zoo.cfg
Usage: ./zkServer.sh [--config <conf-dir>] {start|start-foreground|stop|version|restart|status|print-cmd}

asher at MacBook-Air-3 in ~/Documents/apache-zookeeper-3.7.0-bin/bin
$ ./zkServer.sh version
ZooKeeper JMX enabled by default
Using config: /Users/asher/Documents/apache-zookeeper-3.7.0-bin/bin/../conf/zoo.cfg
Apache ZooKeeper, version 3.7.0 2021-03-17 09:46 UTC

asher at MacBook-Air-3 in ~/Documents/apache-zookeeper-3.7.0-bin/bin
$ ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /Users/asher/Documents/apache-zookeeper-3.7.0-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Error contacting service. It is probably not running.

asher at MacBook-Air-3 in ~/Documents/apache-zookeeper-3.7.0-bin/bin
$ ./zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /Users/asher/Documents/apache-zookeeper-3.7.0-bin/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

asher at MacBook-Air-3 in ~/Documents/apache-zookeeper-3.7.0-bin/bin
$ ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /Users/asher/Documents/apache-zookeeper-3.7.0-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: standalone

asher at MacBook-Air-3 in ~/Documents/apache-zookeeper-3.7.0-bin/bin
$ 

后续,可以直接启动运行在后台:

$ ./zkServer.sh start &
[1] 81042

asher at MacBook-Air-3 in ~/Documents/apache-zookeeper-3.7.0-bin/bin
$ ZooKeeper JMX enabled by default
Using config: /Users/asher/Documents/apache-zookeeper-3.7.0-bin/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

[1]+  Done                    ./zkServer.sh start

asher at MacBook-Air-3 in ~/Documents/apache-zookeeper-3.7.0-bin/bin
$ 

二 安装配置启动Memcached

由于Mac上安装Memcached的方式很多,我这里选择采用编译源码的方式进行安装。编译Memcached源代码的方式进行安装,有两个基本前提条件:

Mac上已经有C编译器;

Mac上已经安装lib events library;

如果你的系统行,没有安装lib events library的话,那么在后面编译Memcached进行安装的时候,你可能会遇到下述错误:

$ cd memcached-1.6.9

asher at MacBook-Air-3 in ~/Documents/memcached-1.6.9
$ ./configure && make && make test 
checking build system type... x86_64-apple-darwin18.7.0
...
checking for library containing gethostbyname... none required
checking for libevent directory... configure: error: libevent is required.  You can get it from https://www.monkey.org/~provos/libevent/

      If it's already installed, specify its path using --with-libevent=/dir/


asher at MacBook-Air-3 in ~/Documents/memcached-1.6.9
$ make install
make: *** No rule to make target `install'.  Stop.

asher at MacBook-Air-3 in ~/Documents/memcached-1.6.9
$ 
1 检查C编译器
$ which gcc
/usr/bin/gcc

asher at MacBook-Air-3 in ~
$ gcc -v
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

asher at MacBook-Air-3 in ~
$ 

如果没有C编译器的话,则需要先安装它。可以通过安装苹果自家的XCode软件,进而自动安装C编译器。

2 检查并安装lib events library

下载解压lib events library:

$ curl -OL https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/libevent-2.1.8-stable.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   632  100   632    0     0    922      0 --:--:-- --:--:-- --:--:--   921
100 1002k  100 1002k    0     0   771k      0  0:00:01  0:00:01 --:--:--  771k

asher at MacBook-Air-3 in ~
$ tar -xvf libevent-2.1.8-stable.tar.gz 
x libevent-2.1.8-stable/
...
asher at MacBook-Air-3 in ~
$

编译安装lib events library:

$ cd libevent-2.1.8-stable

asher at MacBook-Air-3 in ~/libevent-2.1.8-stable
$ ./configure --prefix=/usr/local/
checking for a BSD-compatible install... /usr/local/bin/ginstall -c
...
$ make
  GEN      test/rpcgen-attempted
...
asher at MacBook-Air-3 in ~/libevent-2.1.8-stable
$ make install
/Library/Developer/CommandLineTools/usr/bin/make  install-am
...
 /usr/local/bin/ginstall -c -m 644 libevent.pc libevent_core.pc libevent_extra.pc libevent_pthreads.pc '/usr/local/lib/pkgconfig'

asher at MacBook-Air-3 in ~/libevent-2.1.8-stable
$

安装之后,可以通过otool来检查libevent。

$ otool -L /usr/local/lib/libevent.a 
Archive : /usr/local/lib/libevent.a
/usr/local/lib/libevent.a(buffer.o):
/usr/local/lib/libevent.a(bufferevent.o):
/usr/local/lib/libevent.a(bufferevent_filter.o):
/usr/local/lib/libevent.a(bufferevent_pair.o):
/usr/local/lib/libevent.a(bufferevent_ratelim.o):
/usr/local/lib/libevent.a(bufferevent_sock.o):
/usr/local/lib/libevent.a(event.o):
/usr/local/lib/libevent.a(evmap.o):
/usr/local/lib/libevent.a(evthread.o):
/usr/local/lib/libevent.a(evutil.o):
/usr/local/lib/libevent.a(evutil_rand.o):
/usr/local/lib/libevent.a(evutil_time.o):
/usr/local/lib/libevent.a(listener.o):
/usr/local/lib/libevent.a(log.o):
/usr/local/lib/libevent.a(select.o):
/usr/local/lib/libevent.a(poll.o):
/usr/local/lib/libevent.a(kqueue.o):
/usr/local/lib/libevent.a(signal.o):
/usr/local/lib/libevent.a(evdns.o):
/usr/local/lib/libevent.a(event_tagging.o):
/usr/local/lib/libevent.a(evrpc.o):
/usr/local/lib/libevent.a(http.o):

asher at MacBook-Air-3 in ~
$ 
3 下载、安装、启动Memcached

Memcached官网:https://memcached.org/

下载解压:

$ cp Downloads/memcached-1.6.9.tar.gz ~/Documents/

asher at MacBook-Air-3 in ~
$ cd Documents/

asher at MacBook-Air-3 in ~/Documents
$ tar -zxvf memcached-1.6.9.tar.gz 
x memcached-1.6.9/
...

执行安装:

参照官网给的安装命令:https://memcached.org/downloads

wget http://memcached.org/latest
tar -zxvf memcached-1.x.x.tar.gz
cd memcached-1.x.x
./configure && make && make test && sudo make install
$ cd memcached-1.6.9

asher at MacBook-Air-3 in ~/Documents/memcached-1.6.9
$ ./configure  && make && make test && sudo make install
checking build system type... x86_64-apple-darwin18.7.0
checking host system type... x86_64-apple-darwin18.7.0
...
中间执行make test过程中出现失败,导致后面的安装命令没执行。这里先跳过测试,直接重新执行安装的命令。
$  make install
/Library/Developer/CommandLineTools/usr/bin/make  install-recursive
Making install in doc
/Library/Developer/CommandLineTools/usr/bin/make  install-am
make[4]: Nothing to be done for `install-exec-am'.
 /usr/local/bin/gmkdir -p '/usr/local/share/man/man1'
 /usr/local/bin/ginstall -c -m 644 memcached.1 '/usr/local/share/man/man1'
 /usr/local/bin/gmkdir -p '/usr/local/bin'
  /usr/local/bin/ginstall -c memcached '/usr/local/bin'
 /usr/local/bin/gmkdir -p '/usr/local/include/memcached'
 /usr/local/bin/ginstall -c -m 644 protocol_binary.h '/usr/local/include/memcached'

asher at MacBook-Air-3 in ~/Documents/memcached-1.6.9
$ 

启动Memcached:

$ which memcached
/usr/local/bin/memcached

asher at MacBook-Air-3 in ~/Documents/memcached-1.6.9
$ memcached -d

asher at MacBook-Air-3 in ~/Documents/memcached-1.6.9
$ 

三 小结

至此,在Mac上完成了对Zookeeper和Memcached软件的安装配置和启动。

四 参考

How to install Memcached Server on Mac OS

https://stackoverflow.com/questions/2184775/getting-a-list-of-used-libraries-by-a-running-process-unix

留言