Spring Cloud基础之Hystrix熔断器

机器学习
• 阅读 1170

首先需要在Discovery Service章节中的创建Discovery Server服务
创建weather-service服务

//导入包,实际是通过Spring Initializr导入的,Eureka Discovery,Spring Web
spring-boot-starter-web
spring-cloud-starter-netflix-eureka-client
//配置启动类注解
@SpringBootApplication
@RestController
@EnableDiscoveryClient
public class WeatherServiceApplication {
   private String[] weather = new String[] {"sunny", "cloudy","rainy","windy"};
   public static void main(String[] args) {
       SpringApplication.run(WeatherServiceApplication.class, args);
   }
   @GetMapping("/weather")
   public String getWeather(){
       int rand = ThreadLocalRandom.current().nextInt(0,4);
       return weather[rand];
   }
}
//配置文件
server.port=9000
spring.application.name=weather-service
eureka.client.service-url.defaultZone=http://localhost:8761/eureka

创建weather-app服务

//导入包,实际是通过Spring Initializr导入的,Eureka Discovery,Spring Web,Hystrix,Actuator
spring-boot-starter-web
spring-cloud-starter-netflix-eureka-client
//配置启动类注解
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
@RestController
public class WeatherAppApplication {
   @Autowired
   public WeatherService weatherService;
   public static void main(String[] args) {
       SpringApplication.run(WeatherAppApplication.class, args);
   }
   @Bean
   @LoadBalanced
   public RestTemplate restTemplate(){
       return new RestTemplate();
   }
   @GetMapping("/current/weather")
   public String getWeather(){
       return "The current weather is " +weatherService.getWeather();
   }
}
//配置业务类,HystrixCommand配置了fallbackMethod方法为unknown
@Service
public class WeatherService {
   @Autowired
   private RestTemplate restTemplate;
   @HystrixCommand(fallbackMethod = "unknown")
   public String getWeather() {
       return restTemplate.getForEntity("http://weather-service/weather",String.class).getBody();
   }
   public String unknown(){
       return "unknown";
   }
}
//配置文件
server.port=8000
spring.application.name=weather-app
eureka.client.service-url.defaultZone=http://localhost:8761/eureka

//通过访问http://localhost:8000/current/weather
//当weather服务down掉时,输出
The current weather is unknown
//当weather服务ok时,输出
The current weather is windy

配置hystirx-dashboard服务

//导入包,实际是通过Spring Initializr导入的,Hystrix Dashboard
spring-cloud-starter-netflix-hystrix-dashboard
//配置启动类注解
@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboardApplication {
   public static void main(String[] args) {
       SpringApplication.run(HystrixDashboardApplication.class, args);
   }
}
//需要在weather-app服务的配置文件增加如下配置才可以监控到weather-app的服务
management.endpoints.web.exposure.include=hystrix.stream
//打开http://localhost:8080/hystrix/
//配置monitor地址http://localhost:8000/actuator/hystrix.stream
//新增后即可monitor服务的情况,如下图

Spring Cloud基础之Hystrix熔断器

配置turbine服务

//导入包,实际是通过Spring Initializr导入的,Turbine
spring-cloud-starter-netflix-turbine
//配置启动类注解
@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboardApplication {
   public static void main(String[] args) {
       SpringApplication.run(HystrixDashboardApplication.class, args);
   }
}
//配置文件
server.port=3000
spring.application.name=turbine-aggregator
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
turbine.app-config=weather-app,datetime-app
turbine.cluster-name-expression='default'
//启动服务后,需要在dashboard项目中新建monitor的时候增加http://localhost:3000/turbine.stream
//即可监控多个服务
点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
美凌格栋栋酱 美凌格栋栋酱
6个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
Easter79 Easter79
3年前
springcloud eureka.instance
1.在springcloud中服务的 InstanceID默认值是:${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance\_id:${server.port}},也就是:主机名:应用名:应用端口。如图1
Wesley13 Wesley13
3年前
java实现使用QQ邮箱发送验证码功能
首先当然是导入jar包了啊如果是maven项目可以进maven资源库进行搜索导入,在此附上地址:https://mvnrepository.com这是需要导入的jar包 commonsemail1.x.jar、mail.jar  activation.jar,其中activation.jar我并没有导入,但是还是发送成功了,但看网上有蛮多都说需
Stella981 Stella981
3年前
Excel数据转化为sql脚本
在实际项目开发中,有时会遇到客户让我们把大量Excel数据导入数据库的情况。这时我们就可以通过将Excel数据转化为sql脚本来批量导入数据库。1在数据前插入一列单元格,用来拼写sql语句。 具体写法:"insertintot\_student(id,name,age,class)value("&B2&",'"&C2&"',"&D2&"
Stella981 Stella981
3年前
SpringBoot权限管理开发实战1
开发工具IntellijIdea,版本Ultimate2020.2JDK:1.81.使用SpringInitializr新建项目,如果是社区版Idea,可以在线创建然后导入!(https://oscimg.oschina.net/oscnet/up5d92896117accfe498515c85fd59048bf19.png)
Easter79 Easter79
3年前
SpringBoot权限管理开发实战1
开发工具IntellijIdea,版本Ultimate2020.2JDK:1.81.使用SpringInitializr新建项目,如果是社区版Idea,可以在线创建然后导入!(https://oscimg.oschina.net/oscnet/up5d92896117accfe498515c85fd59048bf19.png)
Wesley13 Wesley13
3年前
FLV文件格式
1.        FLV文件对齐方式FLV文件以大端对齐方式存放多字节整型。如存放数字无符号16位的数字300(0x012C),那么在FLV文件中存放的顺序是:|0x01|0x2C|。如果是无符号32位数字300(0x0000012C),那么在FLV文件中的存放顺序是:|0x00|0x00|0x00|0x01|0x2C。2.  
Stella981 Stella981
3年前
Hystrix实现ThreadLocal上下文的传递 转
springcloud微服务中,服务间传输全局类参数,如session信息等。一、问题背景Hystrix有2个隔离策略:THREAD以及SEMAPHORE,当隔离策略为THREAD时,是没办法拿到ThreadLocal中的值的。Hystrix提供了基于信号量和线程两种隔离模式,通过在Hystrix基础章节中已经验证过,通过
WeiSha100 WeiSha100
2年前
考试系统源码搭建
开源考试系统,可以模拟测试,正式考试,后台调取学员学习情况,批量导出学习成绩。支持外网和局域网私有化部署,搭建在自己的服务器上,使用自己的域名。1、创建专业和课程2、题库导入试题,可单个增加试题或excel批量导入试题,支持五种题型:单选,多选,判断,简答,填空,试题增加或导入时写上解析,答题时无论对错都会显示解析3、创建试卷,可按课程或者章节组卷,每个课程
SpringCloud-Hystrix服务熔断与降级工作原理&源码 | 京东物流技术团队
在生活中,如果电路的负载过高,保险箱会自动跳闸,以保护家里的各种电器,这就是熔断器的一个活生生例子。在Hystrix中也存在这样一个熔断器,当所依赖的服务不稳定时,能够自动熔断,并提供有损服务,保护服务的稳定性。在运行过程中,Hystrix会根据接口的执行状态(成功、失败、超时和拒绝),收集并统计这些数据,根据这些信息来实时决策是否进行熔断。