dubbo中启用druid sql监控

Posted 浪子尘晨

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dubbo中启用druid sql监控相关的知识,希望对你有一定的参考价值。

在dubbo项目中,使用druid的监控功能,通过查找参考资料,显示的都是需要在web.xml中配置

<servlet>
      <servlet-name>DruidStatView</servlet-name>
      <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>DruidStatView</servlet-name>
      <url-pattern>/druid/*</url-pattern>
  </servlet-mapping>

可是,我们一般搭建的dubbo项目,使用的shell脚本启动的,java命令启动,没有使用到Tomcat容器,虽然可以使用,但是会显得,dubbo提供的容器有SpringContainer、Log4jContainer、JettyContainer、JavaConfigContainer、LogbackContainer其中jetty大家都熟悉,更轻量级的servlet容器。dubbo里面内置了JettyContainer,可以提供汇报一些dubbo的执行状态。但是我们无法直接配置druid的sql监听器,所以必须借助于dubbo启动jetty容器的形式,自己进行JettyContainer扩展,而同时dubbo启动服务时可以指定多个容器,所以我们启动时指定启动spring容器与我们扩展的JettyContainer容器,完成druid监控。

1.添加jetty依赖

<dependency>
    <groupId>org.mortbay.jetty</groupId>
    <artifactId>jetty</artifactId>
    <version>6.1.26</version>
</dependency>

2.在本地创建MyJettyContainer类

import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.alibaba.dubbo.common.utils.ConfigUtils;
import com.alibaba.dubbo.common.utils.NetUtils;
import com.alibaba.dubbo.container.Container;
import com.alibaba.dubbo.container.jetty.JettyContainer;
import com.alibaba.dubbo.container.page.PageServlet;
import com.alibaba.dubbo.container.page.ResourceFilter;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.servlet.FilterHolder;
import org.mortbay.jetty.servlet.ServletHandler;
import org.mortbay.jetty.servlet.ServletHolder;

public class MyJettyContainer implements Container {
    private static final Logger logger = LoggerFactory.getLogger(JettyContainer.class);
    public static final String JETTY_PORT = "dubbo.jetty.port";
    public static final String JETTY_DIRECTORY = "dubbo.jetty.directory";
    public static final String JETTY_PAGES = "dubbo.jetty.page";
    public static final int DEFAULT_JETTY_PORT = 8080;
    SelectChannelConnector connector;

    public MyJettyContainer() {
    }

    public void start() {
        String serverPort = ConfigUtils.getProperty("dubbo.jetty.port");
        int port;
        if (serverPort != null && serverPort.length() != 0) {
            port = Integer.parseInt(serverPort);
        } else {
            port = 8080;
        }

        this.connector = new SelectChannelConnector();
        this.connector.setPort(port);
        ServletHandler handler = new ServletHandler();
        String resources = ConfigUtils.getProperty("dubbo.jetty.directory");
        if (resources != null && resources.length() > 0) {
            FilterHolder resourceHolder = handler.addFilterWithMapping(ResourceFilter.class, "/*", 0);
            resourceHolder.setInitParameter("resources", resources);
        }

        ServletHolder pageHolder = handler.addServletWithMapping(PageServlet.class, "/*");
        pageHolder.setInitParameter("pages", ConfigUtils.getProperty("dubbo.jetty.page"));
        pageHolder.setInitOrder(2);
        //这里是新添加的,作用于druid日志监控系统的
        handler.addServletWithMapping(StatViewServlet.class, "/druid/*");
        Server server = new Server();
        server.addConnector(this.connector);
        server.addHandler(handler);

        try {
            server.start();
        } catch (Exception var8) {
            throw new IllegalStateException("Failed to start jetty server on " + NetUtils.getLocalHost() + ":" + port + ", cause: " + var8.getMessage(), var8);
        }
    }

    public void stop() {
        try {
            if (this.connector != null) {
                this.connector.close();
                this.connector = null;
            }
        } catch (Throwable var2) {
            logger.error(var2.getMessage(), var2);
        }

    }
}

3.更改duboo启动时加载容器配置

在src/main/resources/META-INF  下面创建dubbo文件夹,再添加文件com.alibaba.dubbo.container.Container

文件内容

myjetty=自定义包.JettyContainer

我的截图如下:

 

 

4.更改或添加dubbo.properties内容

   说明:dubbo加载spring容器时,默认加载src/main/resources/META-INF/spring 文件夹里面的配置文件。此处更改内容也是更改spring配置加载位置,指定dubbo加载哪些容器,配置日志

# 加入myjetty配置
dubbo.container=spring,myJetty
# druid访问端口,地址为http://127.0.0.1:8629/druid/index.html
dubbo.jetty.port=8629

截图如下: 

5.启动

public static void main(String[] args) {
        args = new String[]{"spring","myjetty"};
        com.alibaba.dubbo.container.Main.main(args);
}

脚本启动

conf/ dubbo.properties

dubbo.container=spring,myjetty
dubbo.log4j.file=logs/dubbo-demo-provider.logdubbo.log4j.level=WARN

最后,启动,访问

http://localhost:8629/druid/index.html

分类: Java

以上是关于dubbo中启用druid sql监控的主要内容,如果未能解决你的问题,请参考以下文章

springboot2.x配置druid sql监控

druid sql监控 怎么看

druid控制台无法监控sql问题

dubbo-客户端请求连接并发数量监控

如何使用Druid监控sql

案例: 模拟登陆,获取Druid SQL监控数据