Apache Kafka监控之Kafka Web Console
Kafka Web Console:是一款开源的系统,源码的地址在https://github.com/claudemamo/kafka-web-console中。Kafka Web Console也是用Scala语言编写的Java web程序用于监控Apache Kafka。这个系统的功能和KafkaOffsetMonitor很类似,但是我们从源码角度来看,这款系统实现比KafkaOffsetMonitor要复杂很多,而且编译配置比KafkaOffsetMonitor较麻烦。
要想运行这套系统我们需要的先行条件为:
- Play Framework 2.2.x
- Apache Kafka 0.8.x
- Zookeeper 3.3.3 or 3.3.4
同样,我们从https://github.com/claudemamo/kafka-web-console上面将源码下载下来,然后用sbt进行编译,在编译前我们需要做如下的修改:
1、Kafka Web Console默认用的数据库是H2,它支持以下几种数据库:
- H2 (default)
- PostgreSql
- Oracle
- DB2
- MySQL
- ApacheDerby
- Microsoft SQL Server
- #############################################################################
- User:过往记忆
- Date:14-08-08
- Time:11:37
- bolg: https://www.iteblog.com
- 本文地址:https://www.iteblog.com/archives/1084
- 过往记忆博客,专注于hadoop、hive、spark、shark、flume的技术博客,大量的干货
- 过往记忆博客微信公共帐号:iteblog_hadoop
- #############################################################################
- 将这个
- db.default.driver=org.h2.Driver
- db.default.url="jdbc:h2:file:play"
- # db.default.user=sa
- # db.default.password=""
- 修改成
- db.default.driver=com.mysql.jdbc.Driver
- db.default.url="jdbc:mysql://localhost:3306/test"
- db.default.user=root
- db.default.pass=123456
- "mysql"%"mysql-connector-java"%"5.1.31"
修改后的bulid.sbt :
- name := "kafka-web-console"
- version := "2.1.0-SNAPSHOT"
- libraryDependencies ++= Seq(
- jdbc,
- cache,
- "org.squeryl" % "squeryl_2.10" % "0.9.5-6",
- "com.twitter" % "util-zk_2.10" % "6.11.0",
- "com.twitter" % "finagle-core_2.10" % "6.15.0",
- "org.quartz-scheduler" % "quartz" % "2.2.1",
- "org.apache.kafka" % "kafka_2.10" % "0.8.1.1",
- "mysql" % "mysql-connector-java" % "5.1.31"
- exclude("javax.jms", "jms")
- exclude("com.sun.jdmk", "jmxtools")
- exclude("com.sun.jmx", "jmxri")
- )
- play.Project.playScalaSettings
2、执行conf/evolutions/default/bak目录下面的1.sql、2.sql和3.sql三个文件。需要注意的是,这三个sql文件不能直接运行,有语法错误,需要做一些修改。
修改后的1.sql :
- CREATE TABLE zookeepers (
- name VARCHAR(100),
- host VARCHAR(100),
- port INT(100),
- statusId INT(100),
- groupId INT(100),
- PRIMARY KEY (name)
- );
- CREATE TABLE groups (
- id INT(100),
- name VARCHAR(100),
- PRIMARY KEY (id)
- );
- CREATE TABLE status (
- id INT(100),
- name VARCHAR(100),
- PRIMARY KEY (id)
- );
- INSERT INTO groups (id, name) VALUES (0, 'ALL');
- INSERT INTO groups (id, name) VALUES (1, 'DEVELOPMENT');
- INSERT INTO groups (id, name) VALUES (2, 'PRODUCTION');
- INSERT INTO groups (id, name) VALUES (3, 'STAGING');
- INSERT INTO groups (id, name) VALUES (4, 'TEST');
- INSERT INTO status (id, name) VALUES (0, 'CONNECTING');
- INSERT INTO status (id, name) VALUES (1, 'CONNECTED');
- INSERT INTO status (id, name) VALUES (2, 'DISCONNECTED');
- INSERT INTO status (id, name) VALUES (3, 'DELETED');
修改后的2.sql :
- ALTER TABLE zookeepers ADD COLUMN chroot VARCHAR(100);
修改后的3.sql :
- ALTER TABLE zookeepers DROP PRIMARY KEY;
- ALTER TABLE zookeepers ADD COLUMN id int(100) NOT NULL AUTO_INCREMENT PRIMARY KEY;
- ALTER TABLE zookeepers MODIFY COLUMN name VARCHAR(100) NOT NULL;
- ALTER TABLE zookeepers MODIFY COLUMN host VARCHAR(100) NOT NULL;
- ALTER TABLE zookeepers MODIFY COLUMN port INT(100) NOT NULL;
- ALTER TABLE zookeepers MODIFY COLUMN statusId INT(100) NOT NULL;
- ALTER TABLE zookeepers MODIFY COLUMN groupId INT(100) NOT NULL;
- ALTER TABLE zookeepers ADD UNIQUE (name);
- CREATE TABLE offsetHistory (
- id int(100) AUTO_INCREMENT PRIMARY KEY,
- zookeeperId int(100),
- topic VARCHAR(255),
- FOREIGN KEY (zookeeperId) REFERENCES zookeepers(id),
- UNIQUE (zookeeperId, topic)
- );
- CREATE TABLE offsetPoints (
- id int(100) AUTO_INCREMENT PRIMARY KEY,
- consumerGroup VARCHAR(255),
- timestamp TIMESTAMP,
- offsetHistoryId int(100),
- partition int(100),
- offset int(100),
- logSize int(100),
- FOREIGN KEY (offsetHistoryId) REFERENCES offsetHistory(id)
- );
- CREATE TABLE settings (
- key_ VARCHAR(255) PRIMARY KEY,
- value VARCHAR(255)
- );
- INSERT INTO settings (key_, value) VALUES ('PURGE_SCHEDULE', '0 0 0 ? * SUN *');
- INSERT INTO settings (key_, value) VALUES ('OFFSET_FETCH_INTERVAL', '30');
project/build.properties的
sbt.version=0.13.0 要修改实际的sbt版本,比如我用的是sbt.version=0.13.15
上面的注意事项弄完之后,我们就可以编译下载过来的源码:
# sbt package |
编译的过程比较慢,有些依赖包下载速度非常地慢,请耐心等待。
- [warn]modulenot found: com.typesafe.play#sbt-plugin;2.2.1
- [warn]==== typesafe-ivy-releases: tried
- [warn] http://repo.typesafe.com/typesafe/ivy-releases/
- com.typesafe.play/sbt-plugin/scala_2.9.2/sbt_0.12/2.2.1/ivys/ivy.xml
- [warn]==== sbt-plugin-releases: tried
- [warn] http://scalasbt.artifactoryonline.com/scalasbt/sbt-plugin-releases/
- com.typesafe.play/sbt-plugin/scala_2.9.2/sbt_0.12/2.2.1/ivys/ivy.xml
- [warn]====local: tried
- [warn]/home/iteblog/.ivy2/local/com.typesafe.play/
- sbt-plugin/scala_2.9.2/sbt_0.12/2.2.1/ivys/ivy.xml
- [warn]====Typesafe repository: tried
- [warn] http://repo.typesafe.com/typesafe/releases/com/
- typesafe/play/sbt-plugin_2.9.2_0.12/2.2.1/sbt-plugin-2.2.1.pom
- [warn]====public: tried
- [warn] http://repo1.maven.org/maven2/com/typesafe/play/
- sbt-plugin_2.9.2_0.12/2.2.1/sbt-plugin-2.2.1.pom
- [warn]::::::::::::::::::::::::::::::::::::::::::::::
- ====local: tried
- /home/iteblog/.ivy2/local/org.scala-sbt/collections/0.13.0/jars/collections.jar
- ::::::::::::::::::::::::::::::::::::::::::::::
- :: FAILED DOWNLOADS ::
- ::^ see resolution messages for details ^::
- ::::::::::::::::::::::::::::::::::::::::::::::
- :: org.scala-sbt#collections;0.13.0!collections.jar
- ::::::::::::::::::::::::::::::::::::::::::::::
启动的时候需要把.sql文件删除掉,否则会报错。
http://localhost:9000最后,我们可以通过下面命令启动Kafka Web Console监控系统:
# sbt run |
并可以在http://localhost:9000查看。下面是一张效果图
-
Before you can monitor a broker, you need to register the Zookeeper server associated with it:
Kafka Web Console
Kafka Web Console is a Java web application for monitoring Apache Kafka. With a modern web browser, you can view from the console:
- Registered brokers
- Topics, partitions, log sizes, and partition leaders
- Consumer groups, individual consumers, consumer owners, partition offsets and lag
- Graphs showing consumer offset and lag history as well as consumer/producer message throughput history.
- Latest published topic messages (requires web browser support for WebSocket)
Furthermore, the console provides a JSON API described in RAML. The API can be tested using the embedded API Console accessible through the URL http://[hostname]:[port]/api/console.
Requirements
- Play Framework 2.2.x
- Apache Kafka 0.8.x
- Zookeeper 3.3.3 or 3.3.4
Deployment
Consult Play!'s documentation for deployment options and instructions.
Getting Started
-
Kafka Web Console requires a relational database. By default, the server connects to an embedded H2 database and no database installation or configuration is needed. Consult Play!'s documentation to specify a database for the console. The following databases are supported:
- H2 (default)
- PostgreSql
- Oracle
- DB2
- MySQL
- Apache Derby
- Microsoft SQL Server
Changing the database might necessitate making minor modifications to the DDL to accommodate the new database.
-
Before you can monitor a broker, you need to register the Zookeeper server associated with it:
Filling in the form and clicking on Connect will register the Zookeeper server. Once the console has successfully established a connection with the registered Zookeeper server, it can retrieve all necessary information about brokers, topics, and consumers: