Sayan
Sayan

Reputation: 15

How to use camel consul component for agent API?

As per camel documentation for consul(camel.apache.org/consul-component.html), the supported HTTP API are kv, event and agent. There are example of kv (key/value store) which are working fine but there is no such example for agent API. I went thruogh the documentation of Consul [www.consul.io/docs/agent/http/agent.html] and the corresponding java client [github.com/OrbitzWorldwide/consul-client] as well and tried to figure out how consul:agent component should work but I have found nothing simple there.

main.getCamelTemplate().sendBodyAndHeader(
            "consul:agent?url=http://localhost:8500/v1/agent/service/register", 
            payload,
            ConsulConstants.CONSUL_ACTION, ConsulAgentActions.AGENT); //also tried with ConsulAgentActions.SERVICES, but no luck

I also checked the test cases mention at https://github.com/apache/camel/tree/master/components/camel-consul/src/test/java/org/apache/camel/component/consul but unable to find anything related to agent api.

So my question is that how to use consul:agent component.

UPDATE: I tried the below code and able to get the services.

Object res = main.getCamelTemplate().requestBodyAndHeader("consul:agent", "", ConsulConstants.CONSUL_ACTION, ConsulAgentActions.SERVICES);

It seems that the consul component only work for the GET operation of the HTTP agent API. But in that case how do I register a new service (like /v1/agent/service/register : Registers a new local service) with consul component?

Upvotes: 1

Views: 872

Answers (2)

dschulten
dschulten

Reputation: 3152

One can use

.to("consul:agent?action=SERVICES")

to retrieve the registered Services as Map<String, Service>, with service id as map key.

And

.to("consul:catalog?action=REGISTER") 

to write registrations, expecting an ImmutableCatalogRegistration as body

Note that you can employ a CamelServiceRegistrationRoutePolicy to register Camel routes as services automatically.

Upvotes: 0

Vadim R
Vadim R

Reputation: 11

This code works for me:

ImmutableService service =
        ImmutableService.builder()
                .id("service-1")
                .service("service")
                .addTags("camel", "service-call")
                .address("127.0.0.1")
                .port(9011)
                .build();

ImmutableCatalogRegistration registration =
        ImmutableCatalogRegistration.builder()
                .datacenter("dc1")
                .node("node1")
                .address("127.0.0.1")
                .service(service)
                .build();

ProducerTemplate template = main.getCamelTemplate();
Object res = template.requestBodyAndHeader("consul:catalog", registration, ConsulConstants.CONSUL_ACTION, ConsulCatalogActions.REGISTER);

But it's looking some inelegantly (like workaround), and i think there are other solutions.

Upvotes: 1

Related Questions