Cloud Foundry logo in white on a blue background
blog single gear
Engineering

Context Path Routing for Creating and Managing Multiple Microservices

We’d like to share a feature developed by the OSS Routing team at Cloud Foundry called Context Path Routing, which is meant to simplify the creation and management of multiple microservices.

Although this was introduced late last year, we have just completed CLI support for it, so this is a good time to review it. More importantly, this should save time for most of you by streamlining your development and reducing the complexity associated with many applications.

The Challenge of Managing Multiple Microservices

Most Cloud Foundry developers know that if you push your app to Cloud Foundry and want to have data traffic reach that app and do something meaningful, you need to create and map a route to your app.

How to Typically Create and Map a Route

The way you create a route is by adding a hostname to the domain.

Here is an example:

cf create-route myspace example.com –hostname myapp

This will create a route myapp.example.com which you would then map to your app. Let’s say you named your app as “my-app” as follows:

cf map-route my-app example.com –hostname myapp

This will map route myapp.example.com to your application named my-app. Now your application is reachable to your clients at myapp.example.com. So, if your clients hit https://myapp.example.com/home or https://myapp.example.com/somewhereelse, both of these requests will be routed by Gorouter to your app `my-app`.

Learn about the importance of using example.com for testing.

This works perfectly fine if all the paths under the domain myapp.example.com can be served by one single app. However, in this era of microservices, it is possible – even likely – to have to create an application consisting of multiple microservices that have the same top domain myapp.example.com but serve just a few context paths in the URL. All of these microservices together serve all the supported paths under the domain myapp.example.com.

So how do you solve this issue in the Cloud Foundry platform?

Introducing Context Path Routing for Microservices

Context Path Routing helps manage multiple microservices efficiently by using both domain names and the specified URL path.

Gorouter can now route requests to different apps based on path as well. This will enable your microservices architecture to use the same top-level domain (myapp.example.com) and have different apps providing support for different paths in the URL. Now that you can achieve context path routing, you can independently scale up or down those portions of your app that are in high/low demand or are experiencing high/low traffic.

Example of Creating and Mapping a Route With Paths

To create a route based on path:

cf create-route myspace example.com –hostname myapp –path foo

This will create a route myapp.example.com/foo and then you would map it to your app as follows:

cf map-route my-app example.com –hostname myapp –path foo

This will map your app my-app to myapp.example.com/foo.

Similarly you can create another route with same top level domain but different path and map it to different app:

cf create-route myspace example.com –hostname myapp –path bar
cf map-route my-another-app example.com –hostname myapp –path bar

Now requests to myapp.example.com/foo will be routed to app my-app while requests to myapp.example.com/bar will be routed to app my-another-app.

We hope this feature will help you realize your microservices architecture more quickly and effectively with Cloud Foundry. As always, we love feedback – please share your experiences in the comments!

Abbey Weintraub-Sklar Profile Image

Abbey Weintraub-Sklar, AUTHOR

SEE ALL ARTICLES