Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

欢迎试用workflow项目内置Consul Client #1021

Closed
Barenboim opened this issue Aug 20, 2022 · 0 comments
Closed

欢迎试用workflow项目内置Consul Client #1021

Barenboim opened this issue Aug 20, 2022 · 0 comments
Labels
documentation Improvements or additions to documentation

Comments

@Barenboim
Copy link
Contributor

Barenboim commented Aug 20, 2022

workflow项目已经自带了Consul client用于对接Consul服务治理,接口非常简单,熟悉consul的用户看一看这两个文件就可以很好理解:
https://github.com/sogou/workflow/blob/master/src/client/WFConsulClient.h
https://github.com/sogou/workflow/blob/master/src/client/WFConsulClient.cc
对于服务注册,只需要在server启动之后,产生一个register task,异步注册到consul上。server退出前,也最好注销一下。例如:

int main()
{
    WFRedisServer server(redis_process);  // 我们也内置redis server
    WFConsulClient client;
    WFConsulTask *consul_task;

    client.init("http://127.0.0.1:8500/"); // 祖传不支持RAII
    if (server.start(...) == 0)
    {
        consul_task = client.create_register_task(...);
        consul_task->start();
        wait_group.wait();

        consul_task = client.create_deregister_task(...);
        consul_task->start();
        ...
        server.stop();
    }
    client.deinit();

    return 0;
}

使用服务发现,可以启动一个后台series,通过WFConsulClient::create_discovery_task()接口获得server list,并与timer结合来循环访问。利用workflow的upstream功能,选择一种upstream方法,将server加入upstream或从upstream删除,对应用程序完全透明。
由于workflow启动http server非常方便,如果需要让Consul探活,直接后台启动一个http服务并注册给Consul就可以了。此外,我们接口上都有service_namespace参数,由于免费版Consul不支持namespace,这里需要传一个空的std::string。
希望使用Consul的同学多试用一下并提一些建议。谢谢。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant