Redis 学习笔记(三) 【原创】_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Redis 学习笔记(三) 【原创】

Redis 学习笔记(三) 【原创】

 2018/1/8 21:16:46  zhenggm  程序员俱乐部  我要评论(0)
  • 摘要:在Redis学习笔记(二)中,已经介绍了sentinel+redis的高可用方案部署,本文基于spring-data-redis访问sentinel+redis服务。1.pom.xml<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0
  • 标签:笔记 原创 学习 学习笔记
在Redis 学习笔记(二)中,已经介绍了sentinel+redis的高可用方案部署,本文基于spring-data-redis访问sentinel+redis服务。
1. pom.xml
class="xml" name="code">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>cn.gov.zjport.demo.redis</groupId>
  <artifactId>redis-sentinel</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>redis-sentinel</name>
  <url>http://maven.apache.org</url>

  <properties>
    <springframework.version>4.3.12.RELEASE</springframework.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>${springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>${springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>${springframework.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>1.8.9.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.9.0</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>redis-sentinel</finalName>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>


2. spring-redis-sentinel.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">
    
    <context:annotation-config />
	<context:component-scan base-package="cn.gov.zjport.demo" />
    <context:property-placeholder location="classpath:redis.properties" />
	
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    	<!-- 说明一个pool可以有多少个Jedis实例 -->
  		<property name="maxTotal" value="${zjport.jedis.cache.pool.maxTotal}" />
        <!-- 最大空闲连接数 -->
        <property name="maxIdle" value="${zjport.jedis.cache.pool.maxIdle}" />
        <!-- 最小空闲连接数 -->
        <property name="minIdle" value="${zjport.jedis.cache.pool.minIdle}" />
        <!--从连接池中获取对象最大等待wait时间-->
  		<property name="maxWaitMillis" value="${zjport.jedis.cache.pool.maxWaitMillis}" />
        <!-- 在获取连接的时候检查有效性 -->
        <property name="testOnBorrow" value="${zjport.jedis.cache.pool.testOnBorrow}" />
        <!-- 是否开启空闲检测 -->
        <property name="testWhileIdle" value="${zjport.jedis.cache.pool.testWhileIdle}" />
        <!-- 一次最多evict的pool里的jedis实例个数 -->
        <property name="numTestsPerEvictionRun" value="${zjport.jedis.cache.pool.numTestsPerEvictionRun}" />
        <!-- test idle 线程的时间间隔 -->
        <property name="timeBetweenEvictionRunsMillis" value="${zjport.jedis.cache.pool.timeBetweenEvictionRunsMillis}" />
    </bean>

    <bean id="sentinelConfig"
        class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
        <!-- redis主节点名称 -->
        <constructor-arg name="master" value="${zjport.redis.masterName}" />
        <!-- sentinel的ip和端口列表 -->
        <constructor-arg name="sentinelHostAndPorts">
            <set>
                <value>${zjport.redis.sentinel1}</value>
                <value>${zjport.redis.sentinel2}</value>
                <value>${zjport.redis.sentinel3}</value>
            </set>
        </constructor-arg>
    </bean>

    <bean id="jedisConnectionFactory"
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <constructor-arg ref="sentinelConfig" />
        <constructor-arg ref="jedisPoolConfig" />
        <!-- redis密码 -->
  		<property name="password" value="${zjport.redis.password}" />
  		<!-- redis db -->
  		<property name="database" value="${zjport.redis.database}" />
    </bean>
    
    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory" />
    </bean>

    <bean id="redisCache" class="cn.gov.zjport.demo.redis.RedisCache">
        <property name="redisTemplate" ref="redisTemplate"/>
    </bean>
</beans>


3.redis.properties
#\u8bf4\u660e\u4e00\u4e2apool\u53ef\u4ee5\u6709\u591a\u5c11\u4e2aJedis\u5b9e\u4f8b
zjport.jedis.cache.pool.maxTotal=100
#\u6700\u5927\u7a7a\u95f2\u6570
zjport.jedis.cache.pool.maxIdle=50
#\u6700\u5c0f\u7a7a\u95f2\u6570
zjport.jedis.cache.pool.minIdle=20
#\u4ece\u8fde\u63a5\u6c60\u4e2d\u83b7\u53d6\u5bf9\u8c61\u6700\u5927\u7b49\u5f85wait\u65f6\u95f4
zjport.jedis.cache.pool.maxWaitMillis=10000
#\u83b7\u5f97\u4e00\u4e2ajedis\u5b9e\u4f8b\u7684\u65f6\u5019\u662f\u5426\u68c0\u67e5\u8fde\u63a5\u53ef\u7528\u6027
zjport.jedis.cache.pool.testOnBorrow=true
#\u662F\u5426\u5F00\u542F\u7A7A\u95F2\u68C0\u6D4B
zjport.jedis.cache.pool.testWhileIdle=true
#\u4E00\u6B21\u6700\u591Aevict\u7684pool\u91CC\u7684jedis\u5B9E\u4F8B\u4E2A\u6570
zjport.jedis.cache.pool.numTestsPerEvictionRun=10
#test idle \u7EBF\u7A0B\u7684\u65F6\u95F4\u95F4\u9694 
zjport.jedis.cache.pool.timeBetweenEvictionRunsMillis=60000
#redis\u4E3B\u8282\u70B9\u540D\u79F0
zjport.redis.masterName=mymaster
#redis\u5bc6\u7801 
zjport.redis.password=123456
#redis db
zjport.redis.database=15
#sentinel\u7684ip\u548C\u7AEF\u53E3\u5217\u8868
zjport.redis.sentinel1=192.168.3.9:26379
zjport.redis.sentinel2=192.168.3.10:26379
zjport.redis.sentinel3=192.168.3.18:26379



4.RedisCache.java
package cn.gov.zjport.demo.redis;

import javax.annotation.Resource;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

@Component("redisCache")
public class RedisCache {
	@Resource
	private StringRedisTemplate redisTemplate;
	
	public String get(String key){
        return redisTemplate.boundValueOps(key).get();
    }
	
	public void set(String key, String value){
        redisTemplate.boundValueOps(key).set(value);
	}

	public StringRedisTemplate getRedisTemplate() {
		return redisTemplate;
	}

	public void setRedisTemplate(StringRedisTemplate redisTemplate) {
		this.redisTemplate = redisTemplate;
	}
}


5. Launch.java
package cn.gov.zjport.demo.redis;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Hello world!
 *
 */
public class Launch 
{
	public static void main(String[] args) {
        ApplicationContext ac =  new ClassPathXmlApplicationContext("classpath:/config/spring-redis-sentinel.xml");
        RedisCache redisCache = (RedisCache) ac.getBean("redisCache");
        redisCache.set("USER_ID", "zhenggm");
        String userId = redisCache.get("USER_ID");
        System.out.printf(userId);
    }
}
上一篇: 百度回应App偷窥隐私事件:没有能力“监听电话” 下一篇: 没有下一篇了!
发表评论
用户名: 匿名