前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >第七节 熔断路由和监控

第七节 熔断路由和监控

作者头像
用户1418372
发布2018-09-13 10:20:11
3140
发布2018-09-13 10:20:11
举报
文章被收录于专栏:清晨我上码清晨我上码

熔断路由

  1. 解释 通过生活常识我们知道电路中的电路由的作用是为了保护电路,阻止电流继续工作的一种自动装置。

这里的熔断路由其实类似,他跟踪eureka服务、其他服务等的可用性。这是微服务架构中一个 重要的思想,当我们的服务不能响应服务的访问者的调用时,给出的一种自我保护的功能(如果不这样做失败的请求会因为超时等其他因素拖垮服务提供者)

Netflix开源了Hystrix组件,实现了断路器模式,SpringCloud对这一组件进行了整合

  1. 使用步骤
  • 添加依赖
代码语言:javascript
复制
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  </dependency>
  • 程序的启动类ServiceRibbonApplication 加@EnableHystrix注解开启Hystrix:
代码语言:javascript
复制
@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熔断方法.当服务不可用是熔断方法直接返回
代码语言:javascript
复制
@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是作为断路器状态的一个组件,提供了数据监控和友好的图形化界面。

  • 添加依赖
代码语言:javascript
复制
</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
代码语言:javascript
复制
@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);
    }
}

hystrix-turbine集成了hystrix看板和 turbine,用来监控实现了hystrix的工程项目

  • 原本的hystrix看板只能监控一台服务器上的服务调用情况,使用了turbine后就可以监控多台服务器的情况。Turbine原理如下

图片.png

  • 增加依赖(具体可参考源码turbine-server)
代码语言:javascript
复制
 <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 的配置
代码语言:javascript
复制
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

spring could 的使用就不再叙述了,具体参考这个系列博客https://blog.csdn.net/forezp/article/details/70148833/

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.08.26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 熔断路由
  • Hystrix提供web用户的仪表盘
  • hystrix-turbine集成了hystrix看板和 turbine,用来监控实现了hystrix的工程项目
相关产品与服务
腾讯云 BI
腾讯云 BI(Business Intelligence,BI)提供从数据源接入、数据建模到数据可视化分析全流程的BI能力,帮助经营者快速获取决策数据依据。系统采用敏捷自助式设计,使用者仅需通过简单拖拽即可完成原本复杂的报表开发过程,并支持报表的分享、推送等企业协作场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档