码迷,mamicode.com
首页 > 编程语言 > 详细

springboot 集成J2Cache

时间:2019-05-28 16:53:02      阅读:610      评论:0      收藏:0      [点我收藏+]

标签:adc   col   targe   就是   spro   clust   ica   serial   app   

 

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

#组播的通道名称
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的主机

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
  open-spring-cache: true
  allow-null-values: true
  cache-clean-mode: active
  redis-client: jedis
  l2-cache-open: false

我们可以关闭第二级缓存,也就是只用本地缓存。

 

springboot 集成J2Cache

标签:adc   col   targe   就是   spro   clust   ica   serial   app   

原文地址:https://www.cnblogs.com/yg_zhang/p/10937977.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!