Now we need a multi-process file writing program
If you write directly, you need to ensure that the process locks the file handle
Now I want to register a singleton writing service through spring
When multiple programs write files, they request the spring interface to write files
Is this solution feasible?
Or use a singleton RPC service?
A single instance can ensure that it is an instance when writing a file, avoiding multi-thread and multi-process synchronization problems!
我认为这样还是不行的,本质上Spring只是帮你管理实例,线程安全的问题还是要自己解决
再者,你说的是多进程写文件,通过rpc也仅仅是多线程方式吧?
其实本质就是想实现一个线程安全的文件写接口,单例是OK的,自己的实例当作锁就行了
=_=! 一个多进程的程序?
用zookeeper,zookepper一个特点是写同一个znode只能有一个客户端(进程)写入成功,就用这个客户端写文件,或者利用数据库写入同一个主键只有一个可以插入成功的特点