redis示例
1. 引入redis相关包
<!-- redis 相关包-->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.6.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
2. 配置applicationContext.xml
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.shsxt" />
<context:annotation-config/> <bean id="sentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
<constructor-arg name="master" value="mymaster" />
<constructor-arg name="sentinelHostAndPorts">
<set>
<value>172.16.54.135:26380</value>
</set>
</constructor-arg>
</bean> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<property name="password" value="123456" />
<property name="usePool" value="true" />
<constructor-arg name="sentinelConfig" ref="sentinelConfiguration" />
</bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean> </beans>
applicationContext.xml
3. 编写redis配置文件
/**
* Java 配置的操作步骤:
* 1、编写 RedisConfig 的配置类
* 2、编写测试类
* 3、添加 Spring 的配置文件
* 4、启动测试查看结果
*/
//@Configuration
public class RedisConfig { @Bean
public StringRedisTemplate redisTemplate(){
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
stringRedisTemplate.setConnectionFactory(jedisConnectionFactory());
return stringRedisTemplate;
} @Bean
public static RedisConnectionFactory jedisConnectionFactory(){
RedisSentinelConfiguration sentinelConfiguration = new RedisSentinelConfiguration().master("mymaster")
.sentinel("172.16.54.135",26380);
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(sentinelConfiguration);
jedisConnectionFactory.setPassword("123456");
return jedisConnectionFactory;
}
}
RedisConfig
4. 编写启动文件,支持xml和java配置两种方法
package com.shsxt; import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate; public class Application { /**
* 方法一、java配置
* @param args
*/
/*
public static void main(String[] args) { try {
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:applicationContext.xml"); JedisConnectionFactory connectionFactory = context.getBean(JedisConnectionFactory.class); StringRedisTemplate redisTemplate = new StringRedisTemplate(connectionFactory);
BoundValueOperations<String,String> boundValueOperations = redisTemplate.boundValueOps("abcd"); boundValueOperations.set("11111");
System.out.println("设置完毕。。。");
String result = boundValueOperations.get();
System.out.println("获取值为:" + result);
} catch (Exception e) {
e.printStackTrace();
}
}
*/ /**
* 方法二、xml 配置
* @param args
*/
public static void main(String[] args) { try {
ApplicationContext context = new FileSystemXmlApplicationContext("classpath:applicationContext.xml"); RedisTemplate redisTemplate = context.getBean(RedisTemplate.class);
BoundValueOperations<String,String> boundValueOperations = redisTemplate.boundValueOps("abde111");
boundValueOperations.set("111111");
System.out.println("设置完毕");
String result = boundValueOperations.get();
System.out.println("获取值为:"+ result); } catch (BeansException e) {
e.printStackTrace();
}
}
}
redis示例的更多相关文章
- 从JAVA客户端访问Redis示例(入门)
转自:http://blog.csdn.net/kkdelta/article/details/7217761 本文记录了安装Redis和从JAVA端访问Redis的步骤 从http://downlo ...
- JAVA入门[21]-Jedis操作redis示例
本节目标 通过JedisPool获取Jedis示例,并完成对redis 简单的Key-value读写操作. 完整代码结构如下: redis服务端 在本地运行redis-server.exe,然后在re ...
- redis示例 - 限速器,计时器
INCR INCR key 将 key 中储存的数字值增一. 如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 INCR 操作. 如果值包含错误的类型,或字符串类型的值不能表示 ...
- asp.net core 上使用redis探索(3)--redis示例demo
由于是基于.net-core平台,所以,我们最好是基于IDistributedCache接口来实现.ASP.NET-CORE下的官方redis客户端实现是基于StackExchange的.但是官方提供 ...
- Spring Data Redis示例
说明 关于Redis:一个基于键值对存储的NoSQL内存数据库,可存储复杂的数据结构,如List, Set, Hashes. 关于Spring Data Redis:简称SDR, 能让Spring应用 ...
- 用Jedis操作redis示例一,Key,value HashMap
简单的key,value形式 public class RedisDemo { public static void main(String[] args) { Jedis jedis = new J ...
- springboot redis 示例代码
pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...
- go操作redis和mysql示例
一:redis示例 使用redis的包是: github.com/garyburd/redigo/redis 1:编写第一个示例: 链接,设置,获取 redis_basic.go package ma ...
- redis sentinel 高可用(HA)方案部署,及python应用示例
redis sentinel(哨兵)高可用集群的部署方法,并通过 python 程序实例讲解如何使用 redis sentinel 简介 介绍 redis sentinel(哨兵)集群的部署,配置一主 ...
随机推荐
- 10 分钟了解 Actor 模型
http://www.moye.me/2016/08/14/akka-in-action_actor-model/ 过去十几年CPU一直遵循着摩尔定律发展,单核频率越来越快,但是最近这几年,摩尔定律已 ...
- LeetCode 931. Minimum Falling Path Sum
原题链接在这里:https://leetcode.com/problems/minimum-falling-path-sum/ 题目: Given a square array of integers ...
- 洛谷 UVA12563 Jin Ge Jin Qu hao 题解
这道题其实是一道01背包的变形题,主要思路如下:在不把剩余时间用光的前提下(剩余时间>0),尽可能的多唱歌.于是我们可以用dp[i]表示的是到当前i秒时,最多可以唱多少歌. 状态转换方程:dp[ ...
- 程序复杂程度(步长) N
我们知道计算机在运算时速度是固定的,程序运行的时间就与程序复杂程度有关.例如我们计算1-10相加 与 1-100相加,后者就要比前者多10倍时间. 例1 找出n个数中最大的一个 max= n[0] ...
- dinoql 使用nodejs 运行的几个问题
dinoql 是一个很不错的javascript objects 查询处理方案,基于graphql,当前版本有点问题 node 环境运行 ReferenceError: window is not d ...
- PKUWC2019 Round 2 没去祭
因为今年有两场 PKUWC,所以叫 PKUWC2019 Round 2. 因为一些沙雕原因,今年去不了了. Day 0 一如既往,没有集训就去上数学课,今天讲几何变换,一如既往的只会说"少女 ...
- 洛谷P2877 [USACO07NOV]防晒霜Sunscreen
题目 此题有多种贪心方法. 首先简化题意: 有几个在数轴上的区间,和几个在数轴上确定的位置的点,问用这些数目的点,最多能满足多少个区间里有点. 注意:此题跟区间选点问题不一样,每个点只能满足一个区间, ...
- 什么是 Kafka Rebalance 以及关于 Rebalance Kafka-Python 社区客户端应该关注的地方
什么是 Rebalance? Rebalance 为什么会发生?Rebalance 的情况下 consumer 是否还能正确消费消息呢? 记得之前在一段时间密集面试的时候总会问候选人这些问题. 重平衡 ...
- BZOJ1034 ZJOJ2008 泡泡堂BNB
BZOJ1034 ZJOJ2008 泡泡堂BNB Description 第XXXX届NOI期间,为了加强各省选手之间的交流,组委会决定组织一场省际电子竞技大赛,每一个省的代表 队由n名选手组成,比赛 ...
- Go内置函数
append go语言中的append函数作用是在切片变量的后面追加新的数据,然后返回新的切片变量 func append(slice []Type, elems ...Type) []type sl ...