spring cloud hystrix断路器

这个例子基于服务注册后的代码来搞的,因为测试需要访问两个站点,模拟其中一个站点故障。

https://my.oschina.net/uwith/blog/1929697

1、首先添加pom引用

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

2、在application启动类中添加注解

package com.example.clienttest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;

//添加熔断器
@EnableCircuitBreaker
@SpringBootApplication
public class ClientTestApplication {
    public static void main(String[] args) {
        SpringApplication.run(ClientTestApplication.class, args);
    }
}

3、然后编写测试类

package com.example.clienttest.web;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class HomeController {

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @HystrixCommand(fallbackMethod = "hello")
    @RequestMapping("/index")
    public String index() {
        String httpUrl = loadBalancerClient.choose("server-test").getUri().toString();
        System.out.println("loadBalancerClient:" + httpUrl);

        //拿到服务的地址然后进行一次请求看看效果
        String result = new RestTemplate().getForObject(httpUrl + "/index", String.class);

        System.out.println("请求拿到的结果:" + result);
        return "这里是client-test";
    }

    //定义一个回调函数
    public String hello() {
        return "我是回调函数";
    }

}

 

然后服务注册的时候,开两个端口不同其余都相同的服务,然后,关闭其中一个服务,调用接口失效时会自动调用fallbackMethod定义的方法。如果服务恢复后,断路器也会恢复已恢复的服务调用。

同时这个注解也包含了线程隔离效果,接口调用超时熔断。

 

再来搞下针对于断路器的监控工具

1、在上面代码中继续添加pom引用

<!--hystrix-dashboard 断路器仪表板-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

2、添加配置文件bootstrap.yml

management:
  endpoints:
    web:
      exposure:
        include: '*'

3、然后启动类添加启动监控工具的注解

package com.example.clienttest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

//添加熔断器
@EnableCircuitBreaker
//开启断路器仪表板
@EnableHystrixDashboard
@SpringBootApplication
public class ClientTestApplication {
    public static void main(String[] args) {
        SpringApplication.run(ClientTestApplication.class, args);
    }
}

4、完事,然后在浏览器查看http://localhost:8082/hystrix

6698dc05e53b5a76b75c1857c742846d97b.jpg

5、在地址栏输入你需要监控的断路器地址,我这里是和监控工具在一起,输入http://localhost:8082/actuator/hystrix.stream,然后点击monitor stream即可,就可以看到断路器的一些信息,左边竖着两排对应红框中的相应说明。

05128320305a9324d31dfc4f2d699cca6b2.jpg

 

参考链接:

https://www.cnblogs.com/small-k/p/8149227.html

 

转载于:https://my.oschina.net/uwith/blog/3062062

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值