JHipster一知半解- 3.5Database相关配置

回文集目录:JHipster一知半解

Using Cache 翻译 (不包含Infinispan)

http://www.jhipster.tech/using-cache/

JHipster的缓存使用分为3个层次。

第一层次,作为Hibernate的2级缓存,此方案能大大地提升应用的性能。这也是JHipster的一般性配置,但是它只对SQL数据库生效。

第二层次,Spring实现了抽象缓存,可以同过@EnableCaching注释,它能自动识别
Ehcache/Hazelcast/Infinispan三种缓存,这个功能通常与具体的业务需求息息相关,并且它处于比Hibernate二级缓存更为高的层级(通常位于Service层)。但是,我们并不
推荐同时使用Spring抽象缓存和Hibernate二级缓存,因为这样可能引起缓存不一致问题,是的问题定位相当复杂。

第三层次,对于HTTP会话集群环境,一个好的缓存解决方案需要在多个节点间共享、替换用户的HTTP会话,并且能支持应用平滑水平扩展。目前,Hazelcast是一个可行的解决方案,但它要求应用相当稳定(无频繁重启,更新),因此它不是JHipster的默认设置,也不推荐使用。通常的做法是在应用节点之前,有前置负载均衡节点(Nginx)。

一般性配置

Cache 配置位于CacheConfiguration(config package),它能使用JHipster的通用应用属性进行配置。
代码位于io.github.jhipster.config.JHipsterProperties的内部类Cache.每个子类如Hazelcast又有不同的具体配置.

public static class Cache {

        private final Hazelcast hazelcast = new Hazelcast();

        private final Ehcache ehcache = new Ehcache();

        private final Infinispan infinispan = new Infinispan();
}

使用Ehcache缓存

对于JHipster单体应用,Ehcache是默认的缓存方案。Ehcache具有简单配置,简单安装,启动快速的特点,因而,对于“普通”单体应用,它是一个完美的解决方案。

对于JHipster,Ehcache存在两点限制:

它无法应用于HTTP会话集群;

它无法以分布式缓存的模式工作,由于它并没有API允许编程式添加新节点。

在Spring配置bean(CacheConfiguration)中,Ehcache有两个配置属性(生存时间与最大实体数),在应用中可以增加配置更多的特性应用属性,覆盖默认的属性。

默认地,生存时间设置为3600秒(1小时),测试和生产模式一致。最多实体默认就有所不同了,测试模式为100个,生产模式为1000个。

这些值应该根据具体的业务需求进行调整,JHipster的监控页面可以显示应用程序中缓存的使用情况,方便理解和调整为合适的数值。请参考Ehcache的文档,以便于合理地调整参数值。

使用Hazelcast缓存

Hazelcast 能作为一个本地缓存(类似Ehcache),但它还能作为分布式缓存.因此:

它能用来完成HTTP会话集群
它是微服务架构的默认选项,因为微服务需要比起单体应用更需要平滑扩展的功能.JHipster registry心选项需要Hazelcast实现扩展性.而且对于单体应用和微服务应用,Hazelcast都能用来做垂直扩展。在微服务架构中,JHipster Registry或Consul都能同时使用上,而对于单体应用,仅JHipster Registry能用上JHipster Registry缓存功能

当新增一个节点时,它将把自己进行注册,以便于服务发现(例如,当存在JHipster Registry时,这便可行),并能通过同样的方法查找其他同类型节点。如果它你那个找到一个或机构同样类型的节点,它将作为集群节点加入它们,这将在每个节点中产生日志消息,开始是下面的例子这样:

[172.18.0.10]:5701 [dev] [3.7]
Members [4] {
Member [172.18.0.10]:5701 - 3cbddfcd-0229-4cd5-be55-4611927a9071 this
Member [172.18.0.5]:5701 - 204d457d-f6fe-43f2-8e8d-497e96b3f08e
Member [172.18.0.14]:5701 - 7804d535-86fb-46be-b2a5-d7801dc6a4df
Member [172.18.0.11]:5701 - 6114ae28-56cd-4840-a575-4d73a6003744
}

源码解析

Hibernate使用RegionFactory实现2级缓存,因而从
org.hibernate.cache.spi.RegionFactory入手查看2种实现的不同

  1. Ehcache

pom.xml

<dependency>
    <groupId>org.ehcache</groupId>
    <artifactId>ehcache</artifactId>
</dependency>
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-jcache</artifactId>
    <version>${hibernate.version}</version>
</dependency>

这里可以看出,ehcache是hibernate的默认2级缓存,因而是由Hibernate实现二者之间的适配.具体可参考org.hibernate.cache.jcache.JCacheRegionFactory.

application-×.yml

spring:
    jpa:
        properties:
            hibernate.id.new_generator_mappings: true
            hibernate.cache.use_second_level_cache: true
            hibernate.cache.use_query_cache: false
            hibernate.generate_statistics: true
            hibernate.cache.region.factory_class: io.github.jhipster.config.jcache.NoDefaultJCacheRegionFactory

这里指定了factory_class为没有实现的RegionFactory,那就是说,实际上用的是Hibernate的实现?

CacheConfiguration

@Bean
    public JCacheManagerCustomizer cacheManagerCustomizer() {
        return cm -> {
            cm.createCache("users", jcacheConfiguration);
            cm.createCache(io.github.jhipster.sample.domain.User.class.getName(), jcacheConfiguration);
            cm.createCache(io.github.jhipster.sample.domain.Authority.class.getName(), jcacheConfiguration);
            cm.createCache(io.github.jhipster.sample.domain.User.class.getName() + ".authorities", jcacheConfiguration);
            cm.createCache(io.github.jhipster.sample.domain.BankAccount.class.getName(), jcacheConfiguration);
            cm.createCache(io.github.jhipster.sample.domain.BankAccount.class.getName() + ".operations", jcacheConfiguration);
            cm.createCache(io.github.jhipster.sample.domain.Label.class.getName(), jcacheConfiguration);
            cm.createCache(io.github.jhipster.sample.domain.Label.class.getName() + ".operations", jcacheConfiguration);
            cm.createCache(io.github.jhipster.sample.domain.Operation.class.getName(), jcacheConfiguration);
            cm.createCache(io.github.jhipster.sample.domain.Operation.class.getName() + ".labels", jcacheConfiguration);
            // jhipster-needle-ehcache-add-entry
        };

这里仅针对cm进行设置,毕竟ehcache是默认实现,无需特殊的额外配置.

  1. Hazelcast

pom.xml

<dependency>
    <groupId>com.hazelcast</groupId>
    <artifactId>hazelcast</artifactId>
</dependency>
<dependency>
    <groupId>com.hazelcast</groupId>
    <artifactId>hazelcast-hibernate52</artifactId>
    <version>${hazelcast-hibernate52.version}</version>
</dependency>
<dependency>
    <groupId>com.hazelcast</groupId>
    <artifactId>hazelcast-spring</artifactId>
</dependency>

是Hazelcast适配Hibernate,因而需要引入hazelcast-hibernate52实现RegionFactory.
com.hazelcast.hibernate.HazelcastCacheRegionFactory

application-×.yml

spring:
    jpa:
        properties:
            hibernate.id.new_generator_mappings: true
            hibernate.cache.use_second_level_cache: true
            hibernate.cache.use_query_cache: false
            hibernate.generate_statistics: true
            hibernate.cache.region.factory_class: com.hazelcast.hibernate.HazelcastCacheRegionFactory
            hibernate.cache.hazelcast.instance_name: jhipster
            hibernate.cache.use_minimal_puts: true
            hibernate.cache.hazelcast.use_lite_member: true

这里配置了spring-jpa的2级缓存设置。特别注意的是hibernate.cache.region.factory_class指定了hazelcast的实现。

CacheConfiguration

这里通过编程方式实例化了hazelcastInstance,并传递给cacheManager,作为spring的抽象缓存使用.

@Bean
    public CacheManager cacheManager(HazelcastInstance hazelcastInstance) {
        log.debug("Starting HazelcastCacheManager");
        CacheManager cacheManager = new com.hazelcast.spring.cache.HazelcastCacheManager(hazelcastInstance);
        return cacheManager;
    }
资源和书籍推荐

1.javax.cache
http://blog.csdn.net/caomiao2006/article/details/51934326

2.spring的cache(@EnableCache)
http://blog.csdn.net/taiyangdao/article/details/51095410
https://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/
http://jinnianshilongnian.iteye.com/blog/2001040
http://www.jianshu.com/p/49fc4065201a
https://zhuanlan.zhihu.com/p/24796537

3.Jhipster的Ehcache
http://www.cnblogs.com/zhangzhen894095789/p/6525845.html
http://zzc1684.iteye.com/blog/2118881
http://www.genshuixue.com/i-cxy/p/8018215

4.jhipster的hazelcast
http://angelbill3.iteye.com/blog/2342989
http://www.jianshu.com/p/3a0eb8af48fd
http://docs.hazelcast.org/docs/3.8.3/manual/html-single/index.html#spring-integration

Hibernate Cache的深入认识
http://www.litianhua.net/blog/hibernate-cache.html
http://www.litianhua.net/blog/jpa-second-level-cache.html
http://leobluewing.iteye.com/blog/2032396
https://en.wikibooks.org/wiki/Java_Persistence/Caching
Hibernate二级缓存详解(http://www.blogjava.net/supercrsky/articles/238580.html)

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,107评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,993评论 6 342
  • 要加“m”说明是MB,否则就是KB了. -Xms:初始值 -Xmx:最大值 -Xmn:最小值 java -Xms8...
    dadong0505阅读 4,941评论 0 53
  • 在我搭建基于Spring Cloud的微服务体系应用的时候所需要或者是常用的属性配置文件,还有这些属性的用途,此配...
    StrongManAlone阅读 4,114评论 0 18
  • 世界很纷乱,陌生已不如从前。昨日重走东湖,湖滨芳草路,繁花似锦,春天的气息迎面而来。遥记当年,朗日晴空,踢踏...
    比秋乡阅读 165评论 4 4