要:此次定位,颇为复杂,而且有时让人没有思路,因为同一个接口,换一个环境又好使了,又不敢去怀疑代码;然而有问题的环境,换一个接口也好使了,又不得不怀疑代码
本文分享自华为云社区《一次网络请求超时分析-云社区-华为云》,作者:xiewenci 。
发起http请求,后端服务接口/v5/iot/11c9c88e6fb26bead43b75514dc380eb/routing-rule/rules?limit=10&marker=ffffffffff&offset=0,一直等待,一分钟后返回响应,且中文乱码显示
异常环境的流信息
正常环境的流信息
从图中看出正常环境是,服务端发送响应完成之后,由客户端正常返回ACK,然后由客户端发起FIN断链请求。而异常环境则是服务端发送响应完成之后,客户端也回了ACK,但是没有主动发起断链请求,直到超过keep-alive时间之后,由服务端发起了断链请求(因为超时主动断链)。现象是客户端一直在等服务端发送响应(可能没有发送完),所以怀疑服务端有缓存之类,没有及时将响应流发送出去,所以继续查看代码
3. 查看代码,在返回客户端的时候,没有flush和close操作,代码如下
然后以为找到问题原因所在了,就尝试修改代码,如下:
增加了flush操作和close操作(放在了try语快中),最后测试执行,还是跟最初的现象一致,还是会等待1分钟后才会返回响应。这时就有点纳闷了,再重新仔细分析,其实从流中可以看出来,服务端的响应是立马返回给了客户端的,如下图
既然给了响应,那为什么客户端为什么还要继续等待呢?这里有注意到图中有几个问号,这其实是中文字符乱码了,所以这里又怀疑是不是编码格式导致客户端收到的Content-Length长度和收到的响应的长度不一致,也就是客户端实际收到的响应的长度小于Content-Length的长度,然后一直等待下去,所以继续修改代码代码
4. 修改代码,指定编码格式为UTF-8,代码如下:
替换环境版本,测试执行,响应就立马返回了,果然是这个编码格式的锅
1. 编码格式不一致就会导致响应流的实际长度不一致?答案是一定的,编码格式不一致,实际长度就是会不一致,测试结果如下:
2. 为什么之前的没有出现过这个问题?是什么原因导致编码格式丢失的
查看后端服务jar包,发现spring版本已经升级到5.2.21.RELEASE,此版本中没有指定默认编码格式为UTF-8
所以需要后端服务去指定编码格式,或者网关服务统一处理
此次定位,颇为复杂,而且有时让人没有思路,因为同一个接口,换一个环境又好使了,又不敢去怀疑代码;然而有问题的环境,换一个接口也好使了,又不得不怀疑代码;还有就是在容器中去调用接口,也是一样的问题,所以感觉又跟网络没啥关系。其实应该静下心来去思考为什么客户端那里一直等待,没有主动发起断链,这里还是说明了对HTTP协议细节,不够熟练掌握,才导致中间走了一些误区,需要去巩固和加深对http协议的理解。
点击下方,第一时间了解华为云新鲜技术~
华为云博客_大数据博客_AI博客_云计算博客_开发者中心-华为云
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
<!-- 使用thymeleaf解析 -->
<bean id="templateResolver"
class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML" />
<property name="cacheable" value="false" />
<property name="characterEncoding" value="UTF-8"/><!--不加会乱码-->
</bean>
<bean id="templateEngine"
class="org.thymeleaf.spring4.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<!--解决中文乱码-->
<property name="characterEncoding" value="UTF-8"/>
</bean>
<!-- 配置视图解析器 -->
<!--<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
<!--<property name="prefix" value="/WEB-INF/" />-->
<!--<property name="suffix" value=".jsp" />-->
<!--</bean>-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-ioc.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--SpringMVC配置文件的名字 <servlet-name>-servlet.xml
默认位置:src / resources
如果放在了 src/resources(maven)
contextConfigLocation:classpath:文件名即可!
Web-INF/xx.xml
contextConfigLocation:/WEB-INF/xx.xml
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 访问DispatcherServlet对应的路径 -->
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern> <!--/不过滤jsp防止死循环-->
</servlet-mapping>
<!--配置thymeleaf -->
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
package com.test.action;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("thymeleaf")
public class ThymeleafAction {
@RequestMapping("/testThymeleaf")
public String testThymeleaf(Model model)
{
model.addAttribute("uname","zhangsan");
return "testThymeleaf";// WEB-INF/views/testThymeleaf.html
}
}
根据之前设置的前缀
在web-inf/views/文件夹下创建html文件
访问传递过来的数据,注意声明Thymeleaf命名空间
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1 th:text="${uname}">aaaa</h1>
</body>
</html>
http://localhost:8080/testssm/thymeleaf/testThymeleaf
成功
html有的属性,Thymeleaf基本都有,而常用的属性大概有七八个。其中th属性执行的优先级从1~8,数字越低优先级越高。
一、th:text :设置当前元素的文本内容,相同功能的还有th:utext,两者的区别在于前者不会转义html标签,后者会。优先级不高:order=7
二、th:value:设置当前元素的value值,类似修改指定属性的还有th:src,th:href。优先级不高:order=6
三、th:each:遍历循环元素,和th:text或th:value一起使用。注意该属性修饰的标签位置,详细往后看。优先级很高:order=2
四、th:if:条件判断,类似的还有th:unless,th:switch,th:case。优先级较高:order=3
五、th:insert:代码块引入,类似的还有th:replace,th:include,三者的区别较大,若使用不恰当会破坏html结构,常用于公共代码块提取的场景。优先级最高:order=1
六、th:fragment:定义代码块,方便被th:insert引用。优先级最低:order=8
七、th:object:声明变量,一般和*{}一起配合使用,达到偷懒的效果。优先级一般:order=4
八、th:attr:修改任意属性,实际开发中用得较少,因为有丰富的其他th属性帮忙,类似的还有th:attrappend,th:attrprepend。优先级一般:order=5
由上文也可以知道需要在html中添加:
<html xmlns:th="http://www.thymeleaf.org">
这样,下文才能正确使用th:*形式的标签!
通过**${…}进行取值,这点和ONGL表达式语法一致!**
<h1 th:text="${uname}"></h1>
<input type="text" th:value="${uname}" />
选择变量表达式*{…}
<div th:object="${items}">
<p th:text="*{name}" >产品名</p>
<p th:text="*{detail}" >产品名</p>
</div>
至于p里面的原有的值只是为了给前端开发时做展示用的.这样的话很好地做到了前后端分离。
这也是Thymeleaf非常好的一个特性:在无网络的情况下也能运行,也就是完全可以前端先写出页面,模拟数据展现效果,后端人员再拿此模板修改即可!
用来配合link src href使用的语法,类似的标签有:th:href和th:src
无参:@{/xxx}
有参:@{/xxx(k1=v1,k2=v2)} 对应url结构:xxx?k1=v1&k2=v2
引入本地资源:@{/项目本地的资源路径}
引入外部资源:@{/webjars/资源在jar包中的路径}
列举:第三部分的实战引用会详细使用该表达式
<link th:href="@{/webjars/bootstrap/4.0.0/css/bootstrap.css}" rel="stylesheet">
<link th:href="@{/main/css/itdragon.css}" rel="stylesheet">
<form class="form-login" th:action="@{/user/login}" th:method="post" >
<a class="btn btn-sm" th:href="@{/login.html(l='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/login.html(l='en_US')}">English</a>
<a href="details.html" th:href="@{/thymeleaf/showItemsById(id=${items.id})}">view</a>
变量表达式有丰富的内置方法,使其更强大,更方便。
一、可以获取对象的属性和方法
二、可以使用ctx,vars,locale,request,response,session,servletContext内置对象
三、可以使用dates,numbers,strings,objects,arrays,lists,sets,maps等内置方法(重点介绍)
一、ctx :上下文对象。
二、vars :上下文变量。
三、locale:上下文的语言环境。
四、request:(仅在web上下文)的 HttpServletRequest 对象。
五、response:(仅在web上下文)的 HttpServletResponse 对象。
六、session:(仅在web上下文)的 HttpSession 对象。
七、servletContext:(仅在web上下文)的 ServletContext 对象
这里以常用的Session举例,用户刊登成功后,会把用户信息放在Session中,Thymeleaf通过内置对象将值从session中获取。
// java 代码将用户名放在session中
session.setAttribute("userinfo",username);
// Thymeleaf通过内置对象直接获取
th:text="${session.userinfo}"
一、strings:字符串格式化方法,常用的Java方法它都有。比如:equals,equalsIgnoreCase,length,trim,toUpperCase,toLowerCase,indexOf,substring,replace,startsWith,endsWith,contains,containsIgnoreCase等
二、numbers:数值格式化方法,常用的方法有:formatDecimal等
三、bools:布尔方法,常用的方法有:isTrue,isFalse等
四、arrays:数组方法,常用的方法有:toArray,length,isEmpty,contains,containsAll等
五、lists,sets:集合方法,常用的方法有:toList,size,isEmpty,contains,containsAll,sort等
六、maps:对象方法,常用的方法有:size,isEmpty,containsKey,containsValue等
七、dates:日期方法,常用的方法有:format,year,month,hour,createNow等
文章底部提供了对应的官网链接
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>ITDragon Thymeleaf 内置方法</title>
</head>
<body>
<h2>ITDragon Thymeleaf 内置方法</h2>
<h3>#strings </h3>
<div th:if="${not #strings.isEmpty(itdragonStr)}" >
<p>Old Str : <span th:text="${itdragonStr}"/></p>
<p>toUpperCase : <span th:text="${#strings.toUpperCase(itdragonStr)}"/></p>
<p>toLowerCase : <span th:text="${#strings.toLowerCase(itdragonStr)}"/></p>
<p>equals : <span th:text="${#strings.equals(itdragonStr, 'itdragonblog')}"/></p>
<p>equalsIgnoreCase : <span th:text="${#strings.equalsIgnoreCase(itdragonStr, 'itdragonblog')}"/></p>
<p>indexOf : <span th:text="${#strings.indexOf(itdragonStr, 'r')}"/></p>
<p>substring : <span th:text="${#strings.substring(itdragonStr, 2, 8)}"/></p>
<p>replace : <span th:text="${#strings.replace(itdragonStr, 'it', 'IT')}"/></p>
<p>startsWith : <span th:text="${#strings.startsWith(itdragonStr, 'it')}"/></p>
<p>contains : <span th:text="${#strings.contains(itdragonStr, 'IT')}"/></p>
</div>
<h3>#numbers </h3>
<div>
<p>formatDecimal 整数部分随意,小数点后保留两位,四舍五入: <span th:text="${#numbers.formatDecimal(itdragonNum, 0, 2)}"/></p>
<p>formatDecimal 整数部分保留五位数,小数点后保留两位,四舍五入: <span th:text="${#numbers.formatDecimal(itdragonNum, 5, 2)}"/></p>
</div>
<h3>#bools </h3>
<div th:if="${#bools.isTrue(itdragonBool)}">
<p th:text="${itdragonBool}"></p>
</div>
<h3>#arrays </h3>
<div th:if="${not #arrays.isEmpty(itdragonArray)}">
<p>length : <span th:text="${#arrays.length(itdragonArray)}"/></p>
<p>contains : <span th:text="${#arrays.contains(itdragonArray, 5)}"/></p>
<p>containsAll : <span th:text="${#arrays.containsAll(itdragonArray, itdragonArray)}"/></p>
</div>
<h3>#lists </h3>
<div th:if="${not #lists.isEmpty(itdragonList)}">
<p>size : <span th:text="${#lists.size(itdragonList)}"/></p>
<p>contains : <span th:text="${#lists.contains(itdragonList, 0)}"/></p>
<p>sort : <span th:text="${#lists.sort(itdragonList)}"/></p>
</div>
<h3>#maps </h3>
<div th:if="${not #maps.isEmpty(itdragonMap)}">
<p>size : <span th:text="${#maps.size(itdragonMap)}"/></p>
<p>containsKey : <span th:text="${#maps.containsKey(itdragonMap, 'thName')}"/></p>
<p>containsValue : <span th:text="${#maps.containsValue(itdragonMap, '#maps')}"/></p>
</div>
<h3>#dates </h3>
<div>
<p>format : <span th:text="${#dates.format(itdragonDate)}"/></p>
<p>custom format : <span th:text="${#dates.format(itdragonDate, 'yyyy-MM-dd HH:mm:ss')}"/></p>
<p>day : <span th:text="${#dates.day(itdragonDate)}"/></p>
<p>month : <span th:text="${#dates.month(itdragonDate)}"/></p>
<p>monthName : <span th:text="${#dates.monthName(itdragonDate)}"/></p>
<p>year : <span th:text="${#dates.year(itdragonDate)}"/></p>
<p>dayOfWeekName : <span th:text="${#dates.dayOfWeekName(itdragonDate)}"/></p>
<p>hour : <span th:text="${#dates.hour(itdragonDate)}"/></p>
<p>minute : <span th:text="${#dates.minute(itdragonDate)}"/></p>
<p>second : <span th:text="${#dates.second(itdragonDate)}"/></p>
<p>createNow : <span th:text="${#dates.createNow()}"/></p>
</div>
</body>
</html>
后台给负责给变量赋值,和跳转页面。
@RequestMapping("varexpressions")
public String varexpressions(ModelMap map) {
map.put("itdragonStr", "itdragonBlog");
map.put("itdragonBool", true);
map.put("itdragonArray", new Integer[]{1,2,3,4});
map.put("itdragonList", Arrays.asList(1,3,2,4,0));
Map itdragonMap = new HashMap();
itdragonMap.put("thName", "${#...}");
itdragonMap.put("desc", "变量表达式内置方法");
map.put("itdragonMap", itdragonMap);
map.put("itdragonDate", new Date());
map.put("itdragonNum", 888.888D);
return "grammar/varexpressions";
}
数学运算
逻辑运算
比较运算(为避免转义尴尬,可以使用括号中的英文进行比较运算!)
条件运算
if/unless
使用th:if和th:unless属性进行条件判断,th:unless于th:if恰好相反,只有表达式中的条件不成立,才会显示其内容。
<td ><span th:if="${items.price gt 1000}" >精品</span></td>
<td ><span th:unless="${items.price gt 1000}" >次品</span></td>
switch
<div th:switch="${items.name}">
<p th:case="'aa'">aaaaaaaaa</p>
<p th:case="'bb'">bbbbbbb</p>
<p th:case="'cc'">cccccccc</p>
</div>
th:each
thymeleaf的th:each常见用法
一.th:eath迭代集合用法:
<table border="1" id="stuTable">
<tr>
<td>是否选中</td>
<td>编号</td>
<td>姓名</td>
<td>年龄</td>
</tr>
<tr th:each="stu,userStat:${studentList}" >
<td><input th:type="checkbox" th:name="id" th:value="${stu.id}"></td>
<td th:text="${stu.id}">编号</td>
<td th:text="${stu.name}">姓名</td>
<td th:text="${stu.age}">年龄</td>
</tr>
</table>
二.迭代下标变量用法:
状态变量定义在一个th:每个属性和包含以下数据:
1.当前迭代索引,从0开始。这是索引属性。index
2.当前迭代索引,从1开始。这是统计属性。count
3.元素的总量迭代变量。这是大小属性。 size
4.iter变量为每个迭代。这是目前的财产。 current
5.是否当前迭代是奇数还是偶数。这些even/odd的布尔属性。
6.是否第一个当前迭代。这是first布尔属性。
7.是否最后一个当前迭代。这是last布尔属性。
用法实例:
<table border="1" id="stuTable">
<tr>
<td>是否选中</td>
<td>编号</td>
<td>姓名</td>
<td>年龄</td>
</tr>
<tr th:each="stu,userStat:${studentList}" th:class="${userStat.odd}?'odd':'even'">
<td th:text="${userStat.index}"></td>
<td><input th:type="checkbox" th:name="id" th:value="${stu.id}"></td>
<td th:text="${stu.id}">编号</td>
<td th:text="${stu.name}">姓名</td>
<td th:text="${stu.age}">年龄</td>
</tr>
</table>
package com.test.action;
import com.test.pojo.Items;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Controller
@RequestMapping("thymeleaf")
public class ThymeleafAction {
@RequestMapping("/testThymeleaf")
public String testThymeleaf(Model model)
{
model.addAttribute("uname","zhangsan");
Items items=new Items();
items.setId(1);
items.setName("iphone");
items.setDetail("9999");
items.setPrice(6000);
model.addAttribute("items",items);
List<Items> itemsList=new ArrayList<Items>();
itemsList.add(new Items(101,"aa",1000,"aaa","1.jpg",new Date()));
itemsList.add(new Items(102,"bb",1000,"aaa","1.jpg",new Date()));
itemsList.add(new Items(103,"cc",1000,"aaa","1.jpg",new Date()));
model.addAttribute("itemsList",itemsList);
return "testThymeleaf";
}
@RequestMapping("/showItemsById")
public String showItemsById(Model model,int id)
{
model.addAttribute("id",id);
return "showItemsById";
}
}
tems);
List<Items> itemsList=new ArrayList<Items>();
itemsList.add(new Items(101,"aa",1000,"aaa","1.jpg",new Date()));
itemsList.add(new Items(102,"bb",1000,"aaa","1.jpg",new Date()));
itemsList.add(new Items(103,"cc",1000,"aaa","1.jpg",new Date()));
model.addAttribute("itemsList",itemsList);
return "testThymeleaf";
}
@RequestMapping("/showItemsById")
public String showItemsById(Model model,int id)
{
model.addAttribute("id",id);
return "showItemsById";
}
}
大厂是大部分程序员的梦想,而进大厂的门槛也是比较高的,所以这里整理了一份阿里、美团、滴滴、头条等大厂面试大全其中概括的知识点有:Java基础、spring、springmvc、springboot、springcloud、JVM、Tomcat、dubbo、netty、zookeeper共有500+道面试题
面试题整理十分全面,文末还有答案解析!(文章比较长,耐心看完,让你面试提升一大截!)
获取以下面试专题答案的朋友们请转发此文关注我私信回复“面试资料”即可获取
Java基础124道面试答案
JVM 40道面试答案
Spring 80道面试题答案
SpringMVC 30道面试答案
SpringBoot 40道面试答案
SpringCloud 34道面试答案
Mybatis 面试答案
Redis 70道面试答案解析
Dubbo面试答案
Tomcat面试答案
Zookeeper 面试答案
Netty 面试答案
获取以上面试专题答案的朋友们请转发此文关注我私信回复“面试资料”即可获取
面试答案汇总
Java核心知识283页覆盖了JVM、锁、并发、Java反射、Spring原理、微服务、Zookeeper、数据库、数据结构等大量知识点
如果需要获取到这个【核心知识点整理】文档的话帮忙转发一下然后再关注我私信回复“面试资料”得到获取方式吧!
*请认真填写需求信息,我们会在24小时内与您取得联系。