You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by GitBox <gi...@apache.org> on 2022/02/11 12:58:50 UTC
[GitHub] [incubator-shenyu] dragon-zhang opened a new issue #2872: [Question] How to get the current Tomcat `port` correctly?
dragon-zhang opened a new issue #2872:
URL: https://github.com/apache/incubator-shenyu/issues/2872
### Question
After https://github.com/apache/incubator-shenyu/pull/2861 had been merged, I also want to make `shenyu-client-springmvc` to set port automatically. However, when the `springmvc application` is deployed in an `external` tomcat, how to correctly obtain the port number of the current Tomcat?
Here is the code:
```java
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import java.lang.management.ManagementFactory;
import java.util.Set;
/**
* get the port number exposed by the current tomcat
*/
public class PortUtils {
/**
* logger.
*/
private static final Logger LOG = LoggerFactory.getLogger(PortUtils.class);
private static int tomcatPort = -1;
/**
* get the current tomcat port number
*/
public static Integer getPort() throws MalformedObjectNameException, ReflectionException, AttributeNotFoundException, InstanceNotFoundException, MBeanException {
if (tomcatPort != -1) {
return tomcatPort;
}
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
Set<ObjectName> objectNames = mBeanServer.queryNames(new ObjectName("*:type=Connector,*"), null);
if (CollectionUtils.isNotEmpty(objectNames)) {
//fixme When there are many instances of external tomcat, there may be a problem here
for (ObjectName objectName : objectNames) {
String protocol = String.valueOf(mBeanServer.getAttribute(objectName, "protocol"));
String port = String.valueOf(mBeanServer.getAttribute(objectName, "port"));
// The property name is HTTP1.1, org.apache.coyote.http11.Http11NioProtocol under linux
if ("HTTP/1.1".equals(protocol) || "org.apache.coyote.http11.Http11NioProtocol".equals(protocol)) {
return tomcatPort = Integer.parseInt(port);
}
}
}
throw new IllegalStateException("failed to get the HTTP port of the current tomcat");
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@shenyu.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-shenyu] dragon-zhang closed issue #2872: [Question] How to get the current `external` Tomcat `port` correctly?
Posted by GitBox <gi...@apache.org>.
dragon-zhang closed issue #2872:
URL: https://github.com/apache/incubator-shenyu/issues/2872
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@shenyu.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-shenyu] dragon-zhang commented on issue #2872: [Question] How to get the current `external` Tomcat `port` correctly?
Posted by GitBox <gi...@apache.org>.
dragon-zhang commented on issue #2872:
URL: https://github.com/apache/incubator-shenyu/issues/2872#issuecomment-1037867797
Not supported when there are multiple instances of external Tomcat.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@shenyu.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-shenyu] dragon-zhang commented on issue #2872: [Question] How to get the current `external` Tomcat `port` correctly?
Posted by GitBox <gi...@apache.org>.
dragon-zhang commented on issue #2872:
URL: https://github.com/apache/incubator-shenyu/issues/2872#issuecomment-1037867797
Not supported when there are multiple instances of external Tomcat.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@shenyu.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [incubator-shenyu] dragon-zhang closed issue #2872: [Question] How to get the current `external` Tomcat `port` correctly?
Posted by GitBox <gi...@apache.org>.
dragon-zhang closed issue #2872:
URL: https://github.com/apache/incubator-shenyu/issues/2872
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@shenyu.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org