spring cloud微服务分布式云架构--hystrix的使用

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
简介: hystrix主要作用在服务消费者,进行应用的保护,当请求的服务请求超时时,做出相应的处理,避免客户端一直进行请求等待,避免在高并发的情况出现服务器死机(请求过多,内存不足)接下来的通过一个案例对hystrix的使用进行说明,案例完成的功能:服务消费者根据Id调用服务提供者的接口,获取User...

hystrix主要作用在服务消费者,进行应用的保护,当请求的服务请求超时时,做出相应的处理,避免客户端一直进行请求等待,避免在高并发的情况出现服务器死机(请求过多,内存不足)

接下来的通过一个案例对hystrix的使用进行说明,案例完成的功能:

服务消费者根据Id调用服务提供者的接口,获取User表单的对应的记录,若请求超时则返回id为-1的User记录

一、基于Ribbon

<parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.2.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
  </parent>
 
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
    <mybatis-spring-boot.version>1.2.0</mybatis-spring-boot.version>
    <mysql-connector.version>5.1.39</mysql-connector.version>
</properties>
 
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
        <version>1.3.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-hystrix</artifactId>
        <version>1.3.5.RELEASE</version>
    </dependency>
    
</dependencies>

2、application.yml

server:
  port: 8089
spring:
  application:
    name: customer-user
eureka:
  client: 
    serviceUrl: 
      defaultZone: http://user:zj123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
provider-user: 
  ribbon: 
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

3、Controller调用类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
 
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zhuojing.bean.User;
 
@RestController
public class UserController {
 
    @Autowired
    private RestTemplate restTemplate;
    
    
    @GetMapping(value="/simple/{id}",produces=MediaType.APPLICATION_JSON_VALUE+";charset=utf-8")
    @HystrixCommand(fallbackMethod = "findByIdFallback")
    public User findUserById(@PathVariable Long id){
        //服务提供者地址 PROVIDER-USER
        return this.restTemplate.getForObject("http://PROVIDER-USER/simple/"+id, User.class);
    }
    /**
     * 断路器模式,当请求的消费者provider-user超时的情况下,就会直接调用此方法,此方法的参数和返回类型必须和findUserById方法一致,请求超时时间为1秒
     * @param id
     * @return
     */
    public User findByIdFallback(Long id){
        User user = new User();
        user.setId(0L);
        return user;
    }
    
}

hystrix的默认的超时时间为1秒,若自定hystrix超时时间有一下两种方式

a、@HystrixCommand注解配置

@HystrixCommand(fallbackMethod = "findByIdFallback",commandProperties = {
     @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
})

b、在application.yml配置

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000

4、启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.client.RestTemplate;
 
@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class AppCusRibbonPropertiesTest {
 
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
    public static void main(String[] args) {
        SpringApplication.run(AppCusRibbonPropertiesTest.class, args);
    }
}

二、dashboard的使用

1、pom.xml

<parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.2.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
            <version>1.3.5.RELEASE</version>
        </dependency>
  </dependencies>

2、application.yml

server:
port: 8030

3、启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
 
@EnableHystrixDashboard
@SpringBootApplication
public class DashboardApplication {
  public static void main(String[] args) {
    SpringApplication.run(DashboardApplication.class, args);
  }
}

启动后访问:http:/ /localhost:8030/hystrix 进入首页,在地址栏上输入http:/ /localhost:8089/hystrix.stream(hystrix.stream)进行监控,在使用了hystrix的服务调用后才有数据。

三、Turbine的使用,Turbine是对微服务集群的监听

1、pom.xml

<parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.2.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-turbine</artifactId>
        <version>1.3.5.RELEASE</version>
    </dependency>
  </dependencies>

2、application.yml

server:
  port: 8031
spring:
  application:
    name: microservice-hystrix-turbine
eureka:
  client:
    serviceUrl:
      defaultZone: http://user:zj123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
turbine:
  aggregator:
    clusterConfig: default  #默认defualt,只有一个服务时候可以写服务名称的大写CUSTOMER-USER
  appConfig: customer-user
  clusterNameExpression: "'default'"

3、启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
 
@EnableTurbine
@SpringBootApplication
public class TurbineApplication {
  public static void main(String[] args) {
    SpringApplication.run(TurbineApplication.class, args);
  }
}

hystrix 主页面中将localhost:8031/turbine.stream?cluster=CUSTOMER-USER填入,便可进行数据的图像化。

相关实践学习
部署高可用架构
本场景主要介绍如何使用云服务器ECS、负载均衡SLB、云数据库RDS和数据传输服务产品来部署多可用区高可用架构。
负载均衡入门与产品使用指南
负载均衡(Server Load Balancer)是对多台云服务器进行流量分发的负载均衡服务,可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。 本课程主要介绍负载均衡的相关技术以及阿里云负载均衡产品的使用方法。
目录
相关文章
|
18天前
|
安全 Java API
第7章 Spring Security 的 REST API 与微服务安全(2024 最新版)(上)
第7章 Spring Security 的 REST API 与微服务安全(2024 最新版)
35 0
第7章 Spring Security 的 REST API 与微服务安全(2024 最新版)(上)
|
18天前
|
Java API 微服务
【Spring Boot系列】通过OpenAPI规范构建微服务服务接口
【4月更文挑战第5天】通过OpenAPI接口构建Spring Boot服务RestAPI接口
|
7天前
|
负载均衡 应用服务中间件 nginx
服务器架构、分布式系统、负载均衡、微服务、高可用性
**分布式系统取代单体架构,以微服务实现高扩展性和灵活性。通过负载均衡技术增强性能,防止单点故障,结合冗余备份与故障切换保障高可用性,这种架构是支撑大规模在线业务的关键。**
43 3
|
8天前
|
Java Docker 微服务
|
9天前
|
消息中间件 Java RocketMQ
Spring Cloud RocketMQ:构建可靠消息驱动的微服务架构
【4月更文挑战第28天】消息队列在微服务架构中扮演着至关重要的角色,能够实现服务之间的解耦、异步通信以及数据分发。Spring Cloud RocketMQ作为Apache RocketMQ的Spring Cloud集成,为微服务架构提供了可靠的消息传输机制。
25 1
|
9天前
|
Dubbo Java 应用服务中间件
Spring Cloud Dubbo: 微服务通信的高效解决方案
【4月更文挑战第28天】在微服务架构的发展中,服务间的高效通信至关重要。Spring Cloud Dubbo 提供了一种基于 RPC 的通信方式,使得服务间的调用就像本地方法调用一样简单。本篇博客将探讨 Spring Cloud Dubbo 的核心概念,并通过具体实例展示其在项目中的实战应用。
14 2
|
12天前
|
负载均衡 Java 网络架构
【SpringCloud】如何理解分布式、微服务、集群
【SpringCloud】如何理解分布式、微服务、集群
21 1
|
12天前
|
Java 数据安全/隐私保护 Sentinel
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
微服务学习 | Spring Cloud 中使用 Sentinel 实现服务限流
|
13天前
|
监控 Java 微服务
第八章 Spring Cloud 之 Hystrix
第八章 Spring Cloud 之 Hystrix
14 0
|
18天前
|
安全 Java API
第7章 Spring Security 的 REST API 与微服务安全(2024 最新版)(下)
第7章 Spring Security 的 REST API 与微服务安全(2024 最新版)
22 0