How to run @RestController on a different port?

僤鯓⒐⒋嵵緔 提交于 2019-12-23 20:15:00

问题


I'm using spring-rest to create some @RestController servlets. The application is not run on a webserver, but as a simple command line tool with embedded tomcat.

Most of them should be running on a public port, which is specified using server.port=80 property.

Question: how can I run different @RestController on different ports? 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"; }
}

回答1:


I would recommend to use a proxy. It could be an Apache Web Server, or Ngix. You need to configure the two virtualhosts (in different ports) in the webserver. And redirect the invocation to your tomcat server. You can load your controllers in differents paths so it'll easier to proxyfy the calls from the weberver.

Finally your clients make the invocation through the web server, not directly to tomcat.




回答2:


If you're on Spring Boot, I think you should check out 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/



来源:https://stackoverflow.com/questions/33959423/how-to-run-restcontroller-on-a-different-port

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!