How to run @RestController on a different port?

2019-07-16 19:12发布

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"; }
}

2条回答
贼婆χ
2楼-- · 2019-07-16 19:44

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.

查看更多
仙女界的扛把子
3楼-- · 2019-07-16 20:01

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/

查看更多
登录 后发表回答