在Hystrix中實現(xiàn)了線程隔離、斷路器等一系列的服務保護功能,本文都沒有涉及,也沒有和spring cloud進行整合,本文內(nèi)容:
- Hystrix命令的基本使用
- 當依賴的服務發(fā)生延遲時,對客戶端進行保護
- 回退方法的使用
1 服務提供端
首先需要明確一點,Hystrix是在客戶端使用的,先創(chuàng)建一個服務端項目,提供服務,一個簡單的spring boot項目即可。
提供兩個服務,normalHello可以正常調用;errorHello至少10秒后才會返回結果,Hystrix處于對客戶端的保護,會認為該服務超時。
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@RequestMapping(value = "/normalHello", method = RequestMethod.GET)
public String normalHello() {
return "Hello World";
}
@RequestMapping(value = "/errorHello", method = RequestMethod.GET)
public String errorHello() throws Exception {
Thread.sleep(10000);
return "Error Hello World";
}
}
2 客戶端
再創(chuàng)建一個客戶端項目,在該項目中使用Hystrix,pom依賴如下:
<dependencies>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-core</artifactId>
<version>1.5.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<version>1.7.25</version>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
</dependencies>
Hystrix是命令設計模式的實現(xiàn)者,命令執(zhí)行者和命令調用者完全解耦,將服務的調用作為一個命令。如果想要調用服務器提供的normalHello服務,創(chuàng)建一個實現(xiàn)HystrixCommand的類,并實現(xiàn)run方法,在run方法中實現(xiàn)對服務的調用。
客戶端通過httpclient調用服務
public class HelloCommand extends HystrixCommand<String> {
public HelloCommand() {
super(HystrixCommandGroupKey.Factory.asKey("TestGroup"));
}
protected String run() throws Exception {
String url = "http://localhost:8080/normalHello";
HttpGet httpget = new HttpGet(url);
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpResponse response = httpclient.execute(httpget);
return EntityUtils.toString(response.getEntity());
}
}
當調用延遲的服務時,需要再實現(xiàn)一個getFallback方法,就是上篇文章中的回退方法,當被調用的服務出現(xiàn)問題時,直接調用回退方法。
public class ErrorCommand extends HystrixCommand<String> {
public ErrorCommand() {
super(HystrixCommandGroupKey.Factory.asKey("TestGroup"));
}
protected String run() throws Exception {
String url = "http://localhost:8080/errorHello";
HttpGet httpget = new HttpGet(url);
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpResponse response = httpclient.execute(httpget);
return EntityUtils.toString(response.getEntity());
}
protected String getFallback() {
System.out.println("fall back method");
return "fall back hello";
}
}
3 服務調用
然后,實例化一個調用服務的命令,執(zhí)行命令即可。執(zhí)行ErrorCommand命令時,因為服務延遲,會直接調用getFallback方法返回結果:
public class NormalMain {
public static void main(String[] args) {
HelloCommand command = new HelloCommand();
String result = command.execute();
System.out.println(result);
}
}
public class ErrorMain {
public static void main(String[] args) {
ErrorCommand command = new ErrorCommand();
String result = command.execute();
System.out.println(result);
}
}