日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

10 在Spring Cloud中使用Hystrix

發(fā)布時(shí)間:2025/4/5 60 豆豆
生活随笔 收集整理的這篇文章主要介紹了 10 在Spring Cloud中使用Hystrix 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

  Hystrix主要用于保護(hù)調(diào)用服務(wù)的一方,如果被調(diào)用的服務(wù)發(fā)生故障,符合一定條件,就會(huì)開啟斷路器對(duì)調(diào)用的程序進(jìn)行隔離。

1.準(zhǔn)備測(cè)試程序

  在進(jìn)行Spring Cloud整合Hystrix之前,我們先準(zhǔn)備好測(cè)試程序。測(cè)試程序所用的項(xiàng)目如下:

  > hystrix-server:該項(xiàng)目作為Eureka服務(wù)器,端口為8761。

  > hystrix-provider: 該項(xiàng)目作為服務(wù)的提供者,這里只需要啟動(dòng)一個(gè)實(shí)例,端口為默認(rèn)端口8080,提供person/{personId}服務(wù),它根據(jù)personId的參數(shù)返回一個(gè)Penson實(shí)例,另外還會(huì)提供一個(gè)/hello服務(wù),返回普通的字符串。

  > hystrix-invoker: 該項(xiàng)目作為服務(wù)調(diào)用者,使用的端口是9000。

  項(xiàng)目的目錄結(jié)構(gòu)如下

?  

  本文主要會(huì)使用到hystrix-invoker項(xiàng)目來介紹如何在Spring Cloud中使用Hystrix,故下面會(huì)詳細(xì)介紹hystrix-invoker項(xiàng)目,對(duì)于hystrix-server和 hystrix-provider這兩個(gè)項(xiàng)目不會(huì)詳細(xì)介紹。

2.Spring Cloud整合Hystrix

  為服務(wù)調(diào)用者(hystrix-invoker)項(xiàng)目添加相關(guān)的依賴(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

  在服務(wù)調(diào)用者的應(yīng)用啟動(dòng)類中,加入啟動(dòng)斷路器的注解,應(yīng)用啟動(dòng)類代碼清單如下

  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 阿遠(yuǎn)* 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);} }

   新建服務(wù)類,在服務(wù)方法中調(diào)用服務(wù),代碼清單如下

  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 阿遠(yuǎn)* Date: 2018/9/1* Time: 14:43*/ @Service public class PersonService {@Autowiredprivate RestTemplate restTemplate;@HystrixCommand(fallbackMethod = "getPersonFallback")public Person getPerson(Integer id) {// 使用RestTemplate調(diào)用Eureka服務(wù)Person person = restTemplate.getForObject("http://hystrix-privoder/person/{personId}", Person.class, id);return person;}/*** 定義回退方法* 主要這里傳的參數(shù)與上面的一樣,否則會(huì)報(bào)方法找不到的錯(cuò)誤*/public Person getPersonFallback(Integer id) {Person person = new Person();person.setId(0);person.setAge(21);person.setName("fallback");person.setMessage("request error");return person;} }

  服務(wù)類中注入了RestTemplate,服務(wù)方法使用@HystrixCommand注解進(jìn)行修飾,并且配置了回退方法。@HystrixCommand注解由Hystrix的javanica項(xiàng)目提供,該項(xiàng)目主要是為了簡(jiǎn)化Hystrix的使用。被@HstrixCommand修飾的方法,Hystrix會(huì)使用AspectJ對(duì)其進(jìn)行代理,Spring會(huì)將相關(guān)的類轉(zhuǎn)換成Bean放到容器中,在Spring Cloud中,我們無須過多關(guān)心Hystrix的命令管理。

  注意:此處的回退方法的參數(shù)需要與@HystrixCommand注解的方法一樣,否則在后面調(diào)用該方法時(shí)會(huì)報(bào)如下錯(cuò)誤

  接下來,編寫控制器來調(diào)用服務(wù)類的方法,代碼清單如下

  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 阿遠(yuǎn)* 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;} }

  控制器比較簡(jiǎn)單,直接注入PersonService,然后調(diào)用方法即可。按照以下步驟啟動(dòng)集群:

  > 啟動(dòng)hystrix-server項(xiàng)目

  > 啟動(dòng)hystrix-provider項(xiàng)目

  > 啟動(dòng)hystrix-invoker項(xiàng)目

  打開瀏覽器,訪問http://localhost:9000/router/0,輸出如下

  接下來,我們停止hystrix-provider項(xiàng)目,再訪問http://localhost:9000/router/0,輸出如下

  可以看到,程序直接調(diào)用了回退的方法。

轉(zhuǎn)載于:https://www.cnblogs.com/a-yuan/p/9573501.html

總結(jié)

以上是生活随笔為你收集整理的10 在Spring Cloud中使用Hystrix的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。