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(哨兵)集群的部署,配置一主 ...
随机推荐
- Dubbo源码分析:ChannelHandler
背景 一个请求经过序列化二进制数据转化成对象之后.请求进入netty框架,netty框架经过业务处理把主动权转交给NettyHandler对象.NettyHandler进入ChannelHandler ...
- Git的个人总结
Git Git简史: 同生活中的许多伟大事物一样,Git 诞生于一个极富纷争大举创新的年代. Linux 内核开源项目有着为数众多的参与者. 绝大多数的 Linux 内核维护工作都花在了提交补丁和保存 ...
- java 线程安全(初级)
创建和启动Java线程 Java线程是个对象,和其他任何的Java对象一样.线程是类的实例java.lang.Thread,或该类的子类的实例.除了对象之外,java线程还可以执行代码. 创建和启动线 ...
- Greenplum 与 PostgreSQL 修改元数据(catalog)的方法 allow_system_table_mods
背景 PostgreSQL大量的信息保存在元数据中,所有的元数据都是内部维护的,例如建表.建索引.删表等操作,自动维护元数据. 在某些迫不得已的情况下才可能需要直接对元数据进行修改. 默认情况下,用户 ...
- 开发框架Express
一.使用原因 由于nodejs原生的http核心模块在某些方面不足以应对开发需求,所以就需要使用框架来加快开发效率,让代码更高度统一.在nodejs中有许多web开发框架,以下介绍Express的使用 ...
- AD域服务器组策略实现文件夹重定向 保护文件安全
网络管理员或许都遇到过类似的烦恼.如系统崩溃后,原来存放在 C盘的应用程序专署数据都丢失了;或者桌面上的文件莫名其妙少了,等等.有时候,我们也千方百计提醒员工不要把文件存放在桌面上,但是他们总是不听 ...
- 【JOISC2018|2019】【20190622】minerals
题目 交互题 有\(2n\)个物品,编号为\(1-2n\),存在唯一的两两配对关系,即有\(n\)种物品 有一个盒子,初始为空,盒子上会显示里面存在的物品种类数\(C\) 你每次操作可以将一个物品从盒 ...
- 2016级移动应用开发在线测试13-Location、Sensor & Network
有趣有内涵的文章第一时间送达! 喝酒I创作I分享 生活中总有些东西值得分享 @醉翁猫咪 1. 充分利用智能手机的GPS定位信息,创造了O2O的商业模式,打通了线上与线下的信息流和商流,极大地推动了移 ...
- 算法:贪心、回溯(su)、分治、动态规划,思想简要
贪心算法: 只做出当前看来最好的选择,而不从整体考虑最优,他所作出的是局部最优解.使用该算法的前提是必须具备无后效性,即某个状态以前的选择不会影响以后的状态的选择,只与当前状态有关. 回溯算法: 本质 ...
- Noip2019暑期训练2
题目名称 骑士遍历 和谐俱乐部 农场派对 对称二叉树 存盘文件名 knight Beautiful party tree 输入文件名 knight.in Beautiful.in party.in ...