redis客户端管理之读取xml配置,获取redis链接

获取redis客户端:

public class RedisClientManage {
    //配置文件 这里面有redis配置
    private final static String resource = "redisconnection.xml";

    /**
     * 获取默认Redis客户端
     *
     * @return 默认Redis客户端
     */
    public static RedisClient getRedisClient() {
        return getRedisClient("share");
    }

    /**
     * 根据名称获取Redis客户端
     *
     * @param clientName 客户端名称
     * @return Redis客户端
     */
    public static RedisClient getRedisClient(String clientName) {
        String server;
        Integer port;
        Integer dbIndex;
        String password;
        try (InputStream inputStream = new ClassLoaderWrapper().getResourceStream(resource)) {
            SAXReader reader = new SAXReader();
            reader.setValidation(false);
            reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
            Document doc = reader.read(inputStream);
            Element environments = (Element) doc.selectSingleNode("/configuration/environments");
            List list = environments.selectNodes("environment[@id='" + clientName + "']");
            if (list.size() != 1) {
                DataSourceException e = new DataSourceException(resource + "中未找到配置项:" + clientName);
                LOGGER.error("读取配置异常", e);
                throw e;
            }
            Element element = (Element) list.get(0);
            Element propertyElement = (Element) element.selectSingleNode(".//property[@name=\'server\']");
            server = propertyElement == null ? "127.0.0.1" : propertyElement.attributeValue("value");
            propertyElement = (Element) element.selectSingleNode(".//property[@name=\'port\']");
            port = propertyElement == null ? 6379 : Integer.valueOf(propertyElement.attributeValue("value"));
            propertyElement = (Element) element.selectSingleNode(".//property[@name=\'dbIndex\']");
            dbIndex = propertyElement == null ? 0 : Integer.valueOf(propertyElement.attributeValue("value"));
            propertyElement = (Element) element.selectSingleNode(".//property[@name=\'password\']");
            password = propertyElement == null ? null : propertyElement.attributeValue("value");
        } catch (DocumentException e) {
            LOGGER.error("读取" + resource + "抛出DocumentException", e);
            throw new DataSourceException(resource + "异常");
        } catch (SAXException e) {
            LOGGER.error("读取" + resource + "抛出SAXException", e);
            throw new DataSourceException(resource + "异常");
        } catch (IOException e) {
            LOGGER.error(resource + "文件流异常", e);
            throw new DataSourceException(resource + "异常");
        }
        RedisClient redisClient = new RedisClient(server, port, dbIndex);
        if (password != null) {
            redisClient.setPassword(password);
        }

        return redisClient;
    }
}

redisconnection.xml配置:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>
    <environments>
        <environment id="share">
            <dataSource>
                <property name="server" value="192.168.1.42"/>
                <property name="port" value="6380"/>
                <property name="dbIndex" value="0"/>
                <property name="password" value=""/>
            </dataSource>
        </environment>
    </environments>

</configuration>

调用获取redis客户端代码:

private static IObjectCache getRedis(String key) {
        RedisClient redisClient = RedisClientManage.getRedisClient(key);
        RedisCache redis = new RedisCache();
        redis.setRedisClient(redisClient);
        return redis;
    }

注:部分类为自己封装,只做参考!此博客为自己留存,需要者可以自己取己所需。

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值