在Hystrix中實(shí)現(xiàn)了線程隔離、斷路器等一系列的服務(wù)保護(hù)功能,本文都沒有涉及,也沒有和spring cloud進(jìn)行整合卖局,本文內(nèi)容:
- Hystrix命令的基本使用
- 當(dāng)依賴的服務(wù)發(fā)生延遲時(shí),對(duì)客戶端進(jìn)行保護(hù)
- 回退方法的使用
1 服務(wù)提供端
首先需要明確一點(diǎn)双霍,Hystrix是在客戶端使用的砚偶,先創(chuàng)建一個(gè)服務(wù)端項(xiàng)目,提供服務(wù)洒闸,一個(gè)簡(jiǎn)單的spring boot項(xiàng)目即可染坯。
提供兩個(gè)服務(wù),normalHello可以正常調(diào)用丘逸;errorHello至少10秒后才會(huì)返回結(jié)果单鹿,Hystrix處于對(duì)客戶端的保護(hù),會(huì)認(rèn)為該服務(wù)超時(shí)深纲。
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)建一個(gè)客戶端項(xiàng)目仲锄,在該項(xià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是命令設(shè)計(jì)模式的實(shí)現(xiàn)者湃鹊,命令執(zhí)行者和命令調(diào)用者完全解耦儒喊,將服務(wù)的調(diào)用作為一個(gè)命令。如果想要調(diào)用服務(wù)器提供的normalHello服務(wù)币呵,創(chuàng)建一個(gè)實(shí)現(xiàn)HystrixCommand的類怀愧,并實(shí)現(xiàn)run方法,在run方法中實(shí)現(xiàn)對(duì)服務(wù)的調(diào)用余赢。
客戶端通過httpclient調(diào)用服務(wù)
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());
}
}
當(dāng)調(diào)用延遲的服務(wù)時(shí)芯义,需要再實(shí)現(xiàn)一個(gè)getFallback方法,就是上篇文章中的回退方法妻柒,當(dāng)被調(diào)用的服務(wù)出現(xiàn)問題時(shí)扛拨,直接調(diào)用回退方法。
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 服務(wù)調(diào)用
然后蛤奢,實(shí)例化一個(gè)調(diào)用服務(wù)的命令鬼癣,執(zhí)行命令即可陶贼。執(zhí)行ErrorCommand命令時(shí)啤贩,因?yàn)榉?wù)延遲待秃,會(huì)直接調(diào)用getFallback方法返回結(jié)果:
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);
}
}