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

猜你喜欢

转载自www.cnblogs.com/idefault/p/12625007.html