Multi tenancy with Guice Custom Scopes and Jersey

2019-02-18 22:13发布

I am in the process of developing a multi tenancy application with Jersey using Guice for DI (I also use Dropwizard but I don't think it matters here).

One thing that bothers me is the fact that some kind of tenancy_id is all over the place in my application. Most of my URLs look like this: /:tenancy_id/some_resource/do_stuff. So the method in my Jersey resource is called with the tenancy_id and hands it over to a service which calls other services and so on. These services are configured differently for different tenants.

I managed to resolve this problem by using a @RequestScoped TenancyIdProdiver:

public class TenancyIdProvider {

    @Inject
    public TenancyIdProvider(HttpServletRequest request) {
        this.request = request;
    }

    public TenancyId getTenancyId() {
        // extract the tenancy id from the request
    }
}

`

My GuiceModule contains the following methods:

@RequestScoped 
public TenancyId getTenancyId(TenancyIdProvider tenancyIdFactory) { 
    return tenancyIdFactory.getTenancyId(); 
}

public SomeTenancyService getTenancyId(TenancyId tenancyId, Configuration configuration) { 
    return new SomeTenancyService(configuration.getConfiguration(tenancyId)); 
}

So now I don't need to worry about proper configuration of my services. All is handled by the DI container and the application is tenant agnostic where it doesn't care about the tenant.

My question is though: All these services and resources are created on every single request, since they all have a @RequestScoped dependency. This is not feasible at all. So my idea was to create a custom scope with guice. So every tenant will get its own object graph with all resources and services properly configured (but only once). I tried it following the example here, but I am very unsure if this is even possible with Guice' custom scopes. Where do I need to enter my custom scope from a Jersey point of view? Is a ContainerRequestFilter the right way to do it?

1条回答
The star\"
2楼-- · 2019-02-18 22:59

I finally figured it out by myself. The Guice page about custom scopes was a good starting point. I needed to tweak it a bit though.

First I've created a @TenancyScoped annotation:

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@ScopeAnnotation
public @interface TenancyScoped { }

Then I used a request filter:

@PreMatching
public class TenancyScopeRequestFilter implements ContainerRequestFilter {

   private final TenancyScope      scope;

   @Inject
   public TenancyScopeRequestFilter(TenancyScope scope) {
      this.scope = scope;
   }

   @Override
   public void filter(ContainerRequestContext requestContext) throws IOException {
      Optional<TenancyId> tenancyId = getTenancyId(requestContext);

      if (!tenancyId.isPresent()) {
         scope.exit();
         return;
      }
      scope.enter(tenancyId.get());
   }

   private Optional<TenancyId> getTenancyId(ContainerRequestContext requestContext) {
   }
}

Please note the @PreMatching annotation. It is important that every request is filtered, otherwise your code might behave weirdly (scope could be set incorrectly).

And here comes the TenancyScopeimplementation:

 public class TenancyScope implements Scope, Provider<TenancyId> {

     private final Logger                                        logger             = LoggerFactory.getLogger(TenancyScope.class);

     private final ThreadLocal<Map<TenancyId, Map<Key<?>, Object>>> tenancyIdScopedValues = new ThreadLocal<>();
     private final ThreadLocal<TenancyId>                           tenancyId             = new ThreadLocal<>();

     public void enter(TenancyId tenancyId) {
        logger.debug("Enter scope with tenancy id {}", tenancyId);

        if (this.tenancyIdScopedValues.get() == null) {
           this.tenancyIdScopedValues.set(new HashMap<>());
        }

        this.tenancyId.set(tenancyId);
        Map<Key<?>, Object> values = new HashMap<>();
        values.put(Key.get(TenancyId.class), tenancyId);
        this.tenancyIdScopedValues.get().putIfAbsent(tenancyId, values);
     }

     public void exit() {
        logger.debug("Exit scope with tenancy id {}", tenancyId.get());

        this.tenancyId.set(null);
     }

     public <T> Provider<T> scope(final Key<T> key, final Provider<T> unscoped) {
        return new Provider<T>() {
           public T get() {
              logger.debug("Resolve object with key {}", key);
              Map<Key<?>, Object> scopedObjects = getScopedObjectMap(key);

              @SuppressWarnings("unchecked")
              T current = (T) scopedObjects.get(key);
              if (current == null && !scopedObjects.containsKey(key)) {
                 logger.debug("First time instance with key {} is in tenancy id scope {}", key, tenancyId.get());
                 current = unscoped.get();

                 // don't remember proxies; these exist only to serve circular dependencies
                 if (Scopes.isCircularProxy(current)) {
                    return current;
                 }
                 logger.debug("Remember instance with key {} in tenancy id scope {}", key, tenancyId.get());
                 scopedObjects.put(key, current);
              }
              return current;
           }
        };
     }

     private <T> Map<Key<?>, Object> getScopedObjectMap(Key<T> key) {
        Map<TenancyId, Map<Key<?>, Object>> values = this.tenancyIdScopedValues.get();
        if (values == null || tenancyId.get() == null) {
           throw new OutOfScopeException("Cannot access " + key + " outside of a scoping block with id " + tenancyId.get());
        }
        return values.get(tenancyId.get());
     }

     @Override
     public TenancyId get() {
        if (tenancyId.get() == null) {
           throw new OutOfScopeException("Cannot access tenancy id outside of a scoping block");
        }
        return tenancyId.get();
     }

  }

The last step is to wire everything together in the Guice module:

@Override
protected void configure() {
   TenancyScope tenancyScope = new TenancyScope();
   bindScope(TenancyScoped.class, tenancyScope);
   bind(TenancyScope.class).toInstance(tenancyScope);
   bind(TenancyId.class).toProvider(tenancyScope).in(TenancyScoped.class);
}

What you have now is a scope that is set before each request and all instances the are provided by Guice are cached per tenancy id (also per thread, but that can be changed easily). Basically you have a object graph per tenant id (similar to having one per session e.g.).

Also notice, that the TenancyScope class acts both as a Scope and a TenancyId provider.

查看更多
登录 后发表回答