Ribbon 是一個負載均衡客戶端, 能夠很好的控制HTTP和TCP的一些行爲;java
在微服務架構中, Ribbon 組件負責 每一個獨立服務 之間的 相互通訊調用;git
Hystrix 斷路由處理; web
若是單個服務出現問題, 調用這個服務就會出現線程阻塞, 此時如有大量的請求涌入, Servlet容器的線程資源會被消耗完畢, 致使服務癱瘓.spring
服務與服務之間的依賴性, 故障會傳播, 會對整個微服務系統形成災難性的嚴重後果, 這就是服務故障的「雪崩」效應. apache
斷路打開後,可用避免連鎖故障,fallback方法能夠直接返回一個固定值。架構
1, 啓動註冊中心; 2, 啓動服務B;app
1, cloud-c pom.xml負載均衡
<?xml version="1.0" encoding="UTF-8"?> <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.gy.cloud</groupId> <artifactId>cloud</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>cloud-c</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>cloud-c</name> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- netflix-ribbon 該包能夠不用引用, 其實 eureka-client 已經自動引入了該包 --> <!-- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> --> <!-- 熔斷, 服務調用失敗後的保護組件 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> </dependencies> </project>
2, cloud-c application.ymlmaven
server: port: 8763 spring: application: name: service-c eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
3, cloud-c CloudCApplication 啓動類 : spring-boot
package com.gy.cloud.cloudc; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.hystrix.EnableHystrix; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @EnableHystrix @EnableEurekaClient @SpringBootApplication public class CloudCApplication { public static void main(String[] args) { SpringApplication.run(CloudCApplication.class, args); System.out.println("=== 消費服務C啓動成功 ==="); } @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }
4, cloud-c Controller 接口 :
package com.gy.cloud.cloudc; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HiController { @Autowired private HiService hiService; @GetMapping("hi") public Object hi(String name) { name = name == null ? "SERVICE-C" : name; return hiService.hi(name); } }
5, 調用 SERVICE-B
package com.gy.cloud.cloudc; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service public class HiService { @Autowired private RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "hiError") public String hi(String name) { System.out.println(name); String hi = restTemplate.getForObject("http://SERVICE-B/hi?name=" + name, String.class); System.out.println(hi); return hi; } // 熔斷: 當服務B調用失敗, 執行此方法 public String hiError(String name) { return "Hi, " + name +", Sorry Error !"; } }
6, 啓動 SERVICE-C , 訪問 : http://localhost:8763/hi
7, 斷開 SERVICE-B , 訪問 : http://localhost:8763/hi
學習文檔
方誌朋的博客 : https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f2-ribbon/
項目源碼: https://gitee.com/ge.yang/spring-demo/tree/master/cloud