Skip to content

Discovery API for interaction with consul, this API useful if you have API with Microservices architecture.

Notifications You must be signed in to change notification settings

restuwahyu13/go-discovery-api

Repository files navigation

Golang Service Discovery

This is basic concept for service registry and discovery if you want more complex use case , you can check this video tutorial or you can buy this e-book from amazon.

Benefit Using Consul

  • Monitoring service is health or unhealth
  • Auto failover with cross server when one of service is dead, you can using pairing feature
  • Network security communication using tls encryption
  • Discovery & Registry service, you can find service using id or name
  • Management access control using acl feature, like A allow connect to B and C deny connecto to A
  • You can use Load Balancing, Rate Limit, Circuit Bracker etc
  • Cross cloud platform support and container support like docker or kubernetes for complex use case, you can use kubernetes is recommended because most populer and many fiture
  • Etc

Simple Sample Policy

service_prefix "prod-service-" {
  policy = "read"
}

node_prefix "" {
  policy = "read"
}

Full Sample Policy

acl      = "write"
operator = "write"
mesh     = "write"
peering  = "write"
keyring  = "write"

agent_prefix "" {
  policy = "write"
}

node_prefix "" {
  policy = "write"
}

identity_prefix "" {
  policy = "write"
}

key_prefix "" {
  policy = "write"
}

service_prefix "" {
  policy     = "write"
  intentions = "write"
}

event_prefix "" {
  policy = "write"
}

session_prefix "" {
  policy = "write"
}

query_prefix "" {
  policy = "write"
}

Example Use Service Discovery Direct Connect To Consul

import ConsulHashicorp from 'consul'
import axios from 'axios'

class Consul {
  static consul = new ConsulHashicorp({ host: 'localhost', port: '8500', secure: false })

  static getKV(key) {
    return Consul.consul.kv.get(key)
  }

  static async health(serviceName, options) {
    try {
      const list = await Consul.consul.agent.check.list(options)
      if (!list[serviceName]) {
        return
      }
      return list[serviceName]
    } catch (e) {
      throw new Error(e)
    }
  }

  static async service(serviceName, options) {
    try {
      const list = await Consul.consul.agent.service.list(options)
      if (!list[serviceName]) {
        return
      }
      return list[serviceName]
    } catch (e) {
      throw new Error(e)
    }
  }
}

;(async () => {
  const secretToken = '4cf4f739-e472-dfa5-280e-1c507d5ee326' // I use production secret token

  const svcGreatday = await Consul.service('prod-service-greatday', { token: secretToken })
  const svcMekari = await Consul.service('prod-service-mekari', { token: secretToken })

  axios
    .get(`http://${svcGreatday?.Address}:${svcGreatday?.Port}`)
    .then(({ data }) => console.log(`\n${svcGreatday.Service} Data:\n`, data))
    .catch((e) => console.error(`\n${svcGreatday?.Service} Error:\n`, e?.message || e.cause?.code))

  axios
    .get(`http://${svcMekari?.Address}:${svcMekari?.Port}`)
    .then(({ data }) => console.log(`\n${svcMekari?.Service} Data:\n`, data))
    .catch((e) => console.error(`\n${svcMekari?.Service} Error:\n`, e?.message || e.cause?.code))
})()

About

Discovery API for interaction with consul, this API useful if you have API with Microservices architecture.

Topics

Resources

Stars

Watchers

Forks