微服务熔断限流Hystrix之Dashboard

简介
Hystrix Dashboard是一款针对Hystrix进行实时监控的工具,通过Hystrix Dashboard可以直观地看到各Hystrix Command的请求响应时间,请求成功率等数据。

快速上手
工程说明
工程名 端口 作用
eureka-server 8761 注册中心
service-hi 8762 服务提供者
service-consumer 8763 服务消费者
核心代码
eureka-server 工程
pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
application.yml
server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:/${server.port}/eureka/
spring:
  application:
    name: eureka-server

启动类

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

}

service-hi 工程
pom.xml

   <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>

application.yml

 server:
      port: 8762
    
    spring:
      application:
        name: service-hi
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka/

HelloController

@RestController
public class HelloController {

    @GetMapping("/hi")
    public String hi() {
        return "hello ~";
    }

    @GetMapping("/hey")
    public String hey() {
        return "hey ~";
    }


    @GetMapping("/oh")
    public String oh() {
        return "ah ~";
    }

    @GetMapping("/ah")
    public String ah() {
        //模拟接口1/3的概率超时
        Random rand = new Random();
        int randomNum = rand.nextInt(3) + 1;
        if (3 == randomNum) {
            try {
                Thread.sleep( 3000 );
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return "来了老弟~";
    }

}

启动类

@SpringBootApplication
@EnableEurekaClient
public class ServiceHiApplication {

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

}

service-consumer 工程
pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-ribbon</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>

application.yml

server:
  port: 8763

  tomcat:
    uri-encoding: UTF-8
    max-threads: 1000
    max-connections: 20000

spring:
  application:
    name: service-consumer
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

management:
  endpoints:
    web:
      exposure:
        include: "*"
      cors:
        allowed-origins: "*"
        allowed-methods: "*"

HelloService

@Service
public class HelloService {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 简单用法
     */
    @HystrixCommand
    public String hiService() {
        return restTemplate.getForObject("http://SERVICE-HI/hi" , String.class);
    }

    /**
     * 定制超时
     */
    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "30000") })
    public String heyService() {
        return restTemplate.getForObject("http://SERVICE-HI/hey" , String.class);
    }

    /**
     * 定制降级方法
     */
    @HystrixCommand(fallbackMethod = "getFallback")
    public String ahService() {
        return restTemplate.getForObject("http://SERVICE-HI/ah" , String.class);
    }

    /**
     * 定制线程池隔离策略
     */
    @HystrixCommand(fallbackMethod = "getFallback",
            threadPoolKey = "studentServiceThreadPool",
            threadPoolProperties = {
                    @HystrixProperty(name="coreSize", value="30"),
                    @HystrixProperty(name="maxQueueSize", value="50")
            }
    )
    public String ohService() {
        return restTemplate.getForObject("http://SERVICE-HI/oh" , String.class);
    }


    public String getFallback() {
        return "Oh , sorry , error !";
    }


}
HelloController
@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;


    @GetMapping("/hi")
    public String hi() {
        return helloService.hiService();
    }

    @GetMapping("/hey")
    public String hey() {
        return helloService.heyService();
    }

    @GetMapping("/oh")
    public String oh() {
        return helloService.ohService();
    }

    @GetMapping("/ah")
    public String ah() {
        return helloService.ahService();
    }


}

启动类

@SpringBootApplication
@EnableEurekaClient
@EnableHystrixDashboard
@EnableHystrix
@EnableCircuitBreaker
public class ServiceConsumerApplication {

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

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

}

Hystrix Dashboard 的使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值