hystrix主要做用在服务消费者,进行应用的保护,当请求的服务请求超时时,作出相应的处理,避免客户端一直进行请求等待,避免在高并发的状况出现服务器死机(请求过多,内存不足)java
接下来的经过一个案例对hystrix的使用进行说明,案例完成的功能:须要JAVA Spring Cloud大型企业分布式微服务云构建的B2B2C电子商务平台源码 一零三八七七四六二六mysql
服务消费者根据Id调用服务提供者的接口,获取User表单的对应的记录,若请求超时则返回id为-1的User记录web
1、基于Ribbonspring
<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>
二、application.ymlsql
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
三、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超时时间有一下两种方式mybatis
a、@HystrixCommand注解配置并发
@HystrixCommand(fallbackMethod = "findByIdFallback",commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000") })
b、在application.yml配置app
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000dom
四、启动类
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); } }
2、dashboard的使用
一、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>
二、application.yml
server: port: 8030
三、启动类
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的服务调用后才有数据。
3、Turbine的使用,Turbine是对微服务集群的监听
一、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>
二、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'"
三、启动类
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填入,即可进行数据的图像化。