Note: I am not asking about Consul HTTP port 8500. Can I call an API exposed by a microservice, which is discovered using Consul, just by the .service.consul/ WITHOUT specifying the port number?
I am using Consul for discovering multiple versions of a microservice. This microservice (written in Java) has port predetermined:
Service registered in consul as - my-service.service.consul (service port is, let us say 3030). I have another version of the same micro service registered with consul: my-servicev2.service.consul (port 3033).
Service definition (altered for the example):
{
"Address": <IP address>,
"CreateIndex": 111,
"ModifyIndex": 000,
"Node": <node name>,
"ServiceAddress": "",
"ServiceEnableTagOverride": false,
"ServiceID": "my-servicev2",
"ServiceName": "my-servicev2",
"ServicePort": 3033,
"ServiceTags": [
"v2"
],
"TaggedAddresses": {
"lan": <LAN IP>,
"wan": <WAN IP>
}
}
I can ping and dig the service using dig @localhost my-service.service.consul' or
ping -c2 my-service.service.consul`
But, how can I access one of the APIs exposed by this microservice without explicitly using the ServicePort
?
Here is what is working:
curl http://my-servicev2.service.consul:3033/health -> resolves the service name, maps it to one of the deployed VM IP, and gives back the API response. In this case something like: `{"build"`: 'OK"}`
However, I should be able to access the API without specifying the port number like this:
curl http://my-servicev2.service.consul/health ->
{"build": "OK"}`
Is this possible in Consul?
I've tried registering the service without Port
value, and it added 0
as port value.
No in this case consul is acting purely as a DNS server and returning a healthy instance's ip address. As you have seen with your dig and ping commands.
You have to specify port as you did with your curl call.
To hack this you could modify your application to run on http default port 80.
If this is not possible you can setup what is known as a reverse proxy.
Nginx works great for this. Here is a snippet of a configuration that will allow you to handle this:
This will pass all your port 80 calls to localhost port 3033. Then your curl calls would work as expected without specifying port.