赞
踩
作者:高广超
链接:https://www.jianshu.com/p/3dc01a283c89
本文默认读者已经对Docker有一定了解,且清楚使用Docker进行部署的优势。
官网: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
image.png
1.3双击Docker.app启动
image.png
Mac顶部状态栏会出现鲸鱼图标
image.png
1.4点击鲸鱼图标可以进行设置
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/
image.png
常用命令:
docker ps 查看正在运行的容器docker stop停止正在运行的容器docker start启动容器docker ps -a查看终止状态的容器docker rm -f webserver命令来移除正在运行的容器docker list 列出本地镜像docker rmi 删除的镜像
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 配置挂载路径 宿主机路径:容器内的路径
关于挂载
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 浏览器访问
image.png
在调试过程中往往不会很顺利,这里的技巧是通过阅读error.log中的异常日志进行
2.4 配置反向代理
此处是本机启动一个 SpringBoot web server,端口为:8080,浏览器访问:http://localhost:8080/index/hello
image.png
按照上节中nginx.conf示例中的配置方式,增加upstream、server、proxy_pass相关配置,对80端口进行监听,重启nginx容器。
docker restart mynginx
浏览器访问:http://localhost/index/hello,可以看到正常访问。
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基本一致,在此不再赘述。
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端口
输出启动日志:
image.png
4.3 Check zookeeper是否启动
可以使用一些可视化客户端连接端口,进行监控,如zooInspector、Idea Zookeeper Plugin等
zooInspector示例
Idea Zookeeper Plugin
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端口
输出启动日志:
image.png
5.3 访问Kafka Manager
浏览器访问:http://localhost:9000
按照页面上的操作按钮进行kafka集群的注册,具体使用方式再次不做详细介绍。
image.png
注册配置后的界面:
image.png
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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。