Search code examples
consul

How to register server on Consul [Java]


I don't know how to register a service on Consul server if I got some service like "locahost:8090/user/login/username"? I'm so appreciate for your help!


Solution

  • Assuming that this service is already running, you can use one of the following methods to register it with Consul:

    Using a service definition file

    You can create a service definition json file and use the consul agent running on that host to register the service.

    $ sudo mkdir /etc/consul.d
    $ echo '{"service": {"name": "myService", "tags": ["java"], "port": 8080}}' \
        | sudo tee /etc/consul.d/myService.json
    
    $ consul agent -dev -config-dir=/etc/consul.d
    ==> Starting Consul agent...
    ...
        [INFO] agent: Synced service 'myService'
    ...
    

    More info here: https://www.consul.io/intro/getting-started/services.html

    Using HTTP Rest API

    More info here: https://www.consul.io/api/agent/service.html#register-service.

    What info can be registered?

    Please note that you can only add the IP and the port of your service in consul and not the entire URL.

    Thanks, Arul