springcloud hystrix实战(二)

淡淡的烟草味﹌ 2023-05-29 02:46 67阅读 0赞

我们前面介绍完了springcloud hystrix的相关作用,大家也有了一个认识,这个熔断器的作用这个就不在重复。

下面我们就接着进行代码实战,我们是接着之前的微服务的工程继续的,如果有什么不明白请跟查看前面相关的文章

1,首先我们创建一个微服务工程 microservicecloud-provider-dept-hystrix-8001(注意参考前面的服务提供者8001)

2,将microservicecloud-provider-dept-8001的相关都拷贝到新的微服务工程

3,修改pom文件

  1. <!--引入熔断器Hystrix 相关的依赖包-->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-hystrix</artifactId>
  5. </dependency>

4,修改yml文件

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3l3bDQ3MDgxMjA4Nw_size_16_color_FFFFFF_t_70

5,修改controller类

  1. package com.atguigu.springcloud.controller;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.PathVariable;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestMethod;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import com.atguigu.springcloud.entities.Dept;
  8. import com.atguigu.springcloud.service.DeptService;
  9. import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
  10. @RestController
  11. public class DeptController {
  12. @Autowired
  13. private DeptService service = null;
  14. @RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
  15. //一旦调用服务方法失败并抛出了错误信息后,会自动调用@HystrixCommand标注好的fallbackMethod调用类中的指定方法
  16. @HystrixCommand(fallbackMethod = "processHystrix_Get")
  17. public Dept get(@PathVariable("id") Long id) {
  18. Dept dept = this.service.get(id);
  19. if (null == dept) {
  20. throw new RuntimeException("该ID:" + id + "没有没有对应的信息");
  21. }
  22. return dept;
  23. }
  24. public Dept processHystrix_Get(@PathVariable("id") Long id) {
  25. return new Dept().setDeptno(id).setDname("该ID:" + id + "没有没有对应的信息,null--@HystrixCommand")
  26. .setDb_source("no this database in MySQL");
  27. }
  28. }

对于为什么controller这么写我这里做个思路说明:

这里我们要查询dept的信息,我们传入id为100的id,我们有数据库信息知道只有五条记录没有deptno=100的记录,这时候我们查询的结果会返回null,这时候我们手动抛出异常。

一旦调用服务方法失败并抛出了错误信息后,会自动调用@HystrixCommand标注好的fallbackMethod调用类中的指定方法

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3l3bDQ3MDgxMjA4Nw_size_16_color_FFFFFF_t_70 1

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3l3bDQ3MDgxMjA4Nw_size_16_color_FFFFFF_t_70 2

6,修改主启动类

  1. package com.atguigu.springcloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
  5. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  6. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  7. @SpringBootApplication
  8. @EnableEurekaClient //本服务启动后会自动注册进eureka服务中
  9. @EnableDiscoveryClient //服务发现
  10. @EnableCircuitBreaker//对hystrixR熔断机制的支持
  11. public class DeptProvider8001_Hystrix_App {
  12. public static void main(String[] args) {
  13. SpringApplication.run(DeptProvider8001_Hystrix_App.class, args);
  14. }
  15. }

我们上面已经加入熔断器,我们当然需要告诉主启动类使用这个熔断器处理异常(调用fallback方法)

通过注解就@EnableCircuitBreaker可以告诉主启动类开启熔断器的使用

到这来为止我们的熔断器代码就完成了,下面我们来测试下

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3l3bDQ3MDgxMjA4Nw_size_16_color_FFFFFF_t_70 3

启动之后我们访问eureka注册中心:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3l3bDQ3MDgxMjA4Nw_size_16_color_FFFFFF_t_70 4

通过消费者端调用注册中心暴露的微服务发现我们的服务正常:

20191101193603316.png

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3l3bDQ3MDgxMjA4Nw_size_16_color_FFFFFF_t_70 5

之前我们不是说了如果调用数据库不存在的数据返回null,手动抛异常,一旦抛异常就会调用fallback方法返回

20191101194303763.png

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3l3bDQ3MDgxMjA4Nw_size_16_color_FFFFFF_t_70 6

由上面的结果可知我们的熔断成功。关于熔断器的实战编码就已经完成

发表评论

表情:
评论列表 (有 0 条评论,67人围观)

还没有评论,来说两句吧...

相关阅读

    相关 springcloud实战之7 断路器(Hystrix

    在微服务广泛使用的架构中,成群的服务通过服务注册与订阅来建立关联,但每个微服务都是独立的部分(跨进程,跨机器,跨机房),服务之间通过远程调用的方式相互访问,这种架构就增加了因为