ブルース・ウェイン :
私は、マルチテナントRESTスプリングブートアプリケーションを開発しています。私は動的にすべてのリクエストのヘッダー値に基づいて異なるデータ・ソースを切り替えることができています。しかし、私の問題は、application.propertiesファイルにあります。別のテナントは、そのプロパティファイルで同じプロパティに異なる値を持っています。
どのように私はテナントごとにプロパティファイルを分離しても、動的にリクエストヘッダの値に基づいて使用するファイルをプロパティを決定することができます
アンディ・ブラウン:
あなたは、実行時にプロファイルを切り替えることはできません。あなたのオプションは、新規作成のいずれかに限定されているApplicationContext
独自の欠点が付属しているのか、起動時にテナントプロパティファイルをロードし、テナント固有の実装することができgetProperty
、必要なときに呼び出すメソッドを。
これは、するべきで、後者の場合を処理します。
@Component
public class TenantProperties {
private Map<String, ConfigurableEnvironment> customEnvs;
@Inject
public TenantProperties(@Autowired ConfigurableEnvironment defaultEnv,
@Value("${my.tenant.names}") List<String> tenantNames) {
this.customEnvs = tenantNames
.stream()
.collect(Collectors.toMap(
Function.identity(),
tenantId -> {
ConfigurableEnvironment customEnv = new StandardEnvironment();
customEnv.merge(defaultEnv);
Resource resource = new ClassPathResource(tenantId + ".properties");
try {
Properties props = PropertiesLoaderUtils.loadProperties(resource);
customEnv.getPropertySources()
.addLast(new PropertiesPropertySource(tenantId, props));
return customEnv;
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}));
}
public String getProperty(String tenantId, String propertyName) {
ConfigurableEnvironment ce = this.customEnvs.get(tenantId);
if (ce == null) {
throw new IllegalArgumentException("Invalid tenant");
}
return ce.getProperty(propertyName);
}
}
あなたは追加する必要がmy.tenant.names
テナント名(カンマ区切りのリストが含まれ、メインアプリケーションのプロパティにプロパティをname1, name2
、など)。テナント固有のプロパティがからロードされname1.properties
、...クラスパスから。あなたのアイデアを得ます。