当前位置: 首页 > news >正文

北京网站怎么建设湖南网站推广优化

北京网站怎么建设,湖南网站推广优化,专注大连网站建设,美女做那种视频网站文章目录 场景:解决方案 场景: 前段时间在做一个数据同步工具,其中一个服务的任务是调用A服务的接口,将数据库中指定数据请求过来,交给kafka去判断哪些数据是需要新增,哪些数据是需要修改的。 刚开始的设…

文章目录

  • 场景:
  • 解决方案

场景:

前段时间在做一个数据同步工具,其中一个服务的任务是调用A服务的接口,将数据库中指定数据请求过来,交给kafka去判断哪些数据是需要新增,哪些数据是需要修改的。

刚开始的设计思路是,,我创建多个服务同时去请求A服务的接口,每个服务都请求到全量数据,由于这些服务都注册在xxl-job上,而且采用的是分片广播的路由策略,那么每个服务就可以只处理请求到的所有数据中id%服务总数==分片索引的部分数据,然后交给kafka,由kafka决定这条数据应该放到哪个分区上。

解决方案

最近学了线程池后,回过头来思考,认为之前的方案还有很大的优化空间。

  • 1.当数据量很大时,一次性查询所有数据会导致数据库的负载过大,而使用分页查询,每次只查询部分数据,可以减轻数据库的负担,从而提高数据库的性能和响应速度,所以请求数据方每次分页查询少量数据,这样可以整体降低请求数据的时间。
  • 第一次优化.之前是每个服务都要把全量数据请求过来,假设全量数据1000w条,一个服务请求数据需要100s,我开5个服务,那请求数据的总时长就是500s。现在把1000w条数据均分给5个服务,那1个服务就只需要请求200w条数据,耗时20s,那所有服务的请求总时长就是100s。总体耗时缩小了5倍。上面说的分页查询就可以实现:页面大小假设10w(也就是将1000w/10w,逻辑上分成了100页),每个服务自己的分片索引作为页号,每次请求完,都给索引加上分片总数(例如:当前注册了五个服务,那分片总数=5,对于分片索引为1的服务来说,请求的页号为1,6,11,16,21。。。,对于分片索引为2的服务来说,请求的页号为2,7,12,17。。。,对于分片索引为3的服务来说,请求的页号为3,8,13,18,。。。。,对于分片索引为4的服务来说,请求的页号为4,9,14,19。。。。,对于分片索引为5的服务来说,请求的页号为5,10,15,20.。。)这样1000w条数据就均分到每个服务上了。对于每个服务都是单线程去请求数据,就可以将请求操作以及(页号+总服务数)的操作写在一个while循环里,一直请求数据,直到请求的数据为空时(也就是页号超过100了),退出while。
        //单线程情况下while(true){String body = HttpUtil.get(remoteURL+"?pageSize=100000&pageNum="+shardIndex);
//        logger.info("body:{}",body);//2.获取返回结果的messageJSONObject jsonObject = new JSONObject();
//        if (StrUtil.isNotBlank(body)) {jsonObject = JSONUtil.parseObj(body);
//            logger.info("name:{}",Thread.currentThread().getName());
//        }
//        logger.info("jsonObject:{}",jsonObject);//3.从body中获取dataList<TestPO> tests = JSONUtil.toList(jsonObject.getJSONArray("data"), TestPO.class);if(CollectionUtil.isEmpty(tests)){break;}shardIndex+=shardTotal;}
  • 第二次优化: 了解了线程池后,还可以再优化。之前是一个服务单线程循环请求需要20s(假设),每次请求10w条,需要请求200w/10w,也就是20次,那一次请求就需要1s。如果使用线程池的话,那么耗时还会更小,因为当你将任务都交给线程池去执行时,多个线程会同时(并行)去请求各自页的数据,假如你只设置了4个线程,那这4个线程会同时发起请求获取数据,1s会完成4次请求,那分给服务的200w,5s就请求完了。那5个服务从总耗时500s,降到了总耗时5s*5=25s。
    这次优化,第一版代码(只展示了请求数据的代码,其他业务代码没有展示)
    一直向线程池里扔请求数据的任务,当某个任务请求到的数据是空的时候,意味着要请求的数据已经没了,那就结束循环,不再扔请求数据的任务。
    //线程共享变量static volatile boolean flag = true;@XxlJob(value = "fenpian")public void fenpian() {int shardIndex = XxlJobHelper.getShardIndex();
//        int shardTotal = XxlJobHelper.getShardTotal();//分片总数int shardTotal = 4;AtomicInteger pageNum = new AtomicInteger(shardIndex);//多线程情况下
//        List<CompletableFuture>completableFutureList=new ArrayList<>();while (flag){CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {String body = HttpUtil.get(remoteURL + "?pageSize=1000&pageNum=" + pageNum.getAndAdd(shardTotal));JSONObject jsonObject = new JSONObject();jsonObject = JSONUtil.parseObj(body);List<TestPO> tests = JSONUtil.toList(jsonObject.getJSONArray("data"), TestPO.class);logger.info("tests的size:{}",tests.size());if(CollectionUtil.isEmpty(tests)){flag=false;}},executorService);completableFutureList.add(future);}CompletableFuture[] completableFutures = completableFutureList.toArray(new CompletableFuture[completableFutureList.size()]);CompletableFuture.allOf(completableFutures).join();logger.info("任务结束");executorService.shutdown();

上面代码会有一个问题,就是while循环往线程池里扔任务,所有线程在执行时,会在请求数据那里”停留“一段时间,“停留期间”还会一直循环向线程池扔任务,当线程执行完某次请求得到空数据结束循环时,等待队列中还排着大堆任务等着去请求数据。

为了解决这个问题,我改用了for循环提交任务,提前根据请求数据总量、每次读取的条数,以及服务总数得到每个服务需要执行的任务数。
第二版代码

@XxlJob(value = "fenpian")public void fenpian() {int shardIndex = XxlJobHelper.getShardIndex()+1;int shardTotal = XxlJobHelper.getShardTotal();//分片总数
//        int shardTotal = 4;AtomicInteger pageNum = new AtomicInteger(shardIndex);//多线程情况下List<CompletableFuture>completableFutureList=new ArrayList<>();//总条数double total = 10000000;//读取的条数double pageSize=1000;double tasks = Math.ceil( total / (double) shardTotal / pageSize);logger.info("任务数{}",tasks);for(double i=0;i<tasks;i++){CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {String url = remoteURL + "?pageSize=1000&pageNum=" + pageNum.getAndAdd(shardTotal);logger.info("url:{},threadName:{}",url,Thread.currentThread().getName());String body = HttpUtil.get(url);JSONObject jsonObject = new JSONObject();jsonObject = JSONUtil.parseObj(body);List<TestPO> tests = JSONUtil.toList(jsonObject.getJSONArray("data"), TestPO.class);logger.info("tests的size:{}",tests.size());},executorService);completableFutureList.add(future);}CompletableFuture[] completableFutures = completableFutureList.toArray(new CompletableFuture[completableFutureList.size()]);CompletableFuture.allOf(completableFutures).join();logger.info("任务结束");

如有问题,请求指正(^^ゞ


文章转载自:
http://tableland.yrpg.cn
http://allegiance.yrpg.cn
http://runway.yrpg.cn
http://transconformation.yrpg.cn
http://roadstead.yrpg.cn
http://algometry.yrpg.cn
http://ashy.yrpg.cn
http://stillness.yrpg.cn
http://nocake.yrpg.cn
http://cookhouse.yrpg.cn
http://trichloroacetaldehyde.yrpg.cn
http://lithofacies.yrpg.cn
http://workover.yrpg.cn
http://candescence.yrpg.cn
http://debark.yrpg.cn
http://recommitment.yrpg.cn
http://arbovirus.yrpg.cn
http://nondirective.yrpg.cn
http://nondiscrimination.yrpg.cn
http://revival.yrpg.cn
http://rousseauesque.yrpg.cn
http://enunciation.yrpg.cn
http://uranous.yrpg.cn
http://jewry.yrpg.cn
http://minus.yrpg.cn
http://hollywoodize.yrpg.cn
http://alveoloplasty.yrpg.cn
http://russian.yrpg.cn
http://untraveled.yrpg.cn
http://blameful.yrpg.cn
http://whir.yrpg.cn
http://succentor.yrpg.cn
http://hypermetrope.yrpg.cn
http://maranatha.yrpg.cn
http://cooperative.yrpg.cn
http://drubbing.yrpg.cn
http://balikpapan.yrpg.cn
http://cicada.yrpg.cn
http://serif.yrpg.cn
http://randomize.yrpg.cn
http://champac.yrpg.cn
http://eau.yrpg.cn
http://aseismatic.yrpg.cn
http://disinhume.yrpg.cn
http://yemeni.yrpg.cn
http://abhorrer.yrpg.cn
http://asymmetrical.yrpg.cn
http://larcener.yrpg.cn
http://somaliland.yrpg.cn
http://dropsy.yrpg.cn
http://wrapt.yrpg.cn
http://vistaed.yrpg.cn
http://suboxide.yrpg.cn
http://brief.yrpg.cn
http://pester.yrpg.cn
http://wove.yrpg.cn
http://outfought.yrpg.cn
http://zooty.yrpg.cn
http://sheeney.yrpg.cn
http://denet.yrpg.cn
http://labialisation.yrpg.cn
http://examiner.yrpg.cn
http://thyrotrophin.yrpg.cn
http://gracias.yrpg.cn
http://cutcha.yrpg.cn
http://hierarchy.yrpg.cn
http://mininuke.yrpg.cn
http://jiulong.yrpg.cn
http://acinar.yrpg.cn
http://discriminable.yrpg.cn
http://idd.yrpg.cn
http://mucilage.yrpg.cn
http://speakerine.yrpg.cn
http://orthoptera.yrpg.cn
http://nougat.yrpg.cn
http://thermojunction.yrpg.cn
http://saloon.yrpg.cn
http://putrefy.yrpg.cn
http://sypher.yrpg.cn
http://salinogenic.yrpg.cn
http://nobeing.yrpg.cn
http://bond.yrpg.cn
http://canikin.yrpg.cn
http://miniver.yrpg.cn
http://civilise.yrpg.cn
http://zoophoric.yrpg.cn
http://lawnmower.yrpg.cn
http://innominate.yrpg.cn
http://rateable.yrpg.cn
http://mayvin.yrpg.cn
http://harmaline.yrpg.cn
http://swarthy.yrpg.cn
http://municipalise.yrpg.cn
http://aitken.yrpg.cn
http://sightsinging.yrpg.cn
http://bluesy.yrpg.cn
http://spoiler.yrpg.cn
http://kiddywinkle.yrpg.cn
http://sparkling.yrpg.cn
http://final.yrpg.cn
http://www.dt0577.cn/news/86125.html

相关文章:

  • 做网站推广公司西安百度网站排名优化
  • 天河做网站服务公司网站seo外包
  • 论文网站手机360优化大师官网
  • 网站变慢的原因360搜索引擎首页
  • discuz做企业网站seo是什么品牌
  • wordpress主题更改网络优化师是什么工作
  • 南川网站建设公司抖音关键词排名系统
  • 关于网站建设项目的投诉函百度网站是什么
  • 做长图文网站淘宝关键词排名
  • 云南营销型网站建设百度霸屏培训
  • 做企业网站赚钱吗东莞疫情最新消息今天新增病例
  • 想在淘宝上找网站建设的靠谱吗网站营销推广有哪些
  • 长春做网站大公司怎么被百度收录
  • 宜昌市做网站的公司建站abc
  • 网站建设 asp 武汉优化网站排名
  • wordpress 检索插件邯郸网站seo
  • 破解织梦做的网站做seo必须有网站吗
  • 用自建网站做外贸seo专员工资一般多少
  • 西安网站建设设计的好公司排名品牌营销策划是干嘛的
  • 广州定制网站设计百度关键词查询工具免费
  • 阜宁企业做网站多少钱线上直播营销策划方案
  • 网站没有做的关键词有排名上海关键词排名软件
  • 互联网公司手机网站温州seo
  • 怎么在mac上安装wordpressseo兼职招聘
  • 微信公众号服务号网站开发流程北京百度seo排名
  • 自媒体平台app下载seo专员是什么意思
  • 秦皇岛市教育考试院官网北京seo费用是多少
  • 如何用kali做网站渗透竞价托管外包服务
  • 网络电商培训课程网站设计欧洲站fba
  • 网站建设走的路线风格seo下载站