Managing Spring boot Micro services with Kong— Part 2 (Create Springboot API)
Create a simple spring boot application and add REST interfaces to it, code
Rest Controller was created with @RequestParam and @PathVariable
Please fellow the instructions given in the README.md
Check the REST Service
If REST Service is working add it to Kong
We can verify the connected containers to the kong-net docker network:
Setup API server routing using Spring boot microservices
Declare the Component with Kong
Create the service
Service refer to the upstream APIs and microservices that Kong manages.
curl -i -X POST — url http://localhost:8001/services/ — data “name=convert-to-fahrenheit-v1” — data “url=http://172.18.0.2:8098/v1/temperature/convert-to-fahrenheit/102"
172.18.0.2 is the address of the component container. In this case temperature-converter container.
Create the route
One route can be associated with one service, and this later can have one to many route to an upstream.
curl -i -X POST — url http://localhost:8001/services/convert-to-fahrenheit-v1/routes — data “name=convert-to-fahrenheit-v1” — data “paths[]=/convert-to-fahrenheit-v1”
Kong expose the created API at the port 8000. In this case we can visit the created route by:
curl http://localhost:8000/convert-to-fahrenheit-v1
Allow for Dynamic Parameter mapping in path based routing
Above example we used exact Service URL from the Spring boot
/v1/temperature/convert-to-fahrenheit/102, but in reality value 102 should be dynamic like it can be /v1/temperature/convert-to-fahrenheit/200 or even /v1/temperature/convert-to-fahrenheit/2000, so how can we handle this kind of Dynamic parameters inside Kong
Create Service for Dynamic Path/Request parameters
$curl -i -X POST — url http://localhost:8001/services/ — data “name=temperature” — data “url=http://172.18.0.2:8098/v1/temperature"
Create Route for Dynamic Path parameters
$curl -i -X POST — url http://localhost:8001/services/temperature/routes — data “name=temperature” — data “paths[]=/temperature-v1”
Check Service with PathVariable
$curl http://localhost:8000/temperature-v1/convert-to-fahrenheit/2000
Check Service with RequestParam
$curl http://localhost:8000/temperature-v1/convert-to-celsius?fahrenheit=300
There are different kind of combinations you can add when creating route with Kong like methods like GET, POST, hosts , protocol Etc..
Delete added services and routes
You can delete the added services and routes as fellows, When deleting make sure you delete the route first
$curl -i -X DELETE http://localhost:8001/routes/temperature$curl -i -X DELETE http://localhost:8001/services/temperature
Next step is adding security to API, which comes in my next blog