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

来源:互联网 发布:手机淘宝不能登录原因 编辑:程序博客网 时间:2024/05/16 11:28

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 (服务发布)

@RestControllerpublic 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-serviceserver:  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-serviceserver:  port: 7002  context-path: /eureka:  client:    service-url:      defaultZone: http://localhost:8001/eureka/#启动断路器hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 1000custom:   rest:    connect-timeout: 1000    connection-request-timeout: 1000    read-timeout: 30000

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

custom.rest 配置服务超时时间

端口号 7002


@SpringBootApplication@EnableDiscoveryClient@EnableCircuitBreakerpublic 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

@Servicepublic 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 超时降级策略---";    }}


@RestControllerpublic 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-serviceserver:  port: 7003  context-path: /eureka:  client:    service-url:      defaultZone: http://localhost:8001/eureka/#启动断路器hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 1000custom:   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-dashboardserver:  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-turbineserver:  port: 7005  context-path: /management:  prot:3001turbine:  app-config: request-service    #要监控的服务  cluster-name-expression: "'default'"  combine-host-port: trueeureka:  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 0
原创粉丝点击