跳到主要内容

如何安装项目需要的中间件环境

plus 版本专属

此章节是黑马点评 Plus 版本中专有的内容,而在整套文档中将普通版本和 Plus 版本都融合在了一起,让大家更方便的学习。

概要

项目中设计到的中间件有Mysql、Redis、Kafka,本文介绍如何在本地进行安装这些中间件,以及如何使用本人提供的中间件环境

为了考虑到小伙伴的时间和学习项目的简便,本人提供了除了Mysql以外(数据库实在是没法提供,多人使用数据就乱套了),其余中间件的云环境地址,可直接连接相应的地址即可,但由于给多人使用所以稳定性要差点,如果条件允许的话,还是建议大家本地搭建一套

中间件本地环境安装

安装Mysql

注意:Mysql推荐使用普通安装方式,docker是次要推荐

配置文件 my.cnf

[client]
default_character_set=utf8
[mysqld]
collation_server = utf8_general_ci
character_set_server = utf8

下载镜像

docker pull mysql:5.7

启动容器

docker run -d \
-p 3306:3306 \
--privileged=true \
-v /mysql/log:/var/log/mysql \
-v /mysql/data:/var/lib/mysql \
-v /mysql/conf:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=root \
--name mysql mysql:5.7
  • -p 3306:3306 映射宿主机和容器的端口号
  • --privileged=true 赋予容器几乎与主机相同的权限
  • -v /mysql/log:/var/log/mysql 将Mysql的日志卷持久化到宿主机
  • -v /mysql/data:/var/lib/mysql 将Mysql的数据卷持久化到宿主机
  • -v /mysql/conf:/etc/mysql/conf.d 指定Mysql的配置从宿主机中读取,将第一步的my.cnf 放入宿主机/mysql/conf目录
  • -e MYSQL_ROOT_PASSWORD=root设置数据库密码
  • --name mysql 容器名为mysql

安装Redis

下载镜像

docker pull redis:6.0.8

启动容器

docker run -d --name redis -p 6379:6379 -e "requirepass=123" redis:6.0.8
  • -e "requirepass=123" 为密码

安装Zookeeper

下载镜像

docker search zookeeper
docker pull zookeeper

配置文件

新建文件夹

mkdir -p /usr/local/zookeeper/conf

将zookeeper的配置文件放入此文件夹内

配置文件 文件名zoo.cfg
# The number of  milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/data
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true

启动容器

docker run -d \
--name zookeeper \
--privileged=true \
-p 2181:2181 \
--restart=always \
-v /usr/local/zookeeper/data:/data \
-v /usr/local/zookeeper/conf:/conf \
-v /usr/local/zookeeper/logs:/datalog \
zookeeper

安装Kafka

下载镜像

docker search wurstmeister/kafka
docker pull wurstmeister/kafka

启动容器

docker run -d --name kafka \
-p 9092:9092 \
-e KAFKA_BROKER_ID=0 \
-e KAFKA_ZOOKEEPER_CONNECT=zookeeper地址:2181 \
-e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://本地ip:9092 \
-e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 wurstmeister/kafka
  • -e KAFKA_ZOOKEEPER_CONNECT 指定zookeeper地址,如果zookeeper和kafka部署在一台云服务器,则地址可配置内网ip
  • -e KAFKA_ADVERTISED_LISTENERS Kafka 的一个环境变量,用于指定 Kafka 将如何通告客户端它在外部可访问的地址。如果是云服务器,则得是公网ip,因为客户端连接的kafka地址要和这个一样