springboot + redis(单机版)

迈不过友情╰ 2022-05-16 13:23 360阅读 0赞

原文链接

摘要: 本次和大家分享的是在springboot集成使用redis,这里使用的是redis的jedis客户端,如下添加依赖 redis.clients jedis </dependen…

本次和大家分享的是在springboot集成使用redis,这里使用的是redis的jedis客户端,如下添加依赖

  1. <dependency>
  2. <groupId>redis.clients</groupId>
  3. <artifactId>jedis</artifactId>
  4. </dependency>

然后需要redis的相关配置(这里我的redis密码是空)

  1. spring:
  2. redis:
  3. single: 192.168.146.28:6378
  4. jedis:
  5. pool:
  6. max-idle: 8
  7. max-active: 8
  8. max-wait: 3000
  9. timeout: 3000
  10. password:

这是redis的一般配置,具体调优可以设置这些参数,下面在JedisConfig类中读取这些设置

  1. @Value("${spring.redis.single}")
  2. private String strSingleNode;
  3. @Value("${spring.redis.jedis.pool.max-idle}")
  4. private Integer maxIdle;
  5. @Value("${spring.redis.jedis.pool.max-active}")
  6. private Integer maxActive;
  7. @Value("${spring.redis.jedis.pool.max-wait}")
  8. private Integer maxAWait;
  9. @Value("${spring.redis.timeout}")
  10. private Integer timeout;
  11. @Value("${spring.redis.password}")
  12. private String password;

有上面的配置,就需要有代码里面设置下,这里创建一个返回JedisPoolConfig的方法

  1. /**
  2. * jedis配置
  3. *
  4. * @return
  5. */
  6. public JedisPoolConfig getJedisPoolConfig() {
  7. JedisPoolConfig config = new JedisPoolConfig();
  8. config.setMaxIdle(maxIdle); #最大空闲数
  9. config.setMaxWaitMillis(maxAWait); #最大等待时间
  10. config.setMaxTotal(maxActive); #最大连接数
  11. return config;
  12. }

有了配置,接下来就创建JedisPool,这里把JedisPool托管到spring中

  1. /**
  2. * 获取jedispool
  3. *
  4. * @return
  5. */
  6. @Bean
  7. public JedisPool getJedisPool() {
  8. JedisPoolConfig config = getJedisPoolConfig();
  9. System.out.println("strSingleNode:" + this.strSingleNode);
  10. String[] nodeArr = this.strSingleNode.split(":");
  11. JedisPool jedisPool = null;
  12. if (this.password.isEmpty()) {
  13. jedisPool = new JedisPool(
  14. config,
  15. nodeArr[0],
  16. Integer.valueOf(nodeArr[1]),
  17. this.timeout);
  18. } else {
  19. jedisPool = new JedisPool(
  20. config,
  21. nodeArr[0],
  22. Integer.valueOf(nodeArr[1]),
  23. this.timeout,
  24. this.password);
  25. }
  26. return jedisPool;
  27. }

上面简单区分了无密码的情况,到此jedis的配置和连接池就基本搭建完了,下面就是封装使用的方法,这里以set和get为例;首先创建个JedisComponent组件,代码如下

  1. /**
  2. * Created by Administrator on 2018/8/18.
  3. */
  4. @Component
  5. public class JedisComponent {
  6. @Autowired
  7. JedisPool jedisPool;
  8. public boolean set(String key, String val) {
  9. Jedis jedis = null;
  10. try {
  11. jedis = jedisPool.getResource();
  12. return jedis.set(key, val).equalsIgnoreCase("OK");
  13. } finally {
  14. if (jedis != null) {
  15. jedis.close();
  16. }
  17. }
  18. }
  19. public <T> boolean set(String key, T t) {
  20. String strJson = JacksonConvert.serilize(t);
  21. if (strJson.isEmpty()) {
  22. return false;
  23. }
  24. return this.set(key, strJson);
  25. }
  26. public String get(String key) {
  27. Jedis jedis = null;
  28. try {
  29. jedis = jedisPool.getResource();
  30. return jedis.get(key);
  31. } finally {
  32. if (jedis != null) {
  33. jedis.close();
  34. }
  35. }
  36. }
  37. public <T> T get(String key, Class<T> tClass) {
  38. String strJson = this.get(key);
  39. return JacksonConvert.deserilize(strJson, tClass);
  40. }
  41. }

有了对jedis的调用封装,我们在Controller层的测试用例如下

  1. @Autowired
  2. JedisComponent jedis;
  3. @GetMapping("/setJedis/{val}")
  4. public boolean setJedis(@PathVariable String val) {
  5. return jedis.set("token", val);
  6. }
  7. @GetMapping("/getJedis")
  8. public String getJedis() {
  9. return jedis.get("token");
  10. }

运行set和get的接口效果如
1
2

发表评论

表情:
评论列表 (有 0 条评论,360人围观)

还没有评论,来说两句吧...

相关阅读