Coder Social home page Coder Social logo

go-micro's Introduction

Go Micro GoDoc Travis CI

Go Micro is a pluggable RPC based microservice library which provides the fundamental building blocks for writing distributed applications. It is part of the Micro toolchain. It supports Proto-RPC and JSON-RPC as the request/response protocol out of the box and defaults to Consul for discovery.

Every aspect of go-micro is pluggable.

An example server can be found in examples/server.

Features

Feature Package Built-in Plugin Description
Discovery Registry consul A way of locating services to communicate with
Client Client rpc Used to make RPC requests to a service
Codec Codec proto,json Encoding/Decoding handler for requests
Balancer Selector random Service node filter and pool
Server Server rpc Listens and serves RPC requests
Pub/Sub Broker http Publish and Subscribe to events
Transport Transport http Communication mechanism between services

Go Plugins

By default go-micro only provides a single implementation of each interface. Plugins can be found at github.com/micro/go-plugins. Contributions welcome!

Prerequisites

Consul is the default discovery mechanism provided in go-micro. Discovery is however pluggable.

Install Consul

https://www.consul.io/intro/getting-started/install.html

Getting Started

Run Consul

$ consul agent -server -bootstrap-expect 1 -data-dir /tmp/consul

Run Service

$ go run examples/server/main.go --logtostderr
I1108 11:08:19.926071   11358 server.go:96] Starting server go.micro.srv.example id go.micro.srv.example-04de4cf0-8609-11e5-bf3a-68a86d0d36b6
I1108 11:08:19.926407   11358 rpc_server.go:233] Listening on [::]:54080
I1108 11:08:19.926500   11358 http_broker.go:80] Broker Listening on [::]:54081
I1108 11:08:19.926632   11358 rpc_server.go:158] Registering node: go.micro.srv.example-04de4cf0-8609-11e5-bf3a-68a86d0d36b6

Test Service

$ go run examples/client/main.go 
go.micro.srv.example-59b6e0ab-0300-11e5-b696-68a86d0d36b6: Hello John

Writing a service

Create request/response proto

go-micro/examples/server/proto/example/example.proto:

syntax = "proto3";

message Request {
        string name = 1;
}

message Response {
        string msg = 1;
}

Compile proto protoc -I$GOPATH/src --go_out=$GOPATH/src $GOPATH/src/github.com/micro/go-micro/examples/server/proto/example/example.proto

Create request handler

go-micro/examples/server/handler/example.go:

package handler

import (
	log "github.com/golang/glog"
	c "github.com/micro/go-micro/context"
	example "github.com/micro/go-micro/examples/server/proto/example"
	"github.com/micro/go-micro/server"

	"golang.org/x/net/context"
)

type Example struct{}

func (e *Example) Call(ctx context.Context, req *example.Request, rsp *example.Response) error {
	md, _ := c.GetMetadata(ctx)
	log.Infof("Received Example.Call request with metadata: %v", md)
	rsp.Msg = server.Config().Id() + ": Hello " + req.Name
	return nil
}

Init server

go-micro/examples/server/main.go:

package main

import (
	log "github.com/golang/glog"
	"github.com/micro/go-micro/cmd"
	"github.com/micro/go-micro/examples/server/handler"
	"github.com/micro/go-micro/server"
)

func main() {
	// optionally setup command line usage
	cmd.Init()

	// Initialise Server
	server.Init(
		server.Name("go.micro.srv.example"),
	)

	// Register Handlers
	server.Handle(
		server.NewHandler(
			new(handler.Example),
		),
	)

	// Run server
	if err := server.Run(); err != nil {
		log.Fatal(err)
	}
}

Run service

$ go run examples/server/main.go --logtostderr
I1108 11:08:19.926071   11358 server.go:96] Starting server go.micro.srv.example id go.micro.srv.example-04de4cf0-8609-11e5-bf3a-68a86d0d36b6
I1108 11:08:19.926407   11358 rpc_server.go:233] Listening on [::]:54080
I1108 11:08:19.926500   11358 http_broker.go:80] Broker Listening on [::]:54081
I1108 11:08:19.926632   11358 rpc_server.go:158] Registering node: go.micro.srv.example-04de4cf0-8609-11e5-bf3a-68a86d0d36b6

go-micro's People

Contributors

crackcomm avatar st3v avatar

Watchers

James Cloos avatar Jason avatar

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.