Cloud Foundry Route Service Example
This project is an example of a Cloud Foundry Route Service written with Spring Boot. This application does the following to each request:
- Intercepts an incoming request
- Logs information about that incoming request
- Transforms the incoming request to an outgoing request
- Logs information about that outgoing request
- Forwards the request and response
Requirements
Java, Maven
The application is written in Java 8 and packaged as a self executable JAR file. This enables it to run anywhere that Java is available.
Deployment
The following instructions assume that you have created an account and installed the cf
command line tool.
In order to automate the deployment process as much as possible, the project contains a Cloud Foundry manifest. To deploy run the following commands:
$ ./mvnw clean package
$ cf push
Next, create a user provided service that contains the route service configuration information. To do this, run the following command, substituting the address that the route service is listening on:
$ cf create-user-provided-service test-route-service -r https://<ROUTE-SERVICE-ADDRESS>
The next step assumes that you have an application already running that you'd like to bind this route service to. To do this, run the following command, substituting the domain and hostname bound to that application:
$ cf bind-route-service <APPLICATION-DOMAIN> test-route-service --hostname <APPLICATION-HOST>
In order to view the interception of the requests, you will need to stream the logs of the route service. To do this, run the following command:
$ cf logs route-service-example
Finally, start making requests against your test application. The route service's logs should start returning results that look similar to the following:
INFO Incoming Request: PATCH http://localhost/route-service/patch, {WebTestClient-Request-Id=[1], X-CF-Forwarded-Url=[http://localhost:51751/original/patch], X-CF-Proxy-Metadata=[test-proxy-metadata], X-CF-Proxy-Signature=[test-proxy-signature], Content-Type=[text/plain;charset=UTF-8], Content-Length=[9]}
INFO Outgoing Request: PATCH http://localhost:51751/original/patch, {WebTestClient-Request-Id=[1], X-CF-Proxy-Metadata=[test-proxy-metadata], X-CF-Proxy-Signature=[test-proxy-signature], Content-Type=[text/plain;charset=UTF-8], Content-Length=[9]}
INFO Outgoing Response: 200, {Content-Type=[text/plain], Content-Length=[9]}
INFO Incoming Request: DELETE http://localhost/route-service/delete, {WebTestClient-Request-Id=[1], X-CF-Forwarded-Url=[http://localhost:51755/original/delete], X-CF-Proxy-Metadata=[test-proxy-metadata], X-CF-Proxy-Signature=[test-proxy-signature]}
INFO Outgoing Request: DELETE http://localhost:51755/original/delete, {WebTestClient-Request-Id=[1], X-CF-Proxy-Metadata=[test-proxy-metadata], X-CF-Proxy-Signature=[test-proxy-signature]}
INFO Outgoing Response: 200, {Content-Length=[0]}
INFO Incoming Request: HEAD http://localhost/route-service/head, {WebTestClient-Request-Id=[1], X-CF-Forwarded-Url=[http://localhost:51757/original/head], X-CF-Proxy-Metadata=[test-proxy-metadata], X-CF-Proxy-Signature=[test-proxy-signature]}
INFO Outgoing Request: HEAD http://localhost:51757/original/head, {WebTestClient-Request-Id=[1], X-CF-Proxy-Metadata=[test-proxy-metadata], X-CF-Proxy-Signature=[test-proxy-signature]}
INFO Outgoing Response: 200, {Content-Length=[0]}
INFO Incoming Request: PUT http://localhost/route-service/put, {WebTestClient-Request-Id=[1], X-CF-Forwarded-Url=[http://localhost:51759/original/put], X-CF-Proxy-Metadata=[test-proxy-metadata], X-CF-Proxy-Signature=[test-proxy-signature], Content-Type=[text/plain;charset=UTF-8], Content-Length=[9]}
INFO Outgoing Request: PUT http://localhost:51759/original/put, {WebTestClient-Request-Id=[1], X-CF-Proxy-Metadata=[test-proxy-metadata], X-CF-Proxy-Signature=[test-proxy-signature], Content-Type=[text/plain;charset=UTF-8], Content-Length=[9]}
INFO Outgoing Response: 200, {Content-Type=[text/plain], Content-Length=[9]}
INFO Incoming Request: POST http://localhost/route-service/post, {WebTestClient-Request-Id=[1], X-CF-Forwarded-Url=[http://localhost:51761/original/post], X-CF-Proxy-Metadata=[test-proxy-metadata], X-CF-Proxy-Signature=[test-proxy-signature], Content-Type=[text/plain;charset=UTF-8], Content-Length=[9]}
INFO Outgoing Request: POST http://localhost:51761/original/post, {WebTestClient-Request-Id=[1], X-CF-Proxy-Metadata=[test-proxy-metadata], X-CF-Proxy-Signature=[test-proxy-signature], Content-Type=[text/plain;charset=UTF-8], Content-Length=[9]}
INFO Outgoing Response: 200, {Content-Type=[text/plain], Content-Length=[9]}
INFO Incoming Request: GET http://localhost/route-service/get, {WebTestClient-Request-Id=[1], X-CF-Forwarded-Url=[http://localhost:51763/original/get], X-CF-Proxy-Metadata=[test-proxy-metadata], X-CF-Proxy-Signature=[test-proxy-signature]}
INFO Outgoing Request: GET http://localhost:51763/original/get, {WebTestClient-Request-Id=[1], X-CF-Proxy-Metadata=[test-proxy-metadata], X-CF-Proxy-Signature=[test-proxy-signature]}
INFO Outgoing Response: 200, {Content-Type=[text/plain], Content-Length=[9]}
Developing
The project is set up as a Maven project and doesn't have any special requirements beyond that. It has been created using IntelliJ and contains configuration information for that environment, but should work with other IDEs.
License
The project is released under version 2.0 of the Apache License.