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

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管理操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • Java连接redis及基本操作示例

    本文实例讲述了Java连接redis及基本操作.分享给大家供大家参考,具体如下: 点击此处:本站下载安装. 解压安装 启动redis:使用cd命令切换目录到 D:\redis运行redis-server.exe redis.windows.conf 默认端口为6379 访问:切换到redis目录下运行 redis-cli.exe -h 127.0.0.1 -p 6379. pom.xml: <project xmlns="http://maven.apache.org/POM/4.0.0&

  • Java调用Redis集群代码及问题解决

    前言 需要使用以下jar包 Maven项目引用以下配置: <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> <version>2.6.2</version> </dependency> <dependency> <groupId>redis.clients&l

  • Spring整合Redis完整实例代码

    做过大型软件系统的同学都知道,随着系统数据越来越庞大,越来越复杂,随之带来的问题就是系统性能越来越差,尤其是频繁操作数据库带来的性能损耗更为严重.很多业绩大牛为此提出了众多的解决方案和开发了很多框架以优化这种频繁操作数据库所带来的性能损耗,其中,尤为突出的两个缓存服务器是Memcached和Redis.今天,我们不讲Memcached和Redis本身,这里主要为大家介绍如何使spring与Redis整合. 1.pom构建 <project xmlns="http://maven.apach

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

    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

  • Java连接Redis全过程讲解

    目录 Java连接Redis 引入jar包 编写测试类 Jedis常用方法API 一.首先把 jedis-2.1.0.jar(jedis基础包) 二.创建 jedis对象 三.键操作 四.字符串操作 五.整数和浮点数操作 六.列表(List)操作 七.集合(Set)操作 八.哈希(Hash)操作 九.有序集合(Zsort)操作 十.排序操作 Java连接Redis Jedis Client是Redis官网推荐的一个面向java客户端,库文件实现了对redis各类API进行封装调用. 引入jar包

  • java连接ElasticSearch集群操作

    我就废话不多说了,大家还是直接看代码吧~ /* *es配置类 * */ @Configuration public class ElasticSearchDataSourceConfigurer { private static final Logger LOG = LogManager.getLogger(ElasticSearchDataSourceConfigurer.class); @Bean public TransportClient getESClient() { //设置集群名称

  • Java如何通过Maven管理项目依赖

    项目的依赖 Java最大的一个优势之一应该是整个生态中无数的框架和API,我们创建实际的项目不可避免的都需要用到这些框架和API,而它们通常都是以JAR包的形式提供.我们之前在编译项目的时候,需要在classpath上存放依赖的JAR包.而且这些外部的JAR包还会有其他依赖.我们需要递归地一个个去下载所有这些外部依赖,并且要确保下载的版本都是正确的,当项目越来越复杂的时候,这是极其麻烦的事情,比如碰到JAR Hell的问题. Maven现在来拯救我们了,Maven可以自动帮我们做依赖管理,我们需

  • java 连接Redis的小例子

    需要相应API (jedis-2.1.0.jar) 复制代码 代码如下: package com.redis; import redis.clients.jedis.Jedis; public class Client {    public void getCache(String key){        Jedis jedis = new Jedis("127.0.0.1",6379); for (int i = 0; i < 100000; i++){          

  • 通过Maven进行jedis连接redis的实现

    最近项目要用到redis,很多东西忘得差不多了,稍微回顾了利用Java客户端连接redis的过程,这里jedis是连接redis的Java客户端,如果没有Maven,需要手动下载jar包,很麻烦,于是这里使用Maven,记录下连接过程. 1 .首先打开redis-server.exe,确保服务器开启 2 .之后打开eclipse创建一个maven工程 这里qqq是项目名,aaa是组名, 3 . 点击finish后这样形成的包名为group id.Artifact Id 4 .之后在pom.xml

  • Java连接Vmware中的redis

    本文使用的vmware 11,安装的系统是centos6.7,redis版本是3..0.2 .如何安装请参考上一篇文章<Linux下安装Redis并设置相关服务>.          安装完redis以后,我们是不是要迫不及待的想使用一下呢.那么我们要在程序中对redis进行操作的话,操作redis的插件有好几种,这里使用的jedis.          我们新建一个java程序以后,添加上Jedis.jar以后,并添加junit 的类库.创建一个java类进行测试.项目架构如图所示.  Ja

  • java连接mongoDB并进行增删改查操作实例详解

    本文实例讲述了java连接mongoDB并进行增删改查操作.分享给大家供大家参考,具体如下: 1.安装 MongoDB JDBC驱动程序 在java中使用mongoDB之前,首先需要拥有java连接mongoDB的第三方驱动包(jar包) 1)maven项目可通过在pom.xml中添加依赖 <dependencies> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-ja

  • java Spring Boot 配置redis pom文件操作

    1.创建一个redis maven项目,在pom中添加如下信息 spring boot 版本 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </parent> 项目相关jar配置 &l

随机推荐