spring、spring-data-redis整合使用

來源:互聯網
上載者:User

標籤:開發   daemonize   tab   tar   cli   get   instance   持久化   frame   

一、Redis是一個開源的使用ANSI C語言編寫、支援網路、可基於記憶體亦可持久化的日誌型、Key-Value資料庫,並提供多種語言的API。

  從2010年3月15日起,Redis的開發工作由VMware主持。從2013年5月開始,Redis的開發由Pivotal贊助。

  目前:相信很多人都知道redis的應用和普遍性吧,因為它的靈活性,讓我們在使用的時候就更加得心應手了。

  這裡介紹spring-data-redis的基本用法,這裡需要依賴包jedis的支援

二、redis的部署

1、具體的部署配置查看:http://www.cnblogs.com/ll409546297/p/6993778.html

2、這裡需要注意幾個地方(主要是配置redis.conf)

  1)因為我是守護線程啟動,後台自啟。需要修改daemonize 為yes 

daemonize yes

  2)因為快取服務器,一般都是外部伺服器,在使用的時候需要設定密碼和注釋掉bind 127.0.0.1。不然在使用的時候會拋出pool 和 password異常

  密碼設定:requirepass 123456 (找到相關註解解除,並修改密碼)

  注釋掉bind 127.0.0.1

3、基本測試一下就可以了(可以重啟測試一下)

4、這裡redis的部署基本完成了。

三、pom.xml(導包)

   <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>

四、配置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"     xmlns:jdbc="http://www.springframework.org/schema/jdbc"      xmlns:jee="http://www.springframework.org/schema/jee"     xmlns:tx="http://www.springframework.org/schema/tx"    xmlns:aop="http://www.springframework.org/schema/aop"     xmlns:mvc="http://www.springframework.org/schema/mvc"    xmlns:util="http://www.springframework.org/schema/util"    xmlns:jpa="http://www.springframework.org/schema/data/jpa"    xsi:schemaLocation="        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">      <!-- 配置串連池 -->   <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">           <property name="maxTotal" value="10"></property>          <property name="maxIdle" value="10"></property>          <property name="minIdle" value="2"></property>          <property name="maxWaitMillis" value="15000"></property>          <property name="minEvictableIdleTimeMillis" value="300000"></property>          <property name="numTestsPerEvictionRun" value="3"></property>          <property name="timeBetweenEvictionRunsMillis" value="60000"></property>          <property name="testOnBorrow" value="true"></property>          <property name="testOnReturn" value="true"></property>          <property name="testWhileIdle" value="true"></property>         </bean>   <!-- 串連工廠 -->    <bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">           <property name="hostName" value="192.168.5.100"/>           <property name="port" value="6379"/>           <property name="password" value="123456"/>           <property name="usePool" value="true"/>           <property name="poolConfig" ref="jedisPoolConfig"/>   </bean>   <!-- 用於資料互動 -->   <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">           <property name="connectionFactory" ref="jedisConnFactory"/>   </bean></beans>

五、測試

     JedisShardInfo  jedisShardInfo = new JedisShardInfo("192.168.5.100", 6379);        jedisShardInfo.setPassword("123456");//密碼認證        Jedis jedis = new Jedis(jedisShardInfo);        jedis.set("fucking", "fucking");        System.out.println(jedis.get("fucking"));

六、使用

@Autowiredprivate RedisTemplate<String, String> redisTemplate;redisTemplate.opsForValue().set("test", "test");String obj = redisTemplate.opsForValue().get("test");System.out.println(obj);

注意:這裡注入不一定只有一個,可以是不同類型

 

spring、spring-data-redis整合使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.