Kong integration with consul (its DNS interface) and dynamical routing

hi!
My issue :
I use Kong as API gateway and Consul as a service discovery. Now i have 3 same services running on different random ports (for example user-service on port 44000 , 44001,44002).
As i know consul has DNS interface.
I want to be able to send 3 requests to kong (for example http://127.0.0.1:8000/user-route/{here_name_of_request_mapping_in_my_controller}) and take response from 3 different services.
Can someone explain please or refer to place where i can start?
Sorry if this question is stupid) i’m learning. Thanks!

Helllo @1112

Welcome to Kong Nation!

Please configure the host field of your service object to point to the DNS name of your service in Consul and ensure Kong is using Consul for DNS resolution.

@hbagdi , Hello!
thanks a lot for your quick response :pray::pray::pray:

i’ve done it . I want to leave comment how. maybe it will be helpful for someone.
1)install and run consul
2)register services with it (i made it with simple adding consul-discovery dependency in my pom.xml)
3) open kong.conf and define dns_resolver = 127.0.0.1:8600 ( By default, Consul will listen on 127.0.0.1:8600 for DNS queries in the consul. domain)
4) create Service with Admin API (host should be like {your-service-name}.service.consul). port can be any. specify path (for me it was simple “/”)
5) create Route for this service (define client host (for me it was my local machine - so localhost)) and paths (i made it /ss).
6) By default Kong listens to 8000 port so
i made get request to http://127.0.01:8000/ss/loadbalance (where /loadbalance - request mapping for my method in controller) with header “Host : 127.0.0.1”
7) now it use round robin load balancer

p.s .
I want to clarify.
Just imagine that i have 100 microservices ( i think it possible) . And each of this microservices have own API with 5 methods for example.
So i should use 100 calls to Admin API and set Routes and Services for each service ???

i used api gateway from spring cloud stack and i was able to route request by easily typing
http://localhost:(port_of_api_gateway)/(name_of_service_which_i_want_to_route)/(name_of_request_mapping)

Yes. It should be trivial to write a script to automate this.

1 Like