java程序连接redis服务器
文章目录
- 1 Java socket编程连接redis服务器
- 1.1 创建maven工程
- 1.2 使用java手写redis客户端
- 2 使用Jedis和JedisPool连接redis服务器
- 2.1 导入Jedis依赖
- 2.2 编写测试代码
- 3 使用lettuce连接redis服务器
- 3.1 导入依赖
- 3.2 编写测试代码
1 Java socket编程连接redis服务器
使用java通过socket连接到redis-server
可以实现redis的各种命令
1.1 创建maven工程
1.2 使用java手写redis客户端
在redis里创建MyClient.java
package edu.xja.redis;import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;import static com.oracle.jrockit.jfr.ContentType.Bytes;/*** Java socket编程连接* 针对无密码的redis服务器*/
public class MyClient {//定义属性//redis服务器ip地址private static final String IP_ADDRESS="192.168.10.148";//redis服务器端口号private static final Integer SERVER_PORT=6379;/*** 主函数* @param args*/public static void main(String[] args) throws IOException {//1.建立socketSocket scoket = new Socket(IP_ADDRESS, SERVER_PORT);//2.判断是否连接到服务器System.out.println("是否连接到redis服务器:"+ scoket.isConnected());//3. 获取iOutputStream输入流,这是发给服务器的请求信息OutputStream outputStream = scoket.getOutputStream();//4. 获取inputStream输入流,这是服务器返回的响应InputStream inputStream = scoket.getInputStream();//5.开始执行命令//(1)执行auth命令auth(outputStream,inputStream);//(12)执行ping命令ping(outputStream,inputStream);//(3)执行set命令set(outputStream,inputStream);//6.关闭资源inputStream.close();outputStream.close();scoket.close();}/*** auth发送用户信息* @param outputStream* @param inputStream*/private static String auth(OutputStream outputStream, InputStream inputStream) throws IOException {//1.redis协议字符串 auth 123456String sendInfo="*2\r\n$4\r\nauth\r\n$6\r\n123456\r\n";//2. 发送授权命令outputStream.write(sendInfo.getBytes());//3.创建字节数组装响应数据byte[] responseByte = new byte[1024];//4.读取服务器响应到字节数组int length = inputStream.read(responseByte);//5.将字节数组转换成字符串return new String(responseByte, 0, length);}/*** ping查看是否联通服务器* @param outputStream* @param inputStream*/private static void ping(OutputStream outputStream, InputStream inputStream) throws IOException {//1.redis协议字符串 pingString sendInfo="*1\r\n$4\r\nping\r\n";//2. 发送授权命令outputStream.write(sendInfo.getBytes());//3.创建字节数组装响应数据byte[] responseByte = new byte[1024];//4.读取服务器响应到字节数组int length = inputStream.read(responseByte);//5.将字节数组转换成字符串String responseInfo = new String(responseByte, 0, length);System.out.println(responseInfo);}/*** set创建key* @param outputStream* @param inputStream*/private static void set(OutputStream outputStream, InputStream inputStream) throws IOException {//1.redis协议字符串 set animal monkeyString sendInfo="*3\r\n$3\r\nset\r\n$6\r\nanimal\r\n$6\r\nmonkey\r\n";//2. 发送授权命令outputStream.write(sendInfo.getBytes());//3.创建字节数组装响应数据byte[] responseByte = new byte[1024];//4.读取服务器响应到字节数组int length = inputStream.read(responseByte);//5.将字节数组转换成字符串String responseInfo = new String(responseByte, 0, length);System.out.println(responseInfo);}}
2 使用Jedis和JedisPool连接redis服务器
2.1 导入Jedis依赖
<!--jedis依赖--><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>3.7.0</version></dependency>
2.2 编写测试代码
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;import java.util.List;
import java.util.Set;import static org.junit.jupiter.api.Assertions.*;class RedisClientTest {@Test@DisplayName("jedis连接redis服务器")public void jedis(){//创建jedisJedis jedis = new Jedis("192.168.10.148",6379);//执行命令jedis.auth("123456");String pong = jedis.ping();jedis.keys("*");String set = jedis.set("hello", "world");System.out.println(set);}@Test@DisplayName("jedis连接池连接redis服务器")public void jedisPool(){//创建jedisJedisPool jedisPool = new JedisPool("192.168.10.148",6379);Jedis jedis = jedisPool.getResource();//执行命令jedis.auth("123456");String pong = jedis.ping();String set = jedis.set("animal", "monkey");System.out.println(set);}}
3 使用lettuce连接redis服务器
3.1 导入依赖
<dependency><groupId>io.lettuce</groupId><artifactId>lettuce-core</artifactId><version>6.2.0.RELEASE</version></dependency></dependencies>
3.2 编写测试代码
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;import java.util.List;
import java.util.Set;import static org.junit.jupiter.api.Assertions.*;class RedisClientTest {@Test@DisplayName("生菜连接redis服务器")public void lettce(){//创建jedisRedisClient redisClient = RedisClient.create("redis://123456@192.168.10.148:6379/0");StatefulRedisConnection<String, String> connection = redisClient.connect();RedisCommands<String, String> jedis = connection.sync();//执行命令String pong = jedis.ping();String set = jedis.set("friut", "apple");}}
java程序连接redis服务器相关推荐
- java 启动redis服务器_docker启动redis并使用java连接
一.先查找镜像 docker search redis 二.拉取镜像 docker pull redis 三.等待拉取完毕 四.查看拉去的镜像 docker iamges 五.运行redis 连接1: ...
- redis服务器学习一
一:什么是redis服务器 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zs ...
- Java的Redis连接池代码性能不错
其实这个是引用自网友http://blog.csdn.net/tuposky/article/details/45340183,有2个版本,差别就是ReentrantLock和synchronized ...
- 运维Linux redis,系统运维|如何在 CentOS 7 上安装 Redis 服务器
大家好,本文的主题是 Redis,我们将要在 CentOS 7 上安装它.编译源代码,安装二进制文件,创建.安装文件.在安装了它的组件之后,我们还会配置 redis ,就像配置操作系统参数一样,目标就 ...
- redis 服务器/客户端安装与配置
redis 服务器/客户端安装与配置 1 redis server 1.1 获取redis源码包 //官网 http://redis.io/ wget -c http://redis.googleco ...
- Java在游戏服务器开发中的应用
Java在游戏服务器开发中的应用 width="22" height="16" src="http://hits.sinajs.cn/A1/we ...
- 如何在CentOS 7上安装Redis服务器
大家好,本文的主题是 Redis,我们将要在 CentOS 7 上安装它.编译源代码,安装二进制文件,创建.安装文件.在安装了它的组件之后,我们还会配置 redis ,就像配置操作系统参数一样,目标就 ...
- Windows10【Java连接Redis报错】
报错的原因 应该是:Redis服务器没有打开! 目录 一.报错信息1 二.报错信息2 三.解决方法 一.报错信息1 F:\Java\jdk1.8.0_201\bin\java -ea -Didea.t ...
- java对redis的基本操作
转载:http://www.cnblogs.com/edisonfeng/p/3571870.html package com.redis;import java.util.ArrayList; im ...
最新文章
- SQLSERVER 分页
- opengl 深度详解_一步步学OpenGL(1) -《打开一个窗口》
- Python Django 多表插入之重写save()方法代码示例
- Servlet 请求处理
- CG-CTF-Web-文件包含
- 3dmax高版本转低版本插件_视频编辑干货资料:低版本打开高版本pr文件
- 让 jQuery UI draggable 适配移动端
- Linux邮件系统整合windows 2008 R2 AD域认证更新
- python面试题之Fibonacci数列
- linux下安装chrome
- php parse url 中文,php parse_url()函数解析URL用法总结
- 如何才能精通C++?原来这点才最重要!
- SQL Server-【知识与实战I】条件查询、比较运算符查询、模糊查询、枚举查询、范围查询、空值查询、多重条件查询
- 深度剖析ConcurrentHashMap源码
- c语言 指针 地址溢出,堆栈溢出,指针在C中
- 计算机论文的字体要求,论文格式及字体大小要求(标准)
- python离线安装whl文件.
- 电流反馈型运放电路设计指南
- Hololens开发学习笔记-4
- 数据通信与计算机网络A笔记