Quantcast
Channel: CodeSection,代码区,数据库(综合) - CodeSec
Viewing all articles
Browse latest Browse all 6262

Redis单机及其集群的搭建

$
0
0
一、单机版redeis 1.安装包下载

http://download.redis.io/releases/ 下载redis的压缩包,并放在/usr/soft文件夹下

2.解压压缩包: tar -zxf redis-3.0.7.tar.gz 3.安装

这里安装redis在/usr/local/redis文件夹中

进入安装包:cd /usr/soft/redis-3.0.7,执行命令

make PREFIX=/usr/local/redis/ install

安装成功后


Redis单机及其集群的搭建

redis.conf是redis的配置文件,redis.conf在redis源码目录。注意修改port作为redis进程的端口,port默认6379。

4.在安装目录中创建目录conf,将redis源安装文件中的redis.cinf拷贝到redis的安装目录中 cp /usr/soft/redis-3.0.7/redis.conf /usr/local/redis/bin/ 5.redis启动

直接运行./redis-server 是前台启动,在关闭运行的窗口后redis也将关闭


Redis单机及其集群的搭建

为了关闭窗口后不关闭redis,需要使用后台启动

5.1修改redis.conf的daemonize的no为yes
Redis单机及其集群的搭建

使用以下命令启动

./redis-server redis.conf 6.检测redis是否运行正常 6.1使用 ps -ef|grep redis 查看进程 6.2使用redis的客户端查看
Redis单机及其集群的搭建

当输入ping命令时,返回PONG就表示连接正常

7.通过jedis连接redis单机

需要的依赖

<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.0</version>
</dependency>

可以使用以下代码测试

/**
* jedis测试 单机版
*/
@Test
public void testJedisSingle(){
Jedis jedis = new Jedis("192.168.198.130", 6379);
jedis.set("test", "this i a test");
String str = jedis.get("test");
System.out.println("---:"+str);
//关闭jedis的链接
jedis.close();
}
/**
* 使用连接池
*/
@Test
public void testJedisPool(){
JedisPool jedisPool = new JedisPool("192.168.198.130", 6379);
Jedis jedis = jedisPool.getResource();
String str = jedis.get("test");
System.out.println("---:"+str);
jedis.close();
} 8.jedis与spring整合

添加配置文件applicationContext-jedis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/aop http://www.springframework.org/schema/aop/spring-aop-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/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 读取jedis配置文件; 这里可以不用配置,-dao已经配置了扫描配置文件 -->
<!-- <context:property-placeholder location="classpath:/properties/*.properties"/> -->
<!-- 连接池配置 -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<!-- 最大连接数 -->
<property name="maxTotal" value="30" />
<!-- 最大空闲连接数 -->
<property name="maxIdle" value="10" />
<!-- 每次释放连接的最大数目 -->
<property name="numTestsPerEvictionRun" value="1024" />
<!-- 释放连接的扫描间隔(毫秒) -->
<property name="timeBetweenEvictionRunsMillis" value="30000" />
<!-- 连接最小空闲时间 -->
<property name="minEvictableIdleTimeMillis" value="1800000" />
<!-- 连接空闲多久后释放, 当空闲时间>该值 且 空闲连接>最大空闲连接数 时直接释放 -->
<property name="softMinEvictableIdleTimeMillis" value="10000" />
<!-- 获取连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1 -->
<property name="maxWaitMillis" value="1500" />
<!-- 在获取连接的时候检查有效性, 默认false -->
<property name="testOnBorrow" value="true" />
<!-- 在空闲时检查有效性, 默认false -->
<property name="testWhileIdle" value="true" />
<!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
<property name="blockWhenExhausted" value="false" />
</bean>
<!--jedis客户端单机版 -->
<bean id="redisClient" class="redis.clients.jedis.JedisPool">
<constructor-arg name="host" value="${JEDIS_HOST}"></constructor-arg>
<constructor-arg name="port" value="6379"></constructor-arg>
<constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
</bean>
<bean id="redisClientDao" class="com.taotao.rest.dao.impl.JedisClientSingle"></bean> <!--定义自己定义的jedis工具类-->
</beans>

测试

/**
* 结合spring的redis单机版测试
*/
@Test
public void testSpringSingle(){
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/spring/applicationContext-*.xml");
JedisPool jedisPool = (JedisPool)context.getBean("redisClient");
Jedis jedis = jedisPool.getResource();
jedis.set("key1", "1111");
String str = jedis.get("key1");
System.out.println("--:"+str);
jedis.close();
jedisPool.close();
}

这里可是自己封装一个工具类

package com.taotao.rest.dao.impl;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import com.taotao.rest.dao.JedisClient;
/**
* 在配置文件中注解
*
* @author Administrator
*
*/
public class JedisClientSingle implements JedisClient {
@Autowired
private JedisPool jedisPool;
@Override
public String get(String key) {
Jedis jedis = jedisPool.getResource();
String str = jedis.get(key);
jedis.close();
return str;
}
@Override
public String set(String key, String value) {
Jedis jedis = jedisPool.getResource();
String str = jedis.set(key, value);
jedis.close();
return str;
}
@Override
public String hget(String hkey, String key) {
Jedis jedis = jedisPool.getResource();
String str = jedis.hget(hkey, key);
jedis.close();
return str;
}
@Override
public Long hset(String hkey, String key, String value) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.hset(hkey, key, value);
jedis.close();
return result;
}
@Override
public long incr(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.incr(key);
jedis.close();
return result;
}
@Override
public long expire(String key, int seconds) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.expire(key, seconds);
jedis.close();
return result;
}
@Override
public long ttl(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.ttl(key);
return result;
}
@Override
public long del(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.del(key);
return result;
}
@Override
public long hdel(String hkey, String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.hdel(hkey, key);
return result;
}
} 二、集群版redis搭建

1.1.集群原理

1.1.1.redis-cluster架构图

Viewing all articles
Browse latest Browse all 6262

Trending Articles