關於雪崩效應: html
默認狀況下tomcat只有一個線程去處理客戶端發送的全部請求。高併發狀況下,若是客戶端請求都在同一接口,tomcat的全部線程池去處理,致使其餘接口服務訪問不了,等待。web
Tomcat有個線程池,每一個線程去處理客戶端發送每次請求。spring
在parent項目裏面再建立一個項目,commonapache
Eureka server: 略api
Member :tomcat
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); } }
ymlapp
###服務啓動端口號 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); } }
結果: