Kite Micro-Service Framework

Go 中的微服务框架。「Micro-service framework in Go」

Github stars Tracking Chart

Kite(风筝)微服务框架

Kite 是一个用 Go 开发微服务的框架。

Kite

Kite 既是框架的名字,也是使用这个框架编写的微服务。基本上,Kite 是一个 RPC 服务器,也是一个客户端。它连接到其他的风筝和同行之间进行通信。它们可以使用一个叫做 Kontrol 的服务发现其他风筝,并与它们进行双向通信。通信协议使用 WebSocket(或 XHR)作为传输,以允许 Web 应用程序直接连接到风筝。

风筝可以通过套接字会话发送 dnode 消息来相互交谈。如果客户端知道服务器风筝的 URL,就可以直接连接到它。如果不知道 URL,客户端可以从 Kontrol(服务发现)中索取。

欲了解更多信息,请查看 GopherAcademy 的博客文章,其中详细解释了 Kite:http://blog.gopheracademy.com/birthday-bash-2014/kite-microservice-library/

安装和使用

用安装包安装:

go get github.com/koding/kite

用以下方法导入:

import "github.com/koding/kite"

并在代码中使用 kite 作为包名。

什么是 Kontrol?

Kontrol 是 Kites 使用的服务注册表和认证服务。它本身也是一个风筝。

当一个风筝开始运行时,如果愿意的话,它可以用 Register() 方法将自己注册到 Kontrol。这样别人就可以通过查询 Kontrol 找到它。还有一个代理风筝,用于给注册的风筝提供公共网址。

查询有 7 个字段:

/<username>/<environment>/<name>/<version>/<region>/<hostname>/<id>
  • 你必须至少提供用户名。
  • 字段的顺序是由一般到特殊。
  • 查询的字段之间不能包含空的部分。

安装 Kontrol

安装 Kontrol:

go get github.com/koding/kite/kontrol/kontrol

为 Kite key 生成 keys:

openssl genrsa -out key.pem 2048
openssl rsa -in key.pem -pubout > key_pub.pem

设置环境变量:

KONTROL_PORT=6000
KONTROL_USERNAME="kontrol"
KONTROL_STORAGE="etcd"
KONTROL_KONTROLURL="http://127.0.0.1:6000/kite"
KONTROL_PUBLICKEYFILE="certs/key_pub.pem"
KONTROL_PRIVATEKEYFILE="certs/key.pem"

生成初始 Kite 密钥:

./bin/kontrol -initial

浏览器如何使用风筝?

浏览器也可以是一个 Kite。它有自己的方法("log" 用于将消息记录到控制台,"alert" 用于向用户显示警报,等等)。一个连接的 kite 可以调用网页上定义的方法。

更多信息请参见 kite.js library 库。

如何编写一个新的 kite?

  • Import kite 包。
  • kite.New() 创建一个新实例。
  • 使用 k.HandleFunc()k.Handle() 添加你的方法处理程序。
  • 调用 k.Run()

下面举个例子,一个数学 kite,它可以计算接收数的平方:

package main
import "github.com/koding/kite"
func main() {
    // Create a kite
    k := kite.New("math", "1.0.0")
    // Add our handler method with the name "square"
    k.HandleFunc("square", func(r *kite.Request) (interface{}, error) {
        a := r.Args.One().MustFloat64()
        result := a * a    // calculate the square
        return result, nil // send back the result
    }).DisableAuthentication()
    // Attach to a server with port 3636 and run it
    k.Config.Port = 3636
    k.Run()
}

现在让我们连接到它并发送一个 4 作为参数。

package main
import (
    "fmt"
    "github.com/koding/kite"
)
func main() {
    k := kite.New("exp2", "1.0.0")
    // Connect to our math kite
    mathWorker := k.NewClient("http://localhost:3636/kite")
    mathWorker.Dial()
    response, _ := mathWorker.Tell("square", 4) // call "square" method with argument 4
    fmt.Println("result:", response.MustFloat64())
}

查看 examples 文件夹,了解更多的例子。


Main metrics

Overview
Name With Ownerkoding/kite
Primary LanguageGo
Program languageMakefile (Language Count: 5)
PlatformLinux, Mac, Windows
License:MIT License
所有者活动
Created At2014-02-20 21:14:43
Pushed At2018-10-28 20:12:37
Last Commit At2018-07-10 05:13:47
Release Count0
用户参与
Stargazers Count3.3k
Watchers Count134
Fork Count301
Commits Count1.8k
Has Issues Enabled
Issues Count87
Issue Open Count34
Pull Requests Count130
Pull Requests Open Count0
Pull Requests Close Count18
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

Kite Micro-Service Framework

Kite is a framework for developing micro-services in Go.

GoDoc
Build Status

Kite

Kite is both the name of the framework and the micro-service that is written by
using this framework. Basically, Kite is a RPC server as well as a client. It
connects to other kites and peers to communicate with each other. They can
discover other kites using a service called Kontrol, and communicate with them
bidirectionaly. The communication protocol uses a WebSocket (or XHR) as transport
in order to allow web applications to connect directly to kites.

Kites can talk with each other by sending
dnode
messages over a socket session. If the client knows the URL of the server kite it
can connect to it directly. If the URL is not known, client can ask for it
from Kontrol (Service Discovery).

For more info checkout the blog post at GopherAcademy which explains Kite in more detail: http://blog.gopheracademy.com/birthday-bash-2014/kite-microservice-library/

Install and Usage

Install the package with:

go get github.com/koding/kite

Import it with:

import "github.com/koding/kite"

and use kite as the package name inside the code.

What is Kontrol?

Kontrol is the service registry and authentication service used by Kites. It
is itself a kite too.

When a kite starts to run, it can registers itself to Kontrol with the
Register() method if wished. That enables others to find it by querying
Kontrol. There is also a Proxy Kite for giving public URLs to registered
kites.

Query has 7 fields:

/<username>/<environment>/<name>/<version>/<region>/<hostname>/<id>
  • You must at least give the username.
  • The order of the fields is from general to specific.
  • Query cannot contains empty parts between fields.

Installing Kontrol

Install Kontrol:

go get github.com/koding/kite/kontrol/kontrol

Generate keys for the Kite key:

openssl genrsa -out key.pem 2048
openssl rsa -in key.pem -pubout > key_pub.pem

Set environment variables:

KONTROL_PORT=6000
KONTROL_USERNAME="kontrol"
KONTROL_STORAGE="etcd"
KONTROL_KONTROLURL="http://127.0.0.1:6000/kite"
KONTROL_PUBLICKEYFILE="certs/key_pub.pem"
KONTROL_PRIVATEKEYFILE="certs/key.pem"

Generate initial Kite key:

./bin/kontrol -initial

How can I use kites from a browser?

A browser can also be a Kite. It has it's own methods ("log" for logging a
message to the console, "alert" for displaying alert to the user, etc.). A
connected kite can call methods defined on the webpage.

See kite.js library for more information.

How can I write a new kite?

  • Import kite package.
  • Create a new instance with kite.New().
  • Add your method handlers with k.HandleFunc() or k.Handle().
  • Call k.Run()

Below you can find an example, a math kite which calculates the square of a
received number:

package main

import "github.com/koding/kite"

func main() {
	// Create a kite
	k := kite.New("math", "1.0.0")

	// Add our handler method with the name "square"
	k.HandleFunc("square", func(r *kite.Request) (interface{}, error) {
		a := r.Args.One().MustFloat64()
		result := a * a    // calculate the square
		return result, nil // send back the result
	}).DisableAuthentication()

	// Attach to a server with port 3636 and run it
	k.Config.Port = 3636
	k.Run()
}

Now let's connect to it and send a 4 as an argument.

package main

import (
	"fmt"

	"github.com/koding/kite"
)

func main() {
	k := kite.New("exp2", "1.0.0")

	// Connect to our math kite
	mathWorker := k.NewClient("http://localhost:3636/kite")
	mathWorker.Dial()

	response, _ := mathWorker.Tell("square", 4) // call "square" method with argument 4
	fmt.Println("result:", response.MustFloat64())
}

Check out the examples
folder for more examples.