springboot1.x集成redis单机版和集群版

看了网上好多springboot中使用redis配置麻烦,实际上springboot开箱即用为我们做好了准备,简单方便又能扩展。
单机版:
1.pom.xml添加依赖

		<!-- springboot web 依赖的jar -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.0.RELEASE</version>
        </dependency>
        <!--redis 这个依赖了jedis,spring-data-redis等 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

2.application.yml 文件添加

server:
  port: 8110
spring:
  profiles:
    active: single
  application:
    name: service-redis

3.application-single.yml 此处为了区分单机和集群环境

#单机版redis
spring:
  redis:
    host: 127.0.0.1
    port: 6379
    #默认0
    database: 0
    #密码
    password:
    pool:
      #连接池最大连接数,使用负值表示没有限制
      max-active: 100
      #连接池中的最大空闲连接
      max-idle: 5
      #连接池最大阻塞等待时间(使用负值表示没有限制) 毫秒
      max-wait: 2000
      #连接池中的最小空闲连接
      min-idle: 1
    #连接超时时间(毫秒)
    timeout: 2000

4.在需要使用redis的地方直接注入RedisTemplate

@RestController
public class UserController {
	
    @Resource
    private RedisTemplate redisTemplate;

    @RequestMapping("/getUser")
    public String getUser(){
        return (String)redisTemplate.opsForValue().get("name");
    }

    @RequestMapping("/setUser")
    public String setUser(String name){
        redisTemplate.opsForValue().set("name",name);
        return "name:"+name;
    }
}

集群版:
1.pom.xml依赖同单机版一样
2.application.yml 文件将环境改为cluster集群

server:
  port: 8110
spring:
  profiles:
    active: cluster
  application:
    name: service-redis

3.application-cluster.yml 集群环境配置信息

spring:
  redis:
    #host:
    #port:
    #密码
    #password:
    pool:
      #连接池最大连接数,使用负值表示没有限制
      max-active: 100
      #连接池中的最大空闲连接
      max-idle: 5
      #连接池最大阻塞等待时间(使用负值表示没有限制) 毫秒
      max-wait: 2000
      #连接池中的最小空闲连接
      min-idle: 1
    #连接超时时间(毫秒)
    timeout: 2000
    cluster:
      nodes: 192.168.31.11:6378,192.168.31.11:6379,192.168.31.18:6378,192.168.31.18:6379,192.168.31.25:6378,192.168.31.25:6379
      max-redirects: 3

4.在需要使用redis的地方直接注入RedisTemplate,同单机版4一样
项目目录结构
在这里插入图片描述

redistemplate之所以能直接使用是springboot自动化装配,已经为我们实例化了这个实例
到此结果。


如果有需要我们可以扩展redistemplate

/**
 * Created by lifeng on 2019/3/27.
 * 自定义redis的一些属性
 * 比如序列化和反序列化
 * 此类可以没有
 */
@Configuration
public class RedisConfig {

    /**  此处可能报错提示RedisConnectionFactory 类找不到,不影响运行*/
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate template = new RedisTemplate();
        template.setConnectionFactory(connectionFactory);
        //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值
        Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        serializer.setObjectMapper(mapper);
        template.setValueSerializer(serializer);
        //使用StringRedisSerializer来序列化和反序列化redis的key值
        template.setKeySerializer(new StringRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值