[java]微服务架构连载No4 Hystrix+Dashboard+Turbine实现断路器(限流,超时,异常...)和服务监控

Hyxtrix:通过访问远程系统,服务,和第三方节点,从而对故障和延迟提供了强大的容错能力,具备了服务降级,服务熔断,远程隔离,请求缓存,请求合并已经服务监控等强大功能

本编文章架构图如下



新建6个工程

spring-cloud-03-hystrix-eureka (服务发现中心,同上篇文章)

spring-cloud-03-hystrix-client (服务发布)

spring-cloud-03-hystrix-request-a (服务请求集群a)

spring-cloud-03-hystrix-request-b (服务请求集群b)

spring-cloud-03-hystrix-dashboard (服务监控面板)

spring-cloud-03-hystrix-dashboard-turbine (集群服务监控)


spring-cloud-03-hystrix-client (服务发布)

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() throws InterruptedException {
        System.err.println("---hello --");
        Thread.sleep(4000l);
        return "--- hello --";
    }

    @GetMapping("/dashboard")
    public String dashboard() throws InterruptedException {
        System.err.println("---dashboard --");
        return "--- dashboard --";
    }
}

spring:
  application:
    name: client-service

server:
  port: 7001
  context-path: /

eureka:
  client:
    service-url:
     defaultZone: http://localhost:8001/eureka/


备注:  requestAPI hello配置睡眠4秒相应时间

  dashboard无配置

 端口 7001


spring-cloud-03-hystrix-request-a (服务请求集群a)

pom.xml导入hyxtrix和actuator包用作断路器

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-ribbon</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-hystrix</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>


application.yml

spring:
  application:
    name: request-service

server:
  port: 7002
  context-path: /

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8001/eureka/

#启动断路器
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 1000

custom:
   rest:
    connect-timeout: 1000
    connection-request-timeout: 1000
    read-timeout: 30000


备注: timeoutInMilliseconds 配置断路器超时时间1秒

custom.rest 配置服务超时时间

端口号 7002


@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public class RequestAApplication {

    @Bean
    @ConfigurationProperties(prefix = "custom.rest")
    public HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory(){
        HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory=new HttpComponentsClientHttpRequestFactory();
        return httpComponentsClientHttpRequestFactory;
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate(httpComponentsClientHttpRequestFactory());
    }


    public static void main(String[] args) {
        SpringApplication.run(RequestAApplication.class, args);
    }
}

备注: @EnableCircuitBreaker 开启断路器


HystrixService

@Service
public class HystrixService {

    @Autowired
    private RestTemplate restTemplate;


    /**
     * 超时降级策略
     *
     * @return
     */
    @HystrixCommand(fallbackMethod = "helloFallback")
    public String hello() {
        return restTemplate.getForObject("http://client-service/hello", String.class);

    }

    public String helloFallback() {
        return "------执行服务超时降级策略---";
    }


    /**
     * 异常降级策略
     *
     * @return
     */
    @HystrixCommand(fallbackMethod = "helloExceptionFallback", ignoreExceptions = {IOException.class})
    public String helloException() {
        throw new RuntimeException("helloException RuntimeException 异常了");

    }

    public String helloExceptionFallback(Throwable e) {
        return "------执行服务异常降级策略---" + e.getMessage();
    }

    /**
     * 高并发限流  之一: execution.isolation.strategy=THREAD线程隔离策略(它会独立在一个线程上执行,并且它的并发量受线程池中的数量限制)
     *
     */
    @HystrixCommand(
            commandKey = "threadKey",
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.strategy", value = "HTREAD"),
                    @HystrixProperty(name = "coreSize", value = "10"),
                    @HystrixProperty(name = "maxQueueSize", value = "50"),     //最大数
                    @HystrixProperty(name = "queueSizeRejectionThreshold", value = "30") // 拒绝阈值
            },
            fallbackMethod = "helloThreadFallback")
    public String helloThread() {
        return "------执行线程隔离策略 helloThread---";
    }

    public String helloExceptionFallback() {
        return "------执行线程隔离策略 helloExceptionFallback---";
    }

    /**
     * 高并发限流  之二: execution.isolation.strategy=SEMAPHORE 信号量策略(它则实现再调用的线程上,通过信号量的方式进行隔离,)
     */
    @HystrixCommand(
            commandKey = "semaphoreKey",
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.strategy", value = "SEMAPHORE"),
                    @HystrixProperty(name = "executiion.isolation.strategy.maxConcurrentRequests", value = "50")
            },
            fallbackMethod = "helloSemaphoneFailback"
    )
    public String  helloSemaphone(){
        return "------执行线程隔离策略 helloSemaphone---";
    }


    @HystrixCommand(fallbackMethod = "dashboardFallback")
    public String dashboard() {
        return restTemplate.getForObject("http://client-service/dashboard", String.class);

    }

    public String dashboardFallback() {
        return "------执行服务dashboardFallback 超时降级策略---";
    }
}


@RestController
public class HystrixController {


    @Autowired
    private HystrixService hystrixService;

    @GetMapping(value = "/hello-hystrix")
    public String hello(){
        return hystrixService.hello();
    }


    @GetMapping(value = "/hello-exception")
    public String helloException(){
        return hystrixService.helloException();
    }

    @GetMapping(value = "/hello-dashboard")
    public String dashboardException(){
        return hystrixService.dashboard();
    }
}

备注: 分别写了4个方法,分别是服务超时,服务异常,和高并发限流二种策略


spring-cloud-03-hystrix-request-b (服务请求集群b)

spring:
  application:
    name: request-service

server:
  port: 7003
  context-path: /

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8001/eureka/

#启动断路器
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 1000

custom:
   rest:
    connect-timeout: 1000
    connection-request-timeout: 1000
    read-timeout: 30000


备注:端口号7003,其他同spring-cloud-03-hystrix-request-a


spring-cloud-03-hystrix-dashboard (服务监控面板)

pom.xml导入dashboard

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>

application配置端口7004

spring:
  application:
    name: hystrix-dashboard

server:
  port: 7004
  context-path: /

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8001/eureka/


@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrixDashboard  //启动断路器监控
public class DashboardApplication {

    // 监控台地址  http://192.168.0.102:7004/hystrix

    //查看什么样的数据[监控服务] http://192.168.0.102:7002/hystrix.stream 并输入到监控台


    public static void main(String[] args) {
        SpringApplication.run(DashboardApplication.class, args);
    }
}

备注: @EnableHystrixDashboard 启动断路器监控


spring-cloud-03-hystrix-dashboard-turbine (集群服务监控)

pom.xml导入turbine

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>

spring:
  application:
    name: hystrix-turbine

server:
  port: 7005
  context-path: /

management:
  prot:3001

turbine:
  app-config: request-service    #要监控的服务
  cluster-name-expression: "'default'"
  combine-host-port: true


eureka:
  client:
    service-url:
      defaultZone: http://localhost:8001/eureka/

备注: app-config 配置要监控的服务,spring-cloud-03-hystrix-request-a 和b

cluster-name-expression写默认值


@SpringBootApplication
@EnableDiscoveryClient
@EnableTurbine  //启动收集集群断路器
public class TruebineApplication {

    //监控台地址  http://192.168.0.102:7004/hystrix

    //查看什么样的数据[监控服务] http://192.168.0.102:7002/hystrix.stream 并输入到监控台

    //要监控的turbine地址[监控的hystrix-reuqest服务] http://localhost:7005/trubine.stream,输入到dashboard


    public static void main(String[] args) {
        SpringApplication.run(TruebineApplication.class, args);
    }
}

备注:@EnableTurbine  开启集群服务监控


使用:

1:访问http://192.168.0.102:7003/hello-hystrix ,由于服务睡眠4秒,而配置的服务超时时间custom.rest.read-timeout =3000 ,那么服务会超时,断路器通过fallbackMethod = "helloFallback" 最终执行 helloFallback方法,并返回  "------执行服务超时降级策略---" ,异常同理

2:访问 http://192.168.0.102:7005/turbine.stream 获取服务数据流

3:访问面板路径 http://localhost:7005/trubine.stream 数据要监控的服务地址 http://192.168.0.102:7005/turbine.stream

4:监控面板获取实时监控数据


截图一



截图二



截图三



截图四


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

源14

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值