Hystrix
1:Rest实现服务熔断
测试Service-A是正常的 http://localhost:9001/person/find/1
然后让Cosumer-C9006去访问服务A
(1)引入hystrix的依赖 在工程中添加Hystrix的相关依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
(2)在启动类中激活Hystrix 在启动类中添加 @EnableCircuitBreaker 注解开启对熔断器的支持。
@SpringBootApplication
//激活eurekaClient
@EnableEurekaClient
//@EnableDiscoveryClient
@EnableFeignClients
@EnableCircuitBreaker
public class C9006 {
//创建RestTemplate,由@Bean将实例放到spring容器里面
@Bean
@LoadBalanced
RestTemplate restTemplate(){
RestTemplate r = new RestTemplate();
return r;
}
public static void main(String[] args) {
SpringApplication.run(C9006.class,args);
}
}
(3)配置熔断触发的降级逻辑 (4)在需要收到保护的接口上使用@HystrixCommand配置
@RestController
@RequestMapping("/testHystrixRest")
public class TestHystrixRestController {
@Autowired
private RestTemplate restTemplate ;
@RequestMapping(method = RequestMethod.GET,value = "/get/{id}")
@HystrixCommand(fallbackMethod = "error")
public Person getByID(@PathVariable Long id){
Person p = restTemplate.getForObject("http://Service-A9001/person/find/"+id,Person.class);
return p;
}
//降级方法,默认值,在不能正常访问ServieA获取的数据
public Person error(Long id){
Person p = new Person();
p.setId(id);
p.setName("当前是服务降级方法的数据,为了快速返回结束9006");
return p;
}
}
注意事项:
(1)因为熔断的降级逻辑方法必须跟正常逻辑方法保证: 相同的参数列表和返回值声明 。
(2)在 getByID 方法上 HystrixCommand(fallbackMethod = “error”) 用来声明一个降级逻辑的方法
(3)测试的话只要打开A服务,B服务,先访问B服务 对比,关闭A服务,再访问B服务
2:Feign实现服务熔断
测试Service-A是正常的 http://localhost:9001/person/find/1
然后让Cosumer-C9006去访问服务A
编写Feign
@FeignClient(name="Service-A9001")
public interface ServiceAFeigin {
@RequestMapping(method = RequestMethod.GET,value = "person/find/{id}")
public Person findById(@PathVariable Long id);
}
调用Feign
@RestController
@RequestMapping("/useHystrixFeign")
public class TestHystrixFeignController {
@Autowired
ServiceAFeigin feigin;
@RequestMapping(method = RequestMethod.GET,value = "/get/{id}")
public Person getByID(@PathVariable Long id){
Person p =feigin.findById(id);
return p;
}
}
》1:在Feign中已经内置了hystrix,但是默认是关闭的需要在工程的 application.yml 中开启对hystrix的支持
feign:
hystrix: #在feign中开启hystrix熔断
enabled: true
》2:配置FeignClient接口的实现类 基于Feign实现熔断降级,那么降级方法需要配置到FeignClient接口的实现类中 package com.dev1.feign.impl;
@Component
public class ServiceAFeignFallBack implements ServiceAFeigin {
@Override
public Person findById(Long id) {
Person person = new Person();
person.setId(id);
person.setName("我是服务降级Fein");
return person;
}
}
》3:修改FeignClient添加hystrix熔断 在@FeignClient注解中添加降级方法 @FeignClient 注解中以fallback声明降级方法
@FeignClient(name="Service-A9001",fallback = ServiceAFeignFallBack.class)
public interface ServiceAFeigin {
@RequestMapping(method = RequestMethod.GET,value = "person/find/{id}")
public Person findById(@PathVariable Long id);
}
3:Dashborad服务监控
在Cosumer-C9007中添加
》1:添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
》2:在Application中添加
@EnableHystrixDashboard
》3:添加application.yml配置
management:
endpoints:
web:
exposure:
include: '*'
》4:访问测试
http://localhost:9006/hystrix
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uUOdCV20-1663638365668)(index_files/96a9d398-f23d-4cae-b7d7-de1aee0695f1.jpg)]
输入监控断点展示监控的详细数据
http://localhost:9006/actuator/hystrix.stream
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5jq71aDx-1663638365668)(index_files/a4203016-4797-4468-ad36-5cdf25ebe4bb.png)]
说明: [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Jl36nATO-1663638365669)(index_files/e446c166-480d-4ceb-9064-4b7ec24a7d87.jpg)]
4:Turbine聚合监控
》1:添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
</dependencies>
》2:进行application.yml的配置
server:
port: 9007
spring:
application:
name: Turbine-C9007
eureka:
instance:
prefer-ip-address: true #使用ip地址注册
instance-id: ${spring.cloud.client.ip-address}:${server.port} #向注册中心中注册服务id
lease-renewal-interval-in-seconds: 5 #向注册中心中注册服务id
lease-expiration-duration-in-seconds: 10 #续约到期的时间
client:
service-url:
defaultZone: http://localhost:9000/eureka/,http://localhost:8000/eureka/
turbine:
# 要监控的微服务列表,多个用,分隔
appConfig: Cosumer-C9006
clusterNameExpression: "'default'"
》3:在启动中开启
@SpringBootApplication
@EnableTurbine
@EnableHystrixDashboard
public class C9007 {
public static void main(String[] args) {
SpringApplication.run(C9007.class,args);
}
}
》4:测试
http://localhost:9007/turbine.stream
|