SpringBoot踩坑日记Java程序猿进化之路

springboot & redis & red

2019-08-23  本文已影响1人  上来就是干

一、pom.xml引入相关依赖,redis starter和redis客户端jedis以及redis session管理包

 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-redis</artifactId>
     <version>2.1.7.RELEASE</version>
</dependency>
     <dependency>
     <groupId>redis.clients</groupId>
     <artifactId>jedis</artifactId>
     <version>2.9.0</version>
</dependency>
 <dependency>
     <groupId>org.springframework.session</groupId>
     <artifactId>spring-session-data-redis</artifactId>
</dependency>

二、application.yml配置文件添加redis配置

spring:
  redis:
    database: 0
    host: 127.0.0.1
    port: 6379
    password: 
    jedis:
      pool:
        max-active: 8
        max-wait: -1
        max-idle: 8
        min-idle: 0
    timeout: 100

三、添加redis配置类

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 redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration//注解为配置类
@EnableCaching//开启redis缓存
//配置session共享,设置Session失效时间
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 300)
public class JedisConfig extends CachingConfigurerSupport {//继承该类实现相关方法可以进行缓存配置,比如配置key生成策略、缓存超时时间等,非必须
    @Value("${spring.redis.host}")
    private String host;

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

    @Value("${spring.redis.password}")
    private String password;

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

    @Value("${spring.redis.jedis.pool.max-active}")
    private int maxActive;

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

    @Value("${spring.redis.jedis.pool.min-idle}")
    private int minIdle;

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

    //读取redis配置信息,生成JedisPool 实例并注入到spring容器中
    @Bean
    public JedisPool redisPoolFactory(){
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(maxIdle);
        jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
        jedisPoolConfig.setMaxTotal(maxActive);
        jedisPoolConfig.setMinIdle(minIdle);
        JedisPool jedisPool = new JedisPool(jedisPoolConfig,host,port,timeout,password);
        return  jedisPool;
    }

}

四、测试一波

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

@RestController
public class HelloWorld {
    @Autowired
    private JedisPool jedisPool;

    /**
     * 测试redis 设值取值
     * @return
     */
    @GetMapping("/setAndGetRedisValue")
    public String setAndGetRedisValue(){
        jedisPool.getResource().set("mykey","hello redis");
        return jedisPool.getResource().get("mykey");
    }

     /**
     * 测试redis缓存
     * @return
     */
    @GetMapping("/testRedisCache")
    @Cacheable(value = "redisCacheKey")//启用缓存
    public String testRedisCache(){
        System.out.println("无缓存时会打印这句话-----------------");
        return "test redis cache";
    }

  /**
     * 测试redis session共享,可用Nginx搭个web集群测试
     * @return
     */
 @GetMapping("/uuid")
    public String getUuid(HttpSession session) {
        UUID uuid = (UUID) session.getAttribute("uuid");
        if (uuid == null) {
            uuid = UUID.randomUUID();
        }
        session.setAttribute("uuid", uuid);
        return session.getId();
    }
}
上一篇 下一篇

猜你喜欢

热点阅读