In dubbo project, monitoring the use of druid

Introduction: In dubbo project using the monitoring function druid

 

Question: Since the Internet to find Le, a lot of information, you need to configure are displayed in 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>

However, we generally build dubbo project, shell scripts used to start, java command to start, do not use the Tomcat container, although you can use,

    But it will seem very wasteful

 

solve:

    In fact, the idea is to draw on, thinking dubbo-monitor-simple engineering, project dubbo start, the start jetty vessel, to monitor, the monitor

This container is not great, so there is no loss of performance much.

    Code is posted below, as well as the problems encountered, posted as the main code is added to the project on all existing dubbo

pom.xml

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

Description: dubbo start jetty dependent jar

 /src/main/resources/META-INF/dubbo/com.alibaba.dubbo.container.Container

myjetty=com.xxxx.insurance.service.demo.common.container.JettyContainer

/src/main/java/com/xxxx/insurance/service/demo/common/container/JettyContainer.java

// log monitoring system 
        handler.addServletWithMapping (StatViewServlet. Class , "/ Druid / *");

code show as below:

public class JettyContainer 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 = 8090; //jetty容器端口  
  
    SelectChannelConnector connector;  
  
    public void start() {  
        String serverPort = ConfigUtils.getProperty(JETTY_PORT);  
        int port;  
        if (serverPort == null || serverPort.length() == 0) {  
            port = DEFAULT_JETTY_PORT;  
        } else {  
            port = Integer.parseInt(serverPort);  
        }  
        connector = new SelectChannelConnector();  
        connector.setPort(port);  
        ServletHandler handler = new ServletHandler();  
  
        String resources = ConfigUtils.getProperty(JETTY_DIRECTORY);  
        if (resources != null && resources.length() > 0) {  
            FilterHolder resourceHolder = handler.addFilterWithMapping(ResourceFilter.class, "/*", Handler.DEFAULT);  
            resourceHolder.setInitParameter("resources", resources);  
        }  
          
        ServletHolder pageHolder = handler.addServletWithMapping(PageServlet.class, "/*");  
        pageHolder.setInitParameter("pages", ConfigUtils.getProperty (JETTY_PAGES));   
        pageHolder.setInitOrder ( 2 );  
             // Here is newly added, role in the druid log monitoring system   
                handler.addServletWithMapping (StatViewServlet. Class , "/ druid / *" );   
          
        Server Server = new new Server ();   
        server.addConnector (Connector);   
        server.addHandler (Handler);   
        the try {   
            server.start ();   
        } the catch (Exception E) {  
             the throw  new new IllegalStateException ( "the Failed to Server Start Jetty ON" + NetUtils.getLocalHost () + ":" + port  
                    + ", cause: " + e.getMessage(), e);  
        }  
    }  
  
    public void stop() {  
        try {  
            if (connector != null) {  
                connector.close();  
                connector = null;  
            }  
        } catch (Throwable e) {  
            logger.error(e.getMessage(), e);  
        }  
    }  
  
}  

Description: Direct copy, dubbo of JettyContainer.java over, you can modify it

Boot code

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

Script Start

conf/ dubbo.properties

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

Finally, start, visit

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

Guess you like

Origin www.cnblogs.com/wpcnblog/p/11511944.html