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
    评论
要实现Tomcat访问Redis,并让Redis读取MySQL数据库信息,你可以按照以下步骤进行操作: 1. 首先,确保已经安装并配置好了Tomcat、Redis和MySQL数据库。 2. 在Tomcat的项目中添加Redis的Java客户依赖,比如Jedis或Lettuce。你可以在项目的pom.xml文件中添加相应的依赖,或者将相应的JAR文件拷贝到项目的lib目录下。 3. 在Tomcat的项目中编写代码来连Redis。你需要提供Redis的主机地址、口号等连信息,并使用相应的Java客户库来连Redis。以下是一个使用Jedis连Redis的示例代码: ```java import redis.clients.jedis.Jedis; public class RedisExample { public static void main(String[] args) { // 连Redis Jedis jedis = new Jedis("localhost", 6379); // 执行Redis操作 jedis.set("key", "value"); String value = jedis.get("key"); System.out.println(value); // 关闭Redis jedis.close(); } } ``` 4. 在Redis成功后,你可以使用Redis的相关命令来进行数据读写操作。比如,你可以通过使用`SET`命令将MySQL数据库中的信息存储到Redis中,然后通过`GET`命令从Redis读取数据。 5. 为了将MySQL数据库信息存储到Redis中,你需要在Tomcat项目中使用JDBC连MySQL,并执行相应的查询语句来获取数据。然后,将查询结果存储到Redis中,可以使用Redis的`SET`命令。以下是一个使用JDBC连MySQL并将查询结果存储到Redis的示例代码: ```java import redis.clients.jedis.Jedis; import java.sql.*; public class MySQLToRedisExample { public static void main(String[] args) { // 连MySQL String url = "jdbc:mysql://localhost:3306/database"; String username = "username"; String password = "password"; try (Connection connection = DriverManager.getConnection(url, username, password)) { // 执行查询语句 String sql = "SELECT * FROM table"; Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(sql); // 连Redis Jedis jedis = new Jedis("localhost", 6379); // 将查询结果存储到Redis中 while (resultSet.next()) { String key = resultSet.getString("key"); String value = resultSet.getString("value"); jedis.set(key, value); } // 关闭Redis jedis.close(); } catch (SQLException e) { e.printStackTrace(); } } } ``` 以上是一个简单的示例,你可以根据自己的实际需求进行修改和扩展。希望对你有帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值