简体   繁体   English

如何在不同的端口上运行@RestController?

[英]How to run @RestController on a different port?

I'm using spring-rest to create some @RestController servlets. 我正在使用spring-rest来创建一些@RestController servlet。 The application is not run on a webserver, but as a simple command line tool with embedded tomcat. 应用程序不是在Web服务器上运行,而是作为带有嵌入式tomcat的简单命令行工具运行。

Most of them should be running on a public port, which is specified using server.port=80 property. 其中大多数应该在公共端口上运行,该端口使用server.port=80属性指定。

Question: how can I run different @RestController on different ports? 问题:如何在不同的端口上运行不同的@RestController So that some of them are only accessibly internally? 所以他们中的一些只能在内部访问?

@RestController 
@RequestMapping("test")
public class TestServlet {

    @RequestMapping(value = "/", method = RequestMethod.GET, produces = MediaType.TEXT_PLAIN_VALUE)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public String test() { return "OK"; }
}

I would recommend to use a proxy. 我建议使用代理。 It could be an Apache Web Server, or Ngix. 它可以是Apache Web Server,也可以是Ngix。 You need to configure the two virtualhosts (in different ports) in the webserver. 您需要在Web服务器中配置两个虚拟主机(在不同的端口中)。 And redirect the invocation to your tomcat server. 并将调用重定向到您的tomcat服务器。 You can load your controllers in differents paths so it'll easier to proxyfy the calls from the weberver. 您可以在不同的路径中加载控制器,这样就可以更轻松地代理来自weberver的调用。

Finally your clients make the invocation through the web server, not directly to tomcat. 最后,您的客户端通过Web服务器进行调用,而不是直接进入tomcat。

If you're on Spring Boot, I think you should check out Spring Boot Actuator . 如果您使用的是Spring Boot,我认为您应该查看Spring Boot Actuator Your application can be set to one port while actuator runs off another. 您的应用程序可以设置为一个端口,而执行器可以运行另一个

Here's a guide that shows how to change the port for Actuator - https://spring.io/guides/gs/actuator-service/ 这是一个指南,显示如何更改Actuator的端口 - https://spring.io/guides/gs/actuator-service/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM