第七节 熔断路由和监控
2018-08-26 本文已影响12人
勃列日涅夫
熔断路由
- 解释
通过生活常识我们知道电路中的电路由的作用是为了保护电路,阻止电流继续工作的一种自动装置。
这里的熔断路由其实类似,他跟踪eureka服务、其他服务等的可用性。这是微服务架构中一个
重要的思想,当我们的服务不能响应服务的访问者的调用时,给出的一种自我保护的功能(如果不这样做失败的请求会因为超时等其他因素拖垮服务提供者)
Netflix开源了Hystrix组件,实现了断路器模式,SpringCloud对这一组件进行了整合
- 使用步骤
- 添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
- 程序的启动类ServiceRibbonApplication 加@EnableHystrix注解开启Hystrix:
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableHystrix
public class Application {
public static void main(String[] args) {
SpringApplication.run( Application.class, args );
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
- 具体使用@HystrixCommand注解。该注解对该方法创建了熔断器的功能,并指定了fallbackMethod熔断方法.当服务不可用是熔断方法直接返回
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
//hiError 指定了返回的方法 hiError(String name)
@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!";
}
}
- 然后再使用中调用相关的API就可以
Hystrix提供web用户的仪表盘
在微服务架构中为例保证程序的可用性,防止程序出错导致网络阻塞,出现了断路器模型。断路器的状况反应了一个程序的可用性和健壮性,它是一个重要指标。Hystrix Dashboard是作为断路器状态的一个组件,提供了数据监控和友好的图形化界面。
- 添加依赖
</dependency>
<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>
- 程序入口类Application类,加上@EnableHystrix注解开启断路器,这个是必须的,并且需要在程序中声明断路点HystrixCommand;加上@EnableHystrixDashboard注解,开启HystrixDashboard
@SpringBootApplication
@Controller
@EnableHystrix
@EnableHystrixDashboard
public class DashboardApp extends SpringBootServletInitializer {
//我们把所有来自根的请求全部转发到hystirx
@RequestMapping("/")
public String home() {
return "forward:/hystrix";
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DashboardApp.class).web(true);
}
public static void main(String[] args) {
SpringApplication.run(DashboardApp.class, args);
}
}
-
再配置文件application.yml 配置(参考源码)
-
http://locahost:8762/hystrix 可以看到界面
hystrix-turbine集成了hystrix看板和 turbine,用来监控实现了hystrix的工程项目
-
原本的hystrix看板只能监控一台服务器上的服务调用情况,使用了turbine后就可以监控多台服务器的情况。Turbine原理如下
图片.png -
增加依赖(具体可参考源码turbine-server)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
-
再应用启动类@EnableTurbine ,开启turbine,@EnableTurbine注解包含了@EnableDiscoveryClient注解,即开启了注册服务。
-
配置文件application.yml ,重点是turbine 的配置
server:
port: 8989
management:
port: 8990
turbine:
aggregator:
clusterConfig: USER-SERVICE,RESTAURANT-SERVICE
# clusterNameExpression: new String("default")
appConfig: user-service,restaurant-service
# clusterNameExpression: 'default'
# InstanceMonitor:
# eventStream:
# skipLineLogic: false
eureka:
instance:
leaseRenewalIntervalInSeconds: 10
metadataMap:
instanceId: ${vcap.application.instance_id:${spring.application.name}:${spring.application.instance_id:${random.value}}}
client:
serviceUrl:
defaultZone: ${vcap.services.${PREFIX:}eureka.credentials.uri:http://user:password@localhost:8761}/eureka/
#http://localhost:8761/eureka/
fetchRegistry: true
- 各个工程启动后可以参看,打开:http://localhost:8763/hystrix,输入监控流http://localhost:8764/turbine.stream 查看
spring could 的使用就不再叙述了,具体参考这个系列博客https://blog.csdn.net/forezp/article/details/70148833/