2017-02-14 15 views
1

根據駱駝文檔consul(camel.apache.org/consul-component.html),支持的HTTP API是kv,event和agent。有kv(鍵/值存儲)的例子工作正常,但沒有這樣的代理API的例子。我去了Consul [www.consul.io/docs/agent/http/agent.html]和相應的java客戶端[github.com/OrbitzWorldwide/consul-client]的文檔,並試圖弄清楚consul:代理組件應該可以工作,但我在那裏找到的並不簡單。如何將Camel consul組件用於代理API?

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 

我還檢查測試用例提在https://github.com/apache/camel/tree/master/components/camel-consul/src/test/java/org/apache/camel/component/consul但無法找到相關的代理API什麼。

所以我的問題是如何使用consul:agent組件。

更新:我試了下面的代碼,並能夠獲得服務。

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

看來,consul組件只能用於HTTP代理API的GET操作。但是,在那種情況下,我如何向consul組件註冊新服務(如/ v1/agent/service/register:註冊一個新的本地服務)?

回答

0

此代碼的工作對我來說:

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); 

但它尋找粗暴一些(如解決方法),我認爲還有其他的解決方案。

相關問題