Hi all,

The project I’m currently working on is "*Integration of API microgateway
with etcd for service discovery*". This project is about making the API
microgateway capable of communicating with etcd for service discovery.

*Problem Definition*

   - When an API microgateway is created, it is capable of routing traffic
   to the URL of the API, provided at the time of API Creation.
   - In practical scenarios, micro services will have dynamic URLs. Since
   the microgateway is immutable, these dynamic URL changes will not be
   reflected at the microgateway. So the microgateway has to be stopped and
   restarted providing the new URL.

*Proposed Solution*

   - Integrating the API microgateway with etcd for discovering the target
   endpoints for routing API traffic.
   - etcd is a distributed key value store that provides a reliable way to
   store data across a cluster of machines. Using etcd, we can store a static
   key, and have the corresponding dynamic value which represents the URL of
   the target endpoint. If the URL of the endpoint changes due to some
   situation, etcd should be updated providing the new URL for the relevant
   key. So the API microgateway should know the etcd key and can obtain the
   correct URL using an etcd lookup and route the traffic to the returned URL.
   - Basically an entry in etcd will be a key-value pair as shown below.

worldbank-key                                                         - key
http://api.worldbank.org                                            - value

phoneverify-key                                                       - key
http://ws.cdyne.com/phoneverify/phoneverify.asmx    - value

*Proposed design*

   - During microgateway setup command, if the microgateway needs etcd
   enabled, use the flag -etcd.

micro-gw setup project_name -a WorldBank -v 1.0.0 -etcd

   - When the microgateway is run using bash gateway command, need to
   provide the etcd URL and relevant keys of APIs as CLI Parameters. (These
   keys are used to query etcd to fetch the API URL)

bash gateway -e etcdurl=http://127.0.0.1:2379 -e
WorldBank.1.0.0.prod.etcd.key=worldbank-key


   - I'm currently researching on a way to query etcd with a key, obtain
   the URL and reinitialize the API endpoint if the URL has changed. Proposed
   idea is to run a periodic task to query etcd and maintain a list of
   variables with the values. These variables will hold the actual URL to
   route to and the variables will be updated time to time through a periodic
   check to etcd.


   - The etcdLookup() function used to query etcd by providing the relevant
   key as shown below.

//key and value fields are defined as byte arrays and therefore must be
base64 encoded in JSON.
function etcdLookup(string key10) returns string
{
    http:Request req;
    string key64;
    string value64;
    var key = key10.base64Encode(charset = "utf-8");

    match key {
        string key1 => key64 = key1;
        error err => io:println("error: " + err.message);
    }
    req.setPayload({"key": untaint key64});
    var response = etcdEndpoint->post("/v3alpha/kv/range",req); // call to
etcd node
    match response {
        http:Response resp => {
            var msg = resp.getJsonPayload(); //fetch response
            match msg {
                json jsonPayload => {
                    var val64 = <string>jsonPayload.kvs[0].value;
//obtaining value corresponding to the key in base64 format
                    match val64 {
                        string value => value64 = value;
                        error err => io:println("error: " + err.message);
                    }
                }
                error err => {
                log:printError(err.message, err = err);
                }
            }
        }
        error err => { log:printError(err.message, err = err); }
    }

    var value10 = value64.base64Decode(charset = "utf-8"); //decoding the
base64 value to base10 format
    match value10 {
        string b => prodURL = untaint b;
        error err => io:println("error: " + err.message);
    }
    return backendURL;
}

Your feedback is much appreciated.

Thank you!
-- 
Hisan Hunais | Software Engineer Intern | WSO2 Inc.
(m) +94768526186 | (e) [email protected]

<https://wso2.com/signature>
_______________________________________________
Architecture mailing list
[email protected]
https://mail.wso2.org/cgi-bin/mailman/listinfo/architecture

Reply via email to