经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Go语言 » 查看文章
golang实现简易的分布式系统方法
来源:jb51  时间:2018/10/10 8:47:23  对本文有异议

本文介绍了golang实现简易的分布式系统方法,分享给大家,具体如下:

功能

  • 能够发送/接收请求和响应

  • 能够连接到集群

  • 如果无法连接到群集(如果它是第一个节点),则可以作为主节点启动节点

  • 每个节点有唯一的标识

  • 能够在节点之间交换json数据包

  • 接受命令行参数中的所有信息(将来在我们系统升级时将会很有用)

源码

  1. package main
  2.  
  3. import (
  4.   "fmt"
  5.   "strconv"
  6.   "time"
  7.   "math/rand"
  8.   "net"
  9.   "flag"
  10.   "strings"
  11.   "encoding/json"
  12. )
  13.  
  14. // 节点数据信息
  15. type NodeInfo struct {
  16.  
  17.   // 节点ID,通过随机数生成
  18.   NodeId int `json:"nodeId"`
  19.   // 节点IP地址
  20.   NodeIpAddr string `json:"nodeIpAddr"`
  21.   // 节点端口
  22.   Port string `json: "port"`
  23. }
  24.  
  25. // 将节点数据信息格式化输出
  26. //NodeInfo:{nodeId: 89423,nodeIpAddr: 127.0.0.1/8,port: 8001}
  27. func (node *NodeInfo) String() string {
  28.  
  29.   return "NodeInfo:{ nodeId:" + strconv.Itoa(node.NodeId) + ",nodeIpAddr:" + node.NodeIpAddr + ",port:" + node.Port + "}"
  30. }
  31.  
  32. /* 添加一个节点到集群的一个请求或者响应的标准格式 */
  33. type AddToClusterMessage struct {
  34.   // 源节点
  35.   Source NodeInfo `json:"source"`
  36.   // 目的节点
  37.   Dest NodeInfo `json:"dest"`
  38.   // 两个节点连接时发送的消息
  39.   Message string `json:"message"`
  40. }
  41.  
  42. /* Request/Response 信息格式化输出 */
  43. func (req AddToClusterMessage) String() string {
  44.   return "AddToClusterMessage:{\n source:" + req.Source.String() + ",\n dest: " + req.Dest.String() + ",\n message:" + req.Message + " }"
  45. }
  46.  
  47. // cat vi go
  48. // rm
  49.  
  50. func main() {
  51.  
  52.   // 解析命令行参数
  53.   makeMasterOnError := flag.Bool("makeMasterOnError", false, "如果IP地址没有连接到集群中,我们将其作为Master节点.")
  54.   clusterip := flag.String("clusterip", "127.0.0.1:8001", "任何的节点连接都连接这个IP")
  55.   myport := flag.String("myport", "8001", "ip address to run this node on. default is 8001.")
  56.   flag.Parse() //解析
  57.  
  58.   fmt.Println(*makeMasterOnError)
  59.   fmt.Println(*clusterip)
  60.   fmt.Println(*myport)
  61.  
  62.   /* 为节点生成ID */
  63.   rand.Seed(time.Now().UTC().UnixNano()) //种子
  64.   myid := rand.Intn(99999999) // 随机
  65.  
  66.   //fmt.Println(myid)
  67.  
  68.   // 获取IP地址
  69.   myIp,:= net.InterfaceAddrs()
  70.   fmt.Println(myIp[0])
  71.  
  72.   // 创建NodeInfo结构体对象
  73.   me := NodeInfo{NodeId: myid, NodeIpAddr: myIp[0].String(), Port: *myport}
  74.   // 输出结构体数据信息
  75.   fmt.Println(me.String())
  76.   dest := NodeInfo{ NodeId: -1, NodeIpAddr: strings.Split(*clusterip, ":")[0], Port: strings.Split(*clusterip, ":")[1]}
  77.  
  78.   /* 尝试连接到集群,在已连接的情况下并且向集群发送请求 */
  79.   ableToConnect := connectToCluster(me, dest)
  80.  
  81.   /*
  82.    * 监听其他节点将要加入到集群的请求
  83.    */
  84.   if ableToConnect || (!ableToConnect && *makeMasterOnError) {
  85.     if *makeMasterOnError {fmt.Println("Will start this node as master.")}
  86.     listenOnPort(me)
  87.   } else {
  88.     fmt.Println("Quitting system. Set makeMasterOnError flag to make the node master.", myid)
  89.   }
  90.  
  91. }
  92.  
  93. /*
  94.  * 这是发送请求时格式化json包有用的工具
  95.  * 这是非常重要的,如果不经过数据格式化,你最终发送的将是空白消息
  96.  */
  97. func getAddToClusterMessage(source NodeInfo, dest NodeInfo, message string) (AddToClusterMessage){
  98.   return AddToClusterMessage{
  99.     Source: NodeInfo{
  100.       NodeId: source.NodeId,
  101.       NodeIpAddr: source.NodeIpAddr,
  102.       Port: source.Port,
  103.     },
  104.     Dest: NodeInfo{
  105.       NodeId: dest.NodeId,
  106.       NodeIpAddr: dest.NodeIpAddr,
  107.       Port: dest.Port,
  108.     },
  109.     Message: message,
  110.   }
  111. }
  112.  
  113. func connectToCluster(me NodeInfo, dest NodeInfo) (bool){
  114.   /* 连接到socket的相关细节信息 */
  115.   connOut, err := net.DialTimeout("tcp", dest.NodeIpAddr + ":" + dest.Port, time.Duration(10) * time.Second)
  116.   if err != nil {
  117.     if _, ok := err.(net.Error); ok {
  118.       fmt.Println("未连接到集群.", me.NodeId)
  119.       return false
  120.     }
  121.   } else {
  122.     fmt.Println("连接到集群. 发送消息到节点.")
  123.     text := "Hi nody.. 请添加我到集群.."
  124.     requestMessage := getAddToClusterMessage(me, dest, text)
  125.     json.NewEncoder(connOut).Encode(&requestMessage)
  126.  
  127.     decoder := json.NewDecoder(connOut)
  128.     var responseMessage AddToClusterMessage
  129.     decoder.Decode(&responseMessage)
  130.     fmt.Println("得到数据响应:\n" + responseMessage.String())
  131.  
  132.     return true
  133.   }
  134.   return false
  135. }
  136.  
  137. func listenOnPort(me NodeInfo){
  138.   /* 监听即将到来的消息 */
  139.   ln, _ := net.Listen("tcp", fmt.Sprint(":" + me.Port))
  140.   /* 接受连接 */
  141.   for {
  142.     connIn, err := ln.Accept()
  143.     if err != nil {
  144.       if _, ok := err.(net.Error); ok {
  145.         fmt.Println("Error received while listening.", me.NodeId)
  146.       }
  147.     } else {
  148.       var requestMessage AddToClusterMessage
  149.       json.NewDecoder(connIn).Decode(&requestMessage)
  150.       fmt.Println("Got request:\n" + requestMessage.String())
  151.  
  152.       text := "Sure buddy.. too easy.."
  153.       responseMessage := getAddToClusterMessage(me, requestMessage.Source, text)
  154.       json.NewEncoder(connIn).Encode(&responseMessage)
  155.       connIn.Close()
  156.     }
  157.   }
  158. }

运行程序

  1. /Users/liyuechun/go
  2. liyuechun:go yuechunli$ go install main
  3. liyuechun:go yuechunli$ main
  4. My details: NodeInfo:{ nodeId:53163002, nodeIpAddr:127.0.0.1/8, port:8001 }
  5. 不能连接到集群. 53163002
  6. Quitting system. Set makeMasterOnError flag to make the node master. 53163002
  7. liyuechun:go yuechunli$

获取相关帮助信息

  1. ./bin/main -h
  1. liyuechun:go yuechunli$ ./bin/main -h
  2. Usage of ./bin/main:
  3.  -clusterip string
  4.     ip address of any node to connnect (default "127.0.0.1:8001")
  5.  -makeMasterOnError
  6.     make this node master if unable to connect to the cluster ip provided.
  7.  -myport string
  8.     ip address to run this node on. default is 8001. (default "8001")
  9. liyuechun:go yuechunli$

启动Node1主节点

  1. ./bin/main --makeMasterOnError
  1. liyuechun:go yuechunli$ ./bin/main --makeMasterOnError
  2. My details: NodeInfo:{ nodeId:82381143, nodeIpAddr:127.0.0.1/8, port:8001 }
  3. 未连接到集群. 82381143
  4. Will start this node as master.

添加节点Node2到集群

  1. ./bin/main --myport 8002 --clusterip 127.0.0.1:8001

添加节点Node3到集群

  1. main --myport 8004 --clusterip 127.0.0.1:8001

添加节点Node4到集群

  1. $ main --myport 8003 --clusterip 127.0.0.1:8002

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号