14Spring cloud三(Hystrix断路器)

Spring Cloud—Hystrix断路器


一.断路器简介

我们在基于微服务的架构上设计系统时,通常会开发许多微服务,并且这些微服务之间往往会有数据通信,但是,如果某个微服务出现异常或者服务停止,势必会导致部分业务甚至整个业务系统不稳定,传统的办法是一个服务一个服务地去排查,但这样会变得异常麻烦。这时,我们需要一个能够对整个系统的健康状况进行实时监测的功能,能实现这个功能的就是断路器。

二.代码示例

为演示断路器,我们将创建两个微服务,EurekaClient1和EurekaClient2,EurekaClient2将调用EurekaClient1的接口,我们将用EurekaClient2实现断路器。

1.创建EurekaClient1

1)创建“Spring stater Project”项目,代码如下:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class EurekaClient1Application {

	public static void main(String[] args) {
		SpringApplication.run(EurekaClient1Application.class, args);
	}
}

2)服务器配置:在src\main\resources目录下的application.properties中添加端口号:

server.port = 8098

3)创建Demo1Controller.java,代码如下:

import java.util.ArrayList;
import java.util.List;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class Demo1Controller {
	@RequestMapping(value = "/getdata", method = RequestMethod.GET)
    public List<String> getData() {

		List<String> list = new ArrayList<String>();
		list.add("data1");
		list.add("data2");
		return list; 	
    }
}

4)运行项目,在浏览器中输入地址:http://localhost:8098/,可以看到访问成功。

2.创建EurekaClient2

1).创建"Spring stater Project”项目EurekaClient2Application,加入注解@SpringBootApplication,@EnableHystrixDashboard,@EnableCircuitBreaker,代码如下:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

@SpringBootApplication
@EnableHystrixDashboard
@EnableCircuitBreaker
public class EurekaClient2Application {

   public static void main(String[] args) {
   	SpringApplication.run(EurekaClient2Application.class, args);
   }
}

2)在pom.xml文件引入spring-cloud-starter-hystrix,spring-cloud-starter-hystrix-dashboard代码如下:

<?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>com.example</groupId>
   <artifactId>demo</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>EurekaClient2Application</name>
   <description>Demo project for Spring Boot</description>

   <parent>
   	<groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.3.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </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-actuator</artifactId>
   	</dependency>
   	<dependency>
   		<groupId>org.springframework.cloud</groupId>
   		<artifactId>spring-cloud-starter-hystrix</artifactId>
   	</dependency>
   	<dependency>
   		<groupId>org.springframework.cloud</groupId>
   		<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
   	</dependency>
   	<dependency>
   		<groupId>org.springframework.boot</groupId>
   		<artifactId>spring-boot-starter-data-rest</artifactId>
   	</dependency>
   	<dependency>
   		<groupId>org.springframework.boot</groupId>
   		<artifactId>spring-boot-starter-web</artifactId>
   	</dependency>

   	<dependency>
   		<groupId>org.springframework.boot</groupId>
   		<artifactId>spring-boot-starter-test</artifactId>
   		<scope>test</scope>
   	</dependency>
   	
   	
   	</dependencies>
   <dependencyManagement>
       <dependencies>
           <dependency>
               <groupId>org.springframework.cloud</groupId>
               <artifactId>spring-cloud-dependencies</artifactId>
               <version>Dalston.SR3</version>
               <type>pom</type>
               <scope>import</scope>
           </dependency>
       </dependencies>
   </dependencyManagement>

   <build>
   	<plugins>
   		<plugin>
   			<groupId>org.springframework.boot</groupId>
   			<artifactId>spring-boot-maven-plugin</artifactId>
   		</plugin>
   	</plugins>
   </build>


</project>

3)在src\main\resources目录下application.properties里添加端口号,内容如下:

server.port = 9098    

4)创建Demo2Controller.java,代码如下:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Demo2Controller {
	
	@Autowired
	DataServiceDelegate studentServiceDelegate;
	
	@RequestMapping(value = "/getdata2", method = RequestMethod.GET)
    public String getData() {
		return studentServiceDelegate.getDataFromDemo1(); 	
    }		
}

5)创建DataService.java,代码如下:

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@Service
public class DataService {
	@Autowired
	RestTemplate restTemplate;
	
	@HystrixCommand(fallbackMethod = "callgetData_Fallback")
	public String getDataFromDemo1(){		 
		 String response = restTemplate
	                .exchange("http://localhost:8098/getdata"
	                , HttpMethod.GET
	                , null
	                , new ParameterizedTypeReference<String>() {
	            }).getBody();
		return response;
		
	}
	 
	 @SuppressWarnings("unused")
	private String callgetData_Fallback() {		 
	        return "callgetData_Fallback";
	    }
	 
	 @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

5)运行项目,在浏览器中输入地址:http://localhost:9098/hystrix,显示结果如下:
14Spring cloud三(Hystrix断路器)
6)在上面填写好对应信息后,就可以对系统进行实时监测,如图:
14Spring cloud三(Hystrix断路器)