Spring Cloud Ribbon 整合 Hystrix
Posted PinBo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Cloud Ribbon 整合 Hystrix相关的知识,希望对你有一定的参考价值。
在前面随笔 Spring Cloud 之 Ribbon 的ribbon工程基础上进行改造
1.pom.xml
加入依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>
即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.dzpykj</groupId> <artifactId>ribbon</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>ribbon</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Edgware.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
2.application.yml文件配置不变
spring: application: name: ribbon server: port: 8765 eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
3.启动类加入@EnableCircuitBreaker注解开启Hystrix
package com.dzpykj; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableEurekaClient @EnableCircuitBreaker //@EnableCircuitBreaker注解表明使用断路器 public class RibbonApplication { @Autowired private RestTemplateBuilder builder; @Bean @LoadBalanced public RestTemplate restTemplate() { return builder.build(); } // @Bean // @LoadBalanced // RestTemplate restTemplate() { // return new RestTemplate(); // } public static void main(String[] args) { SpringApplication.run(RibbonApplication.class, args); } }
4.新建HelloService类
重点是@HystrixCommand注解,fallbackMethod为异常时的回调方法
package com.dzpykj.hystrixService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @Service public class HelloService { @Value("${server.port}") String port; @Autowired RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "helloErrorFallBack") public String hello(String name) { return restTemplate.getForObject("http://eurekaclient/hi?name="+name, String.class); } public String helloErrorFallBack(String name) { return "Sorry "+name+",when you are visting ribbon project,port:"+port+",you meet an error"; } }
5.改造HelloController,调用HelloService的hello方法
package com.dzpykj.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import com.dzpykj.hystrixService.HelloService; @RestController public class HelloController { @Autowired RestTemplate restTemplate; @Autowired HelloService helloService; @RequestMapping("/hello/{name}") public String hello(@PathVariable String name) { return restTemplate.getForObject("http://eurekaclient/hi?name="+name, String.class); } @RequestMapping("/hi/{name}") public String hi(@PathVariable String name) { return helloService.hello(name); } }
6.准备工作完成,开始测试.依次启动Eureka服务集群、Eureka单个客户端、Ribbon工程
6.1 按照Spring Cloud Eureka Server集群Demo级搭建的步骤启动Eureka服务peer1,peer2集群
6.2按照Spring Cloud Eureka服务Demo级搭建启动8763的Eureka客户端
6.3启动Ribbon工程
7.访问 http://localhost:8765/hi/chaixy
8.模拟eurekaclient服务异常:手动将eurekaclient服务关闭,再次访问 http://localhost:8765/hi/chaixy
可以看到,当eurekaclient服务关闭时,访问遇到异常,回调了异常时的回调方法helloErrorFallBack方法(HelloService类内的方法)
以上是关于Spring Cloud Ribbon 整合 Hystrix的主要内容,如果未能解决你的问题,请参考以下文章
spring Cloud中,解决Feign/Ribbon整合Hystrix第一次请求失败的问题?
15Spring-Cloud_Eureka-Ribbon-Hystix-Feign-Zuul微服务整合
2.Spring Cloud Alibaba实现负载均衡-Ribbon