javascript
10 在Spring Cloud中使用Hystrix
Hystrix主要用于保護調用服務的一方,如果被調用的服務發(fā)生故障,符合一定條件,就會開啟斷路器對調用的程序進行隔離。
1.準備測試程序
在進行Spring Cloud整合Hystrix之前,我們先準備好測試程序。測試程序所用的項目如下:
> hystrix-server:該項目作為Eureka服務器,端口為8761。
> hystrix-provider: 該項目作為服務的提供者,這里只需要啟動一個實例,端口為默認端口8080,提供person/{personId}服務,它根據(jù)personId的參數(shù)返回一個Penson實例,另外還會提供一個/hello服務,返回普通的字符串。
> hystrix-invoker: 該項目作為服務調用者,使用的端口是9000。
項目的目錄結構如下
?
本文主要會使用到hystrix-invoker項目來介紹如何在Spring Cloud中使用Hystrix,故下面會詳細介紹hystrix-invoker項目,對于hystrix-server和 hystrix-provider這兩個項目不會詳細介紹。
2.Spring Cloud整合Hystrix
為服務調用者(hystrix-invoker)項目添加相關的依賴(spring-cloud-starter-hystrix),pom.xml代碼清單如下
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><groupId>com.triheart</groupId><artifactId>hystrixinvoker</artifactId><version>1.0-SNAPSHOT</version><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Dalston.SR1</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-ribbon</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId><version>1.5.3.RELEASE</version></dependency></dependencies> </project> View Code在服務調用者的應用啟動類中,加入啟動斷路器的注解,應用啟動類代碼清單如下
Invoker.java
package com.triheart.hystrixinvoker;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate;/*** @author 阿遠* Date: 2018/9/1* Time: 14:31*/ @SpringBootApplication @EnableDiscoveryClient @EnableCircuitBreaker public class InvokerApp {@LoadBalanced@Beanpublic RestTemplate getRestTemplate() {return new RestTemplate();}public static void main(String[] args){SpringApplication.run(InvokerApp.class, args);} }新建服務類,在服務方法中調用服務,代碼清單如下
PersonService.java
package com.triheart.hystrixinvoker;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;/*** @author 阿遠* Date: 2018/9/1* Time: 14:43*/ @Service public class PersonService {@Autowiredprivate RestTemplate restTemplate;@HystrixCommand(fallbackMethod = "getPersonFallback")public Person getPerson(Integer id) {// 使用RestTemplate調用Eureka服務Person person = restTemplate.getForObject("http://hystrix-privoder/person/{personId}", Person.class, id);return person;}/*** 定義回退方法* 主要這里傳的參數(shù)與上面的一樣,否則會報方法找不到的錯誤*/public Person getPersonFallback(Integer id) {Person person = new Person();person.setId(0);person.setAge(21);person.setName("fallback");person.setMessage("request error");return person;} }服務類中注入了RestTemplate,服務方法使用@HystrixCommand注解進行修飾,并且配置了回退方法。@HystrixCommand注解由Hystrix的javanica項目提供,該項目主要是為了簡化Hystrix的使用。被@HstrixCommand修飾的方法,Hystrix會使用AspectJ對其進行代理,Spring會將相關的類轉換成Bean放到容器中,在Spring Cloud中,我們無須過多關心Hystrix的命令管理。
注意:此處的回退方法的參數(shù)需要與@HystrixCommand注解的方法一樣,否則在后面調用該方法時會報如下錯誤
接下來,編寫控制器來調用服務類的方法,代碼清單如下
InvokerController.java
package com.triheart.hystrixinvoker;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;/*** @author 阿遠* Date: 2018/9/1* Time: 14:53*/ @RestController @Configuration public class InvokerController {@Autowiredprivate PersonService personService;@RequestMapping(value = "/router/{personId}", method = RequestMethod.GET)public Person router(@PathVariable Integer personId) {Person person = personService.getPerson(personId);return person;} }控制器比較簡單,直接注入PersonService,然后調用方法即可。按照以下步驟啟動集群:
> 啟動hystrix-server項目
> 啟動hystrix-provider項目
> 啟動hystrix-invoker項目
打開瀏覽器,訪問http://localhost:9000/router/0,輸出如下
接下來,我們停止hystrix-provider項目,再訪問http://localhost:9000/router/0,輸出如下
可以看到,程序直接調用了回退的方法。
轉載于:https://www.cnblogs.com/a-yuan/p/9573501.html
總結
以上是生活随笔為你收集整理的10 在Spring Cloud中使用Hystrix的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: greendao引起的NoClassDe
- 下一篇: 部署