Jedis介绍
Redis不仅使用命令来操作,而且可以使用程序客户端操作。现在基本上主流的语言都有客户端支持,比如java、C、C#、C++、php、Node.js、Go等。
在官方网站里列一些Java的客户端,有Jedis、Redisson、Jredis、JDBC-Redis、等其中官方推荐使用Jedis和Redisson。 
Jedis同样也是托管在github上,地址:https://github.com/xetorthio/jedis

 <dependencies><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.9.0</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.0.7.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.0.7.RELEASE</version></dependency><!-- 单元测试Junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency></dependencies><build><plugins><!-- 配置Maven的JDK编译级别 --><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.2</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin></plugins></build>

单实例连接

 @Testpublic void testJedis() {//创建一个Jedis的连接Jedis jedis = new Jedis("10.28.184.25", 6379);//执行redis命令jedis.set("mytest", "hello world, this is jedis client!");//从redis中取值String result = jedis.get("mytest");//打印结果System.out.println(result);//关闭连接jedis.close();}

连接池连接

 @Testpublic void testJedisPool() {//创建一连接池对象JedisPool jedisPool = new JedisPool("10.28.184.25", 6379);//从连接池中获得连接Jedis jedis = jedisPool.getResource();String result = jedis.get("mytest") ;System.out.println(result);//关闭连接jedis.close();//关闭连接池jedisPool.close();}

连接redis集群

@Testpublic void testJedisCluster() throws Exception {//创建一连接,JedisCluster对象,在系统中是单例存在Set<HostAndPort> nodes = new HashSet<>();nodes.add(new HostAndPort("10.28.184.25", 7001));nodes.add(new HostAndPort("10.28.184.25", 7002));nodes.add(new HostAndPort("10.28.184.25", 7003));nodes.add(new HostAndPort("10.28.184.25", 7004));JedisCluster cluster = new JedisCluster(nodes);//执行JedisCluster对象中的方法,方法和redis一一对应。cluster.set("cluster-test", "my jedis cluster test");String result = cluster.get("cluster-test");System.out.println(result);//程序结束时需要关闭JedisCluster对象cluster.close();}

Jedis整合spring
配置spring配置文件applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 连接池配置 --><bean id="jedisPoolConfig"class="redis.clients.jedis.JedisPoolConfig"><!-- 最大连接数 --><property name="maxTotal" value="30" /><!-- 最大空闲连接数 --><property name="maxIdle" value="10" /><!-- 每次释放连接的最大数目 --><property name="numTestsPerEvictionRun" value="1024" /><!-- 释放连接的扫描间隔(毫秒) --><property name="timeBetweenEvictionRunsMillis" value="30000" /><!-- 连接最小空闲时间 --><property name="minEvictableIdleTimeMillis" value="1800000" /><!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 --><property name="softMinEvictableIdleTimeMillis" value="10000" /><!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 --><property name="maxWaitMillis" value="1500" /><!-- 在获取连接的时候检查有效性, 默认false --><property name="testOnBorrow" value="true" /><!-- 在空闲时检查有效性, 默认false --><property name="testWhileIdle" value="true" /><!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true --><property name="blockWhenExhausted" value="false" /></bean><!-- redis单机 通过连接池 --><bean id="jedisPool" class="redis.clients.jedis.JedisPool"destroy-method="close"><constructor-arg name="poolConfig"ref="jedisPoolConfig" /><constructor-arg name="host" value="10.28.184.25" /><constructor-arg name="port" value="6379" /></bean><!-- redis集群 --><bean id="jedisCluster" class="redis.clients.jedis.JedisCluster"><constructor-arg index="0"><set><bean class="redis.clients.jedis.HostAndPort"><constructor-arg index="0" value="10.28.184.25"></constructorarg><constructor-arg index="1" value="7001"></constructor-arg></bean><bean class="redis.clients.jedis.HostAndPort"><constructor-arg index="0" value="10.28.184.25"></constructorarg><constructor-arg index="1" value="7002"></constructor-arg></bean><bean class="redis.clients.jedis.HostAndPort"><constructor-arg index="0" value="10.28.184.25"></constructorarg><constructor-arg index="1" value="7003"></constructor-arg></bean><bean class="redis.clients.jedis.HostAndPort"><constructor-arg index="0" value="10.28.184.25"></constructorarg><constructor-arg index="1" value="7004"></constructor-arg></bean><bean class="redis.clients.jedis.HostAndPort"><constructor-arg index="0" value="10.28.184.25"></constructorarg><constructor-arg index="1" value="7005"></constructor-arg></bean><bean class="redis.clients.jedis.HostAndPort"><constructor-arg index="0" value="10.28.184.25"></constructorarg><constructor-arg index="1" value="7006"></constructor-arg></bean></set></constructor-arg><constructor-arg index="1" ref="jedisPoolConfig"></constructor-arg></bean>
</beans>
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;import javax.annotation.Resource;/*** @author wangbh* @Description: test1* @date 2021/12/8 10:00*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:application.xml")
public class TestJedis2 {@Autowiredprivate JedisPool jedisPool;@Resourceprivate JedisCluster cluster;@Testpublic void testJedisPool() {// 从连接池中获得连接Jedis jedis = jedisPool.getResource();String result = jedis.get("mytest");System.out.println(result);// 关闭连接jedis.close();}@Testpublic void testJedisCluster() throws Exception {// 执行JedisCluster对象中的方法,方法和redis一一对应。cluster.set("cluster-test", "my jedis cluster test");String result = cluster.get("cluster-test");System.out.println(result);}
}
Redisson方式
package com;import org.redisson.Redisson;
import org.redisson.config.Config;/*** @author wangbh* @Description: test* @date 2021/8/19 10:44*/
public class RedissonManager {private static Config config = new Config();//声明redisso对象private static Redisson redisson = null;//实例化redissonstatic {//单个config.useSingleServer().setPassword("!QAZxsw2#EDC(0Ol1)").setAddress("192.168.1.239:6379").setDatabase(2);//        config.useClusterServers()集群状态扫描间隔时间,单位是毫秒//                .setScanInterval(2000)cluster方式至少6个节点(3主3从,3主做sharding,3从用来保证主宕机后可以高可用)//                .addNodeAddress("192.168.1.239:6379").setPassword("!QAZxsw2#EDC(0Ol1)");//得到redisson对象redisson = (Redisson) Redisson.create(config);}//获取redisson对象的方法public static Redisson getRedisson() {return redisson;}
}

JAVA连接Redis客户端多种方式实现相关推荐

  1. Java连接Redis及操作(一)

    Redis简介 Redis是一个开源的使用ANSI c语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.它是一种非关系性的数据库.它是以key-val ...

  2. Redis——Java连接Redis

    Java连接redis,首先修改两项配置文件 bind 127.0.0.1 ::1 注释掉 protected-mode 设置为no 然后需要导入一个依赖 <!--redis--> < ...

  3. Java连接Redis及操作(二)

    前言 上一节我们学习了java连接Redis,并且连接Redis成功,今天博主带领大家进行简单的操作Redis.要想对Redis进行Java的操作,必须先了解Redis API(点击这里),可看到AP ...

  4. java连接redis存取数据(详细)

    声明:本文章仅供参考,学无止境,若有不足之处请指出,非常感谢! 源代码+相关工具下载:https://download.csdn.net/download/corleone_4ever/1081125 ...

  5. Redis集群搭建及java连接redis

    Redis集群搭建及java连接redis Redis集群分为三种: 1.主从关系模式2.Sentinel哨兵关系模式3.Cluster去中心化模式 1.主从关系模式 1.1.什么是主从模式? (1) ...

  6. Java连接Redis

    Java连接Redis Jedis Client是Redis官网推荐的一个面向java客户端,库文件实现了对redis各类API进行封装调用. 引入jar包 我创建的是maven项目,所以只用在pom ...

  7. Java连接Oracle两种方式thin与oci区别

    Java连接Oracle两种方式thin与oci区别 前几天同事跑过来跟我说, 机房中的一台tomcat服务器跟oracle数据库机连接很慢,查看控制台中的hibernate日志, 基本上是一条sql ...

  8. Java连接mysql数据库的方式,java连接mysql数据库的方式(4句语句)

    1 加载mysql驱动: class.forName("con.mysql.jdbc.Driver").newInstance(); 2 根据数据库路径url,账号,密码进行数据库 ...

  9. java 连接redis失败_java 连接Redis问题及demo

    java连接linux Redis遇到的问题 昨天在Linux搭建了Redis服务,今天使用java连接测试了一下.要想使用java连接redis服务,就离不开jedis-2.6.1.jar.使用je ...

最新文章

  1. lvs的十种调度算法概念
  2. CNN回应中方谴责 否认冒犯中国人
  3. Java 获取当前项目的类路径
  4. php类中双冒号和-的区别
  5. oracle常用命令(比较常见好用)
  6. SharePoint品牌化和自定义--第一章节--SharePoint品牌化介绍(1)--为什么要进行SharePoint品牌化...
  7. GIMP 教程在 github 发布文章外,又做视频了
  8. pdf转换成word后有文字叠加_word字体出现重叠 pdf转换成word
  9. 文档管理专家Aspose 2017年首季更新大合集
  10. ContextCapture、EPS、CASS3D房地一体详细操作流程
  11. 刘华:上云还是不上云,这是一个问题
  12. oracle联接,Oracle的联接详解(左连接、右连接、全连接.)
  13. 达人评测 骁龙778g和骁龙780g的区别 选哪个好
  14. CTFHUB-SQL注入
  15. android+开机+无命令,红米手机怎么刷机
  16. python/gurobi计算二人零和博弈纳什均衡精确解(可求解大规划策略空间)
  17. 三元一次方程组例题_最新《三元一次方程组及其解法》例题与讲解
  18. 日历签到html,简单的手机移动端日历签到js代码
  19. Linux命令+shell脚本大全:操作文件系统
  20. 菜鸟小超超开发小记(一)

热门文章

  1. 《人工智能》之《自然语言理解》
  2. ESP32与掌控板IO接口编程入门 | ESP32轻松学(Arduino版)
  3. In aggregated query without GROUP BY
  4. PHP接入微信公众号(一)
  5. MT4如何使用软件开展自动交易详细步骤
  6. 2003加入域提示“用户已存在”
  7. python编写restful接口_Python开发之路系列:RESTful 接口开发
  8. 1.虚拟机克隆后的处理步骤
  9. mysql设置最大连接数
  10. A5932:外置MOS三相正弦信号无传感器风扇控制器