您现在的位置是:主页 > 数据库技术 > 数据库技术
Spring Cloud如何整合Hystrix
IDCBT2021-12-25【服务器技术】人已围观
简介这篇文章给大家分享的是有关Spring Cloud如何整合Hystrix的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。 19 Spring Cloud整合Hystrix Hystrix主要用于保护调用服
这篇文章给大家分享的是有关Spring Cloud如何整合Hystrix的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
19 Spring Cloud整合HystrixHystrix主要用于保护调用服务的一方,如果被调用的服务发生故障,符合一定条件,就开启断路器,对调用的程序进行隔离。在开始讲述本章的内容前,先准备测试项目,本章例子所使用的项目如下:
spring-hystrix-server:Eureka服务器,端口为8761,代码目录codes\06\6.4\spring-hystrix-server。
spring-hystrix-provider:服务提供者,本例只需要启动一个实例,端口为8080,默认提供“/person/{personId}”服务,根据personId参数返回一个Person实例,另外再提供一个“/hello”服务,返回普通的字符串。代码目录为codes\06\6.4\spring-hystrix-provider
spring-hystrix-invoker:服务调用者,9000端口,代码目录codes\06\6.4\spring-hystrix-invoker。
整合Hystrix为服务调用者(spring-hystrix-invoker)项目添加依赖,添加后的依赖如下:
<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>
在服务调用者的应用启动类中,加入启用断路器的注解,请见以下代码片断:
@SpringBootApplication @EnableDiscoveryClient @EnableCircuitBreaker public class InvokerApplication { @LoadBalanced @Bean public RestTemplate getRestTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(InvokerApplication.class, args); } }
新建服务类,在服务方法中调用服务,请见代码清单6-17。
代码清单6-17:
标签:很赞哦! ()