介绍:在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-monitor-simple 工程的思路,在dubbo的启动项目中,启动jetty容器,去监控,这个监控
这个容器也不是很大,所以不会有多少性能的损失。
下面贴出代码,以及遇到的问题,贴出的为主要代码,是在大家已有的dubbo项目上添加即可
pom.xml
<dependency> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty</artifactId> <version>6.1.26</version> </dependency>
描述: dubbo启动jetty依赖的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
// 日志监控系统的 handler.addServletWithMapping(StatViewServlet.class, "/druid/*");
代码如下:
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); //这里是新添加的,作用于druid日志监控系统的 handler.addServletWithMapping(StatViewServlet.class, "/druid/*"); Server server = new Server(); server.addConnector(connector); server.addHandler(handler); try { server.start(); } catch (Exception e) { throw new IllegalStateException("Failed to start jetty server 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); } } }
描述:直接copy,dubbo的JettyContainer.java过来,修改就可以啦
启动代码
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:8080/druid/index.html