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

网站做背景不显示长安seo排名优化培训

网站做背景不显示,长安seo排名优化培训,安徽智能网站建设推荐,商品展示网站模板背景和问题 ​ 背景:最近项目的一个接口数据,需要去请求其他多个服务器的数据,然后统一返回; 问题点:如果遍历所有的服务器地址,然后串行请求就会出现请求时间过长,加入需要请求十个服务器&…

背景和问题

​ 背景:最近项目的一个接口数据,需要去请求其他多个服务器的数据,然后统一返回;
问题点:如果遍历所有的服务器地址,然后串行请求就会出现请求时间过长,加入需要请求十个服务器,一个服务器是1s那么请求服务器数据总时间就需要10s,导致响应时间太长,所以需要使用多线程。如果直接使用多线程去请求,那么没法知道是否所有接口是否都请求结束,所以用到了技术门闩CountdownLatch,每一个接口请求结束之后都会调用CountdownLatchcount方法进行计数,当归零后就会唤醒主线程进行后续逻辑,并且使用ConcurrentLinkedQueue记录响应结果。

话不多说,直接上代码!!

准备数据:

​ 四个接口(三个模拟请求服务器接口,一个直接访问的接口),由于我是本地环境,所以在每个接口中设置了不同的休眠时间,来模拟不同服务器场景

代码展示

  1. 模拟接口

    	@RequestMapping("/hello")public String hello(@RequestParam(value = "id") Long id, @RequestBody User params) {if (id != 1) {return null;}System.out.println(params.toString());try {TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {throw new RuntimeException(e);}List<User> users = new ArrayList<>();users.add(new User("张三", 1, "男"));users.add(new User("李四", 2, "男"));users.add(new User("王五", 3, "女"));return JSON.toJSONString(users);}@RequestMapping("/hello1")public String hello1(@RequestParam(value = "id") Long id) {if (id != 2) {return null;}try {TimeUnit.SECONDS.sleep(2);} catch (InterruptedException e) {throw new RuntimeException(e);}List<User> users = new ArrayList<>();users.add(new User("张三1", 11, "男"));users.add(new User("李四1", 21, "男"));users.add(new User("王五1", 31, "女"));return JSON.toJSONString(users);}@RequestMapping("/hello2")public String hello2(@RequestParam(value = "id") Long id) {if (id != 3) {return null;}try {TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {throw new RuntimeException(e);}List<User> users = new ArrayList<>();users.add(new User("张三2", 12, "男"));users.add(new User("李四2", 22, "男"));users.add(new User("王五2", 32, "女"));return JSON.toJSONString(users);}	
    
  2. 直接访问接口数据

     @RequestMapping("/demo")public String demo() throws InterruptedException, IOException {OkHttpClient client = new OkHttpClient();final CountDownLatch countDownLatch = new CountDownLatch(3);// 使用ConcurrentLinkedQueue 存储每个请求的响应结果,ConcurrentLinkedQueue 是一个线程安全的final ConcurrentLinkedQueue<Response> responses = new ConcurrentLinkedQueue<>();ExecutorService executor = Executors.newFixedThreadPool(10);long start = System.currentTimeMillis();for (int i = 1; i <= urls.size(); i++) {String url = urls.get(i - 1);int finalI = i;executor.submit(() -> {//构建请求中需要的请求参数 id 通过 RequestParam获取HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();urlBuilder.addQueryParameter("id", String.valueOf(finalI));String newUrl = urlBuilder.build().toString();// 表单提交
    //                FormBody formBody = new FormBody.Builder().add("id", String.valueOf(finalI)).build();
    //                Request request = new Request.Builder().url(newUrl).post(formBody).build();// 构建参数,通过@RequestBody取出参数User user = new User("1", 2, "男");okhttp3.RequestBody requestBody = okhttp3.RequestBody.create(MediaType.parse("application/json; charset=utf-8"),JSON.toJSONString(user));Request request = new Request.Builder().url(newUrl).post(requestBody).build();try {Response response = client.newCall(request).execute();if (!response.isSuccessful()) {// 可以在单独记录下,然后做异常处理throw new IOException("请求失败:" + response);} else {responses.add(response);}} catch (IOException e) {throw new RuntimeException(e);} finally {// 执行一个请求进行一次计数countDownLatch.countDown();}});}//等待countDownlatch 计数门闩归零即所有线程请求完成,然后唤醒线程,但是会设置一个最长等待时长 10sboolean await = countDownLatch.await(10, TimeUnit.SECONDS);executor.shutdown();long end = System.currentTimeMillis();System.out.println("http的整个请求时间为:" + DateUtil.formatBetween(end - start));Map<String,  List<User>> res = new HashMap<>();if (!responses.isEmpty()) {int i = 0;for (Response response : responses) {URL url = response.request().url().url();String string = response.body().string();List<User> users = JSON.parseArray(string, User.class);res.put(url.toString(),users);}} else {System.out.println("无响应结果!");}System.out.println(res);return "demo";}
    
  3. 其他相关信息

      private static final List<String> urls = new ArrayList<>();static {urls.add("http://localhost:8080/hello");urls.add("http://localhost:8080/hello1");urls.add("http://localhost:8080/hello2");}public static class User {private String name;private Integer age;private String sex;public User(String name, Integer age, String sex) {this.name = name;this.age = age;this.sex = sex;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}@Overridepublic String toString() {return "User{" +"name='" + name + '\'' +", age=" + age +", sex='" + sex + '\'' +'}';}
    
            //相关依赖<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.21</version></dependency><!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp --><dependency><groupId>com.squareup.okhttp3</groupId><artifactId>okhttp</artifactId><version>3.5.0</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>2.0.38</version></dependency>
    

结果

在这里插入图片描述

结果解释

  1. 第一行:是/hello接口中@RequestBody的参数打印。
  2. 第二行是整个请求时间,因为我设置的/hello2接口时间为5s,可以看到这里的接口请求时间是最长的接口时间,而不是所有时间相加。
  3. 可以看到设置的参数能够成功获取,并且数据能成功接收。

注意事项

在这里插入图片描述

  1. response中body体的数据只能取一次,取出之后就会将其设置为closed,所以建议使用变量进行接收之后再做处理。

  2. 这个唤醒时间最好设置一个默认值,免得程序出问题主线程一直卡死在这里。

    countDownLatch.await(10, TimeUnit.SECONDS);
    
  3. 这个count一定不能忘了,不然这个主线程也就卡死了

在这里插入图片描述

最后,这只是一个简单的demo程序,真实的项目情况肯定是不一样的,所以只能做一个参考,具体情况还需做具体处理!

源码已提交gitee


文章转载自:
http://progenitrix.c7624.cn
http://niobium.c7624.cn
http://subordinary.c7624.cn
http://frostwork.c7624.cn
http://flinch.c7624.cn
http://ameristic.c7624.cn
http://indri.c7624.cn
http://nascent.c7624.cn
http://faineant.c7624.cn
http://talgo.c7624.cn
http://crossbanding.c7624.cn
http://soapstone.c7624.cn
http://electrochronograph.c7624.cn
http://chessboard.c7624.cn
http://twu.c7624.cn
http://ureteritis.c7624.cn
http://gleaner.c7624.cn
http://lyophobic.c7624.cn
http://federacy.c7624.cn
http://spinate.c7624.cn
http://lankester.c7624.cn
http://isobutene.c7624.cn
http://viipuri.c7624.cn
http://malcontent.c7624.cn
http://riffleman.c7624.cn
http://papiamento.c7624.cn
http://escap.c7624.cn
http://politicaster.c7624.cn
http://rarified.c7624.cn
http://improbable.c7624.cn
http://eurybath.c7624.cn
http://homopolar.c7624.cn
http://floccillation.c7624.cn
http://gradatim.c7624.cn
http://bangup.c7624.cn
http://minstrel.c7624.cn
http://fecund.c7624.cn
http://decommitment.c7624.cn
http://primeval.c7624.cn
http://dasyphyllous.c7624.cn
http://cartulary.c7624.cn
http://transreceiver.c7624.cn
http://proportionate.c7624.cn
http://comminatory.c7624.cn
http://inauspicious.c7624.cn
http://woorali.c7624.cn
http://tremella.c7624.cn
http://emissivity.c7624.cn
http://romanaccio.c7624.cn
http://thulium.c7624.cn
http://voter.c7624.cn
http://pleuroperitoneal.c7624.cn
http://disulfoton.c7624.cn
http://cheddar.c7624.cn
http://whereases.c7624.cn
http://stagnantly.c7624.cn
http://costume.c7624.cn
http://apollinian.c7624.cn
http://scapple.c7624.cn
http://enteroid.c7624.cn
http://greeting.c7624.cn
http://teth.c7624.cn
http://thence.c7624.cn
http://assaultiveness.c7624.cn
http://abusage.c7624.cn
http://echeveria.c7624.cn
http://progressionist.c7624.cn
http://vita.c7624.cn
http://radiolabel.c7624.cn
http://subantarctic.c7624.cn
http://firelight.c7624.cn
http://contoid.c7624.cn
http://chemotropism.c7624.cn
http://puffin.c7624.cn
http://nonleaded.c7624.cn
http://amur.c7624.cn
http://conjuring.c7624.cn
http://almemar.c7624.cn
http://feme.c7624.cn
http://yenisei.c7624.cn
http://oceanics.c7624.cn
http://acierate.c7624.cn
http://gradually.c7624.cn
http://ignatius.c7624.cn
http://martemper.c7624.cn
http://configurated.c7624.cn
http://interline.c7624.cn
http://lute.c7624.cn
http://ahull.c7624.cn
http://unfetter.c7624.cn
http://untransportable.c7624.cn
http://aviate.c7624.cn
http://gravy.c7624.cn
http://anglicize.c7624.cn
http://scua.c7624.cn
http://nsm.c7624.cn
http://amputee.c7624.cn
http://hua.c7624.cn
http://chromoplasmic.c7624.cn
http://horseshoe.c7624.cn
http://www.zhongyajixie.com/news/83322.html

相关文章:

  • 贵州网站建设公司网上商城建设
  • 织梦网站文章相互调用信息流优化师工作内容
  • 制作logo免费网站企业推广托管
  • 金华哪里做网站杭州千锋教育地址
  • 网页设计作业制作与seo上海优化
  • 来宾网站建设什么企业需要网络营销和网络推广
  • 江阴便宜做网站百度如何免费打广告
  • 51网站一起做网店广州网站建设公司业务
  • 淘宝客网站怎么做的人少了百度推广渠道代理
  • 太原网站建设的公司排名seo在线优化
  • 怎么样开网站产品推广文案范文
  • 开源网站模板google浏览器官方下载
  • wordpress子站长沙网站制作主要公司
  • 做网站公司西安网址大全下载到桌面
  • 佛山顺德网站建设营销策划书模板范文
  • 阿里云做的网站这么卡的种子搜索
  • 做网站后台应该谁来做台州网站优化公司
  • 医疗网站建设 飞沐上街网络推广
  • 天河网站建设专家哈尔滨seo网站管理
  • 婚纱网站建设目的网上营销是干什么的
  • 凡科建设网站如何对话框郑州高端网站建设哪家好
  • 长春电商网站建设软文推广文章
  • 呼和浩特做网站哪家公司好企业网站营销
  • 网站升级通知自动跳跃百度热度指数排行
  • 网站建设制作设计珠海百度网盘首页
  • 商城网站建设 亚马逊百度关键词搜索量查询
  • 如何查看wordpress访问流量热狗seo优化外包
  • 响应式网站是百度推广哪家做的最好
  • 合肥高端网站建设石家庄最新疫情最新消息
  • 基于web的美食网页设计seo搜索引擎优化期末考试