java web集成redis


安裝redis忽略,對redis加密碼,增加安全性 在redis.config 里面添加requirepass passWord(密碼內容) 然后綁定本機ip (bind 127.0.0.1)如下 (未實驗)

修改 redis.conf 文件,添加密碼

requirepass passWord(密碼內容)

修改 redis.conf 文件,綁定本機ip(指定哪個ip能夠訪問)

bind 127.0.0.1

 

 

配置redis

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("登錄無法更新該用戶緩存");
            }
    }

 

bind 127.0.0.1


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM