使用JMX搭建WebLogic9监控软件(二)

本文介绍如何使用Java Management Extensions (JMX) 和 WebLogic 的 JMX API 实现对 WebLogic 服务器的远程监控。具体包括获取服务器名称、判断是否处于生产模式、监控 JVM 堆大小及连接池状态等。
程序实现 下面提供了部分代码的节选。 1.提供JMXWebLogicHelper作为获取连接的工具。 public class JMXWebLogicHelper implements JMXHelper { /** * 获取JMXMBeanServer连接 * * @param URI * Consts.URI_XXX * @param protocol * 协议 weblogic为T3 * @param hostname * 主机IP地址 * @param port * 端口 * @param username * 管理用户名 weblogic * @param password * 密码 * @return * @throws IOException * @throws MalformedURLException */ private MBeanServerConnection getConnection(String URI, String protocol, String hostname, int port, String username, String password) throws IOException, MalformedURLException { JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port, "/jndi/" + URI); HashMap<string string> h = new HashMap<string string>(); h.put(Context.SECURITY_PRINCIPAL, username); h.put(Context.SECURITY_CREDENTIALS, password); h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote"); JMXConnector connector = JMXConnectorFactory.connect(serviceURL, h); MBeanServerConnection connection = connector.getMBeanServerConnection(); return connection; } /** * 获取编辑连接 */ public MBeanServerConnection getEditConn(String protocol, String hostname, int port, String username, String password) throws IOException, MalformedURLException { return getConnection("weblogic.management.mbeanservers.edit", protocol, hostname, port, username, password); } /** * 获取运行时连接 */ public MBeanServerConnection getRuntimeConn(String protocol, String hostname, int port, String username, String password) throws IOException, MalformedURLException { return getConnection("weblogic.management.mbeanservers.runtime", protocol, hostname, port, username, password); } /** * 获取DomainRuntime */ public MBeanServerConnection getDomainRuntimeConn(String protocol, String hostname, int port, String username, String password) throws IOException, MalformedURLException { return getConnection("weblogic.management.mbeanservers.domainruntime", protocol, hostname, port, username, password); } } 2.获取weblogic服务器名称 MBeanServerConnection conn = JMXWebLogicHelper.getRuntimeConn("t3", "192.168.1.108", 7001, "weblogic", "weblogic"); ObjectName obn = new ObjectName( "com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean"); String serverName = String.valueOf(conn.getAttribute(obn, "ServerName")); System.out.println(serverName); 如果出现java.net.MalformedURLException: Unsupported protocol: t3异常,则需要将weblogic.jar加到classpath中。 如果出现异常javax.management.InstanceNotFoundException: 请检查new ObjectName()中的名称和键值对是否正确,或者创建的MBeanServer连接是否和ObjectName中的不一致,造成无法找到MBean的实例。 获取服务器是否处于生产模式 生产模式的路径是RuntimeServiceMBean-&gt;DomainConfiguration-&gt;ProductionModeEnabled,返回值是boolean。 public static void getProductMode() { MBeanServerConnection conn; try { conn = JMXWebLogicHelper.getRuntimeConn("t3", "192.168.1.108", 7001, "weblogic", "weblogic"); ObjectName obn = new ObjectName( "com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean"); obn = (ObjectName) conn.getAttribute(obn, "DomainConfiguration"); boolean b = Boolean.getBoolean(String.valueOf(conn.getAttribute( obn, "ProductionModeEnabled"))); System.out.println("生产模式:" + b); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (MalformedObjectNameException e) { e.printStackTrace(); } catch (NullPointerException e) { e.printStackTrace(); } catch (AttributeNotFoundException e) { e.printStackTrace(); } catch (InstanceNotFoundException e) { e.printStackTrace(); } catch (MBeanException e) { e.printStackTrace(); } catch (ReflectionException e) { e.printStackTrace(); } 监控当前JVM堆的大小 从RuntimeServiceMBean-&gt;ServerRuntime-&gt;JVMRuntime-&gt;HeapSizeCurrent MBeanServerConnection conn = JMXWebLogicHelper.getRuntimeConn("t3", "192.168.1.108", 7001, "weblogic", "weblogic"); ObjectName obn = new ObjectName( "com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean"); obn = (ObjectName) conn.getAttribute(obn, "ServerRuntime"); obn = (ObjectName) conn.getAttribute(obn, "JVMRuntime"); int b = Integer.parseInt(String.valueOf(conn.getAttribute( obn, "HeapSizeCurrent"))); System.out.println("当前堆大小bytes:" + b); 5.监控连接池运行状态 监控连接池状态我们不能简单的应用上面的方法了,因为连接池可能会有多个,而我们只需要监控其中的某个或某几个,那么我们需要自己开发监控插件来完成对某个连接池对象的监控。在配置文件中,我们指定插件的位置,就可以实现定制监控了。 public String getValue(String info, String appserver, String monitorpoint, String attr) throws Exception { String[] infos = info.split("[|]");// 用竖线分割上下文信息 conn = JMXWebLogicHelper.getRuntimeConn("t3", "192.168.1.108",7001, "weblogic", "weblogic"); ObjectName obn = new ObjectName( "com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean"); // 根对象 ObjectName dc = (ObjectName) conn .getAttribute(obnRoot, "ServerRuntime"); ObjectName[] apps = (ObjectName[]) conn.getAttribute(dc, "ApplicationRuntimes"); for (ObjectName app : apps) { String name = (String) conn.getAttribute(app, "ApplicationName"); if (infos[0].equalsIgnoreCase(name)) { // 如果监控的是这个web应用 ObjectName[] runtimes = (ObjectName[]) conn.getAttribute(app, "ComponentRuntimes"); for (ObjectName run : runtimes) { String runName = (String) conn.getAttribute(run, "Name"); if (infos[1].equalsIgnoreCase(runName)) { return String.valueOf(conn.getAttribute(run, attr)); } } } } return null; } 可扩展性的保证,配置文件 ######################应用服务器监控设置################################ #应用服务器1的要监控的内容 appserver1.monitor.count=2 #获取服务名 #*************************************************** appserver1.monitor1=RuntimeServiceMBean.DomainConfiguration.ProductionModeEnabled appserver1.monitor1.name=生产模式 appserver1.monitor1.notice=true #*************************************************** appserver1.monitor2=custom appserver1.monitor2.name=连接池运行状态 appserver1.monitor2.class=com.wonder.monitor.impl.JDBCStateMonitor #在上面的自定义类中,表示context,前面是数据源的JNDI名字,后面是组件名 appserver1.monitor2.info=TEST|TEST appserver1.monitor2.attr=State appserver1.monitor2.notice=true Wonder 2009-9-30</string></string>
内容概要:本文详细介绍了“秒杀商城”微服务架构的设计与实战全过程,涵盖系统从需求分析、服务拆分、技术选型到核心功能开发、分布式事务处理、容器化部署及监控链路追踪的完整流程。重点解决了高并发场景下的超卖问题,采用Redis预减库存、消息队列削峰、数据库乐观锁等手段保障数据一致性,并通过Nacos实现服务注册发现与配置管理,利用Seata处理跨服务分布式事务,结合RabbitMQ实现异步下单,提升系统吞吐能力。同时,项目支持Docker Compose快速部署和Kubernetes生产级编排,集成Sleuth+Zipkin链路追踪与Prometheus+Grafana监控体系,构建可观测性强的微服务系统。; 适合人群:具备Java基础和Spring Boot开发经验,熟悉微服务基本概念的中高级研发人员,尤其是希望深入理解高并发系统设计、分布式事务、服务治理等核心技术的开发者;适合工作2-5年、有志于转型微服务或提升架构能力的工程师; 使用场景及目标:①学习如何基于Spring Cloud Alibaba构建完整的微服务项目;②掌握秒杀场景下高并发、超卖控制、异步化、削峰填谷等关键技术方案;③实践分布式事务(Seata)、服务熔断降级、链路追踪、统一配置中心等企业级中间件的应用;④完成从本地开发到容器化部署的全流程落地; 阅读建议:建议按照文档提供的七个阶段循序渐进地动手实践,重点关注秒杀流程设计、服务间通信机制、分布式事务实现和系统性能优化部分,结合代码调试与监控工具深入理解各组件协作原理,真正掌握高并发微服务系统的构建能力。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值