遇到一个单节点多实例部署的情况,且配置文件部署平台统一管理了,在这种情况下想到用端口号区分具体实例下载。搜了一圈发现都是一个版本且存在问题的源码,干脆自己搞了一个。废话不说了,直接上源码。
- List<String> getEndPoints() throws MalformedObjectNameException,
- NullPointerException, UnknownHostException, AttributeNotFoundException,
- InstanceNotFoundException, MBeanException, ReflectionException {
- MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
- Set<ObjectName> objs = mbs.queryNames(new ObjectName("*:type=Connector,*"),
- Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
- String hostname = InetAddress.getLocalHost().getHostName();
- InetAddress[] addresses = InetAddress.getAllByName(hostname);
- ArrayList<String> endPoints = Lists.newArrayList();
- for (Iterator<ObjectName> i = objs.iterator(); i.hasNext(); ) {
- ObjectName obj = i.next(); 下载
- String scheme = mbs.getAttribute(obj, "scheme").toString();
- String port = obj.getKeyProperty("port");
- for (InetAddress addr : addresses) {
- String host = addr.getHostAddress();
- String ep = scheme + "://" + host + ":" + port;
- endPoints.add(ep);
- }
- }
- return endPoints;
- }