当微服务挂掉了怎么办?从用户体验的角度来讲,应该给一个服务切换不影响使用为最佳,或者在一些秒杀秒杀活动中可以跳转到排队等待页面,最差也要给个提示,不可以留个报错页面给客户,spring cloud就为我们提供一个断路器Hystrix,专门处理某个服务挂掉了如何善后的问题。
再复用userService,添加依赖包
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId> </dependency>hystrix-dashboard是hystrix的监控后台,各位可以自行查询相关资料,这里不重点介绍
Hystrix在RestTemplate和Feign中用法不同,所以分别做介绍,启动程序中加入Hystrix的注解
package com.tzy; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; import org.springframework.cloud.netflix.hystrix.EnableHystrix; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableEurekaClient @EnableFeignClients @EnableCircuitBreaker @EnableHystrix @EnableHystrixDashboard public class Main { public static void main(String[] args){ SpringApplication.run(Main.class,args); } @Bean @LoadBalanced RestTemplate restTemplate(){ return new RestTemplate(); } }@EnableHystrix是必不可少的,表示该应用使用Hystrix
先说在RestTemplate中的使用,以远程调用service-product服务的getProduct方法为例,
package com.tzy.userService; 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; @Service public class GetProductService { @Autowired RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "getGoodsError") public String getGoods(){ return restTemplate.getForObject("http://service-product/getProduct",String.class); } public String getGoodsError(){ return "getGoodsService进程间调用异常!"; } }
@HystrixCommand注解中的fallbackMethod所指向的方法是如果下面的远程调用出现异常,比如service-product服务挂掉了,就会回调getGoodsError方法中的内容返回给请求者
可以把service-product关掉测试试一下,是可以拿到异常反馈内容的。
Hystrix和Feign的集成要更加好一些,仍以远程调用service-product服务的getProduct方法为例,复用上一节我们创建的接口,新建一个对象实现该接口
package com.tzy.userService; import org.springframework.stereotype.Component; @Component public class GetGoodsHystrix implements GetProductInterface{ @Override public String getProduct(){ return "GetProductInterface接口调用失败"; } }
重写方法中写的是远程调用失败的异常处理,在接口里要加上Hystrix的回调内容:
package com.tzy.userService; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; @FeignClient(value = "service-product",fallback = GetGoodsHystrix.class) public interface GetProductInterface { @RequestMapping(value = "/getProduct") String getProduct(); }
只需在@FeignClient的方法里加入要回调的对象即可,即fallback=GetGoodsHystrix。接下来我们就复用以前的Controller来测一下,关掉service-product服务。
package com.tzy.userService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class GetProductController { @Autowired GetProductInterface getProductInterface; @RequestMapping(value = "/getGoods") public String getGoods(){ return getProductInterface.getProduct(); } }
最终也能得到正确的异常反馈。
会用这几章介绍的spring cloud组件,基本上就可以开发最基础的微服务系统了,当然还有很多的东西要学,这一套东西极大地简化了配置,主要思想是默认大于配置,只要是你能想得到的地方都可以配置,他只是把常用的暴露给你,让开发人员把精力都放在开发上,非常好用,再加上spring-data去操作关系型数据库和费关系型数据库,就算重新架构项目也比用ssm框架开发的周期要短,但还是建议新项目用spring cloud开发,我们的情况是不得已而为之,说了这么多spring cloud的好话,他的缺点也要讲清楚,目前spring cloud只支持java,我最近在研究nodejs,他的事件驱动和异步IO的确很强大,无法融入spring cloud的体系,希望以后spring cloud可以对接更多的平台。