HTTPサーバーはDiarkisのエントリーポイントとなります。アプリケーションサーバーは、HTTPサーバーに接続して、リアルタイム接続のエンドポイントと暗号化キーを取得します。
ここでは、HTTPサーバーの基本的なコードの書き方と、設定について説明します。
HTTPサーバの設定
address |
“127.0.0.1” |
HTTPサーバーのアドレスまたはホスト名を指定します |
port |
“7000” |
HTTPサーバーのポートを指定します |
useFixedPort |
false |
trueの場合、HTTPサーバーは指定されたポートでのみバインドされます |
enableEncryption |
true |
falseの場合は、リアルタイム通信の暗号化が無効になります。他のサーバーにも同様の設定をする必要があります |
サーバーコード
HTTPサーバーの主要なソースファイルです。
servers/http/main.go
package main
import (
"fmt"
"github.com/Diarkis/diarkis"
"your/project/cmds"
"github.com/Diarkis/diarkis/log"
"github.com/Diarkis/diarkis/mesh"
"github.com/Diarkis/diarkis/server/http"
"os"
)
func main() {
rootpath, err := os.Getwd()
if err != nil {
panic(err)
}
log.Setup(fmt.Sprintf("%s/configs/shared/log.json", rootpath))
// Sets up the internal communication network
mesh.Setup(fmt.Sprintf("%s/configs/shared/mesh.json", rootpath))
// This sets up HTTP server as Diarkis cluster entry point
http.SetupAsAuthServer(fmt.Sprintf("%s/configs/http/main.json", rootpath))
// Sets up match making with configurations
matching.Setup(fmt.Sprintf("%s/configs/shared/matching.json", rootpath))
cmds.Setup(rootpath)
// All HTTP REST API implementations are exposed by calling cmds.ExposeHTTP()
cmds.ExposeHTTP()
diarkis.Start()
}
カスタムHTTP RESTエンドポイントの作成方法
DiarkisのHTTPサーバーでは、カスタムRESTエンドポイントを書くことができます。
cmds/http/main.go
package httpcmds
import (
"github.com/Diarkis/diarkis/server/http"
)
func Expose(rootpath string) {
// :message is treated as a parameter and the value can be accessed from *http.Params
http.Get("/hello/:message", handleHello)
}
func handleHello(res *http.Response, req *http.Request, params *http.Params, next func(error)) {
message := params.GetAsString("message")
res.Respond(message, http.Ok)
// move on to other handlers
next(nil)
}
マッチメイキングの設定
マッチメイキングを使用するには、HTTPサーバーとTCP/UDPサーバーの両方がこの設定を行う必要があります。
distributionRate |
30 |
HTTPサーバノード間でのシャードしたマッチメイキングデータの分配率 例:30は30%を意味します。 |
Diarkis Match Makingでは、複数の検索条件を定義することができます。マッチメイキングの定義は、HTTPサーバーで定義する必要があります。
levelAndRank := "levelAndRank"
conds := make(map[string]int)
// Search will be in the range of level 0 to 10, 11 to 20, 21 to 30...
conds["level"] = 10
// Search will be in the range of rank 0 to 50, 51 to 100, 101 to 150...
conds["rank"] = 50
// levelAndRank will search against level and rank within their ranges.
matching.Define(levelAndRank, conds)
マッチメイキングのための複雑なマッチ条件の設定方法
以下の例では、複合的なマッチ条件を定義して、厳しい条件のマッチを最初に行い、十分なマッチが見つからなかった場合には、より厳しい条件のマッチを行うようにしています。Match Makingの定義は、HTTPサーバーで定義する必要があります。
strict = "strict"
strictConds := make(map[string]int)
strictConds["power"] = 5
strictConds["league"] = 1
// This is the definition of the first strict search condition
matching.Define(strict, strictConds)
lessStrict = "lessStrict"
lessStrictConds := make(map[string]int)
lessStrictConds["power"] = 10
lessStrictConds["league"] = 5
// This is the second definition and it is less strict than the first condition
matching.Define(lessStrict, lessStrictConds)
Auth API
Diarkisサーバークラスターに接続するためには、Diarkis HTTPサーバーから接続エンドポイントを取得する必要があります。
APIを以下に示します。
GET /auth/<application user ID>
{
"WS":"<websocket endpoint address and port>",
"UDP":"<udp endpoint address and port>",
"TCP":"<tcp endpoint address and port>"
}
Metrics API
Diarkisには、Diarkisアプリケーションで何が起こっているのかをリアルタイムで確認するためのメトリクスREST APIがあります。 このAPIをスクレイピングして、Prometheus + Grafana, Datadogなどの分析ツールにデータを転送することで、詳細な分析ツールをすぐに利用することができます。
Prometheus フォーマット:
GET /metrics/prometheus/v/2 |
JSON フォーマット:
GET /metrics/json |
Metrics データ - Prometheus フォーマット
※ 下記以外にも様々なメトリクスデータがあります。
Users_<protocol>_node |
各プロトコルのノードあたりのCCU(Concurrent Users) |
<protocol>_Packets_In_<protocol> |
各プロトコルのノードごとの受信パケット数 |
<protocol>_Packets_Out_<protocol> |
各プロトコルのノードごとの送信パケット数 |
Mesh_Packets_In_<protocol>_node |
各プロトコルのノードごとのインバウンド内部パケット |
Mesh_Packets_Out_<protocol>_node |
各プロトコルのノードごとのアウトバウンド内部パケット数 |