springboot 连接redis

4 篇文章 0 订阅

安装文章https://blog.csdn.net/yeluo_vinager/article/details/103680059

问题:

 

F:\soft\Redis-x64-3.2.100>redis-server.exe --service-start --Service-name RedisServer1

[14752] 21 Jan 14:30:59.337 # HandleServiceCommands: system error caught. error code=1060, message = OpenService failed: unknown error

 

解决

没有启动

redis-server.exe --service-install redis.windows.conf --service-name redis1

redis-server.exe --service-start --Service-name redis;启动

springboot 使用

依赖


 

<!--引入redis依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>2.4.0</version>
</dependency>

Application.yml配置redis

spring:

    redis:
    host: 127.0.0.1  #配置redis的主机地址,需要修改成自己的
    database: 50
    port: 6379
    password: #设置自己的密码
    jedis:
      pool:
        max-active: 50 # 如果赋值为-1,则表示不限制;如果pool已经分配了maxActive个jedis实例,则此时pool 的状态为exhausted(耗尽)
        max-idle: 20 # 连接池中的最大空闲连接,默认值也是8。
        min-idle: 5  # 连接池中的最小空闲连接,默认值也是0。
        # max-wait: 5 # 等待可用连接的最大时间,单位毫秒,默认值为-1,表示永不超时。如果超过等待时间,则直接 抛出JedisConnectionException

添加Api

package com.example.client.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;

@Component
public class RedisService {
//redis提供了RedisTemplate 和 StringRedisTemplate模板

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private StringRedisTemplate RedisTemplate;

   /*
新建一个 RedisService,注入 StringRedisTemplate,使用       stringRedisTemplate.opsForValue()
可以获取 ValueOperations<String, String> 对象,通过该对象即可读写 redis 数据库了。
*/

    public void setRediskv(String key,String value){
        ValueOperations<String, String> forValue = RedisTemplate.opsForValue();
        forValue.set(key,value);
    }

    public String getRediskv(String key){
        String s = stringRedisTemplate.opsForValue().get(key);
        return s;
    }
}

测试

package com.example.client.controller;
import com.example.client.service.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@Api(tags = "测试controller")  //接口描述
@RestController
@RequestMapping("Test")
public class TestController {

    @Resource
    private RedisService redisService;

    @ApiOperation(value = "查询子节点" ,  notes="查询树叶子节点(文件夹)")
    @GetMapping("/getMessage")
    public String getMessage() {
        return "hello, I am client.";
    }

    //测试对象类型
    @GetMapping("setRedisObj")
    public String setRedisObj(){
        redisService.setRediskv("csdn","果vinegar");
        return "success";
    }

    @GetMapping("getRedisObj")
    public String getRedisObj(){
        return redisService.getRediskv("csdn");
    }

}

 

先调用http://localhost:8081/Test/setRedisObj

然后调用http://localhost:8081/Test/getRedisObj

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值