oliora / ppconsul

C++ client for Consul (http://consul.io)
Boost Software License 1.0
152 stars 56 forks source link

Service metadata not populated in service discovery #40

Closed joconnor-openet closed 5 years ago

joconnor-openet commented 5 years ago

Followon from https://github.com/oliora/ppconsul/issues/29 Version: git revision 6a1a8aa5 on Centos 7.7.1908

When discovering service info, the "ServiceInfo.meta" map is not populated.

Steps to reproduce:

  1. start consul: ./consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul -node=agent-one -bind=127.0.0.1 -client=127.0.0.1
  2. add service to consul: ./consul services register -address=10.0.0.1 -port=1234 -id=my-id-001 -name=name1 -kind=my-kind -http-addr=http://127.0.0.1:8500 -meta metakey1=metaval1 -meta metakey2=metaval2 -meta secure=true -tag test1 -tag tagtest2 -tag test3
  3. run attached test program

Expected results:

Address: 10.0.0.1
ID: my-id-001
Name: name1
Port: 1234
Tags: 
    tagtest2
    test1
    test3
Metadata: 
    metakey1=>metaval1
    metakey2=>metaval2
    secure=>true

Actual results:

Address: 10.0.0.1
ID: my-id-001
Name: name1
Port: 1234
Tags: 
    tagtest2
    test1
    test3
Metadata: 
joconnor-openet commented 5 years ago
/*
 * g++ -std=c++11 -o ppconsul-meta-bug ppconsul-meta-bug.cc -I/path/to/ppconsul/include/  -I/usr/local64/boost/boost_1_55_0/include  -L/path/to/ppconsul/lib -lppconsul
 */
#include <ppconsul/consul.h>
#include <ppconsul/catalog.h>
#include <chrono>
using namespace ppconsul;
using namespace std;
int main(int argc, char *argv[]) {
    try {
        Consul consul("http://127.0.0.1:8500");
        catalog::Catalog catalog(consul);
        uint64_t index = 0;
        auto response = catalog.service(withHeaders, "name1", catalog::kw::block_for = {std::chrono::seconds(10), index});

        index = response.headers().index();
        std::cout << "index=" << index << std::endl;
        std::for_each(response.data().begin(), response.data().end(), [](catalog::NodeService& nodeService){
            ServiceInfo& serviceInfo = nodeService.second;
            std::cout << "Address: " << serviceInfo.address << std::endl;
            std::cout << "ID: " << serviceInfo.id << std::endl;
            std::cout << "Name: " << serviceInfo.name << std::endl;
            std::cout << "Port: " << serviceInfo.port << std::endl;
            std::cout << "Tags: " << std::endl;
            for (const auto& tag: serviceInfo.tags) {
                std::cout << "\t" << tag << std::endl;
            }
            std::cout << "Metadata: " << std::endl;
            for (const auto& meta: serviceInfo.meta) {
                std::cout << "\t" << meta.first << "=>" << meta.second << std::endl;
            }
        });
    } catch(std::exception& e) {
        std::cout << e.what() << std::endl;
    }
}
joconnor-openet commented 5 years ago

Proposed fix, works when testing locally:

diff --git a/src/catalog.cpp b/src/catalog.cpp
index 5d38e601..cbd03404 100644
--- a/src/catalog.cpp
+++ b/src/catalog.cpp
@@ -20,6 +20,7 @@ namespace json11 {
         load(src, dst.second.address, "ServiceAddress");
         load(src, dst.second.port, "ServicePort");
         load(src, dst.second.tags, "ServiceTags");
+        load(src, dst.second.meta, "ServiceMeta");
     }

     void load(const json11::Json& src, ppconsul::catalog::NodeServices& dst)
oliora commented 5 years ago

Should be fixed now