Spring+Dubbo集成Redis的两种解决方案

本文介绍了Spring+Dubbo集成Redis的两种解决方案,包括环境准备、资源配置、Spring集成Redis、Redis客户端编写以及dubbo服务化的Redis存储器。通过详细步骤展示了如何在Spring中配置Redis,创建Redis客户端,并实现Dubbo服务接口化的Redis存储器。
摘要由CSDN通过智能技术生成

Spring+Dubbo集成Redis的两种解决方案

当下我们的系统数据库压力都非常大,解决数据库的瓶颈问题势在必行,为了解决数据库的压力等需求,我们常用的是各种缓存,比如redis,本文就来简单讲解一下如何集成redis缓存存储,附github源码。


环境准备

· redis 

· IDEA 开发工具

· JDK 1.8及以上

· Maven 4.0及以上

redis的搭建网上有很多例子,这里就不细讲了,友友们可以网上浏览安装一波,下面我们就直接讲如何在spring中集成redis。

资源配置

1、spring集成redis

第一步我们先设置maven的pom.xml引用,代码如下:


<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.6.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.7.3</version>
</dependency>

设置完引用以后,就可以开始着手编写redis在spring中的配置文件了,下面直接上代码 applicationContext.xml 文件:

<!-- redis -->
<import resource="spring-redis.xml" />

spring-redis.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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">

    <!-- 加载redis参数 -->
    <context:property-placeholder location="classpath:redis.properties" />

    <!-- 自动注解 -->
    <!--<context:component-scan base-package="service.impl" />-->

    <!-- jedis 连接池配置参数: -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- 设置最大连接数 -->
        <property name="maxTotal" value="${redis.maxActive}"></property>
        <!-- 设置最大空闲数 -->
        <property name="maxIdle" value="${redis.maxIdle}"></property>
        <!-- 设置超时时间 -->
        <property name="maxWaitMillis" value="${redis.maxWait}"></property>
        <property name="testOnBorrow" value="${redis.testOnBorrow}"></property>
        <property name="testOnReturn" value="${redis.testOnReturn}"></property>
    </bean>

    <!-- jedis 连接池 连接本地redis服务 构造器注入 -->
    <bean id="pool" class="redis.clients.jedis.JedisPool">
        <constructor-arg index="0" ref="poolConfig"/>
        <constructor-arg index="1" value="${redis.host}"/>
        <constructor-arg index="2" value="${redis.port}"/>
        <constructor-arg index="3" value="${redis.maxWait}"/>
        <constructor-arg index="4" value="${redis.pass}"/>
    </bean>

    <!-- redis cache config -->
    <bean id="redisCache" class="client.RedisCache">
        <property name="pool" ref="pool"/>
    </bean<
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值