springboot - 集成redis完整代码实现

1.安装redis:省略


2.application.yaml配置:

spring:
  redis:
    host: ip地址
    port: port端口
    password: null
    pool:
      max-idle: 300
      min-idle: 10
      max-active: 600
      max-wait: 1000
    timeout: 0

application.properties配置:

################# redis 配置
## Redis数据库索引(默认为0)
spring.redis.database=0
## Redis服务器地址
spring.redis.host=IP
##Redis服务器连接端口
spring.redis.port=PORT
##Redis服务器连接密码(默认为空)
spring.redis.password=
#连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
连接池中的最大空闲连接
spring.redis.pool.max-idle=8
连接池中的最小空闲连接
spring.redis.pool.min-idle=0
#连接超时时间(毫秒)
spring.redis.timeout=0

3.maven依赖jar包

  <!--#################################redis##############################-->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.8.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.4.2</version>
        </dependency>

4.RedisConfig代码:

如果stringRedisTemplate 加载不到请调整jedis包的版本。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 如果stringRedisTemplate 加载不到请调整jedis包的版本。
 * */
@Component
public class RedisConfig {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Resource(name = "stringRedisTemplate")
    private ValueOperations<String, String> valOpers;

    public String getKey(String key) {
        return valOpers.get(key);
    }

    public void setKey(String key, String val) {
        valOpers.set(key, val);
    }


    //以下内容没有兼容到
//    @Autowired
//    private RedisTemplate<String, Object> redisTemplate;

//    @Resource(name = "redisTemplate")
//    private ValueOperations<Object, Object> valOpsObj;
//
//
//    public RedisTemplate<String, Object> getRedisTemplate() {
//        return redisTemplate;
//    }
//
//    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
//        this.redisTemplate = redisTemplate;
//    }




   /* @Bean
    public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForHash();
    }

    @Bean
    public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForValue();
    }

    @Bean
    public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForList();
    }

    @Bean
    public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForSet();
    }

    @Bean
    public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForZSet();
    }*/

}

5.RedisController调用

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by WANGBOCHENG691 on 2017/9/11.
 */
@RestController
public class RedisController {
    @Autowired
    private IRedisService redisService;

    @GetMapping("/testValue")
    public String testValue(){
       return redisService.testValue();
    }
}

6.接口

public interface IRedisService {
    public String testValue();

}

7.实现

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by WANGBOCHENG691 on 2017/9/11.
 */
@Service
public class RedisService implements IRedisService {
    @Autowired
    private RedisConfig redisConfig;



    /**
     * 接口测试
     * */
    public String testValue() {
        redisConfig.setKey("yuan", "yuanlaosiji");
        if (redisConfig.getKey("yuan") != null)
            return redisConfig.getKey("yuan");
        return "yuan";
    }
}


没有特殊说明:启动默认8080 服务,完成;这里只是支持字符串格式的存入。可以再深入研究


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Spring Boot集成Redis集群的代码示例: 1. 添加Redis依赖 ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 2. application.properties配置 ``` # Redis Cluster spring.redis.cluster.nodes=192.168.0.1:7001,192.168.0.2:7002,192.168.0.3:7003,192.168.0.4:7004,192.168.0.5:7005,192.168.0.6:7006 spring.redis.cluster.max-redirects=3 ``` 3. Redis Cluster配置类 ``` @Configuration public class RedisConfig { @Autowired private Environment env; @Bean public RedisConnectionFactory redisConnectionFactory() { RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration( Arrays.asList(env.getProperty("spring.redis.cluster.nodes").split(","))); redisClusterConfiguration.setMaxRedirects(Integer.parseInt(env.getProperty("spring.redis.cluster.max-redirects"))); return new JedisConnectionFactory(redisClusterConfiguration); } @Bean public RedisTemplate<?, ?> redisTemplate() { RedisTemplate<byte[], byte[]> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory()); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); return redisTemplate; } } ``` 4. 在代码中使用RedisTemplate ``` @Autowired private RedisTemplate<String, Object> redisTemplate; redisTemplate.opsForValue().set("key", "value"); Object value = redisTemplate.opsForValue().get("key"); ``` 以上代码示例是Spring Boot集成Redis集群的基本配置,可以根据具体需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值