如何检查 MySQL 和 Tomcat 是否正在运行?
我创建了一个 Java 应用程序,该应用程序分为不同的子组件,每个子组件都在单独的 Tomcat 实例上运行。此外,某些组件通过 Hibernate 使用 MySQL 数据库。
我现在正在创建一个管理控制台,在其中报告所有 Tomcat 实例和 MySQL 的状态。我不需要详细信息,但知道它们是否正在运行就足够了。
做到这一点的最佳解决方案是什么?
谢谢
I've created a Java application that is split in different subcomponents, each of those runs on a separate Tomcat instance. Also, some components use a MySQL db through Hibernate.
I'm now creating an administration console where it's reported the status of all my Tomcat instances and of MySQL. I don't need detailed information, but knowing if they are running or not it's enough.
What could be the best solution to do that?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
最直接的方法是连接服务器并查看是否成功。
MySQL:
Tomcat:
如果您想要更具体的状态,例如检查某个数据库表是否可用或特定的 Web 应用程序资源是否可用,那么您必须触发更具体的 SELECT 语句或 HTTP 请求分别。
Most straightforward way would be to just connect the server and see if it succeeds.
MySQL:
Tomcat:
If you want a bit more specific status, e.g. checking if a certain DB table is available or a specific webapp resource is available, then you have to fire a more specific
SELECT
statement or HTTP request respectively.我假设您提前知道正在运行的端口(或从配置文件中知道)。最简单的检查方法是像 telnet 程序一样与这些端口建立套接字连接。类似于:
用法:
但是,我想说这是一个误报检查。有时它说服务器已启动,但服务器卡住或没有响应...
I assume that you know the ports of which are running in advance (or from configuration files). The easiest way to check is to make socket connections to those ports like a telnet program does. Something like:
Usage:
However, I would say that is a false-negative check.. Sometimes it says server UP but the server is stuck or not responding...
我会确保监视您设置的内容实际上是在执行一些代码。事后通过 jmx 监控 JVM 也很有帮助。查看 http://www.cacti.net/ 。
I would make sure that what ever monitoring you setup is actually exercising some code. Monitoring the JVM via jmx can also be helpful after the fact. Check out http://www.cacti.net/ .
通过 MySQL 触发一个简单的固定查询
,并让 .jsp 返回
a-ok
文本。如果超时和/或未响应a-ok
,则表示有问题。如果您需要更详细的信息,可以添加额外的检查,例如请求now()
或更大的检查,例如SHOW INNODB STATUS
。Firing a simple fixed query through MySQL
and have the .jsp return that
a-ok
text. If it times out and/or doesn't respond witha-ok
, then something's hinky. If you need something more detailed, you can add extra checks, like requestingnow()
or something bigger, likeSHOW INNODB STATUS
.最简单的事情是查找 MySQL 和 Tomcat PID 文件。您需要查看启动脚本以确保确切的位置,但一旦找到它,您只需测试 pid 文件是否存在即可。
The easiest thing is to look for the MySQL and Tomcat PID files. You need to look at your start scripts to make sure of the exact location, but once you find it, you simply test for existence of the pid file.
创建 servlet 作为状态页面。在servlet中执行一个廉价的查询,如果查询成功让servlet打印OK,否则打印Error。将 servlet 放入战争中并将其部署到所有实例。
这可以用于通过对所有实例进行循环来检查您的管理控制台。
Create a servlet as a status page. In the servlet perform a cheap query, if the query succeeds let the servlet print OK otherwise Error. Put the servlet into a war and deploy it to all instances.
This could be used for checks in yor admin console by doing a loop over all instances.
我将创建一个简单的 REST Web 服务,该服务在每个 Tomcat 实例上运行并对数据库执行无操作查询。这使得从任何地方(命令行、Web 应用程序、GUI 应用程序等)轻松驱动。
如果这些是公开可用的服务器,您可以使用 binarycanary.com 等服务来轮询应用程序中的页面或服务。
I'd create a simple REST webservice that runs on each Tomcat instance and does a no-op query against the database. That makes it easy to drive from anywhere (command line, web app, GUI app, etc.)
If these are publicly available servers you can use a service like binarycanary.com to poll a page or service in your app.