springboot、redis整合

redis安装
  • 下载:sudo wget http://download.redis.io/releases/redis-3.2.6.tar.gz
    • 解压 sudo tar -zxvf redis-3.2.6.tar.gz
    • 安装gcc sudo apt-get install gcc
    • 编译、安装

      sudo make
      sudo make install
  • redis-conf copy到/etc/redis目录,修改:

    requirepass 123456
    #bind 127.0.0.1 注释该行

    • 启动redis服务
      ./redis-server /etc/redis/redis.conf
springboot中整合
  • redis-client.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
        <aop:aspectj-autoproxy/>
        <bean id="redisConfig" class="redis.clients.jedis.JedisPoolConfig">
            <property name="maxIdle" value="6"></property>
            <property name="minEvictableIdleTimeMillis" value="300000"></property>
            <property name="numTestsPerEvictionRun" value="3"></property>
            <property name="timeBetweenEvictionRunsMillis" value="60000"></property>
        </bean>
        <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
            <property name="poolConfig" ref="redisConfig"></property>
            <property name="hostName" value="${redis.host}"></property>
            <property name="port" value="${redis.port}"></property>
            <property name="password" value="${redis.password}"></property>
            <property name="timeout" value="15000"></property>
            <property name="usePool" value="true"></property>
        </bean>
        <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
            <property name="connectionFactory" ref="jedisConnectionFactory"></property>
            <property name="keySerializer">
                <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
            </property>
            <property name="valueSerializer">
                <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
            </property>
            <property name="hashKeySerializer">
                <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>
            </property>
            <property name="hashValueSerializer">
                <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"></bean>
            </property>
        </bean>
        <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool" >
            <constructor-arg index="0" ref="redisConfig" />
            <constructor-arg index="1">
                <list>
                    <bean class="redis.clients.jedis.JedisShardInfo">
                        <constructor-arg name="host" value="${redis.host}" />
                        <constructor-arg name="port" value="${redis.port}" />
                       <!-- <constructor-arg name="timeout" value="15000" />
                        <constructor-arg name="weight" value="1" />-->
                        <property name="password" value="${redis.password}"></property>
                    </bean>
                </list>
            </constructor-arg>
        </bean>
        <bean id="valueOperations" class="org.springframework.data.redis.core.DefaultValueOperations">
            <constructor-arg name="template" ref="redisTemplate"></constructor-arg>
        </bean>
        <bean id="hashOperations" class="org.springframework.data.redis.core.DefaultHashOperations">
            <constructor-arg name="template" ref="redisTemplate"></constructor-arg>
        </bean>
        <bean id="setOperations" class="org.springframework.data.redis.core.DefaultSetOperations">
            <constructor-arg name="template" ref="redisTemplate"></constructor-arg>
        </bean>
        <bean id="zSetOperations" class="org.springframework.data.redis.core.DefaultZSetOperations">
            <constructor-arg name="template" ref="redisTemplate"></constructor-arg>
        </bean>
        <!-- 用String来做序列化的redis存储 -->
        <bean id="rawRedisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
            <property name="connectionFactory" ref="jedisConnectionFactory"></property>
            <property name="KeySerializer">
                <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
            </property>
            <property name="defaultSerializer">
                <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"></bean>
            </property>
        </bean>
        <bean id="rawValueOperations" class="org.springframework.data.redis.core.DefaultValueOperations">
            <constructor-arg name="template" ref="rawRedisTemplate"></constructor-arg>
        </bean>
    </beans>
  • redis.properties

    #redis
    redis.host=192.168.31.168
    redis.port=6379
    redis.password=123456
测试
@SpringBootApplication
@ImportResource({"classpath*:redis-client.xml"})
public class LiosBootApplication extends WebMvcConfigurerAdapter {
    public static void main(String[] args) {
        SpringApplication.run(LiosBootApplication.class, args);
   }
}

@RestController
public class HelloController {
    @Autowired
    RedisClient redisClient;
    @PostMapping(value = "/lios/boot/ok",produces = {"application/json;charset=utf-8"})
    public void ok(UserParam param){
        redisClient.set("xxxxx","lios");
        String a = redisClient.get("xxxxx");
    }
}

更多文章请关注微信公众号:
https://note.youdao.com/yws/api/personal/file/WEB6c0f1a891fdea03ecc708833e0df9b3b?method=download&shareKey=45f2c5279d958cfcdc96b6c92be0ad96

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值