一、背景

  上篇文章我们介绍了如何在centos7下面进行安装单机版redis以及redis集群。这篇文章,我们来聊一聊如何使用java客户端来进行操作redis。我们知道redis的java客户端有很多,如:jedis、redission等。这篇文章着重介绍我们平常使用最多的redis的java客户端jedis。

二、通过单元测试来小试牛刀

  1.首先在maven的pom.xml中引入jedis-client的依赖

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.2</version>
</dependency>

  2.使用junit进行单元测试

package com.hafiz.redis.test;

import java.util.HashSet;
import java.util.Set; import org.junit.Test; import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool; public class JedisTest { @Test
public void testJedisSingle() {
// 创建一个jedis连接
Jedis client = new Jedis("192.168.25.153", 6379);
client.set("name", "zhangsan");
String name = client.get("name");
System.out.println(name);
// 关闭连接
client.close();
} @Test
public void testJedisPoolSingle() {
// 创建一个连接池对象,系统中应该是单例的
JedisPool pool = new JedisPool("192.168.25.153", 6379);
// 从连接池中获取一个连接
Jedis client = pool.getResource();
client.set("age", "100");
String name = client.get("name");
String age = client.get("age");
System.out.println(name);
System.out.println(age);
// jedis必须关闭连接
client.close(); // 关闭连接池
pool.close();
} @Test
public void testJedisCluster() {
// 创建一个jedisCluster对象的节点集合
Set<HostAndPort> nodes = new HashSet<>();
// 在nodes中指定每个节点的地址
nodes.add(new HostAndPort("192.168.25.153", 7001));
nodes.add(new HostAndPort("192.168.25.153", 7002));
nodes.add(new HostAndPort("192.168.25.153", 7003));
nodes.add(new HostAndPort("192.168.25.153", 7004));
nodes.add(new HostAndPort("192.168.25.153", 7005));
nodes.add(new HostAndPort("192.168.25.153", 7006));
// 创建一个jedisCluster对象,该对象在系统中应该是单例的
JedisCluster cluster = new JedisCluster(nodes);
cluster.set("id", "100");
cluster.set("value", "Hello Jedis");
System.out.println(cluster.get("id"));
System.out.println(cluster.get("value")); // 系统关闭时,关闭集群
cluster.close();
}
}

三、在项目中,使用Spring集成Redis

1.redis.properties文件如下:

#Redis stand-alone config
redis.single.host=192.168.25.153
redis.single.port=6379 #Redis cluster config
redis.cluster.node1.host=192.168.25.153
redis.cluster.node1.port=7001
redis.cluster.node2.host=192.168.25.153
redis.cluster.node2.port=7002
redis.cluster.node3.host=192.168.25.153
redis.cluster.node3.port=7003
redis.cluster.node4.host=192.168.25.153
redis.cluster.node4.port=7004
redis.cluster.node5.host=192.168.25.153
redis.cluster.node5.port=7005
redis.cluster.node6.host=192.168.25.153
redis.cluster.node6.port=7006

2.spring-redis配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
   xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
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-4.0.xsd
  http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
  http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- Redis stand-alone config -->
<bean id = "jedisPool" class = "redis.clients.jedis.JedisPool">
<constructor-arg name="host" value="${redis.single.host}"/>
<constructor-arg name="port" value="${redis.single.port}"/>
</bean>
<bean id = "singleJedisClient" class = "com.taotao.rest.component.impl.SingleJedisClient"/>    <!-- Redis cluster config -->
   <!-- <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
<constructor-arg name="nodes">
<set>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${redis.cluster.node1.host}"/>
<constructor-arg name="port" value="${redis.cluster.node1.port}"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${redis.cluster.node2.host}"/>
<constructor-arg name="port" value="${redis.cluster.node2.port}"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${redis.cluster.node3.host}"/>
<constructor-arg name="port" value="${redis.cluster.node3.port}"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${redis.cluster.node4.host}"/>
<constructor-arg name="port" value="${redis.cluster.node4.port}"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${redis.cluster.node5.host}"/>
<constructor-arg name="port" value="${redis.cluster.node5.port}"/>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="${redis.cluster.node6.host}"/>
<constructor-arg name="port" value="${redis.cluster.node6.port}"/>
</bean>
</set>
</constructor-arg>
</bean>
<bean id="clusterJedisClient" class="com.hafiz.rest.component.impl.ClusterJedisClient"/> -->
</beans>

默认放开的是单机版配置,如需使用集群版请注释上面单机版配置,并打开下面集群版配置。

3.接着我们编写一个JedisClient接口类

package com.hafiz.component;

public interface JedisClient {

    String set(String key, String value);
String get(String key);
Long del(String key);
Long hset(String key, String item, String value);
String hget(String key, String item);
Long hdel(String key, String... item);
Long incr(String key);
Long decr(String key);
Long expire(String key, int seconds);
Long ttl(String key);
}

4.然后我们给出单机版的实现类:SingleJedisClient.java

package com.hafiz.component.impl;

import org.springframework.beans.factory.annotation.Autowired;

import com.taotao.rest.component.JedisClient;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool; /**
* Redis单机客户端工具类
* @author Administrator
*/
public class SingleJedisClient implements JedisClient { @Autowired
private JedisPool jedisPool; @Override
public String set(String key, String value) {
Jedis client = jedisPool.getResource();
String result = client.set(key, value);
client.close();
return result;
} @Override
public String get(String key) {
Jedis client = jedisPool.getResource();
String result = client.get(key);
client.close();
return result;
} @Override
public Long del(String key) {
Jedis client = jedisPool.getResource();
Long result = client.del(key);
client.close();
return result;
} @Override
public Long hset(String key, String item, String value) {
Jedis client = jedisPool.getResource();
Long result = client.hset(key, item, value);
client.close();
return result;
} @Override
public String hget(String key, String item) {
Jedis client = jedisPool.getResource();
String result = client.hget(key, item);
client.close();
return result;
} @Override
public Long hdel(String key, String... item) {
Jedis client = jedisPool.getResource();
Long result = client.hdel(key, item);
client.close();
return result;
} @Override
public Long incr(String key) {
Jedis client = jedisPool.getResource();
Long result = client.incr(key);
client.close();
return result;
} @Override
public Long decr(String key) {
Jedis client = jedisPool.getResource();
Long result = client.decr(key);
client.close();
return result;
} @Override
public Long expire(String key, int seconds) {
Jedis client = jedisPool.getResource();
Long result = client.expire(key, seconds);
client.close();
return result;
} @Override
public Long ttl(String key) {
Jedis client = jedisPool.getResource();
Long result = client.ttl(key);
client.close();
return result;
} }

5.我们再提供集群版的实现类:ClusterJedisClient.java

package com.hafiz.component.impl;

import org.springframework.beans.factory.annotation.Autowired;

import com.taotao.rest.component.JedisClient;

import redis.clients.jedis.JedisCluster;

/**
* Redis集群客户端工具类
* @author Administrator
*/
public class ClusterJedisClient implements JedisClient { @Autowired
private JedisCluster jedisCluster; @Override
public String set(String key, String value) {
return jedisCluster.set(key, value);
} @Override
public String get(String key) {
return jedisCluster.get(key);
} @Override
public Long del(String key) {
return jedisCluster.del(key);
} @Override
public Long hset(String key, String item, String value) {
return jedisCluster.hset(key, item, value);
} @Override
public String hget(String key, String item) {
return jedisCluster.hget(key, item);
} @Override
public Long hdel(String key, String... item) {
return jedisCluster.hdel(key, item);
} @Override
public Long incr(String key) {
return jedisCluster.incr(key);
} @Override
public Long decr(String key) {
return jedisCluster.decr(key);
} @Override
public Long expire(String key, int seconds) {
return jedisCluster.expire(key, seconds);
} @Override
public Long ttl(String key) {
return jedisCluster.ttl(key);
} }

6.spring集成redis单元测试

package com.taotao.rest.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.taotao.rest.component.JedisClient; public class SpringJedisTest { @Test
public void testJedisClientSpring() throws Exception {
//创建一个spring容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
//从容器中获得JedisClient对象
JedisClient jedisClient = applicationContext.getBean(JedisClient.class);
//jedisClient操作redis
jedisClient.set("cliet1", "1000");
String string = jedisClient.get("cliet1");
System.out.println(string);
}
}

四、总结

  通过本文我们对jedis的使用有了进一步的了解,知道了如何使用spring对redis进行集成,也对jedisClient做了单机以及集群的实现。很有成就感,未来走向架构师的路还有很远,继续努力吧!

使用Java客户端对Redis进行操作的更多相关文章

  1. 从JAVA客户端访问Redis示例(入门)

    转自:http://blog.csdn.net/kkdelta/article/details/7217761 本文记录了安装Redis和从JAVA端访问Redis的步骤 从http://downlo ...

  2. java 框架-缓冲-Redis 2Jedis操作

    https://www.cnblogs.com/wlandwl/p/redis.html Redis介绍及Jedis基础操作   1.Redis简介 Redis 是一个开源(BSD许可)的,内存中的数 ...

  3. Kubernetes官方java客户端之七:patch操作

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  4. 使用java客户端调用redis

    Redis支持很多编程语言的客户端,有C.C#.C++.Clojure.Common Lisp.Erlang.Go.Lua.Objective-C.PHP.Ruby.Scala,甚至更时髦的Node. ...

  5. HBase的java客户端测试(二)---DML操作

    测试准备 [首先同步时间:] for node in CloudDeskTop master01 master02 slave01 slave02 slave03;do ssh $node " ...

  6. HBase的java客户端测试(一)---DDL操作

    测试准备 [首先同步时间:] for node in CloudDeskTop master01 master02 slave01 slave02 slave03;do ssh $node " ...

  7. 【Redis学习之十一】Java客户端实现redis集群操作

    客户端:jedis-2.7.2.jar 配置文件两种方式: properties: redis.cluster.nodes1=192.168.1.117 redis.cluster.port1=700 ...

  8. java中的redis常用操作

    https://blog.csdn.net/lixiaoxiong55/article/details/81592800    超详细版 常规操作 public class TestReidsComm ...

  9. Jedis客户端即redis中的pipeline批量操作

    关注公众号:CoderBuff,回复"redis"获取<Redis5.x入门教程>完整版PDF. <Redis5.x入门教程>目录 第一章 · 准备工作 第 ...

随机推荐

  1. UIActivityIndicatorView 的使用

    // //  UIActivityIndicator.m //  ToolBar // //  Created by lanouhn on 15/1/3. //  Copyright (c) 2015 ...

  2. Using Spring.net in console application

    Download Spring.net in http://www.springframework.net/ Install Spring.NET.exe Create a console appli ...

  3. CSS 基础 例子 行高line-height

    “行高“指一行文字的高度,具体来说是指两行文子间基线间的距离.在CSS,line-height被用来控制行与行之间的垂直距离.line-height 属性会影响行框的布局.在应用到一个块级元素时,它定 ...

  4. 转:spring的启动过程-spring和springMVC父子容器的原理

    要想很好理解这三个上下文的关系,需要先熟悉spring是怎样在web容器中启动起来的.spring的启动过程其实就是其IoC容器的启动过程,对于web程序,IoC容器启动过程即是建立上下文的过程. s ...

  5. EBS获取并发程序Trace File

    http://blog.itpub.net/16832682/viewspace-1249765/ 最近因为项目上出现了PL/SQL性能的问题,因此需要对已经开发好的并发程序进行调优的工作.调优有个很 ...

  6. Android-Kotlin-抽象类与多态的表现

    选择包名,然后右键: 选择Class类型,会有class:  选择File类型,不会自动有class: 目录结构: 定义描述抽象类 Person人类: package cn.kotlin.kotlin ...

  7. Android-Recyclerview的简单使用

    由于Recyclerview是在 android.support.v7.widget.包 RecyclerView,所以需要导Recycler库: 导Recycler库: 选择项目,右键-->  ...

  8. asp.net core sdk & runtime 镜像[已更新至2.2.0]

    在官方镜像的脚本上, 增加了System.Drawing相关的依赖库 以北京时间为默认的时间 2.2.0 Windows SDK地址: 官方: https://dotnetcli.blob.core. ...

  9. 迁移桌面程序到MS Store(4)——桌面程序调用Win10 API

    上一篇我们讨论了如何在转制的桌面程序中,通过StartupTask来实现转制版本的开机自启动.实际操作中,我们通过编辑Packaging工程中的Package.appxmanifest文件,来添加自启 ...

  10. AngularJS指令封装高德地图组件

    1 概述 公司移动门户原来是基于AngularJS指令封装的百度地图组件,用于签到.签退.定位等功能,在使用过程中发现百度地图频繁的弹出广告,所以打算重新引用其它地图组件,最后决定基于AngularJ ...