J2Cache 是 OSChina 目前正在使用的两级缓存框架。第一级缓存使用 Ehcache,第二级缓存使用 Redis 。由于大量的缓存读取会导致 L2 的网络成为整个系统的瓶颈,因此 L1 的目标是降低对 L2 的读取次数。该缓存框架主要用于集群环境中。单机也可使用,用于避免应用重启导致的 Ehcache 缓存数据丢失。

j2Cache提供了springboot 的集成。

集成方法如下:

1.引入pom.xml

 <dependency>
<groupId>net.oschina.j2cache</groupId>
<artifactId>j2cache-spring-boot2-starter</artifactId>
<version>2.7.6-release</version>
</dependency> <dependency>
<groupId>net.oschina.j2cache</groupId>
<artifactId>j2cache-core</artifactId>
<version>2.7.7-release</version>
</dependency>

2.增加配置文件

ehcache3.xml

<!-- for ehcache 3.x -->
<config
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns='http://www.ehcache.org/v3'
xsi:schemaLocation="http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core.xsd"> <!-- Don't remote default cache configuration -->
<cache-template name="default">
<key-type>java.lang.String</key-type>
<value-type>java.io.Serializable</value-type>
<expiry>
<ttl unit="seconds">1800</ttl>
</expiry>
<resources>
<heap>1000</heap>
<offheap unit="MB">100</offheap>
</resources> </cache-template>
<!--
<persistence directory="${ecache.path}"/>
-->
<cache alias="default" uses-template="default"/> </config>

j2cache.properties

#J2Cache configuration

#########################################
# Cache Broadcast Method
# values:
# jgroups -> use jgroups's multicast
# redis -> use redis publish/subscribe mechanism
######################################### j2cache.broadcast = net.oschina.j2cache.cache.support.redis.SpringRedisPubSubPolicy # 是否开启二级缓存
j2cache.l2-cache-open=true
j2cache.open-spring-cache= true
j2cache.allow-null-values= true
j2cache.cache-clean-mode= active
j2cache.redis-client=jedis #组播的通道名称
jgroups.channel.name = j2cache #########################################
# Level 1&2 provider
# values:
# none -> disable this level cache
# ehcache -> use ehcache2 as level 1 cache
# ehcache3 -> use ehcache3 as level 1 cache
# caffeine -> use caffeine as level 1 cache(only in memory)
# redis -> use redis(hashs) as level 2 cache
# [classname] -> use custom provider
######################################### j2cache.L1.provider_class = ehcache3
j2cache.L2.provider_class = net.oschina.j2cache.cache.support.redis.SpringRedisProvider
#j2cache.L2.provider_class = redis
j2cache.L2.config_section = redis
#j2cache.L2.provider_class = redis #########################################
# Cache Serialization Provider
# values:
# fst -> fast-serialization
# kyro -> kyro
# java -> java standard
# [classname implements Serializer]
######################################### j2cache.serialization = fst #########################################
# Ehcache configuration
######################################### #ehcache.name=
#ehcache.configXml=/ehcache.xml
ehcache3.configXml = /config/ehcache3.xml #########################################
# Caffeine configuration
# caffeine.region.[name] = size, xxxx[s|m|h|d]
#
######################################### caffeine.region.default = 1000, 1h #########################################
# Redis connection configuration
######################################### #########################################
# Redis Cluster Mode
#
# single -> single redis server
# sentinel -> master-slaves servers
# cluster -> cluster servers (数据库配置无效,使用 database = 0)
# sharded -> sharded servers (密码、数据库必须在 hosts 中指定,且连接池配置无效 ; redis://user:password@127.0.0.1:6379/0)
#
######################################### #redis.mode = sentinel
redis.mode = single
#cluster name just for sharded
redis.cluster_name = mymaster ## redis cache namespace optional, default[j2cache]
redis.namespace = j2cache ## connection
#redis.hosts = 127.0.0.1:26378,127.0.0.1:26379,127.0.0.1:26380
redis.hosts = 192.168.1.100:6379
redis.timeout = 2000
redis.password =
#redis.database = 0 ## redis pub/sub channel name
redis.channel = j2cache ## redis pool properties
redis.maxTotal = -1
redis.maxIdle = 2000
redis.maxWaitMillis = 100
redis.minEvictableIdleTimeMillis = 864000000
redis.minIdle = 1000
redis.numTestsPerEvictionRun = 10
redis.lifo = false
redis.softMinEvictableIdleTimeMillis = 10
redis.testOnBorrow = true
redis.testOnReturn = false
redis.testWhileIdle = false
redis.timeBetweenEvictionRunsMillis = 300000
redis.blockWhenExhausted = true

redis.mode = single 模式支持 single,sentinel,cluster

redis.hosts:需要配置为redis的主机

j2cache.l2-cache-open:是否开启二级缓存

3.注入CacheChannel 

@Component(value = "iCache")
public class J2cacheImpl implements ICache { private String region="rx"; @Autowired
private CacheChannel cacheChannel;

4.修改配置文件

application.properties

j2cache:
config-location: classpath:/config/j2cache.properties

配置j2cache.properties 文件路径

springboot 集成J2Cache的更多相关文章

  1. 【springBoot】springBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  2. SpringBoot集成security

    本文就SpringBoot集成Security的使用步骤做出解释说明.

  3. springboot集成Actuator

    Actuator监控端点,主要用来监控与管理. 原生端点主要分为三大类:应用配置类.度量指标类.操作控制类. 应用配置类:获取应用程序中加载的配置.环境变量.自动化配置报告等与SpringBoot应用 ...

  4. SpringBoot集成Shiro并用MongoDB做Session存储

    之前项目鉴权一直使用的Shiro,那是在Spring MVC里面使用的比较多,而且都是用XML来配置,用Shiro来做权限控制相对比较简单而且成熟,而且我一直都把Shiro的session放在mong ...

  5. SpringBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  6. springboot集成mybatis(二)

    上篇文章<springboot集成mybatis(一)>介绍了SpringBoot集成MyBatis注解版.本文还是使用上篇中的案例,咱们换个姿势来一遍^_^ 二.MyBatis配置版(X ...

  7. springboot集成mybatis(一)

    MyBatis简介 MyBatis本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation迁移到了google code,并且改名为MyB ...

  8. springboot集成redis(mybatis、分布式session)

    安装Redis请参考:<CentOS快速安装Redis> 一.springboot集成redis并实现DB与缓存同步 1.添加redis及数据库相关依赖(pom.xml) <depe ...

  9. SpringBoot集成jsp

    一.springBoot集成jsp: 1.修改pom文件 <!--集成jsp所需jar包--> <!--jsp页面使用jstl标签--> <dependency> ...

随机推荐

  1. 关于window PC机的预定义宏win32

    MSDN 里说,VC 有 3 个预处理常量,分别是 _WIN32,_WIN64,WIN32.这三个常量如何使用呢?看起来简单,其实是很困惑的. 在 Win32 配置下,WIN32 在“项目属性-C/C ...

  2. mac下操作小记

    安装homebrew https://juejin.im/post/5c738bacf265da2deb6aaf97 主要问题:按官网安装方法(/usr/bin/ruby -e "$(cur ...

  3. 10-排序5 PAT Judge (25 分)

    The ranklist of PAT is generated from the status list, which shows the scores of the submissions. Th ...

  4. centos7 中没有service iptables save指令来保存防火墙规则

    解决方法: systemctl stop firewalld  关闭防火墙yum install iptables-services 安装 iptables 服务systemctl enable ip ...

  5. Guava 源码分析之Cache的实现原理

    Guava 源码分析之Cache的实现原理 前言 Google 出的 Guava 是 Java 核心增强的库,应用非常广泛. 我平时用的也挺频繁,这次就借助日常使用的 Cache 组件来看看 Goog ...

  6. Gamma阶段第八次scrum meeting

    每日任务内容 队员 昨日完成任务 明日要完成的任务 张圆宁 #91 用户体验与优化https://github.com/rRetr0Git/rateMyCourse/issues/91(持续完成) # ...

  7. IDEA中,使用maven-可视化-下载jar包的source和javadoc

    需求:想看jar包的源码和文档注释 操作:在Idea的右侧Maven窗口,选中一个Module,然后展开Dependencies|,选中一个依赖包,按住鼠标右键单击,选择Download Source ...

  8. 基于ZYNQ的uart传输任意长度的数据

    1.参考 UG585 网络笔记 参考:ZYNQ进阶之路14–PS端uart串口接收不定长数据 2.理论知识 参见上一次实验:基于ZYNQ 的UART中断实验之串口写数据到DDR3中 3.实验目的 基于 ...

  9. 记一次网络故障——pod间无法通信

    一.背景 集群是二进制部署 部署完成后一起正常,各种资源对象均可正常创建. 部署应用后发现无法跨节点通信,且pod的ip都是172.17.0.0段的 二.排查过程层 查看节点路由,发现docker0网 ...

  10. linux -------------- Linux系统安装jdk

    linux 安装软件有三种方式  tar (解压安装 ) rpm (直接安装) yum(在线) 安装主要步邹 1.下载jdk 软件包 2.检测是否安装 查看已安装jdk软件包 rpm -qa|grep ...