使用java连接Redis,Maven管理操作

所属分类: 软件编程 / java 阅读数: 43
收藏 0 赞 0 分享

pom配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>redis</groupId>
 <artifactId>redis</artifactId>
 <version>1.0-SNAPSHOT</version>

 <properties>
 <spring.version>5.0.2.RELEASE</spring.version>
 <maven.compiler.source>1.8</maven.compiler.source>
 <maven.compiler.target>1.8</maven.compiler.target>
 </properties>

 <dependencies>
 <dependency>
 <groupId>redis.clients</groupId>
 <artifactId>jedis</artifactId>
 <version>2.8.0</version>
 </dependency>
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.9</version>
 </dependency>
 <!-- https://mvnrepository.com/artifact/commons-pool/commons-pool -->
 <dependency>
 <groupId>commons-pool</groupId>
 <artifactId>commons-pool</artifactId>
 <version>1.6</version>
 </dependency>
 <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
 <dependency>
 <groupId>org.springframework.data</groupId>
 <artifactId>spring-data-redis</artifactId>
 <version>2.0.3.RELEASE</version>
 </dependency>
 </dependencies>
</project>

创建db.properties文件

redis.host=bigdata-hpsk01.huadian.com
redis.port=6379
redis.maxIdle=10
redis.minIdle=10
redis.maxTotal=50

书写工具类

package com.huadian.redisUntil;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public final class JedisPoolUntil {
 private static String ADDR = "192.168.59.160";
 //Redis的端口号
 private static int PORT = 6379;

 /* //可用连接实例的最大数目,默认值为8;
 //如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool的状态为exhausted(耗尽)。
 private static int MAX_ACTIVE = 1024;

 //控制一个pool最多有多少个状态为idle(空闲的)的jedis实例,默认值也是8。
 private static int MAX_IDLE = 200;

 //等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接抛出JedisConnectionException;
 private static int MAX_WAIT = 10000;

 private static int TIMEOUT = 10000;*//*

 //在borrow一个jedis实例时,是否提前进行validate操作;如果为true,则得到的jedis实例均是可用的;
 private static boolean TEST_ON_BORROW = true;*/

 private static int MAXTOTAL=20;
 private static int MINIDLE=10;
 private static int MAXIDLE=15;
 private static JedisPool jedisPool = null;
 /**
 * 初始化Redis连接池
 */
 static {
 try {
  JedisPoolConfig config = new JedisPoolConfig();
  config.setMaxTotal(MAXTOTAL);
  config.setMaxIdle(MINIDLE);
  config.setMinIdle(MAXIDLE);

  jedisPool = new JedisPool(config, ADDR, PORT);
  } catch (Exception e) {
  e.printStackTrace();
  }
 }

 /**
 * 获取Jedis实例
 * @return
 */
 public synchronized static Jedis getJedis() {
  try {
  if (jedisPool != null) {
   Jedis resource = jedisPool.getResource();
   return resource;
  } else {
   return null;
  }
  } catch (Exception e) {
  e.printStackTrace();
  return null;
  }
 }

 /**
 * 释放jedis资源
 * @param jedis
 */
 public static void returnResource(final Jedis jedis) {
 if (jedis != null) {
  jedisPool.returnResource(jedis);
  }
 }
}

书写测试类

package com.huadian.jedis;

import com.huadian.redisUntil.JedisPoolUntil;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisDemo {
 private Jedis jedis = null;

 /**
 * 单连接
 */
 @Test
 public void jedisSingleConn(){
 String host = "192.168.59.160";
 int port = 6379;
 Jedis jedis = new Jedis(host, port);
 jedis.set("name","张三");
 jedis.set("age","18");
 String s = jedis.get("name");
 String s1 = jedis.get("age");
 System.out.println(s);
 System.out.println(s1);
 }
 /**
 * 连接池连接
 */
 @Test
 public void jedisPoolConn(){
 JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
 jedisPoolConfig.setMaxTotal(20);
 jedisPoolConfig.setMinIdle(10);
 jedisPoolConfig.setMaxIdle(15);
 JedisPool jedisPool = new JedisPool(jedisPoolConfig, "192.168.59.160", 6379);
 Jedis jedis = jedisPool.getResource();
 //取数据
 String s = jedis.get("name");
 String s1 = jedis.get("age");
 System.out.println(s);
 System.out.println(s1);
 }
 /**
 * 连接池连接
 * 工具类
 */
 @Test
 public void jedisPoolConn1(){
 Jedis jedis1 = JedisPoolUntil.getJedis();
 //取数据
 String s = jedis1.get("name");
 String s1 = jedis1.get("age");
 System.out.println(s);
 System.out.println(s1);
 }
}

补充知识:JAVA使用Redis所需的MAVEN的POM文件

redis不仅可以通过命令行进行操作,同时redis也可以通过javaAPI进行操作,这是操作redis所需的依赖

<dependencies>
  <dependency>
   <groupId>redis.clients</groupId>
   <artifactId>jedis</artifactId>
   <version>2.9.0</version>
  </dependency>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.12</version>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>org.testng</groupId>
   <artifactId>testng</artifactId>
   <version>6.14.3</version>
   <scope>test</scope>
  </dependency>
 </dependencies>
 <build>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.0</version>
    <configuration>
     <source>1.8</source>
     <target>1.8</target>
     <encoding>UTF-8</encoding>
     <!-- <verbal>true</verbal>-->
    </configuration>
   </plugin>
  </plugins>
 </build>

以上这篇使用java连接Redis,Maven管理操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Java输入输出流复制文件所用时间对比

这篇文章主要介绍了Java输入输出流复制文件所用时间对比的相关资料,非常不错,具有参考解决价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Java线程中start和run方法全面解析

这篇文章主要介绍了Java线程中start和run方法的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Java的JSON处理器fastjson使用方法详解

下面小编就为大家带来一篇Java的JSON处理器fastjson使用方法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Java 二维码,QR码,J4L-QRCode 的资料整理

本文主要介绍Java 中二维码,QR码,J4L-QRCode,这里整理了详细的资料供大家学习参考关于二维码的知识,有需要的小伙伴可以参考下
收藏 0 赞 0 分享

java哈夫曼树实例代码

这篇文章主要为大家介绍了java哈夫曼树实例代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android读取本地或网络图片并转换为Bitmap

这篇文章主要为大家详细介绍了Android读取本地或网络图片,并转换为Bitmap,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java日期时间操作的方法

这篇文章主要为大家详细介绍了Java日期时间操作的一些方法,获得Calendar,定义日期/时间的格式等,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java 获取路径的各种方法(总结)

下面小编就为大家带来一篇java 获取路径的各种方法(总结)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java数据结构与算法之奇偶排序算法完整示例

这篇文章主要介绍了java数据结构与算法之奇偶排序算法,较为详细的分析了奇偶算法的原理并结合完整示例形式给出了实现技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

java数据结构与算法之双向循环队列的数组实现方法

这篇文章主要介绍了java数据结构与算法之双向循环队列的数组实现方法,结合实例形式分析了双向循环队列的原理与数组实现技巧,并附带说明了该算法的用途,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多