发表于: 2020-07-12 23:32:19

2 1974


今天完成的事情:redis和压测
明天计划的事情:由于部署到两个服务器,nginx没有成功,所以明天继续弄nginx和安装mysql.
遇到的问题:

1.Could not resolve placeholder 'redis.pool.maxTotal' in string value "${redis.pool.maxTotal}

出现的原因可能是没有引入properties的路径

还有就是同时引入两个properties,发生冲突

收获:

自定义编写redis连接池类JedisPoolWriper和redis的工具类JedisUtil

JedisPoolWriper

/**
* 强指定redisJedisPool接口构造函数,这样才能在centos成功创建jedispool
*/
public class JedisPoolWriper {
  /** Redis连接池对象 */
  private JedisPool jedisPool;

  public JedisPoolWriper(final JedisPoolConfig poolConfig, final String host,
        final int port) {
     try {
        jedisPool = new JedisPool(poolConfig, host, port);
     } catch (Exception e) {
        e.printStackTrace();
     }

  /**
   * 获取Redis连接池对象
   * @return
   */
  public JedisPool getJedisPool() {
     return jedisPool;

  /**
   * 注入Redis连接池对象
   * @param jedisPool
   */
  public void setJedisPool(JedisPool jedisPool) {
     this.jedisPool = jedisPool;
  }
}

JedisUtil工具类,自己只用到了String类型,就只写了String

public class JedisUtil {
  /**
   * 缓存生存时间
   */
  private final int expire = 60000;
  /** 操作Key的方法 */
  public Keys KEYS;
  /** 对存储结构为String类型的操作 */
  public Strings STRINGS;
  /** Redis连接池对象 */
  private JedisPool jedisPool;

  /**
   * 获取redis连接池
   *
   * @return
   */
  public JedisPool getJedisPool() {
     return jedisPool;
  }

  /**
   * 设置redis连接池
   *
   * @return
   */
  public void setJedisPool(JedisPoolWriper jedisPoolWriper) {
     this.jedisPool = jedisPoolWriper.getJedisPool();
  }

  /**
   * jedis连接池中获取获取jedis对象
   *
   * @return
   */
  public Jedis getJedis() {
     return jedisPool.getResource();
  }

  /**
   * 设置过期时间
   *
   * @author xiangze
   * @param key
   * @param seconds
   */
  public void expire(String key, int seconds) {
     if (seconds <= 0) {
        return;
     }
     Jedis jedis = getJedis();
     jedis.expire(key, seconds);
     jedis.close();
  }

  /**
   * 设置默认过期时间
   *
   * @author xiangze
   * @param key
   */
  public void expire(String key) {
     expire(key, expire);
  }

  // *******************************************Keys*******************************************//
  public class Keys {
     public Keys(JedisUtil jedisUtil) {

     }
     /**
      * 清空所有key
      */
     public String flushAll() {
        Jedis jedis = getJedis();
        String stata = jedis.flushAll();
        jedis.close();
        return stata;
     }

     /**
      * 删除keys对应的记录,可以是多个key
      *
      * @param  keys
      * @return 删除的记录数
      */
     public long del(String... keys) {
        Jedis jedis = getJedis();
        long count = jedis.del(keys);
        jedis.close();
        return count;
     }

     /**
      * 删除keys对应的记录,可以是多个key
      *
      * @param keys
      * @return 删除的记录数
      */
     public long del(byte[]... keys) {
        Jedis jedis = getJedis();
        long count = jedis.del(keys);
        jedis.close();
        return count;
     }

     /**
      * 判断key是否存在
      *
      * @param key
      * @return boolean
      */
     public boolean exists(String key) {
        // ShardedJedis sjedis = getShardedJedis();
        Jedis sjedis = getJedis();
        boolean exis = sjedis.exists(key);
        sjedis.close();
        return exis;
     }

     /**
      * 查找所有匹配给定的模式的键
      *
      * @param pattern
      *            key的表达式,*表示多个,?表示一个
      */
     public Set<String> keys(String pattern) {
        Jedis jedis = getJedis();
        Set<String> set = jedis.keys(pattern);
        jedis.close();
        return set;
     }
  }
  // *******************************************Strings*******************************************//
  public class Strings {
     public Strings(JedisUtil jedisUtil) {

     }
     /**
      * 根据key获取记录
      *
      * @param key
      * @return
      */
     public String get(String key) {
        // ShardedJedis sjedis = getShardedJedis();
        Jedis sjedis = getJedis();
        String value = sjedis.get(key);
        sjedis.close();
        return value;
     }

     /**
      * 添加记录,如果记录已存在将覆盖原有的value
      *
      * @param String
      *            key
      * @param String
      *            value
      * @return 状态码
      */
     public String set(String key, String value) {
        return set(SafeEncoder.encode(key), SafeEncoder.encode(value));
     }

     /**
      * 添加记录,如果记录已存在将覆盖原有的value
      *
      * @param String
      *            key
      * @param String
      *            value
      * @return 状态码
      */
     public String set(String key, byte[] value) {
        return set(SafeEncoder.encode(key), value);
     }

     /**
      * 添加记录,如果记录已存在将覆盖原有的value
      *
      * @param byte[]
      *            key
      * @param byte[]
      *            value
      * @return 状态码
      */
     public String set(byte[] key, byte[] value) {
        Jedis jedis = getJedis();
        String status = jedis.set(key, value);
        jedis.close();
        return status;
     }
  }

}

修改自己的service,查询学生和新建数据-用户注册

学生查询

@Service
public class StudentServiceImpl implements StudentService {
   @Resource
   private StudentMapper studentMapper;
   @Autowired
   private JedisUtil.Keys jedisKeys;
   @Autowired
   private JedisUtil.Strings jedisStrings;
   //常量key
   private static String STULIST =  "stuList";
   private Logger logger = LoggerFactory.getLogger(StudentServiceImpl.class);
   @Override
   public List<Student> selectAll() {
       String key = STULIST;
       List<Student> stuList = null;
       //jackson转换,String和对象的转换
       ObjectMapper mapper = new ObjectMapper();
       //如果缓存没有key,则查询数据库,把值放到缓存中,最后返回数据
       //否则key存在,直接从缓存中取数据
       if(!jedisKeys.exists(key)){
           stuList = studentMapper.selectAll();
           String jsonString = null;
           try {
               //list转换为String
               jsonString = mapper.writeValueAsString(stuList);
           } catch (JsonProcessingException e) {
               e.printStackTrace();
               logger.error(e.getMessage());
           }
               jedisStrings.set(key,jsonString);
       }else{
           String jsonString = jedisStrings.get(key);
           //String类型转换为java对象
           JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class,Student.class);
           try {
               stuList = mapper.readValue(jsonString,javaType);
           } catch (JsonProcessingException e) {
               e.printStackTrace();
               logger.error(e.getMessage());
           }
       }
       return stuList;
   }

新建数据-注册用户

@Service
public class UserServiceImpl implements UserService {
   @Resource
   private UserMapper userMapper;
   @Autowired
   private JedisUtil.Keys jedisKey;
   @Autowired
   JedisUtil.Strings jedisString;
   private Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
   @Override
   public User findOne(User user) {
       return userMapper.findOne(user);
   }

   @Override
   public int insert(User user) {
       user.setNumber(9527);
       String key;
       ObjectMapper mapper = new ObjectMapper();
       log.info("新建数据写入缓存中");
       key = user.getName();
       String jsonString = null;
       if(!jedisKey.exists(key)){
           //java对象转换为String类型
              try {
               jsonString = mapper.writeValueAsString(user);
           } catch (JsonProcessingException e) {
               e.printStackTrace();
               log.error(e.getMessage());
           }
           jedisString.set(key,jsonString);
       }
       return userMapper.insert(user);
   }
}


压力测试

1.不加缓存

压测JSP,查询数据

压测Json接口,新建数据-注册用户

2.memcached

压测JSP页面

压测新建数据,返回JSON数据

3.redis

 压测jsp页面

压测新建数据,返回JSON数据

4.缓存+负载均衡

明天做

5.去掉数据库网络影响,不用远程数据库,直接用远程服务器Mysql

明天做



返回列表 返回列表
评论

    分享到