SpringCloud与idea在项目中相遇:第五天,Hystrix

  1. 服务熔断

a)以vlluviaCloud-provider-dept-8001为原型创建vlluviaCloud-provider-dept-hystrix-8001 项目

在这里插入图片描述

b)修改PersonController 类

@RequestMapping(value = "/person/get/{id}", method = RequestMethod.GET)
@HystrixCommand(fallbackMethod = "processHystrix_Get")
public Person getPerson(@PathVariable("id") int id) {
    
    if (id == 0)
        throw new RuntimeException("该ID:" + id + "没有没有对应的信息");

    for (Person person : get()) {
        if (person.getId() == id)
            return person;
    }
    return null;
}
public Person processHystrix_Get(@PathVariable("id")int id)
{
    return new Person(id,"该ID:" + id + "没有没有对应的信息,null--@HystrixCommand","no this database in MySQL");
}

c)修改HystrixApplication 类

在这里插入图片描述

d)修改vlluviaCloud-api 项目

e)添加DeptClientServiceFallbackFactory 类

@Component // 不要忘记添加,不要忘记添加
public class DeptClientServiceFallbackFactory implements FallbackFactory<DeptClientService>
{
   @Override
   public DeptClientService create(Throwable throwable)
   {
      return new DeptClientService() {
         @Override
         public Person get(long id)
         {
            return new Person(Math.toIntExact(id),"该ID:" + id + "没有没有对应的信息,null--@HystrixCommand","no this database in MySQL");
         }

         @Override
         public List<Person> gets()
         {
            return null;
         }
      };
   }
}

f)修改DeptClientService类

在这里插入图片描述

g)修改vlluviaCloud-consumer-dept-feign项目

h)修改application.yml

server:
  port: 80


feign:
  hystrix:
    enabled: true

spring:
  application:
    name: consumer-dept-80

eureka:
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://eureka1:7001/eureka/,http://eureka2:7002/eureka/,http://eureka3:7003/eureka/

i)按顺序运行

	vlluviaCloud-eureka-7001
	vlluviaCloud-provider-dept-hystrix-8001
	vlluviaCloud-consumer-dept-feign

访问http://localhost/consumer/person/gets
在这里插入图片描述

然后访问 http://localhost/consumer/person/get/0

在这里插入图片描述

  1. 服务降级

a)创建项目vlluviaCloud-consumer-hystrix-dashboard
在这里插入图片描述

b)修改application.yml

server:
  port: 9001

c)修改HystrixDashboardApplication
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_37208650/article/details/82811195
今日推荐