设为首页 收藏本站
查看: 682|回复: 0

[经验分享] 监控WebLogic 9.x和10.x解决方案(监控应用服务器系列文章)

[复制链接]

尚未签到

发表于 2017-2-18 06:34:32 | 显示全部楼层 |阅读模式
  前言:做了一个监控应用服务器的项目(支持Tocmat、WebSphere、WebLogic各版本), 过程也算是磕磕绊绊,由于网上缺少相关资料,或者深陷于知识的海洋难以寻觅到有效的资料,因而走过不少弯路,遇过不少困难。为了留下点印记,给后来人留下 点经验之谈,助之少走弯路,故将这些经验整理出来,与大家分享。水平有限,难免疏漏,还望指正。如有疑问,欢迎留言,或者加入Q群参与讨 论:35526521。
   
  其实学习一项新的技术,刚开始最困难就是难以找到有效的资料,OK,首先分享几个官方文档的地址:

几个重要的官方文档地址
  ——相当于葵花宝典的功效(放心,不需自宫)
  一、使用 JMX 开发自定义管理实用工具
http://www.oraclefmw.com/wls92/jmx/index.html
这是针对WebLogic 9.2的中文版,第一好东西,里面不仅讲解有原理讲解,还有Demo和教程
   
  二、使用JMX访问WebLogic Server MBean的Demo和教程
● WebLogic 9.x:
http://download.oracle.com/docs/cd/E13222_01/wls/docs90/jmx/accessWLS.html
● WebLogic 10.x:
http://download.oracle.com/docs/cd/E11035_01/wls100/jmx/accessWLS.html
   
  三、WebLogic的MBean参考手册
http://download.oracle.com/docs/cd/E14571_01/apirefs.1111/e13951/core/index.html
有了MBean参考手册,想要监控什么指标,直接到里面找,纯粹是体力活,剩下的就是考虑怎么把代码写得简单可用(Clean code that works )了
  只要有了这几个地址,开发监控WebLogic 9.x和10.x的项目就变得简单了。想当年,我就是在找资料上花费了大把大把的时间,而且那绝对是相当的痛苦。如果你不是第一时间看到了这篇文章,相信你一定感同身受吧!但是恭喜你,现在你不用那么痛苦了,哈哈,开个玩笑……

访问WebLogic使用的JAR包

访问WebLogic 9.x所使用的JAR包:

wlclient.jar、wljmxclient.jar

在%WL_HOME%\server\lib目录下

 


访问WebLogic 10.x所使用的JAR包:

由于10.x的API有变化,必须使用JarBuilder Tool生成wlfullclient.jar,具体参见官方的教程,上面的“葵花宝典”里面有链接。


监控所依赖的JAR包的存放位置

 
我们会通过这种方式获取和MBean Server的连接:
------------------------------------------------------------------------------------------------------------------------------
JMXConnector connector = JMXConnectorFactory.connect(serviceURL, h);
------------------------------------------------------------------------------------------------------------------------------
 
代码在Eclipse下通过Java程序调用没有任何问题,作为Web应用部署到Tocmat就出问题了,会报如下异常:
------------------------------------------------------------------------------------------------------------------------------
Unsupported protocol: t3
------------------------------------------------------------------------------------------------------------------------------
 
具体原因如下(这是找了很久以后在一个国外的论坛上看到的):
------------------------------------------------------------------------------------------------------------------------------
Here's the issue that I'm facing:
To get a JMX connection using JMXConnectionFactory using below: JMXConnector connector = JMXConnectorFactory.connect(serviceURL, h);
I have to rely on classes in weblogic.jar
That is because the implementation of JMXConnector is in weblogic.jar. However, the interface/abstract class for JMXConnectionFactory lives in rt.jar that comes with JDK1.5 or JDK1.6.
Because rt.jar is (probably) used by the bootstrap classloader, I HAVE to include weblogic.jar in the CATALINA_HOME/common/lib directory. If I do so, I can get the JMX part of the solution to work because both, rt.jar and weblogic.jar are loaded by the same (system) classloader.
------------------------------------------------------------------------------------------------------------------------------
 
解决办法很简单: 将wlfullclient.jar放到Tomcat安装目录下的lib目录下即可。
------------------------------------------------------------------------------------------------------------------------------
刚发现这个问题时很是诧异,始终想不通,后来在老外的一个论坛上找到类似问题,原来, JMXConnector接口是在JDK1.5/JDK1.6的rt.jar里面,而具体实现是在wlfullclient.jar里面。问题就在这里, rt.jar是由系统类加载器加载的, wlfullclient.jar如果放在自己的WEB-INFO/lib目录下,就是由WebappClassLoader去加载了。所以我们将wlfullclient.jar放到Tomcat安装目录下的lib目录下,这样wlfullclient.jar跟rt.jar就是由同一个classloader加载的了,所以问题就迎刃而解了。  
------------------------------------------------------------------------------------------------------------------------------
 
 

一个Demo:采集Web应用的Servlet信息

public class MonitorServlets {
private static MBeanServerConnection connection;
private static JMXConnector connector;
private static final ObjectName service;
// Initializing the object name for DomainRuntimeServiceMBean
// so it can be used throughout the class.
static {
try {
service = new ObjectName(
"com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean");
} catch (MalformedObjectNameException e) {
throw new AssertionError(e.getMessage());
}
}
/**
* 测试本地的WebLogic10.3
*/
public static void main1(String[] args) throws Exception {
String hostname = "localhost";
String portString = "7001";
String username = "weblogic";
String password = "weblogic123";
MonitorServlets monitor = new MonitorServlets();
initConnection(hostname, portString, username, password);
monitor.getServletData();
connector.close();
}
/**
* 测试192.168.1.5服务器上的WebLogic9.2
*/
public static void main(String[] args) throws Exception {
String hostname = "192.168.1.5";
String portString = "7001";
String username = "weblogic";
String password = "weblogic";
MonitorServlets monitor = new MonitorServlets();
initConnection(hostname, portString, username, password);
monitor.getServletData();
connector.close();
}
/**
* Initialize connection to the Domain Runtime MBean Server
*
* @param hostname
* @param portString
* @param username
* @param password
* @throws IOException
* @throws MalformedURLException
*/
public static void initConnection(String hostname, String portString,
String username, String password) throws IOException,
MalformedURLException {
String protocol = "t3";
Integer portInteger = Integer.valueOf(portString);
int port = portInteger.intValue();
String jndiroot = "/jndi/";
String mserver = "weblogic.management.mbeanservers.domainruntime";
JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port,
jndiroot + mserver);
Hashtable<String, String> h = new Hashtable<String, String>();
h.put(Context.SECURITY_PRINCIPAL, username);
h.put(Context.SECURITY_CREDENTIALS, password);
h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,
"weblogic.management.remote");
connector = JMXConnectorFactory.connect(serviceURL, h);
connection = connector.getMBeanServerConnection();
}
/**
* Get an array of ServerRuntimeMBeans
*
* @return
* @throws Exception
*/
public static ObjectName[] getServerRuntimes() throws Exception {
return (ObjectName[]) connection
.getAttribute(service, "ServerRuntimes");
}
/**
* Get an array of WebApplicationComponentRuntimeMBeans
*
* @throws Exception
*/
public void getServletData() throws Exception {
ObjectName[] serverRT = getServerRuntimes();
int length = (int) serverRT.length;
for (int i = 0; i < length; i++) {
System.out
.println("===========================================ServerRuntimes");
String name = (String) connection.getAttribute(serverRT, "Name");
String state = (String) connection.getAttribute(serverRT,
"State");
System.out.println("Server name: " + name + ".Server state: "
+ state);
ObjectName[] appRT = (ObjectName[]) connection.getAttribute(
serverRT, "ApplicationRuntimes");
int appLength = (int) appRT.length;
for (int x = 0; x < appLength; x++) {
System.out
.println("-------------------------------------------ApplicationRuntimes");
System.out.println("Application name: "
+ (String) connection.getAttribute(appRT[x], "Name"));
ObjectName[] compRT = (ObjectName[]) connection.getAttribute(
appRT[x], "ComponentRuntimes");
int compLength = (int) compRT.length;
for (int y = 0; y < compLength; y++) {
String componentName = (String) connection.getAttribute(
compRT[y], "Name");
String componentType = (String) connection.getAttribute(
compRT[y], "Type");
System.out.println("  Component name: " + componentName
+ " , Component type: " + componentType);
if (componentType.toString().equals(
"WebAppComponentRuntime")) {
ObjectName[] servletRTs = (ObjectName[]) connection
.getAttribute(compRT[y], "Servlets");
int servletLength = (int) servletRTs.length;
for (int z = 0; z < servletLength; z++) {
System.out.println("    Servlet name: "
+ (String) connection.getAttribute(
servletRTs[z], "Name"));
System.out.println("       Servlet context path: "
+ (String) connection.getAttribute(
servletRTs[z], "ContextPath"));
System.out
.println("       Invocation Total Count : "
+ (Object) connection.getAttribute(
servletRTs[z],
"InvocationTotalCount"));
}
}
}
}
}
}
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-343585-1-1.html 上篇帖子: weblogic配置修改代码后不需要重启热部署方式 下篇帖子: 通向架构师的道路(第九天)之weblogic的集群与配置
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表