- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在将 JHipster 生成器用于真正的样板代码,其中涉及 HazelCast 作为二级缓存。我能够使用基于 header 的租户上下文来获得 Multi-Tenancy (每个租户的模式)。我现在遇到的问题是,@Cacheable 注释都共享一个上下文。如果缓存很热,我最终会得到跨模式数据。例如,租户 1 从其缓存的表中提取所有记录。租户 2 从他们的表中提取相同的项目,读取缓存,但它永远不会进入实际的租户数据库。一个简单的解决方法是一起禁用缓存,但我不想这样做。我终其一生都无法弄清楚如何让 hazelcast 了解租户上下文——缺少文档。其他一些人已经使用自定义名称解析器解决了这个问题,但它似乎并不像我希望的那样动态(即你必须提前了解所有租户)。想法?
当前缓存配置:
@Configuration
@EnableCaching
public class CacheConfiguration implements DisposableBean {
private final Logger log = LoggerFactory.getLogger(CacheConfiguration.class);
private final Environment env;
private final ServerProperties serverProperties;
private final DiscoveryClient discoveryClient;
private Registration registration;
public CacheConfiguration(Environment env, ServerProperties serverProperties, DiscoveryClient discoveryClient) {
this.env = env;
this.serverProperties = serverProperties;
this.discoveryClient = discoveryClient;
}
@Autowired(required = false)
public void setRegistration(Registration registration) {
this.registration = registration;
}
@Override
public void destroy() throws Exception {
log.info("Closing Cache Manager");
Hazelcast.shutdownAll();
}
@Bean
public CacheManager cacheManager(HazelcastInstance hazelcastInstance) {
log.debug("Starting HazelcastCacheManager");
return new com.hazelcast.spring.cache.HazelcastCacheManager(hazelcastInstance);
}
@Bean
public HazelcastInstance hazelcastInstance(JHipsterProperties jHipsterProperties) {
log.debug("Configuring Hazelcast");
HazelcastInstance hazelCastInstance = Hazelcast.getHazelcastInstanceByName("SampleApp");
if (hazelCastInstance != null) {
log.debug("Hazelcast already initialized");
return hazelCastInstance;
}
Config config = new Config();
config.setInstanceName("SampleApp");
config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
if (this.registration == null) {
log.warn("No discovery service is set up, Hazelcast cannot create a cluster.");
} else {
// The serviceId is by default the application's name,
// see the "spring.application.name" standard Spring property
String serviceId = registration.getServiceId();
log.debug("Configuring Hazelcast clustering for instanceId: {}", serviceId);
// In development, everything goes through 127.0.0.1, with a different port
if (env.acceptsProfiles(Profiles.of(JHipsterConstants.SPRING_PROFILE_DEVELOPMENT))) {
log.debug("Application is running with the \"dev\" profile, Hazelcast " +
"cluster will only work with localhost instances");
System.setProperty("hazelcast.local.localAddress", "127.0.0.1");
config.getNetworkConfig().setPort(serverProperties.getPort() + 5701);
config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true);
for (ServiceInstance instance : discoveryClient.getInstances(serviceId)) {
String clusterMember = "127.0.0.1:" + (instance.getPort() + 5701);
log.debug("Adding Hazelcast (dev) cluster member {}", clusterMember);
config.getNetworkConfig().getJoin().getTcpIpConfig().addMember(clusterMember);
}
} else { // Production configuration, one host per instance all using port 5701
config.getNetworkConfig().setPort(5701);
config.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(true);
for (ServiceInstance instance : discoveryClient.getInstances(serviceId)) {
String clusterMember = instance.getHost() + ":5701";
log.debug("Adding Hazelcast (prod) cluster member {}", clusterMember);
config.getNetworkConfig().getJoin().getTcpIpConfig().addMember(clusterMember);
}
}
}
config.getMapConfigs().put("default", initializeDefaultMapConfig(jHipsterProperties));
// Full reference is available at: http://docs.hazelcast.org/docs/management-center/3.9/manual/html/Deploying_and_Starting.html
config.setManagementCenterConfig(initializeDefaultManagementCenterConfig(jHipsterProperties));
config.getMapConfigs().put("com.test.sampleapp.domain.*", initializeDomainMapConfig(jHipsterProperties));
return Hazelcast.newHazelcastInstance(config);
}
private ManagementCenterConfig initializeDefaultManagementCenterConfig(JHipsterProperties jHipsterProperties) {
ManagementCenterConfig managementCenterConfig = new ManagementCenterConfig();
managementCenterConfig.setEnabled(jHipsterProperties.getCache().getHazelcast().getManagementCenter().isEnabled());
managementCenterConfig.setUrl(jHipsterProperties.getCache().getHazelcast().getManagementCenter().getUrl());
managementCenterConfig.setUpdateInterval(jHipsterProperties.getCache().getHazelcast().getManagementCenter().getUpdateInterval());
return managementCenterConfig;
}
private MapConfig initializeDefaultMapConfig(JHipsterProperties jHipsterProperties) {
MapConfig mapConfig = new MapConfig();
/*
Number of backups. If 1 is set as the backup-count for example,
then all entries of the map will be copied to another JVM for
fail-safety. Valid numbers are 0 (no backup), 1, 2, 3.
*/
mapConfig.setBackupCount(jHipsterProperties.getCache().getHazelcast().getBackupCount());
/*
Valid values are:
NONE (no eviction),
LRU (Least Recently Used),
LFU (Least Frequently Used).
NONE is the default.
*/
mapConfig.setEvictionPolicy(EvictionPolicy.LRU);
/*
Maximum size of the map. When max size is reached,
map is evicted based on the policy defined.
Any integer between 0 and Integer.MAX_VALUE. 0 means
Integer.MAX_VALUE. Default is 0.
*/
mapConfig.setMaxSizeConfig(new MaxSizeConfig(0, MaxSizeConfig.MaxSizePolicy.USED_HEAP_SIZE));
return mapConfig;
}
private MapConfig initializeDomainMapConfig(JHipsterProperties jHipsterProperties) {
MapConfig mapConfig = new MapConfig();
mapConfig.setTimeToLiveSeconds(jHipsterProperties.getCache().getHazelcast().getTimeToLiveSeconds());
return mapConfig;
}
}
使用 cacheNames 的示例存储库...
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
String USERS_BY_LOGIN_CACHE = "usersByLogin";
String USERS_BY_EMAIL_CACHE = "usersByEmail";
String USERS_BY_ID_CACHE = "usersById";
Optional<User> findOneByActivationKey(String activationKey);
List<User> findAllByActivatedIsFalseAndActivationKeyIsNotNullAndCreatedDateBefore(Instant dateTime);
Optional<User> findOneByResetKey(String resetKey);
Optional<User> findOneByEmailIgnoreCase(String email);
Optional<User> findOneByLogin(String login);
@EntityGraph(attributePaths = "roles")
@Cacheable(cacheNames = USERS_BY_ID_CACHE)
Optional<User> findOneWithRolesById(Long id);
@EntityGraph(attributePaths = "roles")
@Cacheable(cacheNames = USERS_BY_LOGIN_CACHE)
Optional<User> findOneWithRolesByLogin(String login);
@EntityGraph(attributePaths = { "roles", "roles.permissions" })
@Cacheable(cacheNames = USERS_BY_LOGIN_CACHE)
Optional<User> findOneWithRolesAndPermissionsByLogin(String login);
@EntityGraph(attributePaths = "roles")
@Cacheable(cacheNames = USERS_BY_EMAIL_CACHE)
Optional<User> findOneWithRolesByEmail(String email);
Page<User> findAllByLoginNot(Pageable pageable, String login);
}
最佳答案
我正在使用每个数据库 (MySQL) 的租户,但只要您在上面设置线程上下文,这就是我正在做的 - 我正在使用 Spring Boot。我创建了一个自定义缓存 key 生成器,它结合了租户名称 + 类 + 和方法。你真的可以选择任何组合。每当我将那个租户传回时,它都会提取正确的条目。在我的 AppointmentType map 类型的 Hazelcast 命令中心,我看到每个租户的条目数增加。
其他一些可能有用的引用资料:
在你想要缓存的类中(我的是服务类):
@Service
public class AppointmentTypeService {
private static final Logger LOGGER = LoggerFactory.getLogger(AppointmentTypeService.class);
private final AppointmentTypeRepository appointmentTypeRepository;
@Autowired
AppointmentTypeService(AppointmentTypeRepository appointmentTypeRepository) {
this.appointmentTypeRepository = appointmentTypeRepository;
}
//ADD keyGenerator value. Name is the name of the bean of the class
@Cacheable(value="appointmentType", keyGenerator = "multiTenantCacheKeyGenerator")
public List<AppointmentType> list() {
return this.appointmentTypeRepository.findAll();
}
@CacheEvict(value="appointmentType", allEntries=true)
public Long create(AppointmentType request) {
this.appointmentTypeRepository.saveAndFlush(request);
return request.getAppointmentTypeId();
}
@CacheEvict(value="appointmentType", allEntries=true)
public void delete(Long id) {
this.appointmentTypeRepository.deleteById(id);
}
public Optional<AppointmentType> findById(Long id) {
return this.appointmentTypeRepository.findById(id);
}
}
创建 key 生成器类
//setting the bean name here
@Component("multiTenantCacheKeyGenerator")
public class MultiTenantCacheKeyGenerator implements KeyGenerator {
@Override
public Object generate(Object o, Method method, Object... os) {
StringBuilder sb = new StringBuilder();
sb.append(TenantContext.getCurrentTenantInstanceName()) //my tenant context class which is using local thread. I set the value in the Spring filter.
.append("_")
.append(o.getClass().getSimpleName())
.append("-")
.append(method.getName());
}
return sb.toString();
}
}
关于java - 为 Multi-Tenancy 设置 Hazelcast 缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61295458/
我目前正在制作一个将订阅作为 Multi-Tenancy 应用程序出售的 web 应用程序。我使用的技术是导轨。 但是,它不仅仅是使用当前应用程序的孤立租户。 每个租户创建产品并将其发布到他们的个人应
我们计划将 Azure Service Fabric 用于面向数据的 Multi-Tenancy 应用程序。通常有 100 多个客户,每个客户有 5 - 100 个用户。 查看文档,我得出的结论是,最
我们正在为我们正在构建的自定义 Saas 应用程序评估 Shiro。似乎一个伟大的框架可以完成我们想要的 90% 的工作,开箱即用。我对 Shiro 的理解是基本的,这就是我想要完成的。 我们有多个客
希望使用 NestJS 6 的新请求注入(inject)范围功能实现 Multi-Tenancy NestJS 解决方案。 对于任何给定的服务,我认为我可以做这样的事情: @Injectable({s
我正在寻找一个基于 PHP 的框架,该框架已准备好具有以下功能 1.带有登录/注销的简单仪表板 2. 多个数据库,每个数据库代表一个客户端 只是基本框架。 3.简单的注册支持 用例: 我从 githu
我正在尝试对这个已经回答的问题进行一些跟进...... Service Fabric multi-tenant 如果我要将我的租户设置为 Azure Service Fabric 无状态服务(他们将获
首先,我很清楚 Keycloak 中的多领域 Multi-Tenancy 方法。我接手了一个没有人想到 Multi-Tenancy 的遗留项目。现在,两年后,突然,客户需要这个功能。实际上,微服务已经
我正在使用 Apache Nifi 开发基于云的应用程序,为此我们需要支持 Multi-Tenancy 。但是当前的 Nifi 实现只支持基于角色的用户访问,对于单个流。 我可以理解流状态被保存为 N
对于我积极维护的客户基于 Web 的 CRM 的分支机构数量不断增加的 Multi-Tenancy ,我需要做出一个艰难的数据库设计决策。 我很早就决定为每个分支使用具有单独数据库的单独应用程序,因为
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improve
很抱歉我的英语不好,希望你能看到我说的。 在Lucene3 Junit测试代码中:org.apache.lucene.queryParser.TestMultiAnalyzer.testMultiAn
假设我们有一个多维数组。 multi[3][10] 那么&multi[0][0]将是multi 如果我们想访问这个数组中的任何元素。我们只需要一次解除引用。因为它位于连续的位置。我无法理解双重取消引用
表结构和示例数据 Wall_Update [INT VARCHAR VARCHAR TIMESTAMP TinyText]
我们需要构建一个软件框架(或中间件),以便在一台机器上运行的不同软件组件(或模块)之间实现消息传递。该框架将提供以下功能: 模块之间的通信是通过“消息传递”。 每个模块都有自己的消息队列和消息处理线程
我正在开发一个在多个域上运行的应用程序。 我想对所有这些都使用 Google 自定义搜索。但是 GCS 需要提供要搜索的网站域。 有没有办法动态指定域?理论上,我可以拥有数千个域,但我不喜欢手动添加所
在 here.com map 类 MapMarker 中,此方法 showInfoBubble () 无法在多 map 标记上显示多信息气泡,对此有任何解决方案吗? 最佳答案 来自 showInfoB
我正在开发一个 Multi-Tenancy 解决方案,我想使用最新的 ASP.NET Identity框架特别是Entity Framework执行。 基本上,我需要允许两个用户使用相同的用户名,尽管
我有 50 台可用台式计算机(配备 i5),每台都运行 Ubuntu 14.04 LTS。我需要通过 C 代码计算某些事件的概率,样本大小至少为 2^45。显然,在一台计算机上运行 C 代码不是一种选
我正在按照页面上的示例进行操作:Multi-input and multi-output models 用于预测新闻标题将收到多少转发和点赞的模型设置。那么 main_output 正在预测有多少
硬件:我们使用 24 核(2*12 核)机器。 SSD 磁盘和 SAS-RAID 0 磁盘有 2 个独立的 Controller 。操作系统:Windows 8.1。超线程已禁用。 软件: 2.1。有
我是一名优秀的程序员,十分优秀!