断路器 Hystrix - 代码示例
ribbon中使用断路器
pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
@EnableHystrix
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class ServiceRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
@HystrixCommand(fallbackMethod = “hiError”)
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "hiError")
public String hiService(String name) {
return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
}
public String hiError(String name) {
return "hi,"+name+",sorry,error!";
}
}
Feign中使用断路器
@FeignClient( fallback = xxx.class)
@FeignClient(value = "service-hi",fallback = SchedualServiceHiHystric.class)
public interface SchedualServiceHi {
@RequestMapping(value = "/hi",method = RequestMethod.GET)
String sayHiFromClientOne(@RequestParam(value = "name") String name);
}
定义实现类
@Component
public class SchedualServiceHiHystric implements SchedualServiceHi {
@Override
public String sayHiFromClientOne(String name) {
return "sorry "+name;
}
}
Hystrix Dashboard (Hystrix 仪表盘)
pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
@EnableHystrixDashboard
@SpringBootApplication
@EnableDiscoveryClient
@RestController
@EnableHystrix
@EnableHystrixDashboard
public class ServiceRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
可视化界面
http://localhost:8764/hystrix
点击monitor stream,进入下一个界面,
访问:http://localhost:8764/hi?name=forezp,此时会出现监控界面:
断路器 聚合监控(Hystrix Turbine)
pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-turbine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
配置文件
spring:
application.name: service-turbine
server:
port: 8769
security.basic.enabled: false
turbine:
aggregator:
clusterConfig: default
# 指定聚合哪些集群,多个使用","分割,默认为default。可使用
http://.../turbine.stream?cluster={clusterConfig之一}访问
appConfig: service-hi,service-lucy
# 配置Eureka中的serviceId列表,表明监控哪些服务
clusterNameExpression: new String("default")
# 1. clusterNameExpression指定集群名称,默认表达式appName;此时: turbine.aggregator.clusterConfig需要配置想要监控的应用名称
# 2. 当clusterNameExpression: default时, turbine.aggregator.clusterConfig可以不写,因为默认就是default
# 3. 当clusterNameExpression: metadata['cluster']时,假设想要监控 的应用配置了eureka.instance.metadata-map.cluster: ABC,则需要配置, 同时turbine.aggregator.clusterConfig: ABC
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
@EnableTurbine
@SpringBootApplication
@EnableTurbine //开启turbine,包含了@EnableDiscoveryClient
public class ServiceTurbineApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(ServiceTurbineApplication.class).
web(true).
run(args);
}
}
测试
http://localhost:8769/turbine.stream
依次请求:http://localhost:8762/hi?name=forezp
打开:http://localhost:8763/hystrix,输入监控流http://localhost:8769/turbine.stream,点击monitor stream 进入页面: