Go Sync 是一个分布式系统的同步框架

网友投稿 623 2022-10-28

Go Sync 是一个分布式系统的同步框架

Go Sync 是一个分布式系统的同步框架

Go Sync is a synchronization framework for distributed systems.

Overview

Distributed systems by their very nature are decoupled and independent. In most cases they must honour 2 out of 3 letters of the CAP theorem e.g Availability and Partitional tolerance but sacrificing consistency. In the case of microservices we often offload this concern to an external database or eventing system. Go Sync provides a framework for synchronization which can be used in the application by the developer.

Getting Started

Data - simple distributed data storageLeader - leadership election for group coordinationLock - distributed locking for exclusive resource accessTask - distributed job executionTime - provides synchronized time

Lock

The Lock interface provides distributed locking. Multiple instances attempting to lock the same id will block until available.

import "github.com/micro/go-sync/lock/consul"lock := consul.NewLock()// acquire lockerr := lock.Acquire("id")// handle err// release lockerr = lock.Release("id")// handle err

Leader

Leader provides leadership election. Useful where one node needs to coordinate some action.

import ( "github.com/micro/go-sync/leader" "github.com/micro/go-sync/leader/consul")l := consul.NewLeader( leader.Group("name"),)// elect leadere, err := l.Elect("id")// handle err// operate while leaderrevoked := e.Revoked()for { select { case <-revoked: // re-elect e.Elect("id") default: // leader operation }}// resign leadershipe.Resign()

Data

Data provides a simple interface for distributed data storage.

import ( "github.com/micro/go-sync/data" "github.com/micro/go-sync/data/consul")keyval := consul.NewData()err := keyval.Write(&data.Record{ Key: "foo", Value: []byte(`bar`),})// handle errv, err := keyval.Read("foo")// handle errerr = keyval.Delete("foo")

Task

Task provides distributed job execution. It's a simple way to distribute work across a coordinated pool of workers.

import ( "github.com/micro/go-sync/task" "github.com/micro/go-sync/task/local")t := local.NewTask( task.WithPool(10),)err := t.Run(task.Command{ Name: "atask", Func: func() error { // exec some work return nil },})if err != nil { // do something}

Time

Time provides synchronized time. Local machines may have clock skew and time cannot be guaranteed to be the same everywhere. Synchronized Time allows you to decide how time is defined for your applications.

import ( "github.com/micro/go-sync/time/ntp")t := ntp.NewTime()time, err := t.Now()

TODO

Event package - strongly consistent event stream e.g kafka

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:全面解析JPA 仓库repository中的findAll()方法
下一篇:HashMap
相关文章

 发表评论

暂时没有评论,来抢沙发吧~