SpringCloud(六) Hystrix入门
2018-04-20 13:38
344 查看
前提
- 一个可用的Eureka注册中心(文中以之前博客中双节点注册中心,不重要)
- 一个连接到这个注册中心的服务提供者
快速入门
项目搭建
搭建一个新maven项目,artifactid为Ribbon-consum-hystrix,依赖是在ribbon-customer项目上加入hystrix依赖,这里直接给出,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.cnblogs.hellxz</groupId> <artifactId>Ribbon-consum-hystrix</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> <version>RELEASE</version> </dependency> <!--暴露各种指标--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--hystrix熔断器--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> <version>RELEASE</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>RELEASE</version> <scope>import</scope> <type>pom</type> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
新建包com.cnblogs.hellxz,新建应用入口类RibbonApplication:
package com.cnblogs.hellxz; import org.springframework.boot.SpringApplication; import org.springframework.cloud.client.SpringCloudApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; /** * @Author : Hellxz * @Description: Hystrix熔断器使用HelloWorld * @Date : 2018/4/20 10:03 */ @SpringCloudApplication public class RibbonApplication { @Bean @LoadBalanced RestTemplate getRestTemplate(){ return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(RibbonApplication.class, args); } }
resources目录下添加配置文件application.yml,其中defaultZone的url为实际注册中心defaultZone地址
server: port: 8088 spring: application: name: ribbon-hystrix eureka: client: serviceUrl: defaultZone: http://peer1:1111/eureka/,http://peer2:1112/eureka/
在com.cnblogs.hellxz包下分别新建controller包和service包,controller包中新建RibbonController,代码如下:
package com.cnblogs.hellxz.controller; import com.cnblogs.hellxz.servcie.RibbonService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @Author : Hellxz * @Description: Ribbon消费者controller * @Date : 2018/4/20 10:07 */ @RestController public class RibbonController { @Autowired RibbonService service; @GetMapping("/hystrix/test") public String helloHystrix(){ //调用服务层方法 return service.helloService(); } }
service包下新建RibbonService,代码如下:
package com.cnblogs.hellxz.servcie; 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 : Hellxz * @Description: Ribbon服务层 * @Date : 2018/4/20 10:08 */ @Service public class RibbonService { @Autowired private RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "hystrixFallback") public String helloService(){ //调用服务提供者接口,正常则返回hello字符串 return restTemplate.getForEntity("http://hello-service/hello", String.class).getBody(); } /** * 调用服务失败处理方法 * @return “error" */ public String hystrixFallback(){ return "error"; } }
项目最后的结构如图
测试
分别启动
注册中心--->
服务提供者--->
刚新建的项目
访问http://localhost:8088/hystrix/test
因为现在服务是正常的,返回了
hello,如图
此时关闭服务提供者项目,再次访问,返回了
error,如图
相关文章推荐
- SpringCloud入门教学|第四篇:断路器(Hystrix)
- springCloud学习02之断路器Hystrix-turbine监控-ribbo/feign对Hystrix的支持
- 史上最简单的SpringCloud教程 | 第十二篇: 断路器监控(Hystrix Dashboard)
- Spring Cloud入门1——Eureka&Ribbon&Hystrix
- springcloud入门之服务提供者client并注册到eureka
- Hystrix使用入门手册(中文)
- Hystrix快速入门
- SpringCloud断路器监控面板——Hystrix Dashboard
- 架构师入门:Spring Cloud系列,Hystrix与Eureka的整合
- SpringCloud自学入门-RestTemplate的使用
- SpringCloud简单入门——写给自己
- 史上最简单的SpringCloud教程 | 第四篇:断路器(Hystrix)
- SpringCloud(第 014 篇)电影 Ribbon 微服务集成 Hystrix 断路器实现失败快速响应,达到熔断效果
- 史上最简单的SpringCloud教程 | 第四篇:断路器(Hystrix)
- SpringCloud 入门教程 | 第一篇: 服务的注册与发现(Eureka)
- SpringCloud (九) Hystrix请求合并的使用
- 史上最简单的SpringCloud教程 | 第十二篇: 断路器监控(Hystrix Dashboard)
- springcloud 入门 11 (Hystrix Dashboard)
- SpringCloud自学入门-服务发现与消费并实现负载均衡
- springcloud(第一篇)springcloud config 入门