Spring整合Redis(windows版)

本文介绍了Redis环境准备和Spring整合Redis的方法。在Redis环境准备方面,包括在github下载windows版本Redis、安装、设置为服务及使用客户端操作。Spring整合Redis则涉及新建applicationContext - jedis.xml文件、配置类,还给出了代码测试相关内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Redis环境准备

一、下载windows版本的Redis

去官网找了很久,发现原来在官网上可以下载的windows版本的,现在官网以及没有下载地址,只能在github上下载,官网只提供linux版本的下载
官网下载地址:http://redis.io/download
github下载地址:https://github.com/MSOpenTech/redis/tags

二、安装Redis

  1. 这里下载的是Redis-x64-3.2.100版本,我的电脑是win10 64位,所以下载64位版本的,在运行中输入cmd,然后把目录指向解压的Redis目录。
    在这里插入图片描述
  2. 启动命令
    redis-server redis.windows.conf,出现下图显示表示启动成功了。
    在这里插入图片描述

三、设置Redis服务

  1. 由于上面虽然启动了redis,但是只要一关闭cmd窗口,redis就会消失。所以要把redis设置成windows下的服务。
    如果之前没设置过,这里是找不到Redis这个服务的。
    在这里插入图片描述
  2. 设置服务命令
    redis-server --service-install redis.windows-service.conf --loglevel verbose,如果redis.windows.conf中设置密码,在pig-config中的application-dev.yml配置文件中要输入redis对应的密码。
    在这里插入图片描述
    输入命令之后没有报错,表示成功了,刷新服务,会看到多了一个redis服务。
  3. 常用的redis服务命令。
    卸载服务:redis-server --service-uninstall
    开启服务:redis-server --service-start
    停止服务:redis-server --service-stop
  4. 启动服务
    在这里插入图片描述
  5. 测试Redis
    在这里插入图片描述
    或者点击安装目录下的redis-cli.exe文件。可以看到对应的地址和端口。在这里插入图片描述

四、使用Redis客户端来操作Redis

RedisDesktopManager
链接:https://pan.baidu.com/s/18kTNtn96bICUDPMGG5og9w
提取码:jhy0

Spring整合Redis

一、新建applicationContext-jedis.xml文件

<?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">
	<!-- 连接池配置 -->
	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<!-- 最大连接数 -->
		<property name="maxTotal" value="30" />
		<!-- 最大空闲连接数 -->
		<property name="maxIdle" value="10" />
		<!-- 每次释放连接的最大数目 -->
		<property name="numTestsPerEvictionRun" value="1024" />
		<!-- 释放连接的扫描间隔(毫秒) -->
		<property name="timeBetweenEvictionRunsMillis" value="30000" />
		<!-- 连接最小空闲时间 -->
		<property name="minEvictableIdleTimeMillis" value="1800000" />
		<!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
		<property name="softMinEvictableIdleTimeMillis" value="10000" />
		<!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
		<property name="maxWaitMillis" value="1500" />
		<!-- 在获取连接的时候检查有效性, 默认false -->
		<property name="testOnBorrow" value="true" />
		<!-- 在空闲时检查有效性, 默认false -->
		<property name="testWhileIdle" value="true" />
		<!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
		<property name="blockWhenExhausted" value="false" />
	</bean>	
	<!-- jedis客户端单机版 -->
	<bean id="redisClient" class="redis.clients.jedis.JedisPool">
		<constructor-arg name="host" value="127.0.0.1"></constructor-arg>
		<constructor-arg name="port" value="6379"></constructor-arg>
		<constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
	</bean>
	<bean id="jedisClient" class="com.daigou.redis.dao.impl.JedisClientSingle"/>
	
	
	<!-- jedis集群版配置 -->
	<!-- <bean id="redisClient" class="redis.clients.jedis.JedisCluster">
		<constructor-arg name="nodes">
			<set>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.25.153"></constructor-arg>
					<constructor-arg name="port" value="7001"></constructor-arg>
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.25.153"></constructor-arg>
					<constructor-arg name="port" value="7002"></constructor-arg>
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.25.153"></constructor-arg>
					<constructor-arg name="port" value="7003"></constructor-arg>
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.25.153"></constructor-arg>
					<constructor-arg name="port" value="7004"></constructor-arg>
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.25.153"></constructor-arg>
					<constructor-arg name="port" value="7005"></constructor-arg>
				</bean>
				<bean class="redis.clients.jedis.HostAndPort">
					<constructor-arg name="host" value="192.168.25.153"></constructor-arg>
					<constructor-arg name="port" value="7006"></constructor-arg>
				</bean>
			</set>
		</constructor-arg>
		<constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
	</bean>
	<bean id="jedisClientCluster" class="com.daigou.redis.dao.impl.JedisClientCluster"></bean> -->
</beans>

二、新建配置类

在这里插入图片描述
JedisClient.java是一个接口

package com.daigou.redis.dao;

public interface JedisClient {

	String get(String key); // 获取key的值

	String set(String key, String value); // 这只key的值

	String hget(String hkey, String key); // 获取hash+key的值(hash相当于给key分组)

	long hset(String hkey, String key, String value); // 设置hash+key的值

	long incr(String key); // 自增返回,当key值没有是,从0开始

	long decr(String key);

	long incrBy(String key, Long value);

	long decrBy(String key, Long value);

	long expire(String key, int second); // 设置值得生存时间

	long ttl(String key); // 查看值的生存时间

	long del(String key); // 删除key

	long hdel(String hkey, String key); // 删除hash+key

}

建两个实现类,根据配置文件中规定加载的不同的实现类,来实现单机版redis或者集群版redis的配置
JedisClientCluster.java

package com.daigou.redis.dao.impl;

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

import com.daigou.redis.dao.JedisClient;

import redis.clients.jedis.JedisCluster;

public class JedisClientCluster implements JedisClient {

	@Autowired
	private JedisCluster jedisCluster;

	@Override
	public String get(String key) {
		return jedisCluster.get(key);
	}

	@Override
	public String set(String key, String value) {
		return jedisCluster.set(key, value);
	}

	@Override
	public String hget(String hkey, String key) {
		return jedisCluster.hget(hkey, key);
	}

	@Override
	public long hset(String hkey, String key, String value) {
		return jedisCluster.hset(hkey, key, value);
	}

	@Override
	public long incr(String key) {
		return jedisCluster.incr(key);
	}

	@Override
	public long decr(String key) {
		return jedisCluster.decr(key);
	}

	@Override
	public long incrBy(String key, Long value) {
		return jedisCluster.incrBy(key, value);
	}

	@Override
	public long decrBy(String key, Long value) {
		return jedisCluster.decrBy(key, value);
	}

	@Override
	public long expire(String key, int second) {
		return jedisCluster.expire(key, second);
	}

	@Override
	public long ttl(String key) {
		return jedisCluster.ttl(key);
	}

	@Override
	public long del(String key) {

		return jedisCluster.del(key);
	}

	@Override
	public long hdel(String hkey, String key) {

		return jedisCluster.hdel(hkey, key);
	}

}

JedisClientSingle.java

package com.daigou.redis.dao.impl;

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

import com.daigou.redis.dao.JedisClient;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class JedisClientSingle implements JedisClient {

	@Autowired
	private JedisPool jedisPool;

	@Override
	public String get(String key) {
		Jedis jedis = jedisPool.getResource();
		String string = jedis.get(key);
		jedis.close();
		return string;
	}

	@Override
	public String set(String key, String value) {
		Jedis jedis = jedisPool.getResource();
		String string = jedis.set(key, value);
		jedis.close();
		return string;
	}

	@Override
	public String hget(String hkey, String key) {
		Jedis jedis = jedisPool.getResource();
		String string = jedis.hget(hkey, key);
		jedis.close();
		return string;
	}

	@Override
	public long hset(String hkey, String key, String value) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.hset(hkey, key, value);
		jedis.close();
		return result;
	}

	@Override
	public long incr(String key) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.incr(key);
		jedis.close();
		return result;
	}

	@Override
	public long decr(String key) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.decr(key);
		jedis.close();
		return result;
	}

	@Override
	public long incrBy(String key, Long value) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.incrBy(key, value);
		jedis.close();
		return result;
	}

	@Override
	public long decrBy(String key, Long value) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.decrBy(key, value);
		jedis.close();
		return result;
	}

	@Override
	public long expire(String key, int second) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.expire(key, second);
		jedis.close();
		return result;
	}

	@Override
	public long ttl(String key) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.ttl(key);
		jedis.close();
		return result;
	}

	@Override
	public long del(String key) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.del(key);
		jedis.close();
		return result;
	}

	@Override
	public long hdel(String hkey, String key) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.hdel(hkey, key);
		jedis.close();
		return result;
	}

}

到此Spring和Redis整合结束

三、代码测试

	/**
	* 单机版
	*/
	@Test
	public void testJedisSingle() {
		// 创建一个jedis的对象。
		Jedis jedis = new Jedis("122.112.241.51", 6379);
		// 调用jedis对象的方法,方法名称和redis的命令一致。
		jedis.set("key1", "jedis test");
		String string = jedis.get("key1");
		System.out.println(string);
		// 关闭jedis。
		jedis.close();
	}

	/**
	 * 使用连接池
	 */
	@Test
	public void testJedisPool() {
		// 创建jedis连接池
		JedisPool pool = new JedisPool("192.168.1.188", 6379);
		// 从连接池中获得Jedis对象
		Jedis jedis = pool.getResource();
		String string = jedis.get("key1");
		System.out.println(string);
		// 关闭jedis对象
		jedis.close();
		pool.close();
	}

	/**
	 * 集群版测试
	 * <p>
	 * Title: testJedisCluster
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 */
	@Test
	public void testJedisCluster() {
		LOGGER.debug("调用redisCluster开始");
		HashSet<HostAndPort> nodes = new HashSet<>();
		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));
		LOGGER.info("创建一个JedisCluster对象");
		JedisCluster cluster = new JedisCluster(nodes);
		LOGGER.debug("设置key1的值为1000");
		cluster.set("key1", "1000");

		LOGGER.debug("从Redis中取key1的值");
		String string = cluster.get("key1");
		System.out.println(string);
		cluster.close();
		try {
			int a = 1 / 0;
		} catch (Exception e) {
			LOGGER.error("系统发送异常", e);
		}
	}

	/**
	 * 单机版测试整合spring
	 * <p>
	 * Title: testSpringJedisSingle
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 */
	@Test
	public void testSpringJedisSingle() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"classpath:spring/applicationContext-*.xml");
		JedisPool pool = (JedisPool) applicationContext.getBean("redisClient");
		Jedis jedis = pool.getResource();
		String string = jedis.get("key1");
		System.out.println(string);
		jedis.close();
		pool.close();
	}
	/**
	 * 集群版测试整合spring
	 * <p>
	 * Title: testSpringJedisSingle
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 */
	@Test
	public void testSpringJedisCluster() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
				"classpath:spring/applicationContext-*.xml");
		JedisCluster jedisCluster = (JedisCluster) applicationContext.getBean("redisClient");
		String string = jedisCluster.get("key1");
		System.out.println(string);
		jedisCluster.close();
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值