在JVM中禁用JMX/MBean

1tu0hz3e  于 2022-11-07  发布在  其他
关注(0)|答案(1)|浏览(228)

我有两个Debezium SQL Server连接器,它们必须连接到一个数据库并发布两个不同的表。它们的namedatabase.history.kafka.topic是唯一的。当添加第二个连接器(使用POST请求)时,我仍然会遇到以下异常。我不想为database.server.name使用唯一值,因为这违反直觉地被用作指标名称。

java.lang.RuntimeException: Unable to register the MBean 'debezium.sql_server:type=connector-metrics,context=schema-history,server=mydatabase'
Caused by: javax.management.InstanceAlreadyExistsException: debezium.sql_server:type=connector-metrics,context=schema-history,server=mydatabase

我们不会使用JMX/MBean,所以禁用它是可以的,但问题是如何禁用。如果有一个通用的方法来为JVM禁用它,请提供建议。
我甚至在Debezium中看到了下面的代码,它注册了一个MBean。只看前两行,似乎绕过这个问题的一种方法是强制ManagementFactory.getPlatformMBeanServer()返回null。那么问同一个问题的另一种方法可能是如何强制ManagementFactory.getPlatformMBeanServer()返回null

public synchronized void register() {
    try {
        final MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        if (mBeanServer == null) {
            LOGGER.info("JMX not supported, bean '{}' not registered", name);
            return;
        }
        // During connector restarts it is possible that Kafka Connect does not manage
        // the lifecycle perfectly. In that case it is possible the old metric MBean is still present.
        // There will be multiple attempts executed to register new MBean.
        for (int attempt = 1; attempt <= REGISTRATION_RETRIES; attempt++) {
            try {
                mBeanServer.registerMBean(this, name);
                break;
            }
            catch (InstanceAlreadyExistsException e) {
                if (attempt < REGISTRATION_RETRIES) {
                    LOGGER.warn(
                            "Unable to register metrics as an old set with the same name exists, retrying in {} (attempt {} out of {})",
                            REGISTRATION_RETRY_DELAY, attempt, REGISTRATION_RETRIES);
                    final Metronome metronome = Metronome.sleeper(REGISTRATION_RETRY_DELAY, Clock.system());
                    metronome.pause();
                }
                else {
                    LOGGER.error("Failed to register metrics MBean, metrics will not be available");
                }
            }
        }
        // If the old metrics MBean is present then the connector will try to unregister it
        // upon shutdown.
        registered = true;
    }
    catch (JMException | InterruptedException e) {
        throw new RuntimeException("Unable to register the MBean '" + name + "'", e);
    }
}
62lalag4

62lalag41#

您应该使用单个Debezium SQL Server连接器来执行此操作,并使用连接器上的table.include.list属性来列出您要捕获的两个表。
https://debezium.io/documentation/reference/stable/connectors/sqlserver.html#sqlserver-property-table-include-list

相关问题