当前位置:   article > 正文

docker 安装kafka_使用Docker(Mac)搭建 Nginx/Openresty - Kafka - kafkaManager

mac 安装kafka-manager

作者:高广超

链接:https://www.jianshu.com/p/3dc01a283c89

本文默认读者已经对Docker有一定了解,且清楚使用Docker进行部署的优势。

1.安装Docker(Mac)

官网:https://docs.docker.com/docker-for-mac/install/

1.1 下载 Docker for Mac

地址:https://store.docker.com/editions/community/docker-ce-desktop-mac

1.2 下载完成以后,双击打开文件Docker.dmg

e13ce56fd0d66ddf57bfb1f1524792c2.png

image.png

1.3双击Docker.app启动

774f0c550e3acde27a3589d9a7f441e5.png

image.png

Mac顶部状态栏会出现鲸鱼图标

6457a1108902016cce88a0f5bb544188.png

image.png

1.4点击鲸鱼图标可以进行设置

d066050a0babb7240d811b6edd65394b.png

image.png

1.5 Check versions

$ docker --versionDocker version 18.03, build c97c6d6$ docker-compose --versiondocker-compose version 1.21.2, build 8dd22a9$ docker-machine --versiondocker-machine version 0.14.0, build 9ba6da9

1.6 Hello Word

1.6.1 打开命令行终端,通过运行简单的Docker映像测试您的安装工作。

$ docker run hello-worldUnable to find image 'hello-world:latest' locallylatest: Pulling from library/hello-worldca4f61b1923c: Pull completeDigest: sha256:ca0eeb6fb05351dfc8759c20733c91def84cb8007aa89a5bf606bc8b315b9fc7Status: Downloaded newer image for hello-world:latestHello from Docker!This message shows that your installation appears to be working correctly....

1.6.2 启动Dockerized web server

$ docker run -d -p 80:80 --name webserver nginx

1.6.3 打开浏览器,输入http://localhost/

5452827cf0edf53b54f0f0a623a08982.png

image.png

常用命令:

docker ps 查看正在运行的容器docker stop停止正在运行的容器docker start启动容器docker ps -a查看终止状态的容器docker rm -f webserver命令来移除正在运行的容器docker list 列出本地镜像docker rmi 删除的镜像

2.使用Docker安装Nginx

Docker Store 地址:https://store.docker.com/images/nginx

其实在上文中Hello World即已经安装了nginx。

2.1 拉取 image

docker pull nginx

3.2 创建Nginx容器

docker run --name mynginx -p 80:80 -v /Users/gaoguangchao/Work/opt/local/nginx/logs:/var/log/nginx -v /Users/gaoguangchao/Work/opt/local/nginx/conf.d:/etc/nginx/conf.d -v /Users/gaoguangchao/Work/opt/local/nginx/nginx.conf:/etc/nginx/nginx.conf:ro -v /Users/gaoguangchao/Work/opt/local/nginx/html:/etc/nginx/html -d nginx

-d 以守护进程运行(运行在后台)

--name nginx 容器名称;

-p 80:80 端口映射

-v 配置挂载路径 宿主机路径:容器内的路径

关于挂载

  1. 为了能直接修改配置文件,以实现对Nginx的定制化,需要进行Docker的相关目录挂在宿主机上。
  2. 需要挂载的目录/文件:/etc/nginx/conf.d /etc/nginx/nginx.conf /etc/nginx/html
  3. 有一点尤其需要注意,当挂载的为文件而非目录时,需要注意以下两点:
  • a. 挂载文件命令: -v 宿主机路径:容器内的路径:ro
  • b.宿主机需要先创建后文件,无法自动创建,反之将报错

nginx.conf 示例

#user nobody;worker_processes 1;#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream demo { server 127.0.0.1:8080; } server { listen 80; server_name request_log; location / { root html; #index index.html index.htm; proxy_connect_timeout 3;  proxy_send_timeout 30;  proxy_read_timeout 30;  proxy_pass http://demo;  }  #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }}

2.3 浏览器访问

5452827cf0edf53b54f0f0a623a08982.png

image.png

在调试过程中往往不会很顺利,这里的技巧是通过阅读error.log中的异常日志进行

2.4 配置反向代理

此处是本机启动一个 SpringBoot web server,端口为:8080,浏览器访问:http://localhost:8080/index/hello

af17f52a0207c3ca883d25c665b5a424.png

image.png

按照上节中nginx.conf示例中的配置方式,增加upstream、server、proxy_pass相关配置,对80端口进行监听,重启nginx容器。

docker restart mynginx

浏览器访问:http://localhost/index/hello,可以看到正常访问。

3.使用Docker安装Openresty

Openresty是在Nginx基础上做了大量的定制扩展,其安装过程和Nginx基本一致。

Docker Store 地址:https://store.docker.com/community/images/openresty/openresty

3.1 拉取 image

docker pull openresty/openresty

3.2 创建Openresty容器

docker run -d --name="openresty" -p 80:80 -v /Users/gaoguangchao/Work/opt/local/openresty/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf:ro -v /Users/gaoguangchao/Work/opt/local/openresty/logs:/usr/local/openresty/nginx/logs -v /Users/gaoguangchao/Work/opt/local/openresty/conf.d:/etc/nginx/conf.d -v /Users/gaoguangchao/Work/opt/local/openresty/html:/etc/nginx/html openresty/openresty

注意事项和安装Nginx基本一致,在此不再赘述。

4.使用Docker安装Kafka

Docker Store 地址:https://store.docker.com/community/images/spotify/kafka

4.1 拉取 image

docker pull spotify/kafka

4.2 创建Kafka容器

运行命令:

docker run -p 2181:2181 -p 9092:9092 --env ADVERTISED_HOST=`127.0.0.1` --env ADVERTISED_PORT=9092 spotify/kafka

2181为zookeeper端口,9092为kafka端口

输出启动日志:

40c435765d90f7787a27ef9d1de1497f.png

image.png

4.3 Check zookeeper是否启动

可以使用一些可视化客户端连接端口,进行监控,如zooInspector、Idea Zookeeper Plugin等

e0b3355b14fafc891c98d3aa29d71b9d.png

zooInspector示例

7b9c0a8e41f15df06b0980a810157da3.png

Idea Zookeeper Plugin

5.使用Docker安装Kafka Manager

Kafka Manager 是Yahoo开源的kafka监控和配置的web系统,可以进行kafka的日常监控和配置的动态修改。

Docker Store 地址:https://store.docker.com/community/images/sheepkiller/kafka-manager

5.1 拉取 image

docker pull sheepkiller/kafka-manager

5.2 创建Kafka Manager容器

运行命令:

docker run -it --rm -p 9000:9000 -e ZK_HOSTS="127.0.0.1:2181" -e APPLICATION_SECRET=letmein sheepkiller/kafka-manager

2181为上节中部署的zookeeper端口,9000为kafka-manager的web端口

输出启动日志:

4711a9203b1e472f8c512ae26aa34de9.png

image.png

5.3 访问Kafka Manager

浏览器访问:http://localhost:9000

按照页面上的操作按钮进行kafka集群的注册,具体使用方式再次不做详细介绍。

ecae7e5cdb1d0a9277dba3af5aee1cff.png

image.png

注册配置后的界面:

5f9618b30d00074e1f51968b4d2f1463.png

image.png

6.Kafka消息生产与消费

6.1创建maven项目

** pom依赖**

 org.slf4j jcl-over-slf4j ${org.slf4j-version}runtimeorg.apache.logging.log4j log4j-1.2-api ${log4j2-version}org.apache.logging.log4j log4j-slf4j-impl ${log4j2-version}org.apache.logging.log4j log4j-api ${log4j2-version}org.apache.logging.log4j log4j-core ${log4j2-version}com.lmax disruptor 3.2.0org.apache.kafka kafka-clients 0.10.1.0

6.2 增加log4j2配置

配置log4j2为能正常打印debug日志,方便进行异常排查 (重要)

在resources目录下增加log4j2.xml文件

<?xml version="1.0" encoding="UTF-8"?>%d %-5p (%F:%L) - %m%n/logs

关于log4j2的使用,有兴趣的可以了解:Log4j1升级Log4j2实战

6.3 创建生产者示例

package com.moko.kafka;import org.apache.kafka.clients.producer.*;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.util.Properties;public class MokoProducer extends Thread { private static final Logger LOGGER = LoggerFactory.getLogger(MokoProducer.class); private final KafkaProducer producer; private final String topic; private final boolean isAsync; public MokoProducer(String topic, boolean isAsync) { Properties properties = new Properties(); properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "78c4f4a0f989:9092");//broker 集群地址 properties.put(ProducerConfig.CLIENT_ID_CONFIG, "MokoProducer");//自定义客户端id properties.put(ProducerConfig.ACKS_CONFIG, "all"); properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");//key 序列号方式 properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");//value 序列号方式 this.producer = new KafkaProducer(properties); this.topic = topic; this.isAsync = isAsync; } @Override public void run() { int seq = 0; while (true) { String msg = "Msg: " + seq; if (isAsync) {//异步 producer.send(new ProducerRecord(this.topic, msg)); } else {//同步 producer.send(new ProducerRecord(this.topic, msg), new MsgProducerCallback(msg)); } seq++; try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } /** * 消息发送后的回调函数 */ class MsgProducerCallback implements Callback { private final String msg; public MsgProducerCallback(String msg) { this.msg = msg; } public void onCompletion(RecordMetadata recordMetadata, Exception e) { if (recordMetadata != null) { LOGGER.info(msg + " be sended to partition no : " + recordMetadata.partition()); } else { LOGGER.info("recordMetadata is null"); } if (e != null) e.printStackTrace(); } } public static void main(String args[]) { new MokoProducer("access-log
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/192854?site
推荐阅读
相关标签
  

闽ICP备14008679号