Spring Cloud之Feign客户端超时时间配置
关于雪崩效应:
默认情况下tomcat只有一个线程去处理客户端发送的所有请求。高并发情况下,如果客户端请求都在同一接口,tomcat的所有线程池去处理,导致其他接口服务访问不了,等待。
Tomcat有个线程池,每个线程去处理客户端发送每次请求。
在parent项目里面再创建一个项目,common
Eureka server: 略
Member :
service
package com.toov5.api.entity; import lombok.Data; @Data public class UserEntity { private String name; private Integer age; }
package com.toov5.api.service; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.toov5.api.entity.UserEntity; import com.toov5.base.ResponseBase; @RestController public interface IMemberService { @RequestMapping("/getMember") //接口加@RequestMapping 被其他项目调用时候 feign客户端可以继承 public UserEntity getMember(@RequestParam("name") String name); @RequestMapping("/getUserInfo") public ResponseBase getUserInfo(); }
实现类:
package com.toov5.api.service.impl; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.toov5.api.entity.UserEntity; import com.toov5.api.service.IMemberService; import com.toov5.base.BaseApiService; import com.toov5.base.ResponseBase; //注意加在实现类上面!!! 接口不能加 接口不能被实例化 @RestController public class MemberServiceImpl extends BaseApiService implements IMemberService { @RequestMapping("/getMember") public UserEntity getMember(@RequestParam("name") String name) { UserEntity userEntity = new UserEntity(); userEntity.setName(name); userEntity.setAge(10); return userEntity; } @RequestMapping("/getUserInfo") public ResponseBase getUserInfo() { try { Thread.sleep(1500); } catch (Exception e) { } return setResultSuccess("订单服务接口调用会员服务接口成功...."); } }
启动类
package com.toov5.api.service.impl; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication (scanBasePackages={"com.toov5.*"}) @EnableEurekaClient @EnableFeignClients public class AppMember { public static void main(String[] args) { SpringApplication.run(AppMember.class, args); } }
yml
###服务启动端口号 server: port: 8000 ###服务名称(服务注册到eureka名称) spring: application: name: app-toov5-member ###服务注册到eureka地址 eureka: client: service-url: defaultZone: http://localhost:8100/eureka ###因为该应用为注册中心,不会注册自己 register-with-eureka: true ###是否需要从eureka上获取注册信息 fetch-registry: true
pom:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.toov5</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>toov5-api-member-service-impl</artifactId> <dependencies> <dependency> <groupId>com.toov5</groupId> <artifactId>toov5-api-member-service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.toov5</groupId> <artifactId>toov5-api-order-service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
Order:
service
pom:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.toov5</groupId> <artifactId>toov5-api-service</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>toov5-api-order-service</artifactId> <dependencies> <dependency> <groupId>com.toov5</groupId> <artifactId>toov5.common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
接口
package com.toov5.api.service; import org.springframework.web.bind.annotation.RequestMapping; import com.toov5.base.ResponseBase; public interface IOrderService { //订单服务带哦用会员服务接口信息fegin @RequestMapping("/orderToMember") public String orderToMember(String name); //订单服务接口调用会员服务接口 @RequestMapping("/orderToMemberUserInfo") public ResponseBase orderToMemberUserInfo(); }
pom:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.toov5</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>toov5-api-order-service-impl</artifactId> <dependencies> <dependency> <groupId>com.toov5</groupId> <artifactId>toov5-api-order-service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.toov5</groupId> <artifactId>toov5-api-member-service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
package com.toov5.api.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.toov5.api.entity.UserEntity; import com.toov5.api.feign.MemberServiceFeign; import com.toov5.api.service.IOrderService; import com.toov5.base.ResponseBase; @RestController public class OrderServiceImpl implements IOrderService { @Autowired private MemberServiceFeign memberServiceFeign; @RequestMapping("/orderToMmeber") public String orderToMember(String name) { UserEntity user = memberServiceFeign.getMember(name); return user==null ? "没有找到用户先关信息" : user.toString(); } @RequestMapping("/orderToMemberUserInfo") public ResponseBase orderToMemberUserInfo() { return memberServiceFeign.getUserInfo(); } }
package com.toov5.api.feign; import org.springframework.cloud.openfeign.FeignClient; import com.toov5.api.service.IMemberService; //避免了冗余代码 直接过来就ok了 @FeignClient("app-toov5-member") public interface MemberServiceFeign extends IMemberService { //实体类是存放接口项目还是存放在实现项目 实体类存放在接口项目里面 //实体类和定义接口信息存放在接口项目 //代码实现放在接口实现类里面 }
yml:
###服务启动端口号 server: port: 8001 ###服务名称(服务注册到eureka名称) spring: application: name: app-toov5-order ###服务注册到eureka地址 eureka: client: service-url: defaultZone: http://localhost:8100/eureka ###因为该应用为注册中心,不会注册自己 register-with-eureka: true ###是否需要从eureka上获取注册信息 fetch-registry: true ###设置feign客户端超时时间 ribbon: ###指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间。 ReadTimeout: 5000 ###指的是建立连接后从服务器读取到可用资源所用的时间。 ConnectTimeout: 5000 #spring cloud 默认开启ribbon
启动类
package com.toov5.api; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication(scanBasePackages={"com.toov5.*"}) @EnableEurekaClient @EnableFeignClients public class AppOrder { public static void main(String[] args) { SpringApplication.run(AppOrder.class, args); } }
结果: