redis读写分离 java_读写分离的java redis 接口

本文介绍了如何在Spring Boot应用中实现Redis的读写分离。通过定义接口、实现类和利用注解,实现了根据配置文件自动创建只读和读写操作的Redis服务。在启动类上使用@EnableDynamicKeyValueServiceCreation注解,配置文件中指定读写分离的Redis配置(如以.r和.w结尾的bean名),并在需要的Service中注入相应的Redis接口。
摘要由CSDN通过智能技术生成

目的:为了spring上下文添加读写分离的接口,可以在启动后自动注入redis只读接口或可读写接口

1. 定义一个相关的functional interface - 在spring boot的启动类里面要用到(@EnableDynamicKeyValueServiceCreation)

@Retention(value = RetentionPolicy.RUNTIME)

@Import(KeyValueServiceRegister.class)

public @interface EnableDynamicKeyValueServiceCreation {

}

2. 定义只读接口和可读写接口

public interface KeyValueReadService { String get(String key); ...}

public interface KeyValueReadWriteService extends KeyValueReadService { void set(String key, String value); ..}

3. 实现类

public class RedisConfigInfo {

private String hostName;

private int port;

private String password;

private JedisPoolConfig poolConfig;

}

public abstract class AbstractRedisOperation {

private final JedisPool jedisPool;

public AbstractRedisOperation (RedisConfigInfo info) { jedisPool = ...;}

}

public class RedisReadServiceImpl extends AbstractRedisOperation implements KeyValueReadService {

public RedisReadServiceImpl(RedisConfigInfo temp) {

super(temp);

}

@Override

public String get(String key) {

return stringValueOps.get(key);

}

}

4. 通过spring去获取redis的配置信息

@Component

public class KeyValueServiceRegister implements ImportBeanDefinitionRegistrar, EnvironmentAware {

private static Logger logger = LoggerFactory.getLogger(KeyValueServiceRegister.class);

private Map beanDefinitionMap = null;

@Override

public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata,

BeanDefinitionRegistry registry) {

if (beanDefinitionMap == null || beanDefinitionMap.isEmpty()) {

logger.warn("beanDefinitions is empty");

return;

}

String beanName;

BeanDefinition beanDefinition;

for (Map.Entry entry : beanDefinitionMap.entrySet()) {

beanName = entry.getKey();

beanDefinition = entry.getValue();

registry.registerBeanDefinition(beanName, beanDefinition);

}

}

@SuppressWarnings("rawtypes")

@Override

public void setEnvironment(Environment environment) {

String prefix = environment.getProperty("prefix.keyvalue.cache", "kv.");

Properties props = new Properties();

MutablePropertySources propSrcs = ((AbstractEnvironment) environment).getPropertySources();

StreamSupport.stream(propSrcs.spliterator(), false)

.filter(ps -> ps instanceof EnumerablePropertySource)

.map(ps -> ((EnumerablePropertySource) ps).getPropertyNames())

.flatMap(Arrays::stream).filter(name -> name.startsWith(prefix)).forEach(

propName -> props.setProperty(propName, environment.getProperty(propName)));

beanDefinitionMap = Collections

.unmodifiableMap(KeyValueServiceHelper.buildBeanDefinitionMap(props));

}

}

5. 根据配置信息构建bean

public class KeyValueServiceHelper {

private static final String POSFIX_READ = ".r";

private static final String POSFIX_WRITE = ".w";

public static final Map buildDataSourceMap(Properties prop) {

if (prop == null || prop.isEmpty()) {

return Collections.emptyMap();

}

Map result = new HashMap<>();

Set keys = prop.stringPropertyNames();

Properties temp;

String dsName;

for (String key : keys) {

dsName = key;

if (!isKeyValueServiceBeanName(key)) {

dsName = removePosfix(key);

}

temp = result.get(dsName);

if (temp == null) {

temp = new Properties();

result.put(dsName, temp);

}

temp.setProperty(key, prop.getProperty(key));

}

return result;

}

public static final boolean isKeyValueServiceBeanName(String str) {

if (StringUtils.isBlank(str)) {

return false;

}

return str.endsWith(POSFIX_READ) || str.endsWith(POSFIX_WRITE);

}

public static final String removePosfix(String str) {

if (StringUtils.isBlank(str)) {

return str;

}

int idx = str.lastIndexOf(".");

if (idx == -1) {

return str;

}

return str.substring(0, idx);

}

public static final String resolveBeanClassName(final String beanName,

final String connectionStr) {

if (StringUtils.isBlank(beanName)) {

throw new IllegalArgumentException("bean name is blank");

}

if (StringUtils.isBlank(connectionStr)) {

throw new IllegalArgumentException("connectionStr is blank");

}

if (!RedisConnectionHelper.isRedisConnectionString(connectionStr)) {

throw new IllegalArgumentException("connectionStr is invalid");

}

if (beanName.endsWith(POSFIX_READ)) {

return RedisReadServiceImpl.class.getName();

}

if (beanName.endsWith(POSFIX_WRITE)) {

return RedisReadWriteServiceImpl.class.getName();

}

throw new IllegalArgumentException("beanName is invalid,should end with '.r' or '.w'");

}

public static final Map buildBeanDefinitionMap(Properties props) {

Map cacheSources = buildDataSourceMap(props);

if (cacheSources == null || cacheSources.isEmpty()) {

return Collections.emptyMap();

}

BeanDefinition def;

String beanName;

String connectionStr;

String beanClassName;

Properties prop;

RedisConfigInfo info;

Map beanMap =

new HashMap(cacheSources.size());

for (Map.Entry entry : cacheSources.entrySet()) {

beanName = entry.getKey();

prop = entry.getValue();

connectionStr = prop.getProperty(beanName);

if (!RedisConnectionHelper.isRedisConnectionString(connectionStr)) {

continue;

}

info = RedisConnectionHelper.parseRedisConfigInfo(prop, beanName);

def = new GenericBeanDefinition();

beanClassName = KeyValueServiceHelper.resolveBeanClassName(beanName, connectionStr);

def.setBeanClassName(beanClassName);

def.getConstructorArgumentValues().addGenericArgumentValue(info);

beanMap.put(beanName, def);

}

return beanMap;

}

}

6. 如何使用

a. 在SpringBoot的启动类里面加上@EnableDynamicKeyValueServiceCreation

b. 配置文件里面加上读写分离的redis的配置信息 - 读的是.r结尾,可写的是.w结尾

c. 在需要只读redis的service里面, 直接注入(@KeyValueReadService (name=**.r)

d. 在需要读写redis的service里面, 直接注入(@KeyValueReadService (name=**.w)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值