java web專案配置Redis(windows環境)
阿新 • • 發佈:2019-01-27
背景介紹
- redis是一個key-value儲存系統。和Memcached類似,它支援儲存的value型別相對更多,包括string(字串)、list(連結串列)、set(集合)、zset(sorted set --有序集合)和hash(雜湊型別)。這些資料型別都支援push/pop、add/remove及取交集並集和差集及更豐富的操作,而且這些操作都是原子性的。在此基礎上,redis支援各種不同方式的排序。與memcached一樣,為了保證效率,資料都是快取在記憶體中。
- 在此介紹一下怎麼將redis配置到Java Web專案中。
安裝redis
-
環境準備:win 7環境、reids3.0
-
步驟1:下載redis解壓到任意碟符,結果如下:
-
點選redis-server.exe啟動redis服務,出現如下介面:
-
選擇redis-cli.exe啟動redis客戶端,進行測試。輸入set mykey redis 回車,輸入 get mykey 回車,輸出:“redis”。安裝完成
-
Redis 預設情況下,會繫結在 0.0.0.0:6379,這樣將會將 Redis 服務暴露到公網上,如果在沒有開啟認證的情況下,可以導致任意使用者在可以訪問目標伺服器的情況下未授權訪問 Redis 以及讀取 Redis 的資料。攻擊者在未授權訪問 Redis 的情況下可以利用 Redis 的相關方法,可以成功在 Redis 伺服器上寫入公鑰,進而可以使用對應私鑰直接登入目標伺服器。
-
所以強烈建議對redis加安全防護。
-
1.修改 redis.conf 檔案,新增密碼
requirepass passWord(密碼內容)
- 2.修改 redis.conf 檔案,繫結本機ip
bind 127.0.0.1
專案配置reids
spring專案下,需要匯入commons-pool-1.5.6.jar、commons-pool2-2.4.2.jar、jedis-2.7.3.jar、spring-data-redis-1.6.0.RELEASE.jar檔案。在spring配置檔案中加入以下元素:
<bean id="redisUtil" class="com.project.controller.Redis.RedisUtil"> <!-- 初始化類 --> <property name="addr"><value>127.0.0.1</value></property> <!-- 訪問地址,預設本地 --> <property name="port"><value>6379</value></property> <!-- 埠號 --> <property name="auth"><value>master</value></property> <property name="maxIdle"><value>200</value></property> <property name="maxActive"><value>1024</value></property> <property name="maxWait"><value>10000</value></property> <property name="timeOut"><value>10000</value></property> <property name="testOnBorrow"><value>true</value></property> </bean>
RedisUtil程式碼:
public class RedisUtil implements Serializable{
private static final long serialVersionUID = -1149678082569464779L;
//Redis伺服器IP
private static String addr;
//Redis的埠號
private static int port;
//訪問密碼
private static String auth;
//可用連線例項的最大數目,預設值為8;
//如果賦值為-1,則表示不限制;如果pool已經分配了maxActive個jedis例項,則此時pool的狀態為exhausted(耗盡)。
private static int maxActive;
//控制一個pool最多有多少個狀態為idle(空閒的)的jedis例項,預設值也是8。
private static int maxIdle;
//等待可用連線的最大時間,單位毫秒,預設值為-1,表示永不超時。如果超過等待時間,則直接丟擲JedisConnectionException;
private static int maxWait;
private static int timeOut;
//在borrow一個jedis例項時,是否提前進行validate操作;如果為true,則得到的jedis例項均是可用的;
private static boolean testOnBorrow;
public static Jedis jedis;//非切片額客戶端連線
public static JedisPool jedisPool;//非切片連線池
public static ShardedJedis shardedJedis;//切片額客戶端連線
public static ShardedJedisPool shardedJedisPool;//切片連線池
private static void initialPool()
{
// 池基本配置
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(maxActive);
config.setMaxIdle(maxIdle);
config.setMaxWaitMillis(maxWait);
config.setTestOnBorrow(testOnBorrow);
jedisPool = new JedisPool(config, addr, port);
}
private static void initialShardedPool()
{
// 池基本配置
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(maxActive);
config.setMaxIdle(maxIdle);
config.setMaxWaitMillis(maxWait);
config.setTestOnBorrow(testOnBorrow);
// slave連結
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
shards.add(new JedisShardInfo(addr, port, auth));
// 構造池
shardedJedisPool = new ShardedJedisPool(config, shards);
}
public static void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
initialPool();
initialShardedPool();
try {
shardedJedis = shardedJedisPool.getResource();
} catch (Exception e) {
System.out.println("連線shardedJedisPool失敗!");
}
try {
jedis = jedisPool.getResource();
} catch (Exception e) {
System.out.println("連線jedisPool失敗!");
}
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getAuth() {
return auth;
}
public void setAuth(String auth) {
this.auth = auth;
}
public int getMaxActive() {
return maxActive;
}
public void setMaxActive(int maxActive) {
this.maxActive = maxActive;
}
public int getMaxIdle() {
return maxIdle;
}
public void setMaxIdle(int maxIdle) {
this.maxIdle = maxIdle;
}
public int getMaxWait() {
return maxWait;
}
public void setMaxWait(int maxWait) {
this.maxWait = maxWait;
}
public int getTimeOut() {
return timeOut;
}
public void setTimeOut(int timeOut) {
this.timeOut = timeOut;
}
public boolean isTestOnBorrow() {
return testOnBorrow;
}
public void setTestOnBorrow(boolean testOnBorrow) {
this.testOnBorrow = testOnBorrow;
}
}
功能測試
//redis-server.exe啟動狀態下
public void selectReviewsByGoodsId(HttpServletRequest request){
try {
Jedis jedis = new Jedis();
Date time1 = new Date();
jedis.set("goodsName","IphoneX");
Date time2 = new Date();
System.out.println("消耗時間:"+(time2.getTime()-time1.getTime()));
System.out.println("商品列表存入redis完畢");
System.out.println(jedis.get("goodsName"));
} catch (Exception e) {
//如果快取連不上,則不處理
System.out.println("登入無法更新該使用者快取");
}
}
列印輸出:IphoneX