第一部官网下载
2️⃣传到 /opt 目录
tar -zxvf redis-4.0.2.tar.gz
mv redis-4.0.2 /usr/local/redis
cd 到 redis 目录
yum install -y gcc g++ gcc-c++ make
make -j 4 执行的时候报错了
make MALLOC=libc 就可以编译了
make install 安装
vi redis.conf
修改两个参数
第一个bind 0.0.0.0
将daemonize no改为daemonize yes
改好了保存
redis-server ./redis.conf 启动‘
ps -ef|grep redis看一下
redis-cli 启动客户端
set 1 123 你输入的默认就是字符串。
get 1
显示“123” 成功了
如果想要给redis登录密码
vi redis.conf
requirepass 123 这个就是密码,自己设置。
在客户端中 shutdown save 就关闭了服务
重启服务
NOAUTH Authentication required.
auth 密码
用java连接redis
这段代码是找的JedisPool中的源码,我们确定了timeout是毫秒。
* @param endpoint the {@code SocketAddress}
* @param timeout the timeout value to be used in milliseconds.
* @throws IOException if an error occurs during the connection
* @throws SocketTimeoutException if timeout expires before connecting
* @throws java.nio.channels.IllegalBlockingModeException
* if this socket has an associated channel,
* and the channel is in non-blocking mode
* @throws IllegalArgumentException if endpoint is null or is a
* SocketAddress subclass not supported by this socket
* @since 1.4
* @spec JSR-51
*/
public void connect(SocketAddress endpoint, int timeout) throws IOException {
Unsatisfied dependency expressed through field 'jedisPool'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'JFactory' defined in class p ath resource [com/home/miaosha/redis/RedisSer.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [redis.clients.jedis.JedisPool]: Circular reference involving containing bean 'redisSer' - consider declaring the factory method as static for independence from its containing instance. Factory method 'JFactory' threw exception; nested exception is java.lang.NullPointerException
这个是启动报错,主要是我们在实例化Service的时候,需要注入工厂,但是工厂的实例化,需要service,也就是还没有创建工厂我们就引用了
application配置redis
redis.host=192.168
redis.port=6379
redis.timeout=3
redis.password=123
redis.poolMaxTotal=10
redis.poolMaxIdle=10
redis.poolMaxWait=3
java中获取配置
package com.home.miaosha.redis;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "redis")
public class Config {
private String host;
private int port;
private int timeout;//秒
private String password;
private int poolMaxTotal;
private int poolMaxIdle;
private int poolMaxWait;//秒
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public int getTimeout() {
return timeout;
}
public void setTimeout(int timeout) {
this.timeout = timeout;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getPoolMaxTotal() {
return poolMaxTotal;
}
public void setPoolMaxTotal(int poolMaxTotal) {
this.poolMaxTotal = poolMaxTotal;
}
public int getPoolMaxIdle() {
return poolMaxIdle;
}
public void setPoolMaxIdle(int poolMaxIdle) {
this.poolMaxIdle = poolMaxIdle;
}
public int getPoolMaxWait() {
return poolMaxWait;
}
public void setPoolMaxWait(int poolMaxWait) {
this.poolMaxWait = poolMaxWait;
}
}
建立Jedis
工厂
package com.home.miaosha.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
@Service
public class RedisFac {
@Autowired
Config config;
@Bean
public JedisPool JFactory() {
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(config.getPoolMaxIdle());
jedisPoolConfig.setMaxTotal(config.getPoolMaxTotal());
jedisPoolConfig.setMaxWaitMillis(config.getPoolMaxWait());
JedisPool jedisPool = new JedisPool(jedisPoolConfig, config.getHost(), config.getPort(), config.getTimeout() * 1000, config.getPassword(), 0);
return jedisPool;
}
}
自己简单封装redis的方法
package com.home.miaosha.redis;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
@Service
public class RedisSer {
@Autowired
JedisPool jedisPool;
public <T> T get(String key, Class<T> clazz) {
Jedis je=null;
try {
je = jedisPool.getResource();
String s = je.get("1");
T t =stringToBean(s,clazz);
return t;
} finally {
returnToPool(je);
}
}
private <T> T stringToBean(String s,Class<T> clazz) {
if (s == null || s.length() <= 0||clazz==null) {
return null;
}
if (clazz == Integer.class || clazz == int.class) {
return (T) Integer.valueOf(s);
} else if (clazz == String.class) {
return (T) s;
} else if (clazz == Long.class||clazz==long.class) {
return (T) Long.valueOf(s);
}else {
return JSON.toJavaObject(JSON.parseObject(s),clazz);
}
}
public <T> boolean set(String key, T value) {
Jedis je=null;
try {
je = jedisPool.getResource();
String str = beanToString(value);
if (str == null || str.length() <= 0) {
return false;
}
je.set(key, str);
return true;
} finally {
returnToPool(je);
}
}
private <T> String beanToString(T value) {
if (value == null) {
return null;
}
Class<?> clazz = value.getClass();
if (clazz == Integer.class || clazz == int.class) {
return ""+value;
} else if (clazz == String.class) {
return (String) value;
} else if (clazz == Long.class||clazz==long.class) {
return ""+value;
}else {
return JSON.toJSONString(value);
}
}
private void returnToPool(Jedis jedis) {
if (jedis != null) {
jedis.close();
}
}
}
测试了一下,成功了。