1. 引入redis相关包

  1. <!-- redis 相关包-->
  2. <dependency>
  3. <groupId>org.springframework.data</groupId>
  4. <artifactId>spring-data-redis</artifactId>
  5. <version>1.8.6.RELEASE</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>redis.clients</groupId>
  9. <artifactId>jedis</artifactId>
  10. <version>2.9.0</version>
  11. </dependency>

2. 配置applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. 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">
  6.  
  7. <context:component-scan base-package="com.shsxt" />
  8. <context:annotation-config/>
  9.  
  10. <bean id="sentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
  11. <constructor-arg name="master" value="mymaster" />
  12. <constructor-arg name="sentinelHostAndPorts">
  13. <set>
  14. <value>172.16.54.135:26380</value>
  15. </set>
  16. </constructor-arg>
  17. </bean>
  18.  
  19. <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
  20. <property name="password" value="123456" />
  21. <property name="usePool" value="true" />
  22. <constructor-arg name="sentinelConfig" ref="sentinelConfiguration" />
  23. </bean>
  24.  
  25. <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
  26. <property name="connectionFactory" ref="connectionFactory" />
  27. </bean>
  28.  
  29. </beans>

applicationContext.xml

3. 编写redis配置文件

  1. /**
  2. * Java 配置的操作步骤:
  3. * 1、编写 RedisConfig 的配置类
  4. * 2、编写测试类
  5. * 3、添加 Spring 的配置文件
  6. * 4、启动测试查看结果
  7. */
  8. //@Configuration
  9. public class RedisConfig {
  10.  
  11. @Bean
  12. public StringRedisTemplate redisTemplate(){
  13. StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
  14. stringRedisTemplate.setConnectionFactory(jedisConnectionFactory());
  15. return stringRedisTemplate;
  16. }
  17.  
  18. @Bean
  19. public static RedisConnectionFactory jedisConnectionFactory(){
  20. RedisSentinelConfiguration sentinelConfiguration = new RedisSentinelConfiguration().master("mymaster")
  21. .sentinel("172.16.54.135",26380);
  22. JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(sentinelConfiguration);
  23. jedisConnectionFactory.setPassword("123456");
  24. return jedisConnectionFactory;
  25. }
  26. }

RedisConfig

4. 编写启动文件,支持xml和java配置两种方法

  1. package com.shsxt;
  2.  
  3. import org.springframework.beans.BeansException;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.FileSystemXmlApplicationContext;
  6. import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
  7. import org.springframework.data.redis.core.BoundValueOperations;
  8. import org.springframework.data.redis.core.RedisTemplate;
  9. import org.springframework.data.redis.core.StringRedisTemplate;
  10.  
  11. public class Application {
  12.  
  13. /**
  14. * 方法一、java配置
  15. * @param args
  16. */
  17. /*
  18. public static void main(String[] args) {
  19.  
  20. try {
  21. ApplicationContext context = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");
  22.  
  23. JedisConnectionFactory connectionFactory = context.getBean(JedisConnectionFactory.class);
  24.  
  25. StringRedisTemplate redisTemplate = new StringRedisTemplate(connectionFactory);
  26. BoundValueOperations<String,String> boundValueOperations = redisTemplate.boundValueOps("abcd");
  27.  
  28. boundValueOperations.set("11111");
  29. System.out.println("设置完毕。。。");
  30. String result = boundValueOperations.get();
  31. System.out.println("获取值为:" + result);
  32. } catch (Exception e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. */
  37.  
  38. /**
  39. * 方法二、xml 配置
  40. * @param args
  41. */
  42. public static void main(String[] args) {
  43.  
  44. try {
  45. ApplicationContext context = new FileSystemXmlApplicationContext("classpath:applicationContext.xml");
  46.  
  47. RedisTemplate redisTemplate = context.getBean(RedisTemplate.class);
  48. BoundValueOperations<String,String> boundValueOperations = redisTemplate.boundValueOps("abde111");
  49. boundValueOperations.set("111111");
  50. System.out.println("设置完毕");
  51. String result = boundValueOperations.get();
  52. System.out.println("获取值为:"+ result);
  53.  
  54. } catch (BeansException e) {
  55. e.printStackTrace();
  56. }
  57. }
  58. }

redis示例的更多相关文章

  1. 从JAVA客户端访问Redis示例(入门)

    转自:http://blog.csdn.net/kkdelta/article/details/7217761 本文记录了安装Redis和从JAVA端访问Redis的步骤 从http://downlo ...

  2. JAVA入门[21]-Jedis操作redis示例

    本节目标 通过JedisPool获取Jedis示例,并完成对redis 简单的Key-value读写操作. 完整代码结构如下: redis服务端 在本地运行redis-server.exe,然后在re ...

  3. redis示例 - 限速器,计时器

    INCR INCR key 将 key 中储存的数字值增一. 如果 key 不存在,那么 key 的值会先被初始化为 0 ,然后再执行 INCR 操作. 如果值包含错误的类型,或字符串类型的值不能表示 ...

  4. asp.net core 上使用redis探索(3)--redis示例demo

    由于是基于.net-core平台,所以,我们最好是基于IDistributedCache接口来实现.ASP.NET-CORE下的官方redis客户端实现是基于StackExchange的.但是官方提供 ...

  5. Spring Data Redis示例

    说明 关于Redis:一个基于键值对存储的NoSQL内存数据库,可存储复杂的数据结构,如List, Set, Hashes. 关于Spring Data Redis:简称SDR, 能让Spring应用 ...

  6. 用Jedis操作redis示例一,Key,value HashMap

    简单的key,value形式 public class RedisDemo { public static void main(String[] args) { Jedis jedis = new J ...

  7. springboot redis 示例代码

    pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  8. go操作redis和mysql示例

    一:redis示例 使用redis的包是: github.com/garyburd/redigo/redis 1:编写第一个示例: 链接,设置,获取 redis_basic.go package ma ...

  9. redis sentinel 高可用(HA)方案部署,及python应用示例

    redis sentinel(哨兵)高可用集群的部署方法,并通过 python 程序实例讲解如何使用 redis sentinel 简介 介绍 redis sentinel(哨兵)集群的部署,配置一主 ...

随机推荐

  1. docker更换源

    $ cd /etc/docker $ cat daemon.json { "registry-mirrors": [ "https://kfwkfulq.mirror.a ...

  2. python - 手机号正则匹配

    Python 手机号正则匹配 # -*- coding:utf-8 -*- import re def is_phone(phone): phone_pat = re.compile('^(13\d| ...

  3. junit3和junit4的区别总结

    先来看一个例子: 先用junit3来写测试用例,如下: junit3测试结果: 从上面可看出: 1.junit3必须要继承TestCase类 2.每次执行一个测试用例前,junit3执行一遍setup ...

  4. LeetCode 1219. Path with Maximum Gold

    原题链接在这里:https://leetcode.com/problems/path-with-maximum-gold/ 题目: In a gold mine grid of size m * n, ...

  5. Goexit

    package main import ( "fmt" "runtime" ) func test() { defer fmt.Println("cc ...

  6. learning java FileInputStream

    public class FileInputStreamTest { public static void main(String[] args) throws IOException { var f ...

  7. C++ EH Exception(0xe06d7363)----抛出过程

    C++ EH Exception是Windows系统VC++里对c++语言的throw的分类和定义,它的代码就是0xe06d7363.在VC++里其本质也是SEH结构化异常机制.在我们分析用户崩溃的例 ...

  8. Building a Service Mesh with HAProxy and Consul

    转自:https://www.haproxy.com/blog/building-a-service-mesh-with-haproxy-and-consul/ HashiCorp added a s ...

  9. TimescaleDB1.3 的新特性——Continuous aggregates: faster queries with automatically maintained materialized views

    One characteristic of time-series data workloads is that the dataset will grow very quickly. Without ...

  10. springcloud - bus

    在重新设置了后的bootstrap.yml和application.yml后,可以看到bus-refresh的端点请求了.在之前bootstrap也可以设定哪个端点是可见,哪个未见. 如: #actu ...