英文:
Golang Singleton for all replicas of microservice
问题
我正在使用sync.Once来在我的微服务中实现单例模式。但我不确定这是否能确保所有副本都只有一个实例。
如何在Golang中为分布式微服务创建一个单例对象?
英文:
I am using sync.once to implement singleton in my microservice. But I am not sure if this will ensure a single instance for all the replicas.
How to create a Singleton object for distributed microservices in golang?
答案1
得分: 4
它无法确保在不同环境中运行的多个进程之间的单个实例。sync
用于同步运行在主例程进程中的 go routines。
要添加在多个进程和多个微服务之间工作的锁定,您需要一个单独的第三方实例来控制它。它可以是队列消息/缓存代理(例如RabbitMQ,Redis)或数据库(例如PostgreSQL advisory locks)。
英文:
It cannot ensure a single instance between multiple processes running in different environments. sync
is used to synchronise go routines, which are running against the main routine process.
To add locking that would work between multiple processes and multiple microservices, you need a single third-party instance, that would control it. It can be a queue message / cache broker (e.g. RabbitMQ, Redis) or database (e.g. PostgreSQL advisory locks).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论