spring boot 实际应用(一) 内置tomcat 实现JMX配置

项目需要增加JMX 监控,SO翻了翻官方文档,有几种实现方式,下面说一个我选择的,一是方便扩展,二是方便我底层封装

package com.ecej.nove.autoconfigure.jmx;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.jmx.support.ConnectorServerFactoryBean;
import org.springframework.remoting.rmi.RmiRegistryFactoryBean;

/**
 * JMX监控
 *
 * @author QIANG
 *
 */
@Configuration
public class JmxAutoConfiguration {

    private Logger LOGGER = LoggerFactory.getLogger(JmxAutoConfiguration.class);

    @Value("${jmx.rmi.host:localhost}")
    private String rmiHost;

    @Value("${jmx.rmi.port:9999}")
    private Integer rmiPort;

    @Bean
    public RmiRegistryFactoryBean rmiRegistry() {
        final RmiRegistryFactoryBean rmiRegistryFactoryBean = new RmiRegistryFactoryBean();
        rmiRegistryFactoryBean.setPort(rmiPort);
        rmiRegistryFactoryBean.setAlwaysCreate(true);
        LOGGER.info("RmiRegistryFactoryBean create success !!");
        return rmiRegistryFactoryBean;
    }

    @Bean
    @DependsOn("rmiRegistry")
    public ConnectorServerFactoryBean connectorServerFactoryBean() throws Exception {
        final ConnectorServerFactoryBean connectorServerFactoryBean = new ConnectorServerFactoryBean();
        connectorServerFactoryBean.setObjectName("connector:name=rmi");
        connectorServerFactoryBean.setServiceUrl(
                String.format("service:jmx:rmi://%s:%s/jndi/rmi://%s:%s/jmxrmi", rmiHost, rmiPort, rmiHost, rmiPort));
        LOGGER.info("ConnectorServerFactoryBean create success !!");
        return connectorServerFactoryBean;
    }
}

如此的简单
阅读更多
版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/luqiang81191293/article/details/60870553
个人分类: spring boot
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭