无需请求即可从tomcat获取服务器端口号
是否有任何可用的 Tomcat API 或配置可以告诉应用程序(可能在启动时)无需请求即可在哪个端口上运行?
想象这样一种场景,有两个 Web 应用程序在同一个 Tomcat 中运行,其中一个需要从另一个应用程序调用 Web 服务。我们不希望请求离开 Tomcat(如果您使用 Apache 服务器名称或绝对 URL,请求将发出并再次返回,并且可以转到任何实例)并返回。为此,我知道机器的名称,但无法获取端口号。我知道我可以对这些信息进行硬编码,但我不想这样做,因为我希望我的 war
文件与应用程序服务器无关。
我知道如果我们有 HTTPServletRequest 就可以找到它
这仅适用于 Tomcat 6,不适用于 Tomcat 7
Is there any Tomcat API or configuration available which can tell an application (probably on startup), what port its running on without a request?
Imagine a scenario where there are two web applications running in the same Tomcat and one of which need to invoke a web service from the other one. We don't want the request to leave the Tomcat (if you use the Apache server name or absolute URL, the request will go out and come back again and it can go to any instance) and come back in. For that I know the name of the machine but no way to get the port number. I know I can hard code this information but I don't want to do this as I want my war
file to be application server agnostic.
I know that we can find it if we have a HTTPServletRequest
This works only for Tomcat 6 and will not work on Tomcat 7
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
这样:
你将得到一个像这样的列表:
With this:
You will get a List like this:
对于任何对我们如何解决这个问题感兴趣的人,这里是模拟代码
For anybody who is interested in how we solved this, here is the mock code
服务器端口号不存在。它可以有任意数量的端口号。所以你问的没有意义。与特定请求关联的端口号确实有意义。
The server port number doesn't exist. It can have any number of port numbers. So what you're asking doesn't make sense. The port number associated with a specific request does make sense.
检查 http://svn-mirror.glassfish.org/glassfish-svn/tags/ embedded-gfv3-prelude-b07/web/web-glue/src/main/java/com/sun/enterprise/web/WebContainer.java 供参考
然后可以通过以下方式公开 MBeanServer 的内容各种协议,由协议连接器[RMI/IIOP]或协议适配器[SNMP/HTTP]实现。在这种情况下,使用 SNMP 适配器将是更好的方法,这样可以在不知道其他应用程序服务器的确切 IP/端口的情况下放置SNMP 陷阱
Check http://svn-mirror.glassfish.org/glassfish-svn/tags/embedded-gfv3-prelude-b07/web/web-glue/src/main/java/com/sun/enterprise/web/WebContainer.java for reference
The content of the MBeanServer can then be exposed through various protocols, implemented by protocol connectors[RMI/IIOP], or protocol adapters[SNMP/HTTP]. In this case, use of SNMP adapter will be a better approach so that a SNMP trap can be placed without knowing the exact IP/port of other Application Servers
这些类型的服务器被设计为能够侦听(几乎)任意端口,并对通常不需要知道的所包含的应用程序隐藏这些详细信息。
唯一的方法是自己读取配置文件,并且可以访问启动配置文件可能已被覆盖的服务器的命令行参数。您必须对正在运行的系统有很多了解才能使其发挥作用。没有办法可以移植。
即使有,在某些情况下它也并不重要,例如位于 NAT、某些防火墙后面等。
These types of servers are designed to be able to listen on (almost) arbitrary ports and to hide these details from the contained applications which normally do not need to know.
The only way is to read the configuration files yourself and have access to the command line arguments that started the server where the configuration files may have been overridden. You have to know a lot about the system you are running on for this to work. There is no way of doing it portably.
Even if there were, there are cases in which it simply does not matter like being behind a NAT, certain firewalls, etc.
您可以使用 crossContext。但我不认为这与应用程序服务器无关。
我将共享一个自定义类,充当通过 JNDI 在同一 tomcat 实例中运行应用程序的注册表,正如我所解释的 此处创建。
在启动期间,通过 ContextListener 或 Spring 容器事件,我将通过 JNDI 查找获取注册表,使用从 servletcontext.contextpath 获取的 url 添加我的 Web 应用程序实例,最后注册一个侦听器听到其他应用程序自行注册。这是我能想到的与服务器更加无关的。
获取端口与服务器无关,您应该使用上下文参数。
编辑:我很抱歉,忘了说我所描述的是在上下文之间共享对象,但是不,你不能不知道端口,除非你使用一些服务器API(根本不可知)。
You could use crossContext. But I don't think that's app server agnostic.
I would share a custom class, behaving as a registry of running applications in the same tomcat instance through JNDI, as I explained here.
During startup, through a
ContextListener
or through an Spring container event, I would obtain the registry through a JNDI lookup, add my web app instance with an url obtained from the servletcontext.contextpath, and finally register a listener to hear other applications registering themselves. That's the more server agnostic I can think of.Obtaining the port won't be server agnostic, you should use a context parameter.
EDIT: I'm sorry, forgot to say that what I've described is to share objects among contexts, but no, you can't not know the port unless you use some server API (not agnostic at all).
我不完全确定您是否可以从所需的环境配置中的代码访问 Tomcat 端口。您是否考虑过将 Web 服务的完整 URL 作为配置参数/设置(可能在 .properties 文件中)传递给应用程序?
这样,您就不必对端口进行硬编码并解耦两个应用程序,这样您就可以在技术上将 Web 服务放在外部 tomcat 上,但仍然只需更改属性即可访问它,从而避免代码重新构建。
I am not entirely sure if you can access the Tomcat port from code in the environment configuration you need. Did you consider actually having the full URL to the web service passed as a configuration param/setting (probably in a .properties file) to the app?
This way you wouldn't have to hardcode the port and de-couple both your apps so that you could technically have the web service on an external tomcat but still access it by just changing the property, avoiding code re-build.
以前在一个大型分布式项目中,我使用的设计是让集中式服务使用中央服务的 URL(和端口)初始化多个服务。
显然,这意味着中央服务必须维护要初始化的服务列表(URL 和端口)。
Previously on a large distributed project, the design I used was to have the centralised service initialise the several services with the central service's URL(& port).
Obviously this means that the central service must maintain a list of the services (URL & port) to initialise.
如果要访问同一服务器实例上的应用程序,只需省略 URL 的服务器部分即可。一些您可以实现的目标的示例。当前文档位于
http://example.com:8080/app2/doc.html
xxx.html
变为http://example.com:8080 /app2/xxx.html
../xxx.html
变为http://example.com:8080/xxx.html
../ xxx.html
变为http://example.com:8080/xxx.html
../foo/xxx.html
变为http:// /example.com:8080/foo/xxx.html
../../xxx.html
变为http://example.com:8080/xxx.html< /code> (没有办法超越根)
/xxx.html
变成http://example.com:8080/xxx.html
这个可能就是您要寻找的内容。//other.com/xxx.html
变为http://example.com:8080/xxx.html
如果你想保留“https:”If you want to access an application on the same server instance, just omit the server part of the URL. Some examples what you can achieve. The current document is at
http://example.com:8080/app2/doc.html
xxx.html
becomeshttp://example.com:8080/app2/xxx.html
../xxx.html
becomeshttp://example.com:8080/xxx.html
../xxx.html
becomeshttp://example.com:8080/xxx.html
../foo/xxx.html
becomeshttp://example.com:8080/foo/xxx.html
../../xxx.html
becomeshttp://example.com:8080/xxx.html
(there is no way to go beyond the root)/xxx.html
becomeshttp://example.com:8080/xxx.html
This is probably what you look for.//other.com/xxx.html
becomeshttp://example.com:8080/xxx.html
Useful if you want to keep "https:"嗯,如果没有请求,应用程序如何在 Tomcat 中启动?也许我在这里暂时脑死亡,但我认为在请求到达之前不会加载任何类。当然,您可以拥有独立于任何特定请求的类,但它们需要一个请求才能在某个时刻触发它们。
Hmm, how would an application get started in Tomcat without a request? Maybe I'm going brain dead for a moment here, but I don't think any classes will load until a request hits. Sure, you could have classes independent of any particular request, but they'd need a request to get them fired off at some point.