赞
踩
在开始整合之间先来回答三个问题
1、什么是jedis
2、它解决了什么问题
3、如何使用
基本上所有的技术都可以用这三问快速入门
答1: jedis集成了redis的一些命令操作,封装了redis的java客户端。提供了连接池管理。
答2:jedis提供了更为方便的redis操作,类比mybatis、hibernate等框架合数据库直接的关系
答3 : 本文的主题,我们通过springboot来整合jedis
第一步:添加依赖,如下是我的整个pom.xml 建议github查看jedis官网文档获取最近版本 https://github.com/xetorthio/jedis
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.1.7.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.eqxiu</groupId>
- <artifactId>eqxiu-hdh5-rmq</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>eqxiu-hdh5-rmq</name>
-
- <properties>
- <java.version>1.8</java.version>
- </properties>
-
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
-
- <!-- 引入jedis jar包 -->
- <dependency>
- <groupId>redis.clients</groupId>
- <artifactId>jedis</artifactId>
- <version>2.9.0</version>
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
-
- </project>
第二步 配置文件添加配置
- redis.server.host=localhost
- redis.server.port=6379
- redis.server.password=
- redis.server.timeOut=5000 #连接超时时间
- redis.server.maxIdle=50 #最小空闲数 空闲数依据访问缓存的频率设置,如果有较高并发建议设置大些,避免反复销毁创建连接,反之设置小些
- redis.server.maxWaitMillis=500 #获取连接最大等待时间,建议不要设置太长时间
- redis.server.maxTotal=500 #最大连接数
第三步 通过springboot注解方式创建JedisPool, 代码如下,其中RedisUtil是我提供的工具类,用来封装一些jedis常用操作的,第四步会介绍到
- package com.eqxiu.hd.config;
-
- import com.eqxiu.hd.util.RedisUtil;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.boot.context.properties.EnableConfigurationProperties;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import redis.clients.jedis.JedisPool;
- import redis.clients.jedis.JedisPoolConfig;
-
- @Configuration
- @EnableConfigurationProperties(JedisConfig.JedisProperties.class)
- public class JedisConfig {
-
- @Autowired
- private JedisProperties prop;
-
- @Bean
- public JedisPool jedisPool() {
- JedisPoolConfig config = new JedisPoolConfig();
- config.setMaxTotal(prop.getMaxTotal());
- config.setMaxIdle(prop.getMaxIdle());
- config.setMaxWaitMillis(prop.getMaxWaitMillis());
- JedisPool jedisPool;
- if("".equals(prop.getPassword())){
- jedisPool = new JedisPool(config, prop.getHost(), prop.getPort(), prop.getTimeOut());
- }else{
- jedisPool = new JedisPool(config, prop.getHost(), prop.getPort(), prop.getTimeOut(), prop.getPassword());
- }
- // 此处为RedisUtil设置了jedisPool
- RedisUtil.setJedisPool(jedisPool);
- return jedisPool;
- }
-
- @ConfigurationProperties(prefix = JedisProperties.JEDIS_PREFIX)
- class JedisProperties {
-
- public static final String JEDIS_PREFIX = "redis.server";
-
- private String host;
-
- private int port;
-
- private String password;
-
- private int maxTotal;
-
- private int maxIdle;
-
- private int maxWaitMillis;
-
- private int timeOut;
-
- public String getHost() {
- return host;
- }
-
- public void setHost(String host) {
- this.host = host;
- }
-
- 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 getMaxTotal() {
- return maxTotal;
- }
-
- public void setMaxTotal(int maxTotal) {
- this.maxTotal = maxTotal;
- }
-
- public int getMaxIdle() {
- return maxIdle;
- }
-
- public void setMaxIdle(int maxIdle) {
- this.maxIdle = maxIdle;
- }
-
- public int getMaxWaitMillis() {
- return maxWaitMillis;
- }
-
- public void setMaxWaitMillis(int maxWaitMillis) {
- this.maxWaitMillis = maxWaitMillis;
- }
-
- public int getTimeOut() {
- return timeOut;
- }
-
- public void setTimeOut(int timeOut) {
- this.timeOut = timeOut;
- }
- }
-
- }
第四步 编写RedisUtil封装常用操作,这里只些了最简单的两个方法。后续需要的操作都可以写到这里。
- package com.eqxiu.hd.util;
-
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import redis.clients.jedis.Jedis;
- import redis.clients.jedis.JedisPool;
-
- public class RedisUtil {
- private static Logger logger = LoggerFactory.getLogger(RedisUtil.class);
-
- private static JedisPool jedisPool;
-
- public static void setJedisPool(JedisPool jedisPool) {
- RedisUtil.jedisPool = jedisPool;
- }
-
- public static String redisGet(String key) {
- Jedis jedis = null;
- try {
- jedis = jedisPool.getResource();
- return jedis.get(key);
- } catch (Exception e) {
- logger.error(e.getMessage(), e);
- } finally {
- close(jedis);
- }
- return null;
- }
-
- public static void redisPut(String key, String val) {
- Jedis jedis = null;
- try {
- jedis = jedisPool.getResource();
- jedis.set(key, val);
- } catch (Exception e) {
- logger.error(e.getMessage(), e);
- } finally {
- close(jedis);
- }
- }
-
- private static void close(Jedis jedis) {
- if (jedis != null) {
- jedis.close();
- }
- }
- }
第五步, 写个controller测试下,简单粗暴了点,但是测试结果没问题,能用,本文所有代码真实可用
- @RequestMapping(value={"/redis"})
- @ResponseBody
- public String redis() {
- RedisUtil.redisPut("superbing09", "你好!是、啊");
- return RedisUtil.redisGet("superbing09");
- }
其实有个问题 RedisUtil被定义为工具类,对外提供的方法必须是static的,这里为了注入JedisPool对象提供了public的set方法,这样不太好,那么问题来了,你有什么好办法可以分享给我嘛?
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。