4

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"; }
}
membersound
  • 81,582
  • 193
  • 585
  • 1,120

2 Answers2

2

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.

Rafik BELDI
  • 4,140
  • 4
  • 24
  • 38
reos
  • 8,766
  • 6
  • 28
  • 34
  • Well but this would mean I have to deploy my app to an application server explicit, and cannot run it anymore as a console tool. If true, isn't there an alternative sticking to the embedded tomcat approach? – membersound Nov 27 '15 at 16:55
  • other option is to have different context paths, depending on the functionality. After that you can secure your application so only the people you want can access the correct path. Or you can separate the funcionality in two projects, one runs in X port and the other in Y port. You can put the funcionality in a sole jar so you only edit the jar an run the jar in the both tomcat applications. – reos Nov 27 '15 at 18:49
0

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/

mugua
  • 224
  • 1
  • 3
  • 11
  • Yes I'm using `spring-boot`. Looking at the `actuator` project I couldn't find out what you mean - running a different port. Could you give a direct example? As far as I know `actuator` provides several production ready servlets like `health` checks, on a different port. But it's probably not a good idea to add some custom servlets to that actuator port, as it serves another purpose. Anyhow it would be interesting how the actuator project succeeds in launching a servlet on a different port. Do you know? – membersound Nov 27 '15 at 23:16
  • there's a segment in the guide that shows you how to set your actuator endpoints to run off a seperate port, just by modifying your application.properties - https://spring.io/guides/gs/actuator-service/#_switch_to_a_different_server_port – mugua Nov 27 '15 at 23:27
  • sorry, I think I misunderstood your last question. I just know how to activate the actuator, not recreate that feature. – mugua Nov 27 '15 at 23:52