【问题标题】:How to use camel consul component for agent API?如何将骆驼领事组件用于代理 API?
【发布时间】:2017-07-02 10:55:41
【问题描述】:

根据 consul (camel.apache.org/consul-component.html) 的骆驼文档,支持的 HTTP API 是 kv、事件和代理。有 kv(键/值存储)的例子,它们工作正常,但代理 API 没有这样的例子。我还浏览了 Consul [www.consul.io/docs/agent/http/agent.html] 和相应的 java 客户端 [github.com/OrbitzWorldwide/consul-client] 的文档,并试图弄清楚 consul 如何:代理组件应该可以工作,但我发现那里没有什么简单的。

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 :注册新的本地服务)?

【问题讨论】:

    标签: apache-camel consul


    【解决方案1】:

    此代码适用于我:

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

    但它看起来有些不雅(比如解决方法),我认为还有其他解决方案。

    【讨论】:

      【解决方案2】:

      可以使用

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

      Map<String, Service>检索注册的服务,服务ID为映射键。

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

      写注册,期望ImmutableCatalogRegistration作为正文

      请注意,您可以使用 CamelServiceRegistrationRoutePolicy 将 Camel 路线注册为服务 automatically

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-22
        • 1970-01-01
        • 2014-10-27
        • 2014-02-11
        相关资源
        最近更新 更多