- 服务熔断
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
- 服务降级
a)创建项目vlluviaCloud-consumer-hystrix-dashboard
b)修改application.yml
server:
port: 9001
c)修改HystrixDashboardApplication