我正在尝试将MongoDB作为网络范围的配置存储。同一应用在网络上的多台机器上运行,每个应用程序从其本地的MongoDB中获取其配置。这些MongoDB是已同步的。我想要的是,如果一个应用程序更改了任何配置值,则在所有n-1个应用程序中都会收到回调/通知。
这种设置是否可行?(这将使我不必自己进行网络传输/同步等操作。)
我正在尝试将MongoDB作为网络范围的配置存储。同一应用在网络上的多台机器上运行,每个应用程序从其本地的MongoDB中获取其配置。这些MongoDB是已同步的。我想要的是,如果一个应用程序更改了任何配置值,则在所有n-1个应用程序中都会收到回调/通知。
这种设置是否可行?(这将使我不必自己进行网络传输/同步等操作。)
// 1. The database for reactive, real-time applications
MongoClient mongoClient;
// Create a new MongoClient with a MongoDB URI string.
if (args.length == 0) {
// Defaults to a localhost replicaset on ports: 27017, 27018, 27019
mongoClient = new MongoClient(new
MongoClientURI("mongodb://localhost:27017,localhost:27018,localhost:27019"));
} else {
mongoClient = new MongoClient(new MongoClientURI(args[0]));
}
// Select the MongoDB database.
MongoDatabase database = mongoClient.getDatabase("testChangeStreams");
database.drop();
sleep();
// Select the collection to query.
MongoCollection<Document> collection = database.getCollection("documents");
// Create the change stream cursor.
MongoCursor<Document> cursor = collection.watch().iterator();
如果你正在使用C#开发,可以在这里找到示例:
var inventory = database.GetCollection<BsonDocument>("inventory");
var document = new BsonDocument("x", 1);
inventory.InsertOne(document);
new Thread(() =>
{
Thread.Sleep(TimeSpan.FromMilliseconds(100));
var filter = new BsonDocument("_id", document["_id"]);
var update = "{ $set : { x : 2 } }";
inventory.UpdateOne(filter, update);
})
.Start();
// Start Changestream Example 2
var options = new ChangeStreamOptions { FullDocument = ChangeStreamFullDocumentOption.UpdateLookup };
var enumerator = inventory.Watch(options).ToEnumerable().GetEnumerator();
enumerator.MoveNext();
var next = enumerator.Current;
enumerator.Dispose();
// End Changestream Example 2
var expectedFullDocument = document.Set("x", 2);
next.FullDocument.Should().Be(expectedFullDocument);