注意:使用代码方式访问客户端必须要开放端口或者关闭防火墙

systemctl stop firewalld(默认)
systemctl disable firewalld.service(设置开启不启动)

1、Spring访问Redis

(1)新建Spring项目,添加依赖

 <dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>5.2.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>5.2.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.2.5.RELEASE</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.2.5.RELEASE</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><!-- 添加redis依赖 --><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>1.0.3.RELEASE</version></dependency></dependencies>

(2)添加spring配置文件 redis.xm

<?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="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:redis.properties</value></list></property></bean><!-- redis config --><bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"><property name="maxActive" value="${redis.pool.maxActive}" /><property name="maxIdle" value="${redis.pool.maxIdle}" /><property name="maxWait" value="${redis.pool.maxWait}" /><property name="testOnBorrow" value="${redis.pool.testOnBorrow}" /></bean><bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"><property name="hostName" value="${redis.server}"/><property name="port" value="${redis.port}"/><property name="timeout" value="${redis.timeout}" /><property name="poolConfig" ref="jedisPoolConfig" /></bean><bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"><property name="connectionFactory" ref="jedisConnectionFactory"/><property name="KeySerializer"><bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean></property><property name="ValueSerializer"><bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean></property></bean></beans>

(3)添加properties文件

redis.pool.maxActive=100
redis.pool.maxIdle=50
redis.pool.maxWait=1000
redis.pool.testOnBorrow=trueredis.timeout=50000
redis.server=192.168.72.128
redis.port=6379

(4)编写测试用例

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;/*** spring访问Redis*/
@ContextConfiguration("classpath:redis.xml")
public class TestRedis extends AbstractJUnit4SpringContextTests {@AutowiredRedisTemplate<String,String> redisTemplate;@Testpublic void testConn(){redisTemplate.opsForValue().set("name-s","zhaoyun");System.out.println(redisTemplate.opsForValue().get("name-s"));}
}

2、SpringBoot访问Redis

(1)新建springboot项目,添加redis依赖

 <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--添加redis依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency></dependencies>

(2)添加配置文件application.yml

spring:redis:host: 192.168.72.128port: 6379jedis:pool:min-idle: 0max-idle: 8max-active: 80max-wait: 30000timeout: 3000

(3)添加配置类RedisConfig

package com.lagou.sbr2.cache;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;/*** 配置类*/
@Configuration
public class RedisConfig {@AutowiredRedisConnectionFactory factory;@Beanpublic RedisTemplate<String,Object> redisTemplate(){RedisTemplate<String,Object> redisTemplate=new RedisTemplate<>();redisTemplate.setKeySerializer(new StringRedisSerializer());redisTemplate.setValueSerializer(new StringRedisSerializer());redisTemplate.setHashKeySerializer(new StringRedisSerializer());redisTemplate.setHashValueSerializer(new StringRedisSerializer());// 如果我们想存的value为对象类型时,这个时候指定的序列化器为Jackson2RedisSerializerredisTemplate.setConnectionFactory(factory);return redisTemplate;}
}

(4)添加RedisController

package com.lagou.sbr2.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.util.concurrent.TimeUnit;@RestController
@RequestMapping(value="/redis")
public class RedisController {@AutowiredRedisTemplate redisTemplate;@GetMapping("/put")public String put(@RequestParam(required = true) String key,@RequestParam(required = true) String value){// 设置数据 20秒过期redisTemplate.opsForValue().set(key,value,20, TimeUnit.SECONDS);return "SUC";}@GetMapping("/get")public String get(@RequestParam(required = true) String key){return (String)redisTemplate.opsForValue().get(key);}
}

(5)修改Application并运行

package com.lagou.sbr2;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;@SpringBootApplication
@EnableCaching
public class SpringbootRedis2Application {public static void main(String[] args) {SpringApplication.run(SpringbootRedis2Application.class, args);}}

Redis客户端访问相关推荐

  1. C基础 redis缓存访问

    引言 先说redis安装, 这里采用的环境是. Linux version 4.4.0-22-generic (buildd@lgw01-41) (gcc version 5.3.1 20160413 ...

  2. .net core 下使用StackExchange的Redis库访问超时解决

    问题:并发稍微多的情况下Redis偶尔返回超时 StackExchange的Redis类库,用的较多,但偶尔报的问题非常让人迷惑,访问超时,队列XXX- 问题出在Redis服务器吗?可是其他应用访问都 ...

  3. 误用.Net Redis客户端工具CSRedisCore,自己挖坑自己填

    前导 上次Redis MQ分布式改造完成之后, 编排的容器稳定运行了一个多月,昨天突然收到ETL端同事通知,没有采集到解析日志了. 赶紧进服务器看了一下,用于数据接收的receiver容器挂掉了, 尝 ...

  4. 通过windows客户端访问

    文章目录 1. 原因 2. 解决方案 1. 原因 1.防火墙已开启 2.redis服务没启动 3.redis默认只能本地访问 2. 解决方案 1.安装Redis客户端,推荐redis-desktop- ...

  5. linux redis客户端_10个 Linux 顶级开源缓存工具

    可靠的分布式计算系统和应用程序已成为杰出业务的基石,尤其是在自动化和管理关键任务业务流程以及向客户提供服务方面.作为这些系统和应用程序的开发人员和系统管理员,您应该提供各种信息技术(IT)解决方案,以 ...

  6. 使用.NET System.IO.Pipelines和Kestrel套接字库创建Redis客户端

    目录 背景 介绍 Redis协议 软件设计 下载最新的存储库存档 本文是关于为Redis服务器创建异步客户端的系列文章中的第一篇,该客户端低分配,因此GC压力小,数据复制最少.这是使用技术完成的,这些 ...

  7. un7.28:redis客户端常用命令。

    安装完成Redis,我们就可以操作Redis,实现数据的CRUD(增删改查)了.这需要用到Redis客户端常用的一些命令,我给大家分享最常用的一些. 一.Redis命令行客户端 1.输入命令进入,命令 ...

  8. Redis:redis通用命令;redis常见数据结构;redis客户端;redis的序列化

    一.redis命令 1.redis通用命令 Redis 通用命令是一些 Redis 下可以作用在常用数据结构上的常用命令和一些基础的命令 常见的命令有: keys 查看符合模板的所有key,不建议在生 ...

  9. redis客户端通过哨兵获取主机、从机信息

    Master可能会因为某些情况宕机了,如果在客户端是固定一个地址去访问,肯定是不合理的,所以客户端请求是请求哨兵,从哨兵获取主机地址的信息,或者是从机的信息.可以实现一个例子 1.随机选择一个哨兵连接 ...

最新文章

  1. 基于SSH实现教务管理系统
  2. 汇编语言数据传送示例
  3. 使用Template时 typename 关键字的用法
  4. 硬盘结构,主引导记录MBR,硬盘分区表DPT,主分区、扩展分区和逻辑分区,电脑启动过程...
  5. s3c2416开发板 linux,S3C2416移植内核Linux3.1的wm9713声卡过程
  6. 【OpenCV 例程200篇】42. 图像的灰度变换(比特平面分层)
  7. java架构说明书_JDBC 使用说明(流程、架构、编程)
  8. python内容限制_Python --类,实例和访问限制
  9. 银行的起源---》阮一峰,
  10. 2022年第十三届蓝桥杯省赛--难度评价
  11. UiPath手把手中文教程PDF
  12. win 10 虚拟机设置固定 ip
  13. [车联网安全自学篇] Android安全之Android so文件分析「详细版」
  14. 什么是动态代理?动态代理有哪些应用?如何编写动态代理案例
  15. 探索瑞芯微RKNanoD芯片获索尼音箱,采用无线连接稳定无延迟
  16. 大屏展示使用WebSocket记录
  17. 网页(全屏)背景图实现方式(纯CSS向)
  18. [附源码]计算机毕业设计基于springBoot仓库管理系统
  19. 深度学习主机环境配置: Ubuntu16.04+GeForce GTX 1080+TensorFlow
  20. 我的2021保研之路已凉凉

热门文章

  1. 2022-2028年中国建筑设计行业市场调查研究及发展前景展望报告
  2. IDEA 启动tomcat 或者springboot 卡住问题
  3. 书籍《阅读的方法》读后感
  4. excel 根据某单元格的值设置整行颜色(条件格式)
  5. 什么是沾包?如何处理?
  6. uniapp 截图或者生成海报
  7. 浏览器无法访问hdfs界面问题
  8. 手把手教你用python几行代码打造人工智能对话机器人,还说说话!
  9. 数据库实验一:数据库与数据表定义(2)—— 数据表相关操作
  10. OBS直播时编码器、码率控制器、分辨率帧率是什么以及如何向第三方推流