1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

connect to kafka and send metadata request.

This commit is contained in:
winlin 2015-10-15 17:45:58 +08:00
parent a9fdb630d9
commit 8974fe298b
8 changed files with 151 additions and 11 deletions

View file

@ -4298,6 +4298,23 @@ SrsConfDirective* SrsConfig::get_kafka_brokers()
return conf;
}
string SrsConfig::get_kafka_topic()
{
static string DEFAULT = "srs";
SrsConfDirective* conf = root->get("kafka");
if (!conf) {
return DEFAULT;
}
conf = conf->get("topic");
if (!conf || conf->arg0().empty()) {
return DEFAULT;
}
return conf->arg0();
}
SrsConfDirective* SrsConfig::get_vhost(string vhost, bool try_default_vhost)
{
srs_assert(root);

View file

@ -638,6 +638,10 @@ public:
* get the broker list, each is format in <ip:port>.
*/
virtual SrsConfDirective* get_kafka_brokers();
/**
* get the kafka topic to use for srs.
*/
virtual std::string get_kafka_topic();
// vhost specified section
public:
/**

View file

@ -33,6 +33,7 @@ using namespace std;
#include <srs_app_utility.hpp>
#include <srs_kernel_utility.hpp>
#include <srs_kernel_balance.hpp>
#include <srs_kafka_stack.hpp>
#ifdef SRS_AUTO_KAFKA
@ -40,12 +41,16 @@ SrsKafkaProducer::SrsKafkaProducer()
{
lb = new SrsLbRoundRobin();
worker = new SrsAsyncCallWorker();
transport = new SrsTcpClient();
kafka = new SrsKafkaClient(transport);
}
SrsKafkaProducer::~SrsKafkaProducer()
{
srs_freep(lb);
srs_freep(worker);
srs_freep(kafka);
srs_freep(transport);
}
int SrsKafkaProducer::initialize()
@ -86,25 +91,46 @@ int SrsKafkaProducer::request_metadata()
{
int ret = ERROR_SUCCESS;
// ignore when disabled.
bool enabled = _srs_config->get_kafka_enabled();
if (!enabled) {
return ret;
}
// select one broker to connect to.
SrsConfDirective* brokers = _srs_config->get_kafka_brokers();
if (!brokers) {
srs_warn("ignore for empty brokers.");
return ret;
}
srs_assert(!brokers->args.empty());
std::string broker = lb->select(brokers->args);
std::string server;
int port = SRS_CONSTS_KAFKA_DEFAULT_PORT;
if (true) {
srs_assert(!brokers->args.empty());
std::string broker = lb->select(brokers->args);
srs_parse_endpoint(broker, server, port);
}
// connect to kafka server.
if ((ret = transport->connect(server, port, SRS_CONSTS_KAFKA_TIMEOUT_US)) != ERROR_SUCCESS) {
srs_error("kafka connect %s:%d failed. ret=%d", server.c_str(), port, ret);
return ret;
}
// do fetch medata from broker.
std::string topic = _srs_config->get_kafka_topic();
if ((ret = kafka->fetch_metadata(topic)) != ERROR_SUCCESS) {
srs_error("kafka fetch metadata failed. ret=%d", ret);
return ret;
}
// log when completed.
if (true) {
std::string senabled = srs_bool2switch(enabled);
std::string sbrokers = srs_join_vector_string(brokers->args, ",");
srs_trace("kafka ok, enabled:%s, brokers:%s, current:[%d]%s",
senabled.c_str(), sbrokers.c_str(), lb->current(), broker.c_str());
srs_trace("kafka ok, enabled:%s, brokers:%s, current:[%d]%s:%d, topic:%s",
senabled.c_str(), sbrokers.c_str(), lb->current(), server.c_str(), port, topic.c_str());
}
return ret;

View file

@ -31,6 +31,8 @@
class SrsLbRoundRobin;
class SrsAsyncCallWorker;
class SrsTcpClient;
class SrsKafkaClient;
#ifdef SRS_AUTO_KAFKA
@ -42,6 +44,8 @@ class SrsKafkaProducer
private:
SrsLbRoundRobin* lb;
SrsAsyncCallWorker* worker;
SrsTcpClient* transport;
SrsKafkaClient* kafka;
public:
SrsKafkaProducer();
virtual ~SrsKafkaProducer();