springboot+redis

  

  上篇整合了DB层,现在开始整合缓存层,使用redis。

  springboot驱动注解,使用spring注入JedisPool便可封装自己的redis工具类。

package hello.configuration;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
@PropertySource(value = "classpath:/redis.properties")
@EnableCaching
public class RedisCacheConfiguration extends CachingConfigurerSupport {
    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private int port;

    @Value("${spring.redis.timeout}")
    private int timeout;

    @Value("${spring.redis.pool.max-idle}")
    private int maxIdle;

    @Value("${spring.redis.pool.max-wait}")
    private long maxWaitMillis;

    @Bean
    public JedisPool redisPoolFactory() {
        System.out.println("JedisPool注入成功!!");
        System.out.println("redis地址:" + host + ":" + port);
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, host, port);
        return jedisPool;
    }
}

  redis.properties配置文件,为了清晰区分资源文件。

spring.redis.database=0
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.pool.max-active=8
spring.redis.pool.max-idle=8
spring.redis.pool.max-wait=-1
spring.redis.pool.min-idle=0
spring.redis.timeout=0
时间: 2024-12-07 14:53:57

springboot+redis的相关文章

Springboot + redis + 注解 + 拦截器来实现接口幂等性校验

Springboot + redis + 注解 + 拦截器来实现接口幂等性校验 1. SpringBoot 整合篇 2. 手写一套迷你版HTTP服务器 3. 记住:永远不要在MySQL中使用UTF-8 4. Springboot启动原理解析 一.概念 幂等性, 通俗的说就是一个接口, 多次发起同一个请求, 必须保证操作只能执行一次比如: 订单接口, 不能多次创建订单 支付接口, 重复支付同一笔订单只能扣一次钱 支付宝回调接口, 可能会多次回调, 必须处理重复回调 普通表单提交接口, 因为网络超时

springboot+redis实现session共享

1.场景描述 因项目访问压力有点大,需要做负载均衡,但是登录使用的是公司统一提供的单点登录系统,需要做session共享,否则假如在A机器登录成功,在B机器上操作就会存在用户未登录情况. 2. 解决方案 因项目是springboot项目,采用Springboot+Springsession+Redis来实现session共享. 2.1 pom.xml文件 <dependency> <groupId>org.springframework.boot</groupId> &

springboot redis 缓存对象

只要加入spring-boot-starter-data-redis , springboot 会自动识别并使用redis作为缓存容器,使用方式如下 gradle加入依赖 compile("org.springframework.boot:spring-boot-starter-data-redis:${springBootVersion}") redis configuration 中启用缓存 @Configuration @EnableCaching public class Re

Springboot+redis 整合

运行环境: JDK1.7. SpringBoot1.4.7 redis3.0.4 1.生成Springboot项目,分别添加web,redis依赖,具体的maven依赖如下 1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-data-redis</artifactId> 4 </dependency&g

springboot redis简单结合

参考: https://www.cnblogs.com/ityouknow/p/5748830.htmlhttp://blog.csdn.net/i_vic/article/details/53081241https://www.cnblogs.com/gdpuzxs/p/7222309.htmlhttp://www.runoob.com/redis/redis-keys.html #redis命令 springboot和redis结合,这里只是单机版的配置,如果需要配置集群,可以在上面几个博客

springboot redis(单机/集群)

前言 前面redis弄了那么多, 就是为了在项目中使用. 那这里, 就分别来看一下, 单机版和集群版在springboot中的使用吧.  在里面, 我会同时贴出Jedis版, 作为比较. 单机版 1. pom.xml  <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis --> <dependency> <groupId>org.s

springboot+redis+nginx+分布式session

第一步 启动一个redis 我这里用的是windows版本的redis,进入磁盘后启动redis 启动命令:redis-server.exe redis.windows.conf 启动nginx 主要是配置一下 worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; upstream

springboot+redis+Interceptor+自定义annotation实现接口自动幂等

前言: 在实际的开发项目中,一个对外暴露的接口往往会面临很多次请求,我们来解释一下幂等的概念:任意多次执行所产生的影响均与一次执行的影响相同.按照这个含义,最终的含义就是 对数据库的影响只能是一次性的,不能重复处理.如何保证其幂等性,通常有以下手段: 1:数据库建立唯一性索引,可以保证最终插入数据库的只有一条数据 2:token机制,每次接口请求前先获取一个token,然后再下次请求的时候在请求的header体中加上这个token,后台进行验证,如果验证通过删除token,下次请求再次判断tok

springboot+redis实现分布式锁

参考 SpringBoot实现Redis分布式锁 https://www.jianshu.com/p/750ac97eb29e 实现原理 加锁解锁 执行逻辑之前,加锁 执行逻辑之后,删除锁 加锁和删除锁必须是同一个对象的行为. 获取锁删除锁 使用setnx,保证只有一个对象可以设置锁成功,只有一个对象可以拿到锁. (删除锁的时候,必须保证是自己创建的锁,需要验证value. 上面参考文章中,每次设置加锁的时候,设置token,删除锁的时候,对比token) 设置过期时间 设置锁的过期时间,为什么