如何检查 MySQL 和 Tomcat 是否正在运行?

发布于 2024-09-14 03:33:00 字数 209 浏览 8 评论 0原文

我创建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

几味少女 2024-09-21 03:33:00

最直接的方法是连接服务器并查看是否成功。

MySQL:

Connection connection = null;
try {
    connection = DriverManager.getConnection(url, username, password);
    // Succes!
} catch (SQLException e) {
    // Fail!
} finally {
    if (connection != null) try { connection.close(); } catch (SQLException ignore) {}
}

Tomcat:

try {
    new URL(url).openConnection().connect();
    // Succes!
} catch (IOException e) {
    // Fail!
}

如果您想要更具体的状态,例如检查某个数据库表是否可用或特定的 Web 应用程序资源是否可用,那么您必须触发更具体的 SELECT 语句或 HTTP 请求分别。

Most straightforward way would be to just connect the server and see if it succeeds.

MySQL:

Connection connection = null;
try {
    connection = DriverManager.getConnection(url, username, password);
    // Succes!
} catch (SQLException e) {
    // Fail!
} finally {
    if (connection != null) try { connection.close(); } catch (SQLException ignore) {}
}

Tomcat:

try {
    new URL(url).openConnection().connect();
    // Succes!
} catch (IOException e) {
    // Fail!
}

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.

关于从前 2024-09-21 03:33:00

我假设您提前知道正在运行的端口(或从配置文件中知道)。最简单的检查方法是像 telnet 程序一样与这些端口建立套接字连接。类似于:

public boolean isServerUp(int port) {
    boolean isUp = false;
    try {
        Socket socket = new Socket("127.0.0.1", port);
        // Server is up
        isUp = true;
        socket.close();
    }
    catch (IOException e)
    {
        // Server is down
    }
    return isUp;
}

用法:

isTomcatUp = isServerUp(8080);
isMysqlUp = isServerUp(3306);

但是,我想说这是一个误报检查。有时它说服务器已启动,但服务器卡住或没有响应...

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:

public boolean isServerUp(int port) {
    boolean isUp = false;
    try {
        Socket socket = new Socket("127.0.0.1", port);
        // Server is up
        isUp = true;
        socket.close();
    }
    catch (IOException e)
    {
        // Server is down
    }
    return isUp;
}

Usage:

isTomcatUp = isServerUp(8080);
isMysqlUp = isServerUp(3306);

However, I would say that is a false-negative check.. Sometimes it says server UP but the server is stuck or not responding...

柏拉图鍀咏恒 2024-09-21 03:33:00

我会确保监视您设置的内容实际上是在执行一些代码。事后通过 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/ .

顾挽 2024-09-21 03:33:00

通过 MySQL 触发一个简单的固定查询

SELECT 'a-ok';

,并让 .jsp 返回 a-ok 文本。如果超时和/或未响应 a-ok,则表示有问题。如果您需要更详细的信息,可以添加额外的检查,例如请求 now() 或更大的检查,例如 SHOW INNODB STATUS

Firing a simple fixed query through MySQL

SELECT 'a-ok';

and have the .jsp return that a-ok text. If it times out and/or doesn't respond with a-ok, then something's hinky. If you need something more detailed, you can add extra checks, like requesting now() or something bigger, like SHOW INNODB STATUS.

花期渐远 2024-09-21 03:33:00

最简单的事情是查找 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.

时常饿 2024-09-21 03:33:00

创建 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.

水染的天色ゝ 2024-09-21 03:33:00

我将创建一个简单的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文