Apache camel 如何为代理API使用camel-consul组件?
根据consul的camel文档(camel.apache.org/consu component.html),支持的HTTP API是kv、事件和代理。有kv(键/值存储)的例子,它们运行良好,但代理API没有这样的例子。我浏览了concur[www.concur.io/docs/agent/http/agent.html]和相应的java客户端[github.com/OrbitzWorldwide/concur-client]的文档,试图找出concur:agent组件应该如何工作,但我没有发现简单的东西Apache camel 如何为代理API使用camel-consul组件?,apache-camel,consul,Apache Camel,Consul,根据consul的camel文档(camel.apache.org/consu component.html),支持的HTTP API是kv、事件和代理。有kv(键/值存储)的例子,它们运行良好,但代理API没有这样的例子。我浏览了concur[www.concur.io/docs/agent/http/agent.html]和相应的java客户端[github.com/OrbitzWorldwide/concur-client]的文档,试图找出concur:agent组件应该如何工作,但我没有
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
我还检查了提到的测试用例,但找不到任何与代理api相关的东西
所以我的问题是如何使用领事:代理组件
更新:我尝试了下面的代码,并能够得到服务
Object res = main.getCamelTemplate().requestBodyAndHeader("consul:agent", "", ConsulConstants.CONSUL_ACTION, ConsulAgentActions.SERVICES);
concur组件似乎只适用于HTTP代理API的GET操作。但是在这种情况下,我如何向concur组件注册一个新的服务(比如/v1/agent/service/register:Registers一个新的本地服务?这段代码适用于我:
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);
但它看起来有些不雅观(比如变通方法),我认为还有其他解决方案。可以使用
.to("consul:agent?action=SERVICES")
以映射
的形式检索注册的服务,服务id作为映射键
及
要写入注册,应使用ImmutableCatalogRegistration
作为正文
请注意,您可以使用CamelServiceRegistrationRoutePolicy
将camelservices路由注册为服务
.to("consul:catalog?action=REGISTER")