Giter VIP home page Giter VIP logo

microservice-tools's Introduction

Package with shared tools for the microservices

Build Test Coverage Maintainability

This package provides tools that can be used by all microservices, such as:

  • API for service registry (Self registration and unregistration of a microservice)

Installation

To install run:

go get -u github.com/Microkubes/microservice-tools

Install it from the private repository

You'll might get a problem when installing because the github repository is private.

By default go get will try to user a https:// to clone the repository. If you have a valid key for accessing the repository (in ~/.ssh/) run the following command to force git to use ssh:// as protocol for access and to use your access key for the repository:

git config --global url."ssh://[email protected]:".insteadOf "https://github.com"

Microservice self-registration

Configuring the microservice

The service configuration is loaded from a JSON config file. The file has the following structure:

{
  "name": "user-microservice",
  "port": 8080,
  "virtual_host": "user.services.jormugandr.org",
  "hosts": ["localhost", "user.services.jormugandr.org"],
  "weight": 10,
  "slots": 100
}

Fields:

  • name - the name of the microservice. This name will be used to register the API with on Kong. This has to be the same for all instances of the microservices.
  • port - local port of the microservice.
  • virtual_host - this is the domain name for the services group. This has to be the same for all instances of the microservice.
  • hosts - list of valid hosts that can access the microservice. You need to put at least the virtual_host here, otherwise you won't be able to access the API via the gateway.
  • weight - an integer value used by the API gateway for load balancing.
  • slots - maximal number of instances (targets) for the same microservice group. When registering a service with the gateway, each instance creates a target on the gateway associated with the virtual_host. When a request passes through the gateway, the HTTP header Host is inspected, and based on that the virtual_host is determined. Then the gateway passes (proxies) the request to a specific the microservice instance. This number specifies the maximal number of targets for a group like this.

Adding self-registration to a microservice

To add self-registration logic in your microservice, you need to create a gateway.Registration and then use SelfRegister().

Here is an example of self-registration with Kong:

import (
  // other imports here

  // import the gateway package
  gateway "microservice-tools/gateway"
)

// other code in the main.go file

func main(){
  // load the Gateway URL and the config file path
  gatewayURL, configFile := loadGatewaySettings()

  // creates new Kong gateway.Registration with the config settings. We pass the default http client here.
  registration, err := gateway.NewKongGatewayFromConfigFile(gatewayURL, &http.Client{}, configFile)
  if err != nil {
    // if there is an error, it means we failed to build Registration for Kong.
    panic(err)
  }
  // at this point we do a self-registration by calling SelfRegister
  err = registration.SelfRegister()
  if err != nil {
    // if there is an error it means we failed to self-register so we panic with error
    panic(err)
  }

  // the unregistration is deferred for after main() executes. If we shut down
  // the service, it is nice to unregister, although it is not required.
  defer registration.Unregister()


  // rest of the code for main goes here
}

// loadGatewaySettings loads the API Gateway URL and the path to the JSON config file from ENV variables.
func loadGatewaySettings() (string, string) {
  gatewayURL := os.Getenv("API_GATEWAY_URL")
  serviceConfigFile := os.Getenv("SERVICE_CONFIG_FILE")

  if gatewayURL == "" {
    gatewayURL = "http://localhost:8001"
  }
  if serviceConfigFile == "" {
    serviceConfigFile = "config.json"
  }

  return gatewayURL, serviceConfigFile
}

Healthcheck

To add healthcheck to your microservice you need to mount the healtcheck middleware in the microservice main file:

service.Use(healthcheck.NewCheckMiddleware("/healthcheck"))

To test it out, build the microservice locally and do curl http://localhost:8080/healtcheck

It should return OK.

Version endpoint

To add version endpoint to your microservice, first you need to add the version of the microservice into configuration JSON file, for example:

{
    "version": "v1.0.0"
}

Next, mount the version endpoint middleware in the microservice main file:

service.Use(version.NewVersionMiddleware(cfg.Version, "/version"))

To test it out, build the microservice locally and do curl http://localhost:8080/version

It should return the microservice version.

Contributing

For contributing to this repository or its documentation, see the Contributing guidelines.

microservice-tools's People

Contributors

blazhovsky avatar mbocevski avatar ninazid avatar nushkovg avatar vladoohr avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

nushkovg

microservice-tools's Issues

Refactor package "rabbitmq" to a concurrent interace

Current implementation of the messaging API in "rabbitmq" package offers a creation of Connection and AMQPChannel which perform dial and queue declare in the same action.
Although Connection is concurrent and it is encouraged to be reused (in go routines), Channel does not seem to be concurrent and causes errors when used with go routines (errors of type "connection closed" when the connection is actually open).

We need to split this API into two parts:

  • First dial to the RabbitMQ server
  • Provide a structure that provide concurrent Send/Receive methods.

This would require an overhaul of the "rabbitmq" package, and would impact the services using this API (namely microservice-mai, microservice-registration and microservice-user).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.