微服务整合J2cache并改造使用

13 篇文章 0 订阅
10 篇文章 0 订阅

我们的微服务架构中,存在一个单独的基础数据中心,存放了各个服务、页面、app端的所需要的基础数据信息。这些数据的特点就是不易变,查询量大;最适合的场景就是进行缓存。经过一番商讨,决定使用J2Cache二级缓存。

 

整个缓存架构过程:

 

具体更多关于J2cache可以去查看官网文档

 

我这里简述我们的使用方法,因为我们是springboot项目,Spring的IOC可以让我们将所有单例组件都交给他来管理。

 

J2cache原生使用的是静态类启动,使用。这既涉及到一个问题我们项目区分了4套环境,都是使用spring的profile来控制的。

所以我们的缓存框架就需要将多环境做进去。这里我就将他改造成了@component组件。默认L1缓存使用caffeine。L2使用redis

首先将J2cache的配置文件抽离出来,以configurationProperties的形式进行管理。

package org.codingfly.common.cache;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * j2cache配置项
 *
 * @author: wangmeng
 * @date: 2019/12/26
 **/
@ConfigurationProperties("codingfly.j2cache")
@Component
@Getter
@Setter
public class J2cacheProperties {

    private boolean enable = true;

    private String broadcast = "lettuce";

    private L1cacheDefinition l1cache = new L1cacheDefinition();

    private L2cacheDefinition l2cache = new L2cacheDefinition();

    private String serialization = "json";

    private boolean syncTtlToRedis = true;

    private boolean cacheNullObject = true;

    private List<CaffeineDefinition> caffeine = new ArrayList<>();

}
package org.codingfly.common.cache;

import lombok.Getter;
import lombok.Setter;

/**
 * 一级缓存定义
 *
 * @author: wangmeng
 * @date: 2019/12/26
 **/
@Getter
@Setter
class L1cacheDefinition {

    private String name = "caffeine";
}
package org.codingfly.common.cache;

import lombok.Getter;
import lombok.Setter;

/**
 * 二级缓存定义
 *
 * @author: wangmeng
 * @date: 2019/12/26
 **/
@Getter
@Setter
class L2cacheDefinition {

    private String name = "lettuce";

}

 

然后我们使用@conditionsProperties注解配置cacheConfig类

 

package org.codingfly.common.cache;

import net.oschina.j2cache.J2CacheConfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * J2CacheConfig配置
 *
 * @author: wangmeng
 * @date:
 **/
@Configuration
@ConditionalOnProperty(prefix = "codingfly.j2cache", name = "enable", havingValue = "true", matchIfMissing = true)
public class CacheConfig {

    private final J2cacheProperties j2cacheProperties;

    private final RedisProperties redisProperties;

    public CacheConfig(J2cacheProperties j2cacheProperties, RedisProperties redisProperties) {
        this.j2cacheProperties = j2cacheProperties;
        this.redisProperties = redisProperties;
    }

    @Bean
    public J2CacheConfig j2CacheConfig() {
        J2CacheConfig j2CacheConfig = new J2CacheConfig();
        j2CacheConfig.setBroadcast(j2cacheProperties.getBroadcast());
        j2CacheConfig.setL1CacheName(j2cacheProperties.getL1cache().getName());
        j2CacheConfig.setL2CacheName(j2cacheProperties.getL2cache().getName());
        j2CacheConfig.setSerialization(j2cacheProperties.getSerialization());
        j2CacheConfig.setSyncTtlToRedis(j2cacheProperties.isSyncTtlToRedis());
        j2CacheConfig.setDefaultCacheNullObject(j2cacheProperties.isCacheNullObject());
        j2CacheConfig.setBroadcastProperties(getBroadcastProperties());
        j2CacheConfig.setL1CacheProperties(getL1CacheProperties());
        j2CacheConfig.setL2CacheProperties(getBroadcastProperties());
        return j2CacheConfig;
    }

    private Properties getBroadcastProperties() {
        Properties broadcastProperties = new Properties();
        broadcastProperties.setProperty("namespace", "");
        //storage的另一个配置broadcastProperties.setProperty("storage", "hash")
        broadcastProperties.setProperty("storage", "generic");
        broadcastProperties.setProperty("channel", "j2cache");
        broadcastProperties.setProperty("scheme", "redis");
        broadcastProperties.setProperty("hosts", redisProperties.getHost() + ":" + redisProperties.getPort());
        broadcastProperties.setProperty("password", "");
        broadcastProperties.setProperty("database", String.valueOf(redisProperties.getDatabase()));
        broadcastProperties.setProperty("sentinelMasterId", "");
        broadcastProperties.setProperty("maxTotal", "100");
        broadcastProperties.setProperty("maxIdle", "10");
        broadcastProperties.setProperty("minIdle", "10");
        broadcastProperties.setProperty("timeout", "1000");
        return broadcastProperties;
    }

    private Properties getL1CacheProperties() {
        Properties l1CacheProperties = new Properties();
        l1CacheProperties.setProperty("region.default", "1000, 30m");
        j2cacheProperties.getCaffeine().forEach(data ->
                l1CacheProperties.setProperty("region." + data.getKey(), data.getValue()));
        return l1CacheProperties;
    }
}

这时候我们就已经将J2cache需要的J2cacheconfig类注入到spring管理了。

 

package org.codingfly.common.cache;

import net.oschina.j2cache.CacheChannel;
import net.oschina.j2cache.J2CacheBuilder;
import net.oschina.j2cache.J2CacheConfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * channel配置
 *
 * @author: wangmeng
 * @date: 2019/12/25
 **/
@Configuration
@ConditionalOnBean(CacheConfig.class)
public class CacheChannelConfig {

    private final J2CacheConfig j2CacheConfig;

    public CacheChannelConfig(J2CacheConfig j2CacheConfig) {
        this.j2CacheConfig = j2CacheConfig;
    }

    @Bean
    public CacheChannel cacheChannel() {
        return J2CacheBuilder.init(j2CacheConfig).getChannel();
    }

}

接下来我们需要配置我们的cachechannel类注入。

 

这时候我们的完成了注入cacheChannel组件。这里我们注入时因为技术选型已经选好了,所以配置都使用了默认值。即使不配置自定义配置也可以使用。只要配置了redis即可。默认的redis连接使用lettuce。

默认还增加了caffeine的default配置。其他的缓存配置,默认会遵照此配置。

 

我们还可以将它和Spring cache注解集成,这里是官网提供的方法 对其进行了改造。

 

package org.codingfly.common.cache;

import net.oschina.j2cache.CacheChannel;
import net.oschina.j2cache.NullObject;
import org.springframework.cache.CacheManager;
import org.springframework.cache.support.AbstractValueAdaptingCache;

import java.util.concurrent.Callable;

/**
 * Spring Cache Adapter
 *
 * @author wangmeng
 * @date: 2019/12/27
 */
public class J2CacheSpringCacheAdapter extends AbstractValueAdaptingCache {
    private final String name;
    private final CacheChannel j2Cache;
    private boolean allowNullValues;

    /**
     * Create an {@code AbstractValueAdaptingCache} with the given setting.
     *
     * @param allowNullValues whether to allow for {@code null} values
     * @param j2Cache         j2cache instance
     * @param name            cache region name
     */
    J2CacheSpringCacheAdapter(boolean allowNullValues, CacheChannel j2Cache, String name) {
        super(allowNullValues);
        this.allowNullValues = allowNullValues;
        this.name = name;
        this.j2Cache = j2Cache;
    }

    /**
     * Perform an actual lookup in the underlying store.
     *
     * @param key the key whose associated value is to be returned
     * @return the raw store value for the key
     */

    private static String getKey(Object key) {
        return key.toString();
    }

    @Override
    protected Object lookup(Object key) {
        Object value = j2Cache.get(name, getKey(key)).rawValue();
        if (value == null || value.getClass().equals(Object.class)) {
            return null;
        }
        return value;
    }

    /**
     * Return the cache name.
     */
    @Override
    public String getName() {
        return name;
    }

    /**
     * Return the underlying native cache provider.
     */
    @Override
    public Object getNativeCache() {
        return j2Cache;
    }

    /**
     * Return the value to which this cache maps the specified key, obtaining
     * that value from {@code valueLoader} if necessary. This method provides
     * a simple substitute for the conventional "if cached, return; otherwise
     * create, cache and return" pattern.
     * <p>If possible, implementations should ensure that the loading operation
     * is synchronized so that the specified {@code valueLoader} is only called
     * once in case of concurrent access on the same key.
     * <p>If the {@code valueLoader} throws an exception, it is wrapped in
     * a {@link ValueRetrievalException}
     *
     * @param key         the key whose associated value is to be returned
     * @param valueLoader value loader
     * @return the value to which this cache maps the specified key
     * @throws ValueRetrievalException if the {@code valueLoader} throws an exception
     * @since 4.3
     */
    @Override
    public <T> T get(Object key, Callable<T> valueLoader) throws ValueRetrievalException {
        ValueWrapper val = get(key);
        if (val != null) {
            if (val.get() instanceof NullObject) {
                return null;
            }
            return (T) val.get();
        }
        T t;
        try {
            t = valueLoader.call();
        } catch (Exception e) {
            throw new ValueRetrievalException(key, valueLoader, e);
        }
        this.put(key, t);
        return t;
    }

    /**
     * Associate the specified value with the specified key in this cache.
     * <p>If the cache previously contained a mapping for this key, the old
     * value is replaced by the specified value.
     *
     * @param key   the key with which the specified value is to be associated
     * @param value the value to be associated with the specified key
     */
    @Override
    public void put(Object key, Object value) {
        j2Cache.set(name, getKey(key), value, allowNullValues);
    }

    /**
     * Atomically associate the specified value with the specified key in this cache
     * if it is not set already.
     * <p>This is equivalent to:
     * <pre><code>
     * Object existingValue = cache.get(key);
     * if (existingValue == null) {
     *     cache.put(key, value);
     *     return null;
     * } else {
     *     return existingValue;
     * }
     * </code></pre>
     * except that the action is performed atomically. While all out-of-the-box
     * {@link CacheManager} implementations are able to perform the put atomically,
     * the operation may also be implemented in two steps, e.g. with a check for
     * presence and a subsequent put, in a non-atomic way. Check the documentation
     * of the native cache implementation that you are using for more details.
     *
     * @param key   the key with which the specified value is to be associated
     * @param value the value to be associated with the specified key
     * @return the value to which this cache maps the specified key (which may be
     * {@code null} itself), or also {@code null} if the cache did not contain any
     * mapping for that key prior to this call. Returning {@code null} is therefore
     * an indicator that the given {@code value} has been associated with the key.
     * @since 4.1
     */
    @Override
    public ValueWrapper putIfAbsent(Object key, Object value) {
        ValueWrapper existingValue = get(getKey(key));
        if (existingValue == null) {
            put(key, value);
            return null;
        } else {
            return existingValue;
        }

    }

    /**
     * Evict the mapping for this key from this cache if it is present.
     *
     * @param key the key whose mapping is to be removed from the cache
     */
    @Override
    public void evict(Object key) {
        j2Cache.evict(name, getKey(key));
    }

    /**
     * Remove all mappings from the cache.
     */
    @Override
    public void clear() {
        j2Cache.clear(name);
    }
}
package org.codingfly.common.cache;

import net.oschina.j2cache.CacheChannel;
import org.springframework.cache.Cache;
import org.springframework.cache.transaction.AbstractTransactionSupportingCacheManager;

import java.util.Collection;

/**
 * @author Chen
 */
public class J2CacheSpringCacheManageAdapter extends AbstractTransactionSupportingCacheManager {
    /**
     * Load the initial caches for this cache manager.
     * <p>Called by {@link #afterPropertiesSet()} on startup.
     * The returned collection may be empty but must not be {@code null}.
     */
    @Override
    protected Collection<? extends Cache> loadCaches() {
        return null;
    }

    private CacheChannel cacheChannel;

    private boolean allowNullValues;

    /**
     * @param allowNullValues 默认 true
     */
    J2CacheSpringCacheManageAdapter(CacheChannel cacheChannel, boolean allowNullValues) {
        this.cacheChannel = cacheChannel;
        this.allowNullValues = allowNullValues;
    }

    @Override
    protected Cache getMissingCache(String name) {
        return new J2CacheSpringCacheAdapter(allowNullValues, cacheChannel, name);
    }
}
package org.codingfly.common.cache;

import net.oschina.j2cache.CacheChannel;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;

/**
 * j2cache集成spring cache注解使用
 *
 * @author: wangmeng
 * @date: 2019/12/26
 **/
@Configuration
@ConditionalOnBean(CacheChannel.class)
@EnableCaching
public class SpringCacheConfig extends CachingConfigurerSupport {

    private final CacheChannel cacheChannel;

    public SpringCacheConfig(CacheChannel cacheChannel) {
        this.cacheChannel = cacheChannel;
    }

    @Override
    public CacheManager cacheManager() {
        return new J2CacheSpringCacheManageAdapter(cacheChannel, true);
    }

}

这时我们已经将它集成到了Spring cache的注解中,这里嗨解决了一个spring cache'使用redis做缓存时,不可以设置超时时间的一个问题。

 

使用方法同spring cache。

到底spring boot 集成J2cache 的两种方法都写完了。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
J2Cache是OSChina目前正在使用的两级缓存框架(要求至少 Java 8)。第一级缓存使用内存(同时支持 Ehcache 2.x、Ehcache 3.x 和 Caffeine),第二级缓存使用 Redis(推荐)/Memcached。由于大量的缓存读取会导致L2的网络成为整个系统的瓶颈,因此L1的目标是降低对L2的读取次数。该缓存框架主要用于集群环境中。单机也可使用,用于避免应用重启导致的缓存冷启动后对后端业务的冲击。 数据读取: 读取顺序 -> L1 -> L2 -> DB 数据更新 1、从数据库中读取最新数据,依次更新L1 -> L2 ,发送广播清除某个缓存信息 2、接收到广播(手工清除缓存 & 一级缓存自动失效),从L1中清除指定的缓存信息 J2Cache配置: 配置文件位于core/resources目录下,包含三个文件: j2cache.properties J2Cache核心配置文件,可配置两级的缓存,Redis服务器、连接池以及缓存广播的方式 caffeine.properties如果一级缓存选用Caffeine ,那么该文件用来配置缓存信息 ehcache.xml Ehcache的配置文件,配置说明请参考Ehcache文档 ehcache3.xml Ehcache3的配置文件,配置说明请参考Ehcache文档 network.xml JGroups网络配置,如果使用JGroups组播的话需要这个文件,一般无需修改 实际使用过程需要将所需的配置文件复制到应用类路径中,如WEB-INF/classes目录。 J2Cache运行时所需jar包请查看core/pom.xml 测试方法: 1、安装Redis 2、git clone https://gitee.com/ld/J2Cache 3、修改core/resource/j2cache.properties配置使用已安装的Redis服务器 4、在命令行中执行mvn package -DskipTests=true进行项目编译 5、打开多个命令行窗口,同时运行runtest.sh 6、在 > 提示符后输入help查看命令,并进行测试

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值