spring boot 自学笔记(四) Redis集成—Jedis
上一篇笔记Reddis集成,操作Redis使用的是RedisTemplate,但实际中还是有一大部分人习惯使用JedisPool和Jedis来操作Redis, 下面使用Jedis集成示例。
修改RedisConfig类如下:
- package com.vic.config;
- import org.apache.log4j.Logger;
- import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import redis.clients.jedis.JedisPool;
- import redis.clients.jedis.JedisPoolConfig;
- /**
- *
- * @author vic
- * @desc redis config bean
- *
- */
- @Configuration
- @EnableAutoConfiguration
- @ConfigurationProperties(prefix = "spring.redis", locations = "classpath:application.properties")
- public class RedisConfig {
- private static Logger logger = Logger.getLogger(RedisConfig.class);
- private String hostName;
- private int port;
- private String password;
- private int timeout;
- @Bean
- public JedisPoolConfig getRedisConfig(){
- JedisPoolConfig config = new JedisPoolConfig();
- return config;
- }
- @Bean
- public JedisPool getJedisPool(){
- JedisPoolConfig config = getRedisConfig();
- JedisPool pool = new JedisPool(config,hostName,port,timeout,password);
- logger.info("init JredisPool ...");
- return pool;
- }
- public String getHostName() {
- return hostName;
- }
- public void setHostName(String hostName) {
- this.hostName = hostName;
- }
- public int getPort() {
- return port;
- }
- public void setPort(int port) {
- this.port = port;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public int getTimeout() {
- return timeout;
- }
- public void setTimeout(int timeout) {
- this.timeout = timeout;
- }
- }
因为JedisPool实例化对象,是将host,password等参数通过构造传入,所以在这里将整个RedisConfig定义为一个配置类,定义host,password等配置属性,通过spring boot属性文件自动注入。
接下来看看Service中如何使用:
修改IRedisService接口:
- /**
- *
- * @author vic
- * @desc redis service
- */
- public interface IRedisService {
- public Jedis getResource();
- public void returnResource(Jedis jedis);
- public void set(String key, String value);
- public String get(String key);
- }
RedisService实现类代码:
- package com.vic.service.impl;
- import org.apache.log4j.Logger;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import com.vic.service.IRedisService;
- import redis.clients.jedis.Jedis;
- import redis.clients.jedis.JedisPool;
- /**
- *
- * @author vic
- * @desc resdis service
- *
- */
- @Service
- public class RedisServiceImpl implements IRedisService {
- private static Logger logger = Logger.getLogger(RedisServiceImpl.class);
- @Autowired
- private JedisPool jedisPool;
- @Override
- public Jedis getResource() {
- return jedisPool.getResource();
- }
- @SuppressWarnings("deprecation")
- @Override
- public void returnResource(Jedis jedis) {
- if(jedis != null){
- jedisPool.returnResourceObject(jedis);
- }
- }
- @Override
- public void set(String key, String value) {
- Jedis jedis=null;
- try{
- jedis = getResource();
- jedis.set(key, value);
- logger.info("Redis set success - " + key + ", value:" + value);
- } catch (Exception e) {
- e.printStackTrace();
- logger.error("Redis set error: "+ e.getMessage() +" - " + key + ", value:" + value);
- }finally{
- returnResource(jedis);
- }
- }
- @Override
- public String get(String key) {
- String result = null;
- Jedis jedis=null;
- try{
- jedis = getResource();
- result = jedis.get(key);
- logger.info("Redis get success - " + key + ", value:" + result);
- } catch (Exception e) {
- e.printStackTrace();
- logger.error("Redis set error: "+ e.getMessage() +" - " + key + ", value:" + result);
- }finally{
- returnResource(jedis);
- }
- return result;
- }
- }
JedisPool对象使用自动注入,手动获取Jedis对象进行Redis操作,ExampleController进行测试:
- @RequestMapping("/redis/set")
- public ResponseModal redisSet(@RequestParam("value")String value){
- redisService.set("name", value);
- return new ResponseModal(200, true, "success", null);
- }
- @RequestMapping("/redis/get")
- public ResponseModal redisGet(){
- String name = redisService.get("name");
- return new ResponseModal(200, true,"success",name);
- }
测试URL:http://localhost:8080/redis/set?value=vic 响应结果:
{"code":200,"success":true,"message":"success","response":null}
测试URL:http://localhost:8080/redis/get 响应结果:
{"code":200,"success":true,"message":"success","response":"vic"}
spring boot 自学笔记(四) Redis集成—Jedis的更多相关文章
- Spring Boot(十一)Redis集成从Docker安装到分布式Session共享
一.简介 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API,Redis也是技术领域使用最为广泛的存储中间件,它是 ...
- Spring Boot 学习笔记--整合Redis
1.新建Spring Boot项目 添加spring-boot-starter-data-redis依赖 <dependency> <groupId>org.springfra ...
- Spring Boot 2.x 整合 Redis最佳实践
一.前言 在前面的几篇文章中简单的总结了一下Redis相关的知识.本章主要讲解一下 Spring Boot 2.0 整合 Redis.Jedis 和 Lettuce 是 Java 操作 Redis 的 ...
- Spring Boot 学习笔记(六) 整合 RESTful 参数传递
Spring Boot 学习笔记 源码地址 Spring Boot 学习笔记(一) hello world Spring Boot 学习笔记(二) 整合 log4j2 Spring Boot 学习笔记 ...
- spring boot / cloud (十四) 微服务间远程服务调用的认证和鉴权的思考和设计,以及restFul风格的url匹配拦截方法
spring boot / cloud (十四) 微服务间远程服务调用的认证和鉴权的思考和设计,以及restFul风格的url匹配拦截方法 前言 本篇接着<spring boot / cloud ...
- Spring Boot 2.0 图文教程 | 集成邮件发送功能
文章首发自个人微信公众号: 小哈学Java 个人网站: https://www.exception.site/springboot/spring-boots-send-mail 大家好,后续会间断地奉 ...
- Spring Boot 2.x整合Redis
最近在学习Spring Boot 2.x整合Redis,在这里和大家分享一下,希望对大家有帮助. Redis是什么 Redis 是开源免费高性能的key-value数据库.有以下的优势(源于Redis ...
- Spring Boot 2.X 如何快速集成单元测试?
本文将详细介绍下使用Spring Boot 2.X 集成单元测试,对API(Controller)测试的过程. 一.实现原理 使用MockMvc发起请求,然后执行API中相应的代码,在执行的过程中使m ...
- Spring Boot学习笔记2——基本使用之最佳实践[z]
前言 在上一篇文章Spring Boot 学习笔记1——初体验之3分钟启动你的Web应用已经对Spring Boot的基本体系与基本使用进行了学习,本文主要目的是更加进一步的来说明对于Spring B ...
随机推荐
- ExpandoObject与DynamicObject的使用
using ImpromptuInterface; using System; using System.Dynamic; namespace ConsoleApp2 { class Program ...
- Unityclient框架笔记二(组件实体开发模式的思考)
Unity的Entity-Component-System实现的很美丽,很灵活.许多文章也对这样的组件实体的开发模式倍加推崇.由于它契合这么一条规则:优先使用组合而不是继承. 可是实际开发过程中,限制 ...
- STM32 可编程电压监测器(PVD)实现数据掉电保存
STM32内部有一个完整的上电复位和掉电复位电路,当供电电压达到2v时系统即能正常工作. STM32内部自带PVD功能,用于对MCU供电电压VDD进行监控.通过电源控制寄存器中的PLS[2:0]位可以 ...
- Linux常用命令的解释
作者博客已转移,http://www.daniubiji.cn/archives/25 Linux简介及Ubuntu安装 常见指令 系统管理命令 打包压缩相关命令 关机/重启机器 Linux管道 Li ...
- 摘记—Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Cha
What a Unicode string ? The binaries in RAM have the final word. NOT string literals in Text Editor( ...
- [svc][op]关闭linux centos各种声音
现在基本都用xshell了,直接xshell禁止即可 shell报警 #vi /etc/inputrc ================================ set bell-style ...
- 【Android】3.11 地理编码功能
分类:C#.Android.VS2015.百度地图应用: 创建日期:2016-02-04 一.简介 地理编码指的是将地址信息建立空间坐标关系的过程,提供了地理坐标和地址之间相互转换的能力. 地理编码分 ...
- 关于navigationBar的颜色计算与默认透明度
NavigationBar的默认透明度为85% 颜色叠加计算公式: RGB需要分开计算,以叠加白色背景为例: 原始颜色(217,10,20) 设置透明度85% 计算过程: R=217 + (255-2 ...
- 怎样设置linux中Tab键的宽度(可永久设置)
和我的上篇文章一样,能够设置当前用户的Tab键宽度.也能够设置全部用户的Tab键宽度 一.仅设置当前用户的Tab键宽度 输入命令:vim ~/.vimrc 然后:set tabstop=4 //我 ...
- Facebook提出DensePose数据集和网络架构:可实现实时的人体姿态估计
https://baijiahao.baidu.com/s?id=1591987712899539583 选自arXiv 作者:Rza Alp Güler, Natalia Neverova, Ias ...