day08 性能压测

1.压力测试

  • 使用jmeter测试吞吐量、并发量

    https://jmeter.apache.org/download_jmeter.cgi

2.性能监控

  • jvisualvm 能做什么?

    • 监控内存泄露、跟踪垃圾回收、执行时内存、cpu分析、线程分析…
  • 测试结果如何

    image-20220925082959798

3.性能优化

1.Nginx动静分离

  • 静态资源放到nginx

    image-20220925105459854

image-20220925105559856

  • 将indxe.html的所有资源路径前缀改为/static/index/

image-20220925105652158

2.使用redis缓存首页数据

  • redisson官网https://github.com/redisson/redisson/wiki/%E7%9B%AE%E5%BD%95

  • 下载redis。docker安装

  • 导入依赖

         <!--         连接池依赖-->
            <dependency>
                <groupId>org.apache.commons</groupId>
                <artifactId>commons-pool2</artifactId>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/org.redisson/redisson-spring-boot-starter -->
            <dependency>
                <groupId>org.redisson</groupId>
                <artifactId>redisson-spring-boot-starter</artifactId>
                <version>${redisson.version}</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
    
  • yml配置

    spring:
      redis:
        host:
        port: 6379
        password:
        lettuce:
          pool:
            max-active: 8  #最大连接
            max-idle: 8 #最大空闲连接
            min-idle: 0 #最小空闲连接
            max-wait: 100 #连接等待时间
    
  • redisson config配置

    @Configuration
    public class RedissonConfig {
    
        @Bean
        public RedissonClient redissonClient() {
            Config config = new Config();
            config.useSingleServer().setAddress("redis://127.0.0.1:6379");
            return Redisson.create(config);
        }
    }
    
  • 具体代码实现

    @Override
    public Map<String, List<Catelog2Vo>> getCatalogJson() {
        // 1.查询缓存
        String value = stringRedisTemplate.opsForValue().get("catalogJson");
        // 2. 没有数据查DB
        if (StrUtil.isBlank(value)) {
            // 2.1 使用分布式锁
            RLock lock = redissonClient.getLock("catalogJsonLock");
            try {
                // waitTime:等待多少秒
                lock.lock(30,TimeUnit.SECONDS);
                // 2.2 抢到分布式锁后,double一下缓存
                value = stringRedisTemplate.opsForValue().get("catalogJson");
                if (StrUtil.isNotBlank(value)) {
                    return JSONObject.parseObject(value, new TypeReference<Map<String, List<Catelog2Vo>>>() {});
                }
                Map<String, List<Catelog2Vo>> listMap = getStringListMap();
                // 2.3 查到的数据放入redis中
                stringRedisTemplate.opsForValue().set("catalogJson", JSONObject.toJSONString(listMap), 1, TimeUnit.DAYS);
                return listMap;
            } finally {
                lock.unlock();
            }
        }
        // 3. 有数据直接返回
        return JSONObject.parseObject(value, new TypeReference<Map<String, List<Catelog2Vo>>>() {});
    }
    
  • 报错 栈溢出???

    image-20220926211544797

    • 原因是redisson版本太低 我从2.15改成3.15完美解决

3.使用springcache缓存其他数据

  • 官网链接:https://docs.spring.io/spring-framework/docs/5.2.22.RELEASE/spring-framework-reference/integration.html#cache
  • 此次我们使用redis作为缓存的中间件来配合springcache使用
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
@Configuration
public class RedisCacheConfig {

    @Bean
    RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
        //key的序列化
        config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()));
        //值的序列化
        config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericFastJsonRedisSerializer()));

        CacheProperties.Redis redisProperties = cacheProperties.getRedis();
        //将配置文件中所有的配置都生效
        if (redisProperties.getTimeToLive() != null) {
            config = config.entryTtl(redisProperties.getTimeToLive());
        }
        if (redisProperties.getKeyPrefix() != null) {
            config = config.prefixKeysWith(redisProperties.getKeyPrefix());
        }
        if (!redisProperties.isCacheNullValues()) {
            config = config.disableCachingNullValues();
        }
        if (!redisProperties.isUseKeyPrefix()) {
            config = config.disableKeyPrefix();
        }
        return config;
    }
}
spring:
  cache:
    type: redis
    redis:
      time-to-live: 3600000  #设置存活时间毫秒
#      key-prefix: CACHE_    #key前缀 如果制定了前缀就用指定的前缀,如果没有就默认使用缓存的名字作为前缀
      use-key-prefix: true  # 是否使用前缀
      cache-null-values: true # 是否缓存控制 解决缓存穿透
// 启动类上加上这个注解
@EnableCaching
// 缓存方法上加上这个注解
@Cacheable(value = "category", key = "#root.methodName")
// 清除缓存方法上加上这个注解
@CacheEvict(value = "category", allEntries = true)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值