redis +Spingboot 实现dmeo

首先需要下载redis https://redis.io/

这里我只是简单搭建,在本机window上弄的,就不发文件了

首先搭建好springboot,然后开始搭建redis(这里使用最简单的redis搭建方式)

第一步:引入redis 官方指定的jedis jar包


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

第二步:在application.properties文件下加入redis配置属性

jedis.host = 127.0.0.1
jedis.port = 6379
jedis.maxTotal = 101
jedis.maxIdle = 10
jedis.maxWaitMillis = 100000

第三步:添加jedis属性类,配置类,客户端类

jedis属性类


   
   
  1. package com.java.Olym.redis;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. @ConfigurationProperties(prefix = JedisProperties.JEDIS_PREFIX)
  4. public class JedisProperties {
  5. public static final String JEDIS_PREFIX = "jedis";
  6. private String host;
  7. private int port;
  8. private int maxTotal;
  9. private int maxIdle;
  10. private int maxWaitMillis;
  11. public String getHost() {
  12. return host;
  13. }
  14. public void setHost(String host) {
  15. this.host = host;
  16. }
  17. public int getPort() {
  18. return port;
  19. }
  20. public void setPort(int port) {
  21. this.port = port;
  22. }
  23. public int getMaxTotal() {
  24. return maxTotal;
  25. }
  26. public void setMaxTotal(int maxTotal) {
  27. this.maxTotal = maxTotal;
  28. }
  29. public int getMaxIdle() {
  30. return maxIdle;
  31. }
  32. public void setMaxIdle(int maxIdle) {
  33. this.maxIdle = maxIdle;
  34. }
  35. public int getMaxWaitMillis() {
  36. return maxWaitMillis;
  37. }
  38. public void setMaxWaitMillis(int maxWaitMillis) {
  39. this.maxWaitMillis = maxWaitMillis;
  40. }
  41. }

jedis自动配置类


   
   
  1. package com.java.Olym.redis;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.beans.factory.annotation.Qualifier;
  4. import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
  5. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
  6. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.context.annotation.Configuration;
  9. import redis.clients.jedis.JedisPool;
  10. import redis.clients.jedis.JedisPoolConfig;
  11. @Configuration
  12. @EnableConfigurationProperties(JedisProperties.class) //开启属性注入,通过@autowired注入
  13. @ConditionalOnClass(RedisClient.class) //判断这个类是否在classpath中存在
  14. public class JedisAutoConfiguration {
  15. @Autowired
  16. private JedisProperties prop;
  17. @Bean(name= "jedisPool")
  18. public JedisPool jedisPool() {
  19. JedisPoolConfig config = new JedisPoolConfig();
  20. config.setMaxTotal(prop.getMaxTotal());
  21. config.setMaxIdle(prop.getMaxIdle());
  22. config.setMaxWaitMillis(prop.getMaxWaitMillis());
  23. return new JedisPool(config, prop.getHost(), prop.getPort());
  24. }
  25. @Bean
  26. @ConditionalOnMissingBean(RedisClient.class) //容器中如果没有RedisClient这个类,那么自动配置这个RedisClient
  27. public RedisClient redisClient(@Qualifier("jedisPool")JedisPool pool) {
  28. RedisClient redisClient = new RedisClient();
  29. redisClient.setJedisPool(pool);
  30. return redisClient;
  31. }
  32. }

jedis客户端类


   
   
  1. package com.java.Olym.redis;
  2. import redis.clients.jedis.Jedis;
  3. import redis.clients.jedis.JedisPool;
  4. public class RedisClient {
  5. private JedisPool jedisPool;
  6. public void set(String key, String value) throws Exception {
  7. Jedis jedis = null;
  8. try {
  9. jedis = jedisPool.getResource();
  10. jedis.set(key, value);
  11. } finally {
  12. //返还到连接池
  13. if(jedis!= null){
  14. jedis.close();
  15. }
  16. }
  17. }
  18. public String get(String key) throws Exception {
  19. Jedis jedis = null;
  20. try {
  21. jedis = jedisPool.getResource();
  22. return jedis.get(key);
  23. } finally {
  24. //返还到连接池
  25. if(jedis!= null){
  26. jedis.close();
  27. }
  28. }
  29. }
  30. public JedisPool getJedisPool() {
  31. return jedisPool;
  32. }
  33. public void setJedisPool(JedisPool jedisPool) {
  34. this.jedisPool = jedisPool;
  35. }
  36. }

前端jsp代码


   
   
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding= "UTF-8"%>
  3. <%@ include file="./res.jsp"%>
  4. <html>
  5. <head>
  6. <script>
  7. function setter(){
  8. var key = $( 'input[name="key"]').val();
  9. var value = $( 'input[name="value"]').val();
  10. $.getJSON( '<%=ctx%>/redis/set',{ "key":key, "value":value}, function(result1){
  11. if(result1!= null){
  12. alert(result1)
  13. }
  14. });
  15. }
  16. function getter(){
  17. var key = $( 'input[name="key"]').val();
  18. $.getJSON( "<%=ctx%>/redis/get",{ "key":key}, function(result1){
  19. if(result1!= null){
  20. $( 'input[name="value"]').val(result1);
  21. }
  22. });
  23. }
  24. </script>
  25. </head>
  26. <body>
  27. <div>
  28. <span>输入key <input type="text" name="key" > </span> <p>
  29. <span>输入value <input type="text" name="value" > </span> <p>
  30. <input type="button" value="提交" id="setter" onclick="setter();"> <p>
  31. <input type="button" value="拿取" id="getter" onclick="getter();"> <p>
  32. </div>
  33. </body>
  34. </html>

后端controller


   
   
  1. package com.java.Olym.explore.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.ResponseBody;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import com.java.Olym.redis.RedisClient;
  7. @RequestMapping( "redis")
  8. @RestController
  9. public class RedisController {
  10. @Autowired
  11. private RedisClient redisClient;
  12. @RequestMapping( "set")
  13. public
  14. String set (String key,String value) throws Exception{
  15. redisClient.set(key, value);
  16. return "yes";
  17. }
  18. @RequestMapping( "get")
  19. public
  20. String get (String key) throws Exception{
  21. return redisClient.get(key);
  22. }
  23. }

开始测试


点击提交,然后再redis-cli.exe 拿取数据


至此,成功

在其中也遇到问题:jedis异常:NoSuchElementException: Timeout waiting for idle object

然后按照网上的方法,要回收redis对象,然而还是不行,最后根据debug发现jedisproperties类未能读取redis的配置信息,后来改了写法,就可行了。

,注释前读不到,properties前面有,望知道问题的老友,告诉我一声。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值