Spring Redis配置示例

我们的java代码里使用redisTemplate执行redis各类操作。
TODO redis的消息发布/订阅配置示例

1、[b]Maven 依赖配置[/b]

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.13.RELEASE</version>
</dependency>
<!-- Redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.6.3</version>
</dependency>


2、[b]applicationContext-redis.xml[/b]

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<description>Redis配置</description>

<context:property-placeholder location="classpath*:conf/conf*.properties" />

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.maxActive}" />
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="minIdle" value="${redis.minIdle}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
</bean>

<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="hostName" value="${redis.ip}" />
<!--<property name="password" value="${redis.password}" />-->
<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" />
</bean>

<bean id="serialization" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />

</beans>


3、[b]conf/conf-redis.properties[/b]

# [Redis Configuration]
redis.ip=服务器地址/域名
#redis.password=密码
redis.port=6379
redis.timeout=5000
redis.maxIdle=10
redis.minIdle=1
redis.maxActive=16
# The default maximum amount of time (in milliseconds)
redis.maxWait=5000
# testOnBorrow true 指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
redis.testOnBorrow=true
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Spring项目中使用Redis集群的配置,你可以按照以下步骤进行操作: 1. 首先,确保你已经在项目中添加了Spring Data Redis的依赖。你可以在项目的`pom.xml`文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. 在项目的配置文件(如`application.properties`或`application.yml`)中,配置Redis的连接信息。对于Redis集群,你需要提供多个节点的连接信息。示例配置如下: ```yaml spring: redis: cluster: nodes: <redis-node-1>:<port>, <redis-node-2>:<port>, <redis-node-3>:<port> ``` 其中,`<redis-node-1>:<port>`表示Redis集群中的一个节点的主机名(或IP地址)和端口号。你可以根据实际情况添加更多节点的连接信息。 3. 可选地,你可以配置一些其他的Redis相关属性,例如连接超时时间、密码等。以下是一些常用的配置示例: ```yaml spring: redis: cluster: nodes: <redis-node-1>:<port>, <redis-node-2>:<port>, <redis-node-3>:<port> timeout: 2000 # 连接超时时间(单位:毫秒) password: <redis-password> # Redis密码(如果有的话) ``` 4. 在你的代码中,你可以使用`RedisTemplate`或`StringRedisTemplate`来操作Redis集群。例如,你可以使用`StringRedisTemplate`来存储和获取键值对: ```java @Autowired private StringRedisTemplate redisTemplate; public void setValue(String key, String value) { redisTemplate.opsForValue().set(key, value); } public String getValue(String key) { return redisTemplate.opsForValue().get(key); } ``` 这样,你就可以在Spring项目中成功配置和使用Redis集群了。希望对你有所帮助!如果你还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值