springcloud加载j2cache时读取apollo配置中心配置

6 篇文章 0 订阅
1 篇文章 0 订阅

1、创建J2CacheSelfConfig类,用来注入apollo配置文件数据

2、创建J2CacheBuilderUtil类,用来根据配置文件数据动态创建缓存实例

3、项目中需要缓存实例时使用J2CacheBuilderUtil来创建实例

类的名称可以根据需要自己命名,具体代码如下:

J2CacheSelfConfig

/**
 * 
 */
package com.kaitaiming.life.common.j2cache;

import org.springframework.beans.factory.annotation.Value;

/**
 * j2chache自定义配置文件
 * 
 * @author houwei
 * @date 2019年7月10日
 * @version 
 */
public class J2CacheSelfConfig {
	
	@Value("${j2cache.broadcast}")
	private String broadcast;
	@Value("${j2cache.L1.provider_class}")
	private String l1CacheName;
	@Value("${j2cache.L2.provider_class}")
	private String l2CacheName;
	@Value("${j2cache.serialization}")
	private String serialization;
	
	@Value("${redis.channel}")
	private String channel;
	@Value("${redis.mode}")
	private String mode;
	@Value("${redis.storage}")
	private String storage;
	@Value("${redis.cluster_name}")
	private String cluster_name;
	@Value("${redis.namespace}")
	private String namespace;
	@Value("${redis.channel_name}")
	private String channel_name;
	@Value("${redis.hosts}")
	private String hosts;
	@Value("${redis.timeout}")
	private String timeout;
	@Value("${redis.password}")
	private String password;
	@Value("${redis.database}")
	private String database;
	@Value("${redis.maxTotal}")
	private String maxTotal;
	@Value("${redis.maxIdle}")
	private String maxIdle;
	@Value("${redis.maxWaitMillis}")
	private String maxWaitMillis;
	@Value("${redis.minEvictableIdleTimeMillis}")
	private String minEvictableIdleTimeMillis;
	@Value("${redis.minIdle}")
	private String minIdle;
	@Value("${redis.numTestsPerEvictionRun}")
	private String numTestsPerEvictionRun;
	@Value("${redis.lifo}")
	private String lifo;
	@Value("${redis.softMinEvictableIdleTimeMillis}")
	private String softMinEvictableIdleTimeMillis;
	@Value("${redis.testOnBorrow}")
	private String testOnBorrow;
	@Value("${redis.testOnReturn}")
	private String testOnReturn;
	@Value("${redis.testWhileIdle}")
	private String testWhileIdle;
	@Value("${redis.timeBetweenEvictionRunsMillis}")
	private String timeBetweenEvictionRunsMillis;
	@Value("${redis.blockWhenExhausted}")
	private String blockWhenExhausted;
	@Value("${redis.jmxEnabled}")
	private String jmxEnabled;
	@Value("${redis.usePool}")
	private String usePool;

	@Value("${ehcache.name}")
	private String name;
	@Value("${ehcache.configXml}")
	private String configXml;
	
    //get\set方法就不写了
	
}

J2CacheBuilderUtil

/**
 * 
 */
package com.kaitaiming.life.common.j2cache;

import java.util.Properties;

import net.oschina.j2cache.CacheChannel;
import net.oschina.j2cache.J2CacheBuilder;
import net.oschina.j2cache.J2CacheConfig;

/**
 * j2cache自定义配置文件创建
 * 
 * @author houwei
 * @date 2019年7月10日
 * @version
 */
public class J2CacheBuilderUtil {

	private static CacheChannel cache;

	public static CacheChannel createJ2CacheChann(J2CacheSelfConfig j2CacheSelfConfig) {

		if (cache == null) {

			J2CacheConfig config = new J2CacheConfig();
			config.setBroadcast(j2CacheSelfConfig.getBroadcast());
			config.setL1CacheName(j2CacheSelfConfig.getL1CacheName());
			config.setL2CacheName(j2CacheSelfConfig.getL2CacheName());
			config.setSerialization(j2CacheSelfConfig.getSerialization());

			/**
			 * 缓存基本配置
			 */
			Properties redisProperties = new Properties();
			redisProperties.setProperty("channel", j2CacheSelfConfig.getChannel());
			redisProperties.setProperty("hosts", j2CacheSelfConfig.getHosts());
			redisProperties.setProperty("timeout", j2CacheSelfConfig.getTimeout());
			redisProperties.setProperty("password", j2CacheSelfConfig.getPassword());
			redisProperties.setProperty("database", j2CacheSelfConfig.getDatabase());

			redisProperties.setProperty("maxTotal", j2CacheSelfConfig.getMaxTotal());
			redisProperties.setProperty("maxIdle", j2CacheSelfConfig.getMaxIdle());
			redisProperties.setProperty("maxWaitMillis", j2CacheSelfConfig.getMaxWaitMillis());
			redisProperties.setProperty("minEvictableIdleTimeMillis",
					j2CacheSelfConfig.getMinEvictableIdleTimeMillis());
			redisProperties.setProperty("minIdle", j2CacheSelfConfig.getMinIdle());
			redisProperties.setProperty("numTestsPerEvictionRun", j2CacheSelfConfig.getNumTestsPerEvictionRun());
			redisProperties.setProperty("lifo", j2CacheSelfConfig.getLifo());
			redisProperties.setProperty("softMinEvictableIdleTimeMillis",
					j2CacheSelfConfig.getSoftMinEvictableIdleTimeMillis());
			redisProperties.setProperty("testOnBorrow", j2CacheSelfConfig.getTestOnBorrow());
			redisProperties.setProperty("testOnReturn", j2CacheSelfConfig.getTestOnReturn());
			redisProperties.setProperty("testWhileIdle", j2CacheSelfConfig.getTestWhileIdle());
			redisProperties.setProperty("timeBetweenEvictionRunsMillis",
					j2CacheSelfConfig.getTimeBetweenEvictionRunsMillis());
			redisProperties.setProperty("blockWhenExhausted", j2CacheSelfConfig.getBlockWhenExhausted());
			redisProperties.setProperty("jmxEnabled", j2CacheSelfConfig.getJmxEnabled());
			redisProperties.setProperty("usePool", j2CacheSelfConfig.getUsePool());
			config.setBroadcastProperties(redisProperties);

			/**
			 * 一级缓存配置
			 */
			Properties l1CacheProperties = new Properties();
			l1CacheProperties.setProperty("name", j2CacheSelfConfig.getName());
			l1CacheProperties.setProperty("configXml", j2CacheSelfConfig.getConfigXml());
			config.setL1CacheProperties(l1CacheProperties);

			/**
			 * 二级缓存配置
			 */
			redisProperties.setProperty("mode", j2CacheSelfConfig.getMode());
			redisProperties.setProperty("storage", j2CacheSelfConfig.getStorage());
			redisProperties.setProperty("cluster_name", j2CacheSelfConfig.getCluster_name());
			redisProperties.setProperty("namespace", j2CacheSelfConfig.getNamespace());
			redisProperties.setProperty("channel_name", j2CacheSelfConfig.getChannel_name());

			config.setL2CacheProperties(redisProperties);

			// 填充 config 变量所需的配置信息
			J2CacheBuilder builder = J2CacheBuilder.init(config);
			cache = builder.getChannel();
		}
		return cache;
	}
}

使用代码

    //注入配置参数
    @Resource
    private J2CacheComputeConfig j2CacheComputeConfig;
	
	public CacheChannel getCacheChannel() {
	    
	    if(cache == null) {
            //根据配置参数动态生成实例
	    	cache = J2CacheBuilderUtil.createJ2CacheChann(j2CacheComputeConfig);
	    }
	    return cache;
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值