SpringCould-------使用Hystrix 实现断路器进行服务容错保护
Posted 不死码农
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringCould-------使用Hystrix 实现断路器进行服务容错保护相关的知识,希望对你有一定的参考价值。
消费:
<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.sam</groupId> <artifactId>hello-consumer</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <properties> <javaVersion>1.8</javaVersion> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR6</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- 引入eureka 客户端依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- 引入ribbon 依赖 ,用来实现负载均衡,我们这里只是使用,先不作其他介绍--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.6.RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-test</artifactId> <scope>test</scope> </dependency> <!-- 引入hystrix 依赖 ,用来实现服务容错保护--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> </dependencies> </project>
启动类:
/*@SpringBootApplication + @EnableDiscoveryClient+ @EnableCircuitBreaker = @SpringCloudApplication */ @SpringCloudApplication public class CustomerApplication { //@Bean 应用在方法上,用来将方法返回值设为为bean @Bean @LoadBalanced //@LoadBalanced实现负载均衡 public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(CustomerApplication.class, args); } }
service:
@Service public class ConsumerService { @Autowired RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "errorMsg") public String consumer() { return restTemplate.getForObject("http://hello-service/hello", String.class); } /** * 由 @HystrixCommand(fallbackMethod = "errorMsg") * 可知 在起多个服务时,如果其中一个服务宕机,则会调用 errorMsg() * 方法; 相比在不加@EnableCircuitBreaker断路器,的包500d的错 */ public String errorMsg() { return "error!!!"; } }
controller:
@RestController public class ConsumerController { //这里注入的restTemplate就是在com.sam.ConsumerApp中通过@Bean配置的实例 /*@Autowired RestTemplate restTemplate;*/ @Autowired ConsumerService consumerService; @RequestMapping("/consumer") public String helloConsumer() { //调用hello-service服务,注意这里用的是服务名,而不是具体的ip+port // return restTemplate.getForObject("http://hello-service/hello", String.class); return consumerService.consumer(); } }
以上是关于SpringCould-------使用Hystrix 实现断路器进行服务容错保护的主要内容,如果未能解决你的问题,请参考以下文章
ELK+zookeeper+kafka收集springcould日志配置文档