• SpringBoot 之 接口暴露(mysql)+性能调优


    一、生成

    springboot选择
      spring-boot-starter-parent -> 2.2.11.RELEASE
      java -> 1.8
      mybatis-spring-boot-starter -> 2.1.4
      mysql-connector-java -> 5.1.38

    dependencies选择
      spring web
      Mysql driver
      Mybatis Framework

    二、目录

    三、代码

    application.yml

    services/SaleOrderService

    package com.njbdqn.sales.services;
    
    import com.njbdqn.sales.dao.SaleOrderDAO;
    import com.njbdqn.sales.entity.SalesOrder;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    @Service
    public class SaleOrderService {
        @Autowired
        private SaleOrderDAO sodao;
    
        public List<SalesOrder> mypage(int page,int size){
            Map map = new HashMap();
            map.put("rows",(page-1)*size);
            map.put("pageSize",size);
            return sodao.findDataByRow(map);
        }
    }

    entity/SalesOrder:实体类

    public class SalesOrder {
        private int customer_sk;
        private int  product_sk;
        private String  d_date;
        private int  paynum;
        private double  paymoney;
        private int  countnum;
        private double  countmoney;
    => 这样不好!实力类不要下划线!
    => 改成
        private int customerSk;
        private int  productSk;
        private String  dDate;
        private int  paynum;
        private double  paymoney;
        private int  countnum;
        private double  countmoney;

    dao/SaleOrderDAO

    package com.njbdqn.sales.dao;
    
    import com.njbdqn.sales.entity.SalesOrder;
    import org.apache.ibatis.annotations.Select;
    
    import java.util.List;
    import java.util.Map;
    
    public interface SaleOrderDAO {
    
        @Select("select customer_sk as customerSk,product_sk as productSk,paynum from dm_sales_order_count limit ${rows},${pageSize}")
        public List<SalesOrder> findDataByRow(Map map);
    }

    controller/SaleCtrl

    package com.njbdqn.sales.controller;
    
    import com.njbdqn.sales.entity.SalesOrder;
    import com.njbdqn.sales.services.SaleOrderService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    @RestController
    @RequestMapping("/sale") // 模块名
    public class SaleCtrl {
        @Autowired
        private SaleOrderService sos;
    
        @RequestMapping("/findSaleCount") //接口名
        public List<SalesOrder> fsc(int page,int size){
            return sos.mypage(page,size);
        }
    }

    最外层启动类:SalesApplication

    @SpringBootApplication
    @MapperScan("com.njbdqn.sales.dao")
    public class SalesApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SalesApplication.class, args);
        }
    
    }

    注意点:

    1. 实体类不允许写下划线!!!!!
    2. 启动类一定是最外边的

    3. sql语句的空格问题
    4. @RestController => 产生json到前台,适用于ajax交互场景
    5. 扫描只能扫描dao的接口 @MapperScan("com.njbdqn.sales.dao") 

    四、访问

    http://localhost:8080/sale/findSaleCount?page=1&size=10

    五、性能调优

    测试以下接口性能,如瞬时负载测试:https://www.cnblogs.com/sabertobih/p/14105551.html

    调优思路

    1)连接池:使用Druid

    pom中加入:

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid-spring-boot-starter</artifactId>
        <version>1.1.14</version>
    </dependency>

    application.yml改成:

    spring:
      datasource:
        druid:
          url: jdbc:mysql://192.168.56.111:3306/sales_source
          username: root
          password: root
          driver-class-name: com.mysql.jdbc.Driver
          initial-size: 20
          min-idle: 20
          max-active: 50
          max-wait: 60000
          test-on-borrow: true
          pool-prepared-statements: true
          use-global-data-source-stat: true
          connect-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

    2)SQL语句本身调优:EXPLAIN分析执行计划

    3)更改MYSQL配置 

    4)MYSQL作为数据库可能本身不合理 ——— MYSQL吞吐太慢!

      考虑换一个数据库:

      • Redis:读写速度极快,性能极高,可以存JSON对象,相当于大HashMap
      • mongoDB:面向文档存储,吞吐量也很变态

    鉴于Redis优点,

    >>> 把MYSQL数据迁移到Redis上,操作见:https://www.cnblogs.com/sabertobih/p/14101970.html

    >>> SpringBoot暴露接口+Redis,操作见:https://www.cnblogs.com/sabertobih/p/14106243.html

    >>> 再次使用Jmeter测试工具测接口响应速度,发现速度快了10倍!!!

    但Redis缺点是:无法执行复杂查询

    结合Redis优缺点的解决方案:

    1)使用MYSQL做复杂查询,结果存放在Redis中用于大量高速读写

    2)利用Redis做物理化标准索引表,详见:https://www.cnblogs.com/sabertobih/p/14106031.html

     


  • 相关阅读:
    Python疑难杂症:SyntaxError: NonASCII character Python中文处理问题
    程序员健康大全透视身体24小时工作时间表
    ConnectionTimeout,CommandTimeout和executionTimeout的理解
    google map api 与jquery结合使用(3) 图标样式,使用xml和异步请求【转帖】
    新手8周跑步训练计划
    57商城温州地区最大本土网上超市
    7 款仿照 Sinatra 思路的 .NET 框架
    线程池的原理和连接池的原理
    免费网站模版:一个黑色系的公司网站模版(flash幻灯)
    深入浅出REST
  • 原文地址:https://www.cnblogs.com/sabertobih/p/14043895.html
Copyright © 2020-2023  润新知