关于雪崩效应:
默认情况下tomcat只有一个线程去处理客户端发送的所有请求。高并发情况下,如果客户端请求都在同一接口,tomcat的所有线程池去处理,导致其他接口服务访问不了,等待。
Tomcat有个线程池,每个线程去处理客户端发送每次请求。
1.业务描述
订单服务调用会员服务,服务服务处理逻辑代码中延迟1.5s,模拟处理业务逻辑
2.代码
2.1会员服务
package com.taotao.api.service.impl;
import com.fqyd.api.entity.User;
import com.fqyd.api.service.IMemberService;
import com.fqyd.util.BaseApiService;
import com.fqyd.util.BaseDataResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* Description:
* Author: wude
* Date: 2019/7/25 15:36
* Modified By:
*/
@RestController
public class MemberServiceImpl extends BaseApiService implements IMemberService {
@RequestMapping("getUserInfo")
@Override
public BaseDataResult getUserInfo() {
try {
//延迟1.5s
Thread.sleep(1500);
} catch (Exception e) {
}
return setResultSuccess("订单服务调用会员接口成功...");
}
package com.taotao.util;
/**
* Description:
* Author: wude
* Date: 2019/8/6 12:01
* Modified By:
*/
public class BaseApiService {
public BaseDataResult setResultErr(Integer code,String msg){
return setResult(code,msg,null);
}
/**
* 返回错误,可以传msg
* @param msg
* @return
*/
public BaseDataResult setResultError(String msg){
return setResult(500,msg,null);
}
/**
* 返回成功,可以穿data
* @param data
* @return
*/
public BaseDataResult setResultSuccess(Object data){
return setResult(200,"处理成功",data);
}
/**
* 返回成功,没有data值
* @return
*/
public BaseDataResult setResultSuccess(){
return setResult(200,"处理成功",null);
}
public BaseDataResult setResultSuccess(String msg){
return setResult(200,msg,null);
}
public BaseDataResult setResult(Integer code,String msg,Object data){
return new BaseDataResult(code,msg,data);
}
}
package com.taotao.api.service;
import com.fqyd.api.entity.User;
import com.fqyd.util.BaseDataResult;
import com.sun.xml.internal.org.jvnet.staxex.Base64Data;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* Description:
* Author: wude
* Date: 2019/7/25 14:58
* Modified By:
*/
@FeignClient("fqyd-member")
public interface IMemberService {
/**
* 获取会员信息
* @param name
* @return
*/
@RequestMapping("getMember")
public User getMember(@RequestParam("name") String name);
@RequestMapping("/getUserInfo")
public BaseDataResult getUserInfo();
fqyd-member会员服务的服务名
2.2订单服务
package com.taotao.api.feign;
import com.fqyd.api.entity.User;
import com.fqyd.api.service.IMemberService;
import org.springframework.cloud.openfeign.FeignClient;
/**
* Description:
* Author: wude
* Date: 2019/8/1 10:35
* Modified By:
*/
@FeignClient("fqyd-member")
public interface MemberServiceFeign extends IMemberService {
}
package com.taotao.api.service.impl;
import com.fqyd.api.entity.User;
import com.fqyd.api.feign.MemberServiceFeign;
import com.fqyd.api.service.IOrderService;
import com.fqyd.util.BaseDataResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* Description:
* Author: wude
* Date: 2019/7/25 17:39
* Modified By:
*/
@RestController
public class OrderServiceImpl implements IOrderService {
@Autowired
private MemberServiceFeign memberServiceFeign;
@RequestMapping("/orderToMemberUserInfo")
@Override
public BaseDataResult orderToMemberUserInfo() {
return memberServiceFeign.getUserInfo();
}
}
read time读取超时
解决方法:application.yml增加如下配置
###服务启动端口号
server:
port: 8001
tomcat:
max-threads: 10
###服务名称(服务注册到eureka名称)
spring:
application:
name: app-itmayiedu-order
###服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka
###因为该应用为注册中心,不会注册自己
register-with-eureka: true
###是否需要从eureka上获取注册信息
fetch-registry: true
#ribbon的超时时间
ribbon:
ReadTimeout: 5000
ConnectTimeout: 5000