第17章-golang實現簡易的分散式系統
功能
- 能夠傳送/接收請求和響應
- 能夠連線到叢集
- 如果無法連線到群集(如果它是第一個節點),則可以作為主節點啟動節點
- 每個節點有唯一的標識
- 能夠在節點之間交換json資料包
- 接受命令列引數中的所有資訊(將來在我們系統升級時將會很有用)
原始碼
package main
import (
"fmt"
"strconv"
"time"
"math/rand"
"net"
"flag"
"strings"
"encoding/json"
)
// 節點資料資訊
type NodeInfo struct {
// 節點ID,通過隨機數生成
NodeId int `json:"nodeId"`
// 節點IP地址
NodeIpAddr string `json:"nodeIpAddr"`
// 節點埠
Port string `json: "port"`
}
// 將節點資料資訊格式化輸出
//NodeInfo:{nodeId: 89423,nodeIpAddr: 127.0.0.1/8,port: 8001}
func (node *NodeInfo) String() string {
return "NodeInfo:{ nodeId:" + strconv.Itoa(node.NodeId) + ",nodeIpAddr:" + node.NodeIpAddr + ",port:" + node.Port + "}"
}
/* 新增一個節點到叢集的一個請求或者響應的標準格式 */
type AddToClusterMessage struct {
// 源節點
Source NodeInfo `json:"source"`
// 目的節點
Dest NodeInfo `json:"dest"`
// 兩個節點連線時傳送的訊息
Message string `json:"message"`
}
/* Request/Response 資訊格式化輸出 */
func (req AddToClusterMessage) String() string {
return "AddToClusterMessage:{\n source:" + req.Source.String() + ",\n dest: " + req.Dest.String() + ",\n message:" + req.Message + " }"
}
// cat vi go
// rm
func main() {
// 解析命令列引數
makeMasterOnError := flag.Bool("makeMasterOnError", false, "如果IP地址沒有連線到叢集中,我們將其作為Master節點.")
clusterip := flag.String("clusterip", "127.0.0.1:8001", "任何的節點連線都連線這個IP")
myport := flag.String("myport", "8001", "ip address to run this node on. default is 8001.")
flag.Parse() //解析
fmt.Println(*makeMasterOnError)
fmt.Println(*clusterip)
fmt.Println(*myport)
/* 為節點生成ID */
rand.Seed(time.Now().UTC().UnixNano()) //種子
myid := rand.Intn(99999999) // 隨機
//fmt.Println(myid)
// 獲取IP地址
myIp,_ := net.InterfaceAddrs()
fmt.Println(myIp[0])
// 建立NodeInfo結構體物件
me := NodeInfo{NodeId: myid, NodeIpAddr: myIp[0].String(), Port: *myport}
// 輸出結構體資料資訊
fmt.Println(me.String())
dest := NodeInfo{ NodeId: -1, NodeIpAddr: strings.Split(*clusterip, ":")[0], Port: strings.Split(*clusterip, ":")[1]}
/* 嘗試連線到叢集,在已連線的情況下並且向叢集傳送請求 */
ableToConnect := connectToCluster(me, dest)
/*
* 監聽其他節點將要加入到叢集的請求
*/
if ableToConnect || (!ableToConnect && *makeMasterOnError) {
if *makeMasterOnError {fmt.Println("Will start this node as master.")}
listenOnPort(me)
} else {
fmt.Println("Quitting system. Set makeMasterOnError flag to make the node master.", myid)
}
}
/*
* 這是傳送請求時格式化json包有用的工具
* 這是非常重要的,如果不經過資料格式化,你最終傳送的將是空白訊息
*/
func getAddToClusterMessage(source NodeInfo, dest NodeInfo, message string) (AddToClusterMessage){
return AddToClusterMessage{
Source: NodeInfo{
NodeId: source.NodeId,
NodeIpAddr: source.NodeIpAddr,
Port: source.Port,
},
Dest: NodeInfo{
NodeId: dest.NodeId,
NodeIpAddr: dest.NodeIpAddr,
Port: dest.Port,
},
Message: message,
}
}
func connectToCluster(me NodeInfo, dest NodeInfo) (bool){
/* 連線到socket的相關細節資訊 */
connOut, err := net.DialTimeout("tcp", dest.NodeIpAddr + ":" + dest.Port, time.Duration(10) * time.Second)
if err != nil {
if _, ok := err.(net.Error); ok {
fmt.Println("未連線到叢集.", me.NodeId)
return false
}
} else {
fmt.Println("連線到叢集. 傳送訊息到節點.")
text := "Hi nody.. 請新增我到叢集.."
requestMessage := getAddToClusterMessage(me, dest, text)
json.NewEncoder(connOut).Encode(&requestMessage)
decoder := json.NewDecoder(connOut)
var responseMessage AddToClusterMessage
decoder.Decode(&responseMessage)
fmt.Println("得到資料響應:\n" + responseMessage.String())
return true
}
return false
}
func listenOnPort(me NodeInfo){
/* 監聽即將到來的訊息 */
ln, _ := net.Listen("tcp", fmt.Sprint(":" + me.Port))
/* 接受連線 */
for {
connIn, err := ln.Accept()
if err != nil {
if _, ok := err.(net.Error); ok {
fmt.Println("Error received while listening.", me.NodeId)
}
} else {
var requestMessage AddToClusterMessage
json.NewDecoder(connIn).Decode(&requestMessage)
fmt.Println("Got request:\n" + requestMessage.String())
text := "Sure buddy.. too easy.."
responseMessage := getAddToClusterMessage(me, requestMessage.Source, text)
json.NewEncoder(connIn).Encode(&responseMessage)
connIn.Close()
}
}
}
執行程式
/Users/liyuechun/go
liyuechun:go yuechunli$ go install main
liyuechun:go yuechunli$ main
My details: NodeInfo:{ nodeId:53163002, nodeIpAddr:127.0.0.1/8, port:8001 }
不能連線到叢集. 53163002
Quitting system. Set makeMasterOnError flag to make the node master. 53163002
liyuechun:go yuechunli$
獲取相關幫助資訊
$ ./bin/main -h
liyuechun:go yuechunli$ ./bin/main -h
Usage of ./bin/main:
-clusterip string
ip address of any node to connnect (default "127.0.0.1:8001")
-makeMasterOnError
make this node master if unable to connect to the cluster ip provided.
-myport string
ip address to run this node on. default is 8001. (default "8001")
liyuechun:go yuechunli$
啟動Node1主節點
$ ./bin/main --makeMasterOnError
liyuechun:go yuechunli$ ./bin/main --makeMasterOnError
My details: NodeInfo:{ nodeId:82381143, nodeIpAddr:127.0.0.1/8, port:8001 }
未連線到叢集. 82381143
Will start this node as master.
新增節點Node2到叢集
$ ./bin/main --myport 8002 --clusterip 127.0.0.1:8001
新增節點Node3到叢集
main --myport 8004 --clusterip 127.0.0.1:8001
新增節點Node4到叢集
$ main --myport 8003 --clusterip 127.0.0.1:8002

image
相關文章
- go實現簡易分散式系統Go分散式
- 實現一個簡易的響應式系統
- 基於“結構體”實現簡易版學生管理系統(Golang)結構體Golang
- 基於AOP和Redis實現的簡易版分散式鎖Redis分散式
- 分散式系統Session 實現方式分散式Session
- redis分散式鎖實現(golang版)Redis分散式Golang
- 大型分散式系統現場,阿里大牛帶你實戰分散式系統分散式阿里
- [開源] Golang 實現的分散式 WebSocket 微服務Golang分散式Web微服務
- Python實現簡易版選課系統Python
- Golang 實現 Redis(8): TCC分散式事務GolangRedis分散式
- web端作業控制系統簡易實現Web
- 分散式系統1:什麼是分散式系統——簡要的介紹與定義分散式
- VirtualView iOS 簡易字串表示式的實現ViewiOS字串
- gin websocket 簡單分散式實現Web分散式
- Golang 分散式 ID 生成系統,高效能、高可用、易擴充套件的 id 生成服務Golang分散式套件
- Golang 基於單節點 Redis 實現的分散式鎖GolangRedis分散式
- 分散式系統2:分散式系統中的時鐘分散式
- 分散式 - 分散式系統的特點分散式
- 分散式跟蹤系統zipkin簡介分散式
- jQuery實現簡易商城系統專案實操詳解jQuery
- 打造高效的分散式爬蟲系統:利用Scrapy框架實現分散式爬蟲框架
- 分散式系統選主場景分析及實現分散式
- 分散式系統限流演算法分析與實現分散式演算法
- 如何實現簡單的分散式鏈路功能?分散式
- PHP 實現簡單阻塞分散式鎖PHP分散式
- 整合spring cloud雲架構 --spring cloud分散式系統中實現分散式鎖SpringCloud架構分散式
- [分散式限流] 滑動視窗演算法的 Golang 實現分散式演算法Golang
- 基於long pull實現簡易的訊息系統參考
- 【分散式系統設計簡卷(0)】MapReduce分散式
- 基於 Golang 開發的分散式定時任務管理系統Golang分散式
- ECTS——使用 Golang 開發的分散式定時任務管理系統Golang分散式
- 基於golang分散式爬蟲系統的架構體系v1.0Golang分散式爬蟲架構
- 使用Spring Cloud Sleuth實現分散式系統的鏈路追蹤SpringCloud分散式
- 分散式系統的跟蹤系統分散式
- 分散式系統分散式
- 網易雲音樂推薦系統簡單實現系列
- 分散式系統:系統模型分散式模型
- 簡易ATM系統