Hystrix--學習筆記(4)
目錄
一、參考SpringCloud的官方文檔
--1、斷路器:Hystrix客戶端
--2、如何加入Hystrix
--3、如何包含Hystrix儀表板
二、實操
--1、代碼準備
--2、ribbon-demo具體配置
--3、運行結果
三、在feign中加入hystrix
--1、代碼準備
--2、feign-demo的具體配置
--3、運行結果
一、參考SpringCloud的官方文檔
1、斷路器:Hystrix客戶端
在微服務架構中,通常有多層服務調用。
較低級別的服務中的服務故障可能導致用戶級聯(lián)故障。當對特定服務的呼叫達到一定閾值時(Hystrix中的默認值為5秒內的20次故障),電路打開,不進行通話。在錯誤和開路的情況下,開發(fā)人員可以提供后備。
開放式電路會停止級聯(lián)故障,并允許不必要的或失敗的服務時間來愈合。回退可以是另一個Hystrix保護的調用,靜態(tài)數據或一個正常的空值。回退可能被鏈接,所以第一個回退使得一些其他業(yè)務電話又回到靜態(tài)數據。
2、如何加入Hystrix
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
example
@SpringBootApplication
@EnableCircuitBreaker
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
@Component
public class StoreIntegration {
@HystrixCommand(fallbackMethod = "defaultStores")
public Object getStores(Map<String, Object> parameters) {
//do stuff that might fail
}
public Object defaultStores(Map<String, Object> parameters) {
return /* something useful */;
}
}
3、如何包含Hystrix儀表板
要在項目中包含Hystrix儀表板,請使用組org.springframework.cloud
和工件ID spring-cloud-starter-hystrix-dashboard
的啟動器。
要運行Hystrix儀表板使用@EnableHystrixDashboard
注釋您的Spring Boot主類。然后訪問/hystrix
,并將儀表板指向Hystrix客戶端應用程序中的單個實例/hystrix.stream
端點。
二、實操
1、代碼準備
- 注冊中心euraka-demo-server(參考Eureka--學習筆記(1))
- 在原先的ribbon-demo基礎上修改(參考Ribbon--學習筆記(2))
2、ribbon-demo具體配置
啟動類
@EnableCircuitBreaker
@EnableHystrixDashboard
@SpringBootApplication
public class RibbonDemoApplication {
@Bean
@LoadBalanced //負載均衡
public RestTemplate restTemplate(){
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(RibbonDemoApplication.class, args);
}
}
降級服務配置
@RestController
@RequestMapping("/call")
public class CallController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/hello")
@HystrixCommand(fallbackMethod = "hasError")
public String hello(){
return restTemplate.getForObject("http://EUREKA-DEMO-CLIENT-1/",String.class);
}
@GetMapping("/hi/{name}")
public String hi(@PathVariable("name") String name){
return restTemplate.getForObject("http://EUREKA-DEMO-CLIENT-1/test/say/hello/" + name,String.class);
}
public String hasError(){
return "哎呀!出錯了!";
}
}
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
HystrixConfig.java
@Configuration
public class HystrixConfig {
@Bean
public ServletRegistrationBean getServlet(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
3、運行結果
ui的監(jiān)控界面
在瀏覽器中輸入localhost:8082/call/hello
在瀏覽器中輸入localhost:8082/hystrix
,之后再輸入http://localhost:8082/hystrix.stream
按下網頁按鈕Monitor Stream
,之后回到哎呀!出錯了
的網頁刷新4次,再切換回本網頁
三、在feign中加入hystrix
1、代碼準備
- 注冊中心euraka-demo-server(參考Eureka--學習筆記(1))
- 在原先的feign-demo基礎上修改(參考Feign--學習筆記(3))
2、feign-demo的具體配置
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
application.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8083
spring:
application:
name: feign-demo-consumer
feign:
hystrix:
enabled: true
降級配置
@Component
public class HystrixClientFallback implements HelloClient {
@Override
public String sayHello() {
return "哎呀!出錯了!!!";
}
}
設置回調
@FeignClient(name = "eureka-demo-client-1",fallback = HystrixClientFallback.class)
public interface HelloClient {
@GetMapping("/")
String sayHello();
}
3、運行結果
ui界面的監(jiān)控
在瀏覽器中輸入http://localhosst:8083/hello