spring cloud微服務分散式雲架構--hystrix的使用

使用者6866800318發表於2019-03-20

hystrix主要作用在服務消費者,進行應用的保護,當請求的服務請求超時時,做出相應的處理,避免客戶端一直進行請求等待,避免在高併發的情況出現伺服器當機(請求過多,記憶體不足)

接下來的通過一個案例對hystrix的使用進行說明,案例完成的功能:Spring Cloud大型企業分散式微服務雲架構原始碼請加一七九一七四三三八零

服務消費者根據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填入,便可進行資料的影象化。

相關文章