Open Closed

Sync Users Between Modules #1442


User avatar
0
murat.yuceer created

According to my scenario, I have to sync my users between my modules. But unlike your cmskit module, I should be able to add users through my page in my module project. In other words, the user I add from the module must be synchronous to the identity module (i.e. I should not be dependent on identity module screens).

How should I go about this process? (who should throw an event to whom? Should a user be added directly to the identity module via API call? etc..).


2 Answer(s)
  • User Avatar
    1
    alper created
    Support Team Director

    asuming your modules are not connected via project references and those are remote services.

    there are 2 approaches: 1- you can sync the users when it needs. 2- you can sync the users once the main user changes 3- you can keep the users identical all the time

    option 1 if you don't need cumulative reports based on users. you sync a specific user when a transaction is done on the user.... in this approach you don't need to bulk sync the users. for example; you have a support module and support users are stored in its separate database. when you assign a user an issue, you can sync that user in the support website. to do this, you can make a look up service to retrieve users from your main application (via remote service calls) and once you make assignment, you create a user in support website. the disadvantage of this scenario is, the users will not be identical in both databases. only the users that have been in the transactions will be updated. but it's fast! technically you just need to write a service to retrieve users from your main app, and save it to the support database via repository. Take a look at this example -> https://github.com/abpframework/abp/blob/dev/modules/users/src/Volo.Abp.Users.Domain/Volo/Abp/Users/UserLookupService.cs#L35

    option 2 if you want to keep the user information same on both databases all the time, you sync them once (via one time db query), and after that each user update/delete/create will be applied to the other microservice as well. for example, when you create a user in your main app, you raise a distributed event and your microservice subscribes to that event and makes the same operation on its database. The distributed event bus document explains how to create a pub/sub mechanicsm to notify other services about the entity changes. ABP Framework automatically publishes distributed events for create, update and delete operations for an entity once you configure it. This is an efficient way to sync user table. you can use RabbitMQ or Kafka to make this approach robust.

    This is a publish event (used in Identity.Pro module) when creating an IdentityUser entity

    await DistributedEventBus.PublishAsync(new IdentityUserCreatedEto()
    {
    	Id = user.Id,
    	Properties =
    	{
    		{ "SendConfirmationEmail", input.SendConfirmationEmail.ToString().ToUpper() },
    		{ "AppName", "MVC" }
    	}
    });
    
    

    See how to subscribe User events https://docs.abp.io/en/abp/latest/Distributed-Event-Bus#subscribing-to-the-events

    option 3 if you want to keep the user records identical all the time without any domain events, you can write a background worker that runs periodically and sync the user table in all your microservice databases. the disadvantage of this approach is, users will be identical when this bg worker runs, the advantage is no frequent HTTP requests and also you avoid HTTP request network failures.

  • User Avatar
    0
    ServiceBot created
    Support Team Automatic process manager

    This question has been automatically marked as stale because it has not had recent activity.

Made with ❤️ on ABP v9.1.0-preview. Updated on November 18, 2024, 05:54