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(哨兵)集群的部署,配置一主 ...
随机推荐
- NOIP爆炸记
NOIP爆炸游记 Day 0 Day 1 T1 T2 T3 Day 2 T1 T2 T3 最后 Day 0 复习模板 + 做真题 + 方 Day 1 早上吃了一片面包,就进了考场- T1 Exm??这 ...
- Mixed Far-Field and Near-Field Source Localization Based on Subarray Cross-Cumulant
基于子阵列互累积量(Cross-Cumulant)的远场和近场混合声源定位[1]. 文中采用Uniform linear array (ULA)阵列,将其分为两个互相重叠的子阵列,构建关于子阵列输出信 ...
- 2019-2020-1 20199302《Linux内核原理与分析》第八周作业
一.上课学习笔记 1.shell作用:①运行程序 ②重定向(输入/输出重定向) ③可编程(写脚本) 执行一个c程序时,如果切进另一个进程,会进入该进程而切不回原进程,所以需要为调用的进程创一个子进程. ...
- idea中properties配置文件 注释显示中文乱码问题
- [译博文]CUDA是什么
翻译自:https://blogs.nvidia.com/blog/2012/09/10/what-is-cuda-2/ 你可能并没有意识到,GPU的应用有多广泛,它不但用于视频.游戏以及科学研究中, ...
- P4936 题解
\(\text{Update}\)(2019.10.05): 递推公式推法更详细: 通项公式更新详细版: 单位矩阵的推法更加详细. 特别鸣谢 @Smallbasic 苣佬,是他教会了我推递推公式和通项 ...
- node.js切换多个版本
开言 试用场景就是我们开发项目的时候,有可能一个项目需要v10版本,另一个项目需要v8版本,遇到这种问题,我们不能卸载再重新安装对应的版本去开发,遇到这样的问题的时候,那我们就可以去用另一种方式去切换 ...
- DDL 语言
数据库模式定义语言并非程序设计语言,DDL数据库模式定义语言是SQL语言(结构化查询语言)的组成部分. SQL语言包括四种主要程序设计语言类别的语句:数据定义语言(DDL),数据操作语言(DML),数 ...
- 简书 markdown 代码高亮标记
SyntaxHighlight language language_key 1C 1c ActionScript actionscript Apache apache AppleScript a pp ...
- devops 运维平台相关知识
1.https://choerodon.io/zh/community/ (代码 https://github.com/choerodon/choerodon) 猪齿鱼 2.https://www.o ...