Spring Cloud之Feign客户端超时时间配置

浅浅的花香味﹌ 2022-01-28 11:27 415阅读 0赞

Spring Cloud之Feign客户端超时时间配置

关于雪崩效应:

默认情况下tomcat只有一个线程去处理客户端发送的所有请求。高并发情况下,如果客户端请求都在同一接口,tomcat的所有线程池去处理,导致其他接口服务访问不了,等待。

Tomcat有个线程池,每个线程去处理客户端发送每次请求。

在parent项目里面再创建一个项目,common

Eureka server: 略

Member :

service

1179709-20181114221543903-1131685869.png

  1. package com.toov5.api.entity;
  2. import lombok.Data;
  3. @Data
  4. public class UserEntity {
  5. private String name;
  6. private Integer age;
  7. }
  8. package com.toov5.api.service;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RequestParam;
  11. import org.springframework.web.bind.annotation.RestController;
  12. import com.toov5.api.entity.UserEntity;
  13. import com.toov5.base.ResponseBase;
  14. @RestController
  15. public interface IMemberService {
  16. @RequestMapping("/getMember") //接口加@RequestMapping 被其他项目调用时候 feign客户端可以继承
  17. public UserEntity getMember(@RequestParam("name") String name);
  18. @RequestMapping("/getUserInfo")
  19. public ResponseBase getUserInfo();
  20. }

实现类:

1179709-20181114221632326-636747972.png

  1. package com.toov5.api.service.impl;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RequestParam;
  4. import org.springframework.web.bind.annotation.RestController;
  5. import com.toov5.api.entity.UserEntity;
  6. import com.toov5.api.service.IMemberService;
  7. import com.toov5.base.BaseApiService;
  8. import com.toov5.base.ResponseBase;
  9. //注意加在实现类上面!!! 接口不能加 接口不能被实例化
  10. @RestController
  11. public class MemberServiceImpl extends BaseApiService implements IMemberService {
  12. @RequestMapping("/getMember")
  13. public UserEntity getMember(@RequestParam("name") String name) {
  14. UserEntity userEntity = new UserEntity();
  15. userEntity.setName(name);
  16. userEntity.setAge(10);
  17. return userEntity;
  18. }
  19. @RequestMapping("/getUserInfo")
  20. public ResponseBase getUserInfo() {
  21. try {
  22. Thread.sleep(1500);
  23. } catch (Exception e) {
  24. }
  25. return setResultSuccess("订单服务接口调用会员服务接口成功....");
  26. }
  27. }

启动类

  1. package com.toov5.api.service.impl;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  5. import org.springframework.cloud.openfeign.EnableFeignClients;
  6. @SpringBootApplication (scanBasePackages={"com.toov5.*"})
  7. @EnableEurekaClient
  8. @EnableFeignClients
  9. public class AppMember {
  10. public static void main(String[] args) {
  11. SpringApplication.run(AppMember.class, args);
  12. }
  13. }

yml

  1. ###服务启动端口号
  2. server:
  3. port: 8000
  4. ###服务名称(服务注册到eureka名称)
  5. spring:
  6. application:
  7. name: app-toov5-member
  8. ###服务注册到eureka地址
  9. eureka:
  10. client:
  11. service-url:
  12. defaultZone: http://localhost:8100/eureka
  13. ###因为该应用为注册中心,不会注册自己
  14. register-with-eureka: true
  15. ###是否需要从eureka上获取注册信息
  16. fetch-registry: true

 pom:

  1. <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">
  2. <modelVersion>4.0.0</modelVersion>
  3. <parent>
  4. <groupId>com.toov5</groupId>
  5. <artifactId>parent</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. </parent>
  8. <artifactId>toov5-api-member-service-impl</artifactId>
  9. <dependencies>
  10. <dependency>
  11. <groupId>com.toov5</groupId>
  12. <artifactId>toov5-api-member-service</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. </dependency>
  15. <dependency>
  16. <groupId>com.toov5</groupId>
  17. <artifactId>toov5-api-order-service</artifactId>
  18. <version>0.0.1-SNAPSHOT</version>
  19. </dependency>
  20. </dependencies>
  21. </project>

Order:

service

1179709-20181114221811910-692241415.png

pom:

  1. <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">
  2. <modelVersion>4.0.0</modelVersion>
  3. <parent>
  4. <groupId>com.toov5</groupId>
  5. <artifactId>toov5-api-service</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. </parent>
  8. <artifactId>toov5-api-order-service</artifactId>
  9. <dependencies>
  10. <dependency>
  11. <groupId>com.toov5</groupId>
  12. <artifactId>toov5.common</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. </dependency>
  15. </dependencies>
  16. </project>

 接口

  1. package com.toov5.api.service;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import com.toov5.base.ResponseBase;
  4. public interface IOrderService {
  5. //订单服务带哦用会员服务接口信息fegin
  6. @RequestMapping("/orderToMember")
  7. public String orderToMember(String name);
  8. //订单服务接口调用会员服务接口
  9. @RequestMapping("/orderToMemberUserInfo")
  10. public ResponseBase orderToMemberUserInfo();
  11. }

 1179709-20181114221934012-912158528.png

pom:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>com.toov5</groupId>
  7. <artifactId>parent</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. </parent>
  10. <artifactId>toov5-api-order-service-impl</artifactId>
  11. <dependencies>
  12. <dependency>
  13. <groupId>com.toov5</groupId>
  14. <artifactId>toov5-api-order-service</artifactId>
  15. <version>0.0.1-SNAPSHOT</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>com.toov5</groupId>
  19. <artifactId>toov5-api-member-service</artifactId>
  20. <version>0.0.1-SNAPSHOT</version>
  21. </dependency>
  22. </dependencies>
  23. </project>

  

  1. package com.toov5.api.service.impl;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. import com.toov5.api.entity.UserEntity;
  6. import com.toov5.api.feign.MemberServiceFeign;
  7. import com.toov5.api.service.IOrderService;
  8. import com.toov5.base.ResponseBase;
  9. @RestController
  10. public class OrderServiceImpl implements IOrderService {
  11. @Autowired
  12. private MemberServiceFeign memberServiceFeign;
  13. @RequestMapping("/orderToMmeber")
  14. public String orderToMember(String name) {
  15. UserEntity user = memberServiceFeign.getMember(name);
  16. return user==null ? "没有找到用户先关信息" : user.toString();
  17. }
  18. @RequestMapping("/orderToMemberUserInfo")
  19. public ResponseBase orderToMemberUserInfo() {
  20. return memberServiceFeign.getUserInfo();
  21. }
  22. }
  23. package com.toov5.api.feign;
  24. import org.springframework.cloud.openfeign.FeignClient;
  25. import com.toov5.api.service.IMemberService;
  26. //避免了冗余代码 直接过来就ok了
  27. @FeignClient("app-toov5-member")
  28. public interface MemberServiceFeign extends IMemberService {
  29. //实体类是存放接口项目还是存放在实现项目 实体类存放在接口项目里面
  30. //实体类和定义接口信息存放在接口项目
  31. //代码实现放在接口实现类里面
  32. }

yml:

  1. ###服务启动端口号
  2. server:
  3. port: 8001
  4. ###服务名称(服务注册到eureka名称)
  5. spring:
  6. application:
  7. name: app-toov5-order
  8. ###服务注册到eureka地址
  9. eureka:
  10. client:
  11. service-url:
  12. defaultZone: http://localhost:8100/eureka
  13. ###因为该应用为注册中心,不会注册自己
  14. register-with-eureka: true
  15. ###是否需要从eureka上获取注册信息
  16. fetch-registry: true
  17. ###设置feign客户端超时时间
  18. ribbon: ###指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间。 ReadTimeout: 5000 ###指的是建立连接后从服务器读取到可用资源所用的时间。 ConnectTimeout: 5000
  19. #spring cloud 默认开启ribbon
  20. ribbon:
  21. ###指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间。
  22. ReadTimeout: 5000
  23. ###指的是建立连接后从服务器读取到可用资源所用的时间。
  24. ConnectTimeout: 5000
  25. #spring cloud 默认开启ribbon

  

 启动类

  1. package com.toov5.api;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  5. import org.springframework.cloud.openfeign.EnableFeignClients;
  6. @SpringBootApplication(scanBasePackages={"com.toov5.*"})
  7. @EnableEurekaClient
  8. @EnableFeignClients
  9. public class AppOrder {
  10. public static void main(String[] args) {
  11. SpringApplication.run(AppOrder.class, args);
  12. }
  13. }

 结果:

1179709-20181114222214101-289355787.png

发表评论

表情:
评论列表 (有 0 条评论,415人围观)

还没有评论,来说两句吧...

相关阅读