# 从浅入深 学习 SpringCloud 微服务架构(七)Hystrix(4)

从浅入深 学习 SpringCloud 微服务架构(七)Hystrix(4)

一、hystrix:使用 turbine 聚合所有的 hytrix 的监控数据测试。创建父工程 spring_cloud_hystrix_demo,导入相关依赖坐标。并在父工程 spring_cloud_hystrix_demo 下,创建子工程 hystrix_turbine (子模块)。

1、打开 idea 创建父工程

创建 artifactId 名为 spring_cloud_hystrix_demo 的 maven 工程。
父工程,不写代码,可以删除 src 目录。

--> idea --> File --> New --> Project --> Maven Project SDK: ( 1.8(java version "1.8.0_131" ) --> Next --> Groupld : ( djh.it )Artifactld : ( spring_cloud_hystrix_demo )Version : 1.0-SNAPSHOT--> Name: ( spring_cloud_hystrix_demo )Location: ( \spring_cloud_hystrix_demo\ )	--> Finish

2、在父工程 spring_cloud_hystrix_demo 的 pom.xml 文件中导入依赖坐标。


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>djh.it</groupId><artifactId>spring_cloud_hystrix_demo</artifactId><packaging>pom</packaging><version>1.0-SNAPSHOT</version><modules><module>hystrix_turbine</module></modules><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.6.RELEASE</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-logging</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.4</version><scope>provided</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Greenwich.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><repositories><repository><id>spring-snapshots</id><name>Spring Snapshots</name><url>http://repo.spring.io/libs-snapshot-local</url><snapshots><enabled>true</enabled></snapshots></repository><repository><id>spring-milestones</id><name>Spring Milestones</name><url>http://repo.spring.io/libs-milestone-local</url><snapshots><enabled>false</enabled></snapshots></repository><repository><id>spring-releases</id><name>Spring Releases</name><url>http://repo.spring.io/libs-release-local</url><snapshots><enabled>false</enabled></snapshots></repository></repositories><pluginRepositories><pluginRepository><id>spring-snapshots</id><name>Spring Snapshots</name><url>http://repo.spring.io/libs-snapshot-local</url><snapshots><enabled>true</enabled></snapshots></pluginRepository><pluginRepository><id>spring-milestones</id><name>Spring Milestones</name><url>http://repo.spring.io/libs-milestone-local</url><snapshots><enabled>false</enabled></snapshots></pluginRepository></pluginRepositories><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
<!-- \spring_cloud_hystrix_demo\pom.xml -->

3、在父工程 spring_cloud_hystrix_demo 下,创建子工程(子模块)hystrix_turbine

创建 子工程(子模块)

	--> 右键 spring_cloud_hystrix_demo 父工程--> Modules --> Maven --> Groupld : ( djh.it )Artifactld : ( hystrix_turbine )Version : 1.0-SNAPSHOT--> Next --> Module name: ( hystrix_turbine )Content root : ( \spring_cloud_hystrix_demo\hystrix_turbine )Module file location: ( \spring_cloud_hystrix_demo\hystrix_turbine )--> Finish

4、在子工程 hystrix_turbine (子模块)中的 pom.xml 中导入依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>spring_cloud_hystrix_demo</artifactId><groupId>djh.it</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>hystrix_turbine</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-turbine</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></dependencies></project>
<!-- \spring_cloud_hystrix_demo\hystrix_turbine\pom.xml -->

5、在子工程 hystrix_turbine (子模块)中,创建配置文件 application.yml 配置文件。


##  \spring_cloud_hystrix_demo\hystrix_turbine\src\main\resources\application.ymlserver:port: 8031
spring:application:name: hystrix-turbine
eureka:client:service-url:defaultZone: http://localhost:9000/eurekainstance:prefer-ip-address: true
turbine:  # 配置 turbine 聚合 hystrix 所有监控数据appConfig: service-order  # 要监控的微服务列表,多个用 , 逗号分隔。clusterNameExpression: "'default'"

6、在子工程 hystrix_turbine (子模块)中,创建 启动类 TurbinApplication.java

/***   spring_cloud_hystrix_demo\hystrix_turbine\src\main\java\djh\it\TurbinApplication.java**   2024-5-1  启动类 TurbinApplication.java*/
package djh.it;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;@SpringBootApplication
@EnableTurbine  //激活Turbine
@EnableHystrixDashboard  //激活Dashboard
public class TurbinApplication {public static void main(String[] args) {SpringApplication.run(TurbinApplication.class, args);}
}

二、在父工程 spring_cloud_hystrix_demo 下,创建子工程(子模块)eureka_service

1、创建 子工程 eureka_service(子模块)

	--> 右键 spring_cloud_hystrix_demo 父工程--> Modules --> Maven --> Groupld : ( djh.it )Artifactld : ( eureka_service )Version : 1.0-SNAPSHOT--> Next --> Module name: ( eureka_service )Content root : ( \spring_cloud_hystrix_demo\eureka_service )Module file location: ( \spring_cloud_hystrix_demo\eureka_service )--> Finish

2、在子工程 eureka_service (子模块)中的 pom.xml 中导入依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>spring_cloud_hystrix_demo</artifactId><groupId>djh.it</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>eureka_service</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency></dependencies></project>
<!-- spring_cloud_hystrix_demo\eureka_service\pom.xml -->

3、在子工程 eureka_service (子模块)中,创建配置文件 application.yml 配置文件。

##  spring_cloud_hystrix_demo\eureka_service\src\main\resources\application.yml# 如果演示 eureka 高可用,可以模拟两个 EurekaServer, 一个端口 9000,一个端口 8000,两个需要相互注册。server:port: 9000  # 启动端口 命令行注入。spring:application:name: service-eureka  #spring应用名, # 注意 FeignClient 不支持名字带下划线eureka: # 配置 eureka_serverinstance:hostname: localhostclient:register-with-eureka: false  # 是否将自己注册到注册中心,不配置时,默认 true。 配置高可用时,须注销此行,或配置为 truefetch-registry: false  # 是否从 Eureka 中获取注册信息,不配置时,默认 true。 配置高可用时,须注销此行,或配置为 trueservice-url: # 配置暴露给 EurekaClient 的请求地址defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
#      defaultZone: http://127.0.0.1:9000/eureka/  # 配置高可用时,须配置为另一个 EurekaServerApplication 的端口号,如:8000server:enable-self-preservation: false  # 关闭自我保护机制eviction-interval-timer-in-ms: 4000  # 设置剔除服务间隔时间为 4000 毫秒(4秒)。此参数默认为 true。

4、在子工程 eureka_service (子模块)中,创建 启动类 EurekaServerApplication.java

/***   spring_cloud_hystrix_demo\eureka_service\src\main\java\djh\it\eureka\EurekaServerApplication.java**   2024-5-1  eureka 注册中心,启动类 EurekaServerApplication.java*/package djh.it.eureka;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication
@EnableEurekaServer  //激活 Eureka
public class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class, args);}
}

三、在父工程 spring_cloud_hystrix_demo 下,创建子工程(子模块)product_service

1、在父工程 spring_cloud_hystrix_demo 下,创建 生产者 子工程 product_service(子模块)

	--> 右键 spring_cloud_hystrix_demo 父工程--> Modules --> Maven --> Groupld : ( djh.it )Artifactld : ( product_service )Version : 1.0-SNAPSHOT--> Next --> Module name: ( product_service )Content root : ( spring_cloud_hystrix_demo\product_service )Module file location: ( spring_cloud_hystrix_demo\product_service )--> Finish

2、在 生产者 子工程 product_service (子模块)中的 pom.xml 中导入依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>spring_cloud_hystrix_demo</artifactId><groupId>djh.it</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>product_service</artifactId><dependencies><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><!--            <version>5.1.32</version>--><version>8.0.26</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!-- 导入 eureka 注册中心 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency></dependencies>
</project>
<!-- spring_cloud_hystrix_demo\product_service\pom.xml -->

3、在 生产者 子工程 product_service (子模块)中,创建 商品实体类 Product.java

/***  spring_cloud_consul_demo\product_service\src\main\java\djh\it\product\domain\Product.java**  2024-4-27 商品实体类 Product.java*/
package djh.it.product.domain;import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.math.BigDecimal;@Data
@Entity
@Table(name="tb_product")  //对应数据库中的数据表
public class Product {@Idprivate Long id;private String productName;private Integer status;private BigDecimal price;private String productDesc;private String caption;private Integer inventory;
}

4、在 生产者 子工程 product_service (子模块)中,创建 dao 持久层接口类 ProductDao.java

/***   spring_cloud_hystrix_demo\product_service\src\main\java\djh\it\product\dao\ProductDao.java**  2024-5-1  dao 持久层接口 ProductDao.java*/
package djh.it.product.dao;import djh.it.product.domain.Product;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;public interface ProductDaoextends JpaRepository<Product, Long>, JpaSpecificationExecutor<Product> {}

5、在 生产者 子工程 product_service (子模块)中,创建 service 服务层 接口类 ProductService.java

/***   spring_cloud_hystrix_demo\product_service\src\main\java\djh\it\product\service\ProductService.java**  2024-5-1  service 服务层 接口 ProductService.java*/
package djh.it.product.service;import djh.it.product.domain.Product;public interface ProductService {//根据id查询Product findById(Long id);//保存void save(Product product);//更新void update(Product product);//删除void delete(Long id);
}

6、在 生产者 子工程 product_service (子模块)中,创建 service 服务层 实现类 ProductServiceImpl.java


/***  spring_cloud_consul_demo\product_service\src\main\java\djh\it\product\service\Impl\ProductServiceImpl.java**  2024-4-27  service 服务层 实现类*/
package djh.it.product.service.Impl;import djh.it.product.domain.Product;
import djh.it.product.dao.ProductDao;
import djh.it.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class ProductServiceImpl implements ProductService {@Autowiredprivate ProductDao productDao;@Overridepublic Product findById(Long id) {return productDao.findById(id).get();}@Overridepublic void save(Product product) {productDao.save(product);}@Overridepublic void update(Product product) {productDao.save(product);}@Overridepublic void delete(Long id) {productDao.deleteById(id);}
}

7、在 生产者 子工程 product_service (子模块)中,创建 商品的 controller 类 ProductController.java

/***   spring_cloud_hystrix_demo\product_service\src\main\java\djh\it\product\controller\ProductController.java**  2024-5-1 商品的 controller 类 ProductController.java*/
package djh.it.product.controller;import djh.it.product.domain.Product;
import djh.it.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;@RestController
@RequestMapping("/product")
public class ProductController {@Autowiredprivate ProductService productService;//获取服务器端口号@Value("${server.port}")private String port;//获取服务器IP地址@Value("${spring.cloud.client.ip-address}")   //springcloud 自动获取当前应用的IP地址private String ip;@RequestMapping(value = "/{id}", method = RequestMethod.GET)public Product findById(@PathVariable Long id){try {Thread.sleep(2000l);  //延迟2秒} catch (InterruptedException e) {e.printStackTrace();}Product product = productService.findById(id);product.setProductName("访问的服务地址:"+ip+":"+port);return product;}@RequestMapping(value = "", method = RequestMethod.POST)public String save (@RequestBody Product product){productService.save(product);return "保存成功";}
}

8、在 生产者 子工程 product_service (子模块)中,创建配置文件 application.yml


##  spring_cloud_hystrix_demo\product_service\src\main\resources\application.ymlserver:port: 9001  # 启动端口 命令行注入。
#  port: ${port:9001}  # 启动端口设置为动态传参,如果未传参数,默认端口为 9001spring:application:name: service-product  #spring应用名, # 注意 FeignClient 不支持名字带下划线
#  main:
#    allow-bean-definition-overriding: true # SpringBoot2.1 需要设定。datasource:driver-class-name: com.mysql.jdbc.Driver  # mysql 驱动url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghaiusername: rootpassword: 12311jpa:database: MySQLshow-sql: trueopen-in-view: trueeureka:  # 配置 Eurekaclient:service-url:defaultZone: http://localhost:9000/eureka/  # 多个 eurekaserver 用 , 隔开。instance:prefer-ip-address: true  # 使用 ip 地址注册instance-id: ${spring.cloud.client.ip-address}:${server.port}

9、在 生产者 子工程 product_service (子模块)中,创建 启动类 ProductApplication.java


/***   spring_cloud_hystrix_demo\product_service\src\main\java\djh\it\product\ProductApplication.java**   2024-5-1  启动类 ProductApplication.java*/
package djh.it.product;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EntityScan("djh.it.product.domain")
@EnableEurekaClient  //激活 EurekaClient 同 @EnableDiscoveryClient 注解
public class ProductApplication {public static void main(String[] args) {SpringApplication.run(ProductApplication.class, args);}
}

四、在父工程 spring_cloud_hystrix_demo 下,创建子工程(子模块)order_service

1、创建 消费者 子工程 order_service(子模块)

--> 右键 spring_cloud_hystrix_demo 父工程--> Modules --> Maven --> Groupld : ( djh.it )Artifactld : ( order_service )Version : 1.0-SNAPSHOT--> Next --> Module name: ( order_service )Content root : ( \spring_cloud_hystrix_demo\order_service )Module file location: ( \spring_cloud_hystrix_demo\order_service )--> Finish

2、在消费者子工程(子模块) order_service 的 pom.xml 中导入依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>spring_cloud_hystrix_demo</artifactId><groupId>djh.it</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>order_service</artifactId><dependencies><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><!--            <version>5.1.32</version>--><version>8.0.26</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!-- 导入 eureka 注册中心 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><!-- springcloud 整合 openFeign --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><!-- Hystrix 组件 对 RestTemplate 的支持4步:1)引入 Hystrix 依赖坐标 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency><!-- 引入 hystrix 的监控信息 --><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-dashboard</artifactId></dependency></dependencies>
</project>
<!--  spring_cloud_hystrix_demo\order_service\pom.xml -->

3、在消费者子工程(子模块) order_service 中,创建配置文件 application.yml


##  spring_cloud_hystrix_demo\order_service\src\main\resources\application.ymlserver:port: 9002  # 启动端口 命令行注入。
#  port: ${port:9002}  # 启动端口设置为动态传参,如果未传参数,默认端口为 9002spring:application:name: service-order  #spring应用名, # 注意 FeignClient 不支持名字带下划线
#  main:
#    allow-bean-definition-overriding: true # SpringBoot2.1 需要设定。datasource:driver-class-name: com.mysql.jdbc.Driver  # mysql 驱动url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai# MySQL8.0 可以写成  root, 012311 或  'root', '012311'   # MySQL5.7 只能写成 'root', '012311'  # 注意用户名和密码后一定不能有空格。username: 'root'password: '12311'jpa:database: MySQLshow-sql: trueopen-in-view: trueeureka:  # 配置 Eurekaclient:service-url:defaultZone: http://localhost:9000/eureka/  # 多个 eurekaserver 用 , 隔开。instance:prefer-ip-address: true  # 使用 ip 地址注册instance-id: ${spring.cloud.client.ip-address}:${server.port}# 配置 feign 日志的输出。
# 日志配置:NONE:GI 不输出日志,BASIC:适用于生产环境追踪问题,HEADERS:在BASIC基础上,记录请求和响应头信息,FULL:记录所有。
logging:level:djh.it.order.feign.ProductFeignClient: debugfeign:client:config:default:connectTimeout: 5000   #服务之间建立连接所用的时间  #不设置 connectTimeout 会导致 readTimeout 设置不生效readTimeout: 5000   #建立连接后从服务端读取到数据用的时间service-product:  # 需要调用的服务名称loggerLevel: FULLhystrix:  # 开启对 hystrix 的支持。enabled: truehystrix:  # 配置 hystrix 熔断(Hystrix:基于 RestTemplate 的统一降级配置)command:default:execution:isolation:thread:timeoutInMilliseconds: 5000  # 默认的熔断超时时间为1秒,若1秒没有返回数据,会自动触发降级逻辑。management:   # 配置 Actuator 获取 hystrix 的监控数据 暴躁端点。endpoints:web:exposure:include: '*'   # 暴露所有端点。

4、在消费者子工程(子模块) order_service 中,创建 商品实体类 Product.java

/***   spring_cloud_hystrix_demo\order_service\src\main\java\djh\it\order\domain\Product.java**  2024-5-1  商品实体类 Product.java*/
package djh.it.order.domain;import lombok.Data;
import java.math.BigDecimal;@Data
public class Product {private Long id;private String productName;private Integer status;private BigDecimal price;private String productDesc;private String caption;private Integer inventory;
}

5、在消费者子工程(子模块) order_service 中,创建 controller 类 OrderController.java

/***   spring_cloud_hystrix_demo\order_service\src\main\java\djh\it\order\controller\OrderController.java**  2024-5-1 订单的 controller 类 OrderController.java*/
package djh.it.order.controller;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import djh.it.order.domain.Product;
import djh.it.order.feign.ProductFeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;@RestController
@RequestMapping("/order")
public class OrderController {@Resource    //按类型名称注入 调用 feign 组件的接口类 ProductFeignClient//@Autowired   //自动注入 调用 feign 组件的接口类 ProductFeignClient //有时变量名报红,可替换为 @Resource 注解private ProductFeignClient productFeignClient;/***  Hystrix 组件 对 RestTemplate 的支持4步:4)使用注解配置熔断保护*      fallbackmethod : 配置熔断之后的降级方法* @param id* @return*/@HystrixCommand  //配置了公共的熔断设置后,就不需要传参数。@RequestMapping(value = "/buy/{id}", method = RequestMethod.GET)public Product findById(@PathVariable Long id){Product product = null;//调用 feign 组件的接口类 的方法product = productFeignClient.findById(id);return product;}}

6、在消费者子工程(子模块) order_service 中,创建 feign 组件的接口类 ProductFeignClient.java


/***   spring_cloud_hystrix_demo\order_service\src\main\java\djh\it\order\feign\ProductFeignClient.java**   2024-5-1  创建 调用 feign 组件的接口类 ProductFeignClient.java*   声明需要调用的微服务名称 @FeignClient, name: 服务提供者的名称, fallback:配置熔断发生的降级方法。*/package djh.it.order.feign;import djh.it.order.domain.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;@FeignClient(name="service-product", fallback = ProductFeignClientCallBack.class)  //调用 FeignClient 并添加降级方法的支持
public interface ProductFeignClient {//配置需要调用的微服务接口@RequestMapping(value = "/product/{id}", method = RequestMethod.GET)public Product findById(@PathVariable("id") Long id);
}

7、在消费者子工程(子模块) order_service 中,创建 feign 组件的实现类 ProductFeignClient.java


/***   spring_cloud_hystrix_demo\order_service\src\main\java\djh\it\order\feign\ProductFeignClientCallBack.java**   2024-5-1  创建 feign 组件的接口类 ProductFeignClient 的实现类 ProductFeignClientCallBack.java*   配置熔断触发的降级逻辑。*/package djh.it.order.feign;import djh.it.order.domain.Product;
import org.springframework.stereotype.Component;@Component
public class ProductFeignClientCallBack implements ProductFeignClient{// 熔断降级的方法public Product findById(Long id) {Product product = new Product();product.setProductName("hystrix:基于 feign 调用的熔断配置 -- feign 调用触发熔断降级的方法。");return product;}
}

8、在消费者子工程(子模块) order_service 中,创建 启动类 OrderApplication.java

/***    spring_cloud_hystrix_demo\order_service\src\main\java\djh\it\order\OrderApplication.java**   2024-5-1  启动类 OrderApplication.java*/
package djh.it.order;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication
@EntityScan("djh.it.order.domain")
@EnableEurekaClient  //激活 EurekaClient,同 @EnableDiscoveryClient 注解相同。
@EnableFeignClients  //激活 Feign
@EnableCircuitBreaker  // 2)激活 hystrix,  Hystrix 组件 对 RestTemplate 的支持4步:
@EnableHystrixDashboard  // 激活 Hystrix 基于图形化的 DashBoard(仪表板)监控平台
public class OrderApplication {public static void main(String[] args) {SpringApplication.run(OrderApplication.class, args);}
}

9、运行启动类,进行测试

1)浏览器地址栏输入:http://127.0.0.1:9000 发现3个服务注册。

在这里插入图片描述

2)浏览器地址栏输入:http://127.0.0.1:9001/product/1
正常输出 mysql 数据库的第一条记录:

在这里插入图片描述

3)浏览器地址栏输入:http://127.0.0.1:9002/order/buy/1
正常输出 mysql 数据库的第一条记录:

在这里插入图片描述

4)浏览器地址栏输入:http://localhost:8031/hystrix

在 Hystrix Dashboard 地址框输入:http://localhost:8031/turbine.stream 点击 【Monitor Stream】

刷新几次请求数据(http://127.0.0.1:9002/order/buy/1)会看到数据变化。

在这里插入图片描述

在这里插入图片描述

五、hystrix:hystrix 断路器的工作状态

1、熔断器有三个状态 CLOSED、OPEN、 HALF_OPEN 熔断器默认关闭状态,

当触发熔断后状态变更为 OPEN,在等待到指定的时间,Hystrix 会放请求检测服务是否开启,
这期间熔断器会变为 HALF_OPEN 半开启状态,熔断探测服务可用则继续变更为CLOSED 关闭熔断器。

2、Closed: 关闭状态(断路器关闭),所有请求都正常访问。

代理类维护了最近调用失败的次数如果某次调用失败,则使失败次数加1。
如果最近失败次数超过了在给定时间内允许失败的值则代理类切换到断开(Open)状态。
此时代理开启了一个超时时钟,当该时钟超过了该时间,则切换到半断开(Half-Open)状态。
该超时时间的设定是给了系统一次机会来修正导致调用失败的错误。

3、Open: 打开状态(断路器打开),所有请求都会被降级。

Hystix 会对请求情况计数,当一定时间内失败请求百分比达到阈值,则触发熔断,断路器会完全关闭。
默认失败比例的值是 50%,请求次数最少不低于20次。

4、Half Open: 半开状态,open 状态不是永久的,打开后会进入休眠时间(默认是5S)。

随后断路器会自动进入半开状态。此时会释放1次请求通过,若这个请求是健康的,则会关闭断路器,
否则会开启断路器。

在这里插入图片描述

六、hystrix:hystrix 测试 断路器的工作状态

1、hystrix 测试 断路器的工作状态,模拟环境准备:

1)在订单系统中(服务消费者模块 order_service)加入逻辑判断请求的 id

如果 id=1 : 正常执行(正常调用商品微服务)
如果 id=2 : 抛出异常

2)默认 hystrix 中有触发断路器状态转化的阈值,我们可以修改这些值,让测试体现明显一些。

  • 修改 触发熔断的最小请求次数 : 默认是 20
  • 修改 触发熔断的请求失败的比率: 默认是 50%
  • 修改 断路器开启的时长 : 默认 5秒

2、在消费都子工程(子模块) order_service 中,修改 application.yml 配置类

##  spring_cloud_hystrix_demo\order_service\src\main\resources\application.ymlserver:port: 9002  # 启动端口 命令行注入。
#  port: ${port:9002}  # 启动端口设置为动态传参,如果未传参数,默认端口为 9002spring:application:name: service-order  #spring应用名, # 注意 FeignClient 不支持名字带下划线
#  main:
#    allow-bean-definition-overriding: true # SpringBoot2.1 需要设定。datasource:driver-class-name: com.mysql.jdbc.Driver  # mysql 驱动url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai# MySQL8.0 可以写成  root, 012311 或  'root', '012311'   # MySQL5.7 只能写成 'root', '012311'  # 注意用户名和密码后一定不能有空格。username: 'root'password: '12311'jpa:database: MySQLshow-sql: trueopen-in-view: trueeureka:  # 配置 Eurekaclient:service-url:defaultZone: http://localhost:9000/eureka/  # 多个 eurekaserver 用 , 隔开。instance:prefer-ip-address: true  # 使用 ip 地址注册instance-id: ${spring.cloud.client.ip-address}:${server.port}# 配置 feign 日志的输出。
# 日志配置:NONE:GI 不输出日志,BASIC:适用于生产环境追踪问题,HEADERS:在BASIC基础上,记录请求和响应头信息,FULL:记录所有。
logging:level:djh.it.order.feign.ProductFeignClient: debug#feign:
#  client:
#    config:
#      default:
#        connectTimeout: 5000   #服务之间建立连接所用的时间  #不设置 connectTimeout 会导致 readTimeout 设置不生效
#        readTimeout: 5000   #建立连接后从服务端读取到数据用的时间
#      service-product:  # 需要调用的服务名称
#        loggerLevel: FULL
#  hystrix:  # 开启对 hystrix 的支持。
#    enabled: truehystrix:  # 配置 hystrix 熔断(Hystrix:基于 RestTemplate 的统一降级配置)command:default:execution:isolation:thread:timeoutInMilliseconds: 5000  # 默认的熔断超时时间为1 秒,若1秒没有返回数据,会自动触发降级逻辑。circuitBreaker:requestVolumeThreshold: 5  # 触发熔断的最小请求次数,默认 20 次/秒。sleepWindbwInMilliseconds: 10000  # 熔断多少秒后去尝试请求。默认打开状态的时间是 5秒errorThresholdPercentage: 50  # 触发熔断的失败请求最小占比,默认 50%
management:   # 配置 Actuator 获取 hystrix 的监控数据 暴躁端点。endpoints:web:exposure:include: '*'   # 暴露所有端点。

3、在消费都子工程(子模块) order_service 中,修改 controller 类 OrderController.java

/***   spring_cloud_hystrix_demo\order_service\src\main\java\djh\it\order\controller\OrderController.java**  2024-5-1 订单的 controller 类 OrderController.java*/
package djh.it.order.controller;import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import djh.it.order.domain.Product;
import djh.it.order.feign.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;import javax.annotation.Resource;@RestController
@RequestMapping("/order")
//@DefaultProperties(defaultFallback = "defaultFallBack")
public class OrderController {@Autowired  //注入 restTemplate 对象private RestTemplate restTemplate;//    @Resource    //按类型名称注入 调用 feign 组件的接口类 ProductFeignClient
//    //@Autowired   //自动注入 调用 feign 组件的接口类 ProductFeignClient //有时变量名报红,可替换为 @Resource 注解
//    private ProductFeignClient productFeignClient;/***  Hystrix 组件 对 RestTemplate 的支持4步:4)使用注解配置熔断保护*      fallbackmethod : 配置熔断之后的降级方法* @param id* @return*/
//    @HystrixCommand  //配置了公共的熔断设置后,就不需要传参数。@HystrixCommand(fallbackMethod = "orderFallBack")@RequestMapping(value = "/buy/{id}", method = RequestMethod.GET)public Product findById(@PathVariable Long id){if(id != 1){throw new RuntimeException("服务器异常");}Product product = null;//product = restTemplate.getForObject("http://127.0.0.1:9001/product/1", Product.class);product = restTemplate.getForObject("http://service-product/product/1", Product.class);//       //调用 feign 组件的接口类 的方法
//        product = productFeignClient.findById(id);return product;}/***  Hystrix 组件 对 RestTemplate 的支持4步:3)降级方法*   例如:如果访问的 service-product 服务器宕机,就会触发此降级方法* @param id* @return*/public Product orderFallBack(Long id){Product product = new Product();product.setProductName("触发降级方法");return product;}/***  指定统一的降级方法:无参数*/
//    public Product defaultFallBack(){
//        Product product = new Product();
//        product.setProductName("触发了 统一 的降级方法");
//        return product;
//    }
}

4、在消费都子工程(子模块) order_service 中,修改 启动类 OrderApplication.java 使用 RestTemplate 调用微服务。

/***    spring_cloud_hystrix_demo\order_service\src\main\java\djh\it\order\OrderApplication.java**   2024-5-1  启动类 OrderApplication.java*/
package djh.it.order;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
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.hystrix.dashboard.EnableHystrixDashboard;
//import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;@SpringBootApplication
@EntityScan("djh.it.order.domain")
@EnableEurekaClient  //激活 EurekaClient,同 @EnableDiscoveryClient 注解相同。
//@EnableFeignClients  //激活 Feign
@EnableCircuitBreaker  // 2)激活 hystrix,  Hystrix 组件 对 RestTemplate 的支持4步:
@EnableHystrixDashboard  // 激活 Hystrix 基于图形化的 DashBoard(仪表板)监控平台
public class OrderApplication {@LoadBalanced@Beanpublic RestTemplate restTemplate(){return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(OrderApplication.class, args);}
}

9、运行启动类,进行测试

1)浏览器地址栏输入:http://127.0.0.1:9000 发现3个服务注册。

在这里插入图片描述

2)浏览器地址栏输入:http://127.0.0.1:9001/product/1
正常输出 mysql 数据库的第一条记录:

在这里插入图片描述

3)浏览器地址栏输入:http://127.0.0.1:9002/order/buy/1
正常输出 mysql 数据库的第一条记录:
在这里插入图片描述

4)浏览器地址栏输入:http://localhost:8031/hystrix
在 Hystrix Dashboard 地址框输入:http://localhost:8031/turbine.stream 点击 【Monitor Stream】
在这里插入图片描述

刷新几次请求数据(http://127.0.0.1:9002/order/buy/1)会看到数据变化。
再输入 http://127.0.0.1:9002/order/buy/2 错误请求,会触发降级方法,多刷新几次,

在这里插入图片描述

会发现,偶尔 http://127.0.0.1:9002/order/buy/1 也会触发降级方法,这就是熔断器的作用。
在这里插入图片描述

在这里插入图片描述

七、hystrix:隔离策略的说明 与 总结。

1、熔断器的隔离策略

微服务使用 Hystrix 熔断器实现了服务的自动降级,让微服务具备自我保护的能力,提升了系统的稳定性,也较好的解决雪崩效应。其使用方式目前支持两种策略:

  • 线程池隔离策略: 使用一个线程池来存储当前的请求,线程池对请求作处理,设置任务返回处理超时时间,堆积的请求堆积入线程池队列。
    这种方式需要为每个依赖的服务申请线程池,有一定的资源消耗,好处是可以应对突发流量(流量洪峰来临时,处理不完可将数据存储到线程池队里慢慢处理 )

  • 信号量隔离策略: 使用一个原子计数器(或信号量)来记录当前有多少个线程在运行,请求来先判断计数器的数值,若超过设置的最大线程个数则丢弃改类型的新请求,若不超过则执行计数操作请求来计数器+1,请求返回计数器-1。
    这种方式是严格的控制线程且立即返回模式,无法应对突发流量(流量洪峰来临时,处理的线程超过数量,其他的请求会直接返回,不继续去请求依赖的服务)。

2、线程池和信号量两种策略功能支持对比如下:

在这里插入图片描述

3、Hystrix 的核心源码

Hystrix 底层基于 Rxjava,Rxjava 是响应式编程开发库,
因此 Hystrix 的整个实现策略简单说即:把一个 HystrixCommand 封装成一个 Observable(待观察者),
针对自身要实现的核心功能,对 Observable 进行各种装饰,并在订阅各步装饰的 0bservable,以便在指定事件到达时,添加自己的业务。

4、hystrix 的执行过程如下:

在这里插入图片描述

5、hystrix 总结:

1)服务容错的核心知识:雪崩效应、服务隔离、熔断降级、服务限流。
2)hystrix 可以对 RestTemplate 调用进行熔断,也可以对 Feign 远程调用进行熔断。
使用方法是:使用注解,配置降级方法。
3)为了方便监控熔断信息,可以 搭建 Hystrix 监控平台。
4)可以通过 Turbine 形式 对 Hystrix 断路器进行聚合监控。
5)熔断器的状态与状态转换:CLOSED, OPEN, HALF_OPEN 状态。
6)hystrix 熔断器的隔离策略有:线程池隔离策略、信号量隔离策略。

上一节链接请点击:
# 从浅入深 学习 SpringCloud 微服务架构(七)Hystrix(3)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/322720.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

牛客NC97 字符串出现次数的TopK问题【中等 哈希+优先级队列 Java/Go】

题目 题目链接&#xff1a; https://www.nowcoder.com/practice/fd711bdfa0e840b381d7e1b82183b3ee 核心 哈希&#xff0c;优先级队列Java代码 import java.util.*;public class Solution {/*** 代码中的类名、方法名、参数名已经指定&#xff0c;请勿修改&#xff0c;直接返…

AI智能对话系统源码 内置所有支付接口 功能强大 带完整的安装代码包以及安装部署教程

在数字化日益普及的今天&#xff0c;AI智能对话系统已经成为企业与客户沟通的重要桥梁。为了满足市场的需求&#xff0c;罗峰给大家分享一款全新的AI智能对话系统源码&#xff0c;它集成了所有必要的支付接口&#xff0c;功能强大且易于部署。 以下是部分代码示例&#xff1a;…

vue3创建响应式数据ref和reactive的区别

reactive和ref在Vue.js中都是用于创建响应式数据的&#xff0c;但它们之间存在一些区别 定义数据类型不同。ref主要用于定义基本数据类型&#xff0c;如字符串、数字、布尔值等&#xff1b;reactive主要用于定义对象&#xff08;或数组&#xff09;类型的数据&#xff0c;但re…

备考2024年小学生古诗文大会:吃透10道历年真题和知识点(持续)

对上海小学生的小升初和各种评优争章来说&#xff0c;语文、数学、英语的含金量较高的证书还是很有价值和帮助的。对于语文类的竞赛&#xff0c;小学生古诗文大会和汉字小达人通常是必不可少的&#xff0c;因为这两个针对性强&#xff0c;而且具有很强的上海本地特色。 根据往…

MM模块学习一(供应商创建,物料类型的定义及功能)

物料管理流程&#xff1a; 源头&#xff1a;采购需求->采购申请 MRP&#xff1a;物料需求计划。运行物料需求计划的结果&#xff0c;根据物料的性质来判断是外购&#xff08;采购申请&#xff09;或者是生产&#xff08;计划订单->生产订单&#xff09;。 采购申请&am…

Angular基础-搭建Angular运行环境

这篇文章介绍了在Angular项目中进行开发环境搭建的关键步骤。包括node.js安装和配置、安装Angular CLI工具、安装angular-router、创建Angular项目等步骤。这篇文章为读者提供了清晰的指南&#xff0c;帮助他们快速搭建Angular开发环境&#xff0c;为后续的项目开发奠定基础。 …

改进灰狼算法优化随机森林回归预测

灰狼算法&#xff08;Grey Wolf Optimization&#xff0c;GWO&#xff09;是一种基于自然界灰狼行为的启发式优化算法&#xff0c;在2014年被提出。该算法模仿了灰狼群体中不同等级的灰狼间的优势竞争和合作行为&#xff0c;通过不断搜索最优解来解决复杂的优化问题。 灰狼算法…

如何使用ESOP电子作业指导书系统提高工作效率?

在当今工业生产和制造领域&#xff0c;实现作业标准化是提高生产效率、保证产品质量、提升企业竞争力的重要途径。而 ESOP 无纸化指导书系统作为一种创新的技术手段&#xff0c;正逐渐成为实现作业标准化的关键所在。 ESOP 无纸化指导书系统通过数字化的方式&#xff0c;将传统…

docker学习笔记(四)制作镜像

目录 第1步&#xff1a;编辑Dockerfile 第2步&#xff1a;编辑requirements.txt文件 第3步&#xff1a;编辑app.py文件&#xff0c;我们的程序文件 第4步&#xff1a;生成镜像文件 第5步&#xff1a;使用镜像&#xff0c;启动容器 第6步&#xff1a; 启动redis容器、将容器…

MySQL中JOIN连接的实现算法

目录 嵌套循环算法&#xff08;NLJ&#xff09; 简单嵌套循环&#xff08;SNLJ&#xff09; 索引嵌套循环&#xff08;INLJ&#xff09; 块嵌套循环&#xff08;BNLJ&#xff09; 三种算法比较 哈希连接算法&#xff08;Hash Join&#xff09; 注意事项&#xff1a; 工…

93、动态规划-最长回文子串

思路 首先从暴力递归开始&#xff0c;回文首尾指针相向运动肯定想等。就是回文&#xff0c;代码如下&#xff1a; public String longestPalindrome(String s) {if (s null || s.length() 0) {return "";}return longestPalindromeHelper(s, 0, s.length() - 1);…

django中的cookie与session

获取cookie request.COOKIE.GET 使用cookie response.set-cookie views.py from django.http import HttpResponse from django.shortcuts import render# Create your views here. def cookie_test(request):r HttpResponse("hello world")r.set_cookie(lan, py…

【第38天】SQL进阶-SQL设计优化-范式设计(SQL 小虚竹)

回城传送–》《100天精通MYSQL从入门到就业》 文章目录 零、前言一、练习题目二、SQL思路初始化数据什么是范式设计第一范式&#xff08;1NF&#xff09;第二范式&#xff08;2NF&#xff09;第三范式&#xff08;3NF&#xff09; 三、总结四、参考 零、前言 今天是学习 SQL …

Sealos急速部署生产用k8s集群

最近一段时间部署k8s全部使用sealos了&#xff0c;整体使用感觉良好&#xff0c;基本没有什么坑。推荐给大家。 使用 Sealos&#xff0c;可以安装一个不包含任何组件的裸 Kubernetes 集群。 最大的好处是提供 99 年证书&#xff0c;用到我跑路是足够了。不用像之前kubeadm安装…

CISCN 2023 初赛

Web unzip 文件上传页面 upload.php页面源码显示了出来 <?php error_reporting(0); highlight_file(__FILE__);$finfo finfo_open(FILEINFO_MIME_TYPE); if (finfo_file($finfo, $_FILES["file"]["tmp_name"]) application/zip){exec(cd /tmp &am…

中间件之异步通讯组件RabbitMQ进阶

这里我们必须尽可能确保MQ消息的可靠性&#xff0c;即&#xff1a;消息应该至少被消费者处理1次 那么问题来了&#xff1a; 我们该如何确保MQ消息的可靠性&#xff1f; 如果真的发送失败&#xff0c;有没有其它的兜底方案&#xff1f; 首先&#xff0c;我们一起分析一下消息…

文本批量操作技巧:内容查找不再繁琐,自动化批量移动至指定文件夹

在文本处理和信息管理的日常工作中&#xff0c;我们经常需要处理大量的文件和数据。面对这些海量的信息&#xff0c;如何快速而准确地查找特定的内容&#xff0c;并将它们批量移动至指定的文件夹&#xff0c;成为了一项关键的技能。本文将介绍办公提效工具一些实用的文本批量操…

企业车辆管理系统参考论文(论文 + 源码)

【免费】关于企业车辆管理系统.zip资源-CSDN文库https://download.csdn.net/download/JW_559/89282550 企业车辆管理系统 摘 要 随着经济的日益增长,车辆作为最重要的交通工具,在企事业单位中得以普及,单位的车辆数目已经远远不止简单的几辆,与此同时就产生了车辆资源的合理…

实践精益理念:精益生产培训助力企业持续增长

在日益激烈的市场竞争中&#xff0c;企业如何寻找持续增长的动力&#xff0c;提升整体创新能力和核心竞争力&#xff1f;张驰咨询通过多年来的深入研究和实践&#xff0c;结合众多企业的实际情况&#xff0c;带来了精益生产培训的全新视角。 在近期举办的一次精益生产培训中&am…

DDR4 SDRAM 和DDR5 SDRAM的区别

DDR4 SDRAM和DDR5 SDRAM作为两代内存技术,它们在多个方面展现出了显著的差异和改进,以下是对两者区别的详细介绍: 性能与频率 DDR4 SDRAM 的标准工作频率范围从1600MHz(DDR4-1600)到3200MHz(DDR4-3200),在非超频情况下。它的数据传输速率和带宽因此在该范围内。DDR5 S…