Created
January 29, 2025 13:09
-
-
Save agvxov/82c34ff28c372e9cd90a550f9afbda10 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 1. Set up dependencies | |
| -gateway is all we need: | |
| spring-cloud-starter-gateway | |
| -NOTE: in case you missed it during initialization, | |
| this is how it would look like under gradlew: | |
| { | |
| dependencies { | |
| implementation 'org.springframework.cloud:spring-cloud-starter-gateway' | |
| } | |
| } | |
| 2. Run services | |
| >netcat is going to be the easiest to set up | |
| >we will create 2 instances continously running | |
| $ nc -l -p 8081 | |
| $ nc -l -p 8082 | |
| >NOTE: -p signals the port, make sure to make them distinct | |
| 4. Set up routing | |
| -we will hate to add a route locator bean: | |
| { | |
| @Bean | |
| public RouteLocator rl(RouteLocatorBuilder builder) { | |
| return builder.routes().build(); | |
| } | |
| } | |
| >NOTE: for convinience, might as well add it as a method of our (main) application | |
| 5. Add routes | |
| >we can append route() calls to routes | |
| >the first argument is a string id (it does not actually matter for our purposes) | |
| >the second argument is a lambda configuring a route | |
| >route calls can be chained | |
| >we will add 2 routes, discriminated based on path | |
| { | |
| .route("netcat-1", r -> r.path("/cat1").uri("http://127.0.0.1:8081")) | |
| .route("netcat-2", r -> r.path("/cat2").uri("http://127.0.0.1:8082")) | |
| } | |
| >NOTE: we must reuse to port of our designated services here, | |
| currently its those on which we are running netcat(s) | |
| 6. Compile and run | |
| >we regularly invoke our application from the top directory of our project | |
| $ gradlew bootRun | |
| 7. Validate | |
| >any http connection would do | |
| >using a browser is going to easiest (with curl we would have to acknowledge ssl) | |
| { // Enter to the address bar of your favourite browser | |
| 127.0.0.1:8080/cat2 | |
| } | |
| -now, even tho we contacted port 8080, the cat on port 8082 was reached because | |
| spring forwarded the request. this can be proven by cat's output: | |
| { | |
| $ nc -l -p 8082 | |
| GET /cat2 HTTP/1.1 | |
| User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:111.0) Gecko/20100101 Firefox/111.0 | |
| Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8 | |
| Accept-Language: en-US,en;q=0.5 | |
| Accept-Encoding: gzip, deflate, br | |
| DNT: 1 | |
| Cookie: csrftoken=o6NYlegO2KW4jNrhg1hjJvjI19yqm6KT | |
| Upgrade-Insecure-Requests: 1 | |
| Sec-Fetch-Dest: document | |
| Sec-Fetch-Mode: navigate | |
| Sec-Fetch-Site: none | |
| Sec-Fetch-User: ?1 | |
| Forwarded: proto=http;host="127.0.0.1:8080";for="127.0.0.1:55796" | |
| X-Forwarded-For: 127.0.0.1 | |
| X-Forwarded-Proto: http | |
| X-Forwarded-Port: 8080 | |
| X-Forwarded-Host: 127.0.0.1:8080 | |
| host: 127.0.0.1:8082 | |
| content-length: 0 | |
| } | |
| >NOTE: do not expect the browser tab to load, netcat only listens, it does not answer | |
| >NOTE: the X-Forwarded-* keys hint at how the forwarding server could be | |
| reflected on by the endpoint servers | |
| XXX: | |
| { // File (for easier copy pasting): GatewayApplication.java | |
| package example.gateway; | |
| import org.springframework.boot.SpringApplication; | |
| import org.springframework.boot.autoconfigure.SpringBootApplication; | |
| import org.springframework.cloud.gateway.route.builder.*; | |
| import org.springframework.cloud.gateway.route.*; | |
| import org.springframework.context.annotation.*; | |
| @SpringBootApplication | |
| public class GatewayApplication { | |
| @Bean | |
| public RouteLocator rl(RouteLocatorBuilder builder) { | |
| return builder.routes() | |
| .route("netcat-1", r -> r.path("/cat1").uri("http://127.0.0.1:8081")) | |
| .route("netcat-2", r -> r.path("/cat2").uri("http://127.0.0.1:8082")) | |
| .build() | |
| ; | |
| } | |
| public static void main(String[] args) { | |
| SpringApplication.run(GatewayApplication.class, args); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment