Search code examples
consul

trying to deregister a service from consult not working?


I am using a consul client to deregister a service from my junit tests. I am using vert-consul-client . the consul version i am using is 1.11.1 . the service is not registered with the consul , but just testing what will happen if we try to deregister a service that is not registered.

from the logs i get this error

Status message: 'Not Found'. Body: 'Unknown service "BadService"'

strangely i dont get this error when testing with 1.10.6 consul version.

appreciate if you can help thanks


Solution

  • strangely i dont get this error when testing with 1.10.6 consul version.

    Consul recently changed the HTTP response code that is sent when an attempt is made to deregister a non-existent service.

    Prior to Consul 1.11.0, and when ACLs were disabled, Consul would respond with a HTTP 200 response code and no response body when deregistering a non-existent service.

    $ curl --include \
           --request PUT http://localhost:8500/v1/agent/service/deregister/test
    HTTP/1.1 200 OK
    Vary: Accept-Encoding
    X-Consul-Default-Acl-Policy: allow
    Date: Wed, 05 Jan 2022 03:07:35 GMT
    Content-Length: 0
    

    This behavior was changed in Consul 1.11.0 by PR hashicorp/consul#10632 wherein Consul now returns a HTTP 404 response code if a service does not exist, regardless of whether ACLs are enabled. (See diff of consul/agent/acl.go).

    $ curl --include \
           --request PUT http://localhost:8500/v1/agent/service/deregister/test
    HTTP/1.1 404 Not Found
    Vary: Accept-Encoding
    X-Consul-Default-Acl-Policy: allow
    Date: Wed, 05 Jan 2022 03:24:21 GMT
    Content-Length: 22
    Content-Type: text/plain; charset=utf-8
    
    Unknown service "test"
    

    You're obviously not seeing an error in vertx-consul-client when communicating to Consul 1.10.6 because the HTTP 200 code indicates that the request was successful, whereas the HTTP 404 response correctly signals that the resource does not exist, and an error is correctly raised (see vert-consul-client/src/main/java/io/vertx/ext/consul/impl/ConsulClientImpl.java#L1320-L1333).

    Interestingly in Consul 1.10.x, when ACLs are enabled on the cluster, Consul would reply with a HTTP 500 error code and a corresponding error message instead of the 200 response code. This is because when ACLs are enabled, the vetServiceUpdateWithAuthorizer function does not prematurely return (if authz == nil { return nil }) and proceeds with checking whether service exists, then raising an error because it does not (see consul/agent/acl.go#L96-L104).

    $ curl --include \
           --header "X-Consul-Token: $CONSUL_HTTP_TOKEN" \
           --request PUT http://localhost:8500/v1/agent/service/deregister/test
    HTTP/1.1 500 Internal Server Error
    Vary: Accept-Encoding
    X-Consul-Default-Acl-Policy: deny
    Date: Wed, 05 Jan 2022 03:14:52 GMT
    Content-Length: 22
    Content-Type: text/plain; charset=utf-8
    
    Unknown service "test"
    

    If you had tested 1.10.6 with ACLs enabled, you would've also received a similar error as you are seeing with 1.11.1.