前面文章讲述了Hystrix相关内容,这篇文章主要是搭建一个Hystrix实例。
pom文件
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zsy</groupId>
<artifactId>hystrix-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hystrix-service</name>
<url>http://maven.apache.org</url>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.M3</spring-cloud.version>
</properties>
<dependencies>
<!--SpringCloud eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--spring-cloud-starter-eureka已经引了,所以可引可不引-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<!-- 断路器-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</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>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
application.yml文件
###服务启动端口号
server:
port: 8082
###服务名称(服务注册到eureka名称)
spring:
application:
name: zsy-app
mvc:
view:
suffix: .html
prefix: /
###服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:8081/eureka
###册自己到注册中心
register-with-eureka: true
###是否需要从eureka上获取注册信息
fetch-registry: true
# 心跳检测检测与续约时间
# 测试时将值设置设置小些,保证服务关闭后注册中心能及时踢出服务
instance:
###Eureka客户端向服务端发送心跳的时间间隔,单位为秒(客户端告诉服务端自己会按照该规则)
lease-renewal-interval-in-seconds: 1
####Eureka服务端在收到最后一次心跳之后等待的时间上限,单位为秒,超过则剔除(客户端告诉服务端按照此规则等待自己)
lease-expiration-duration-in-seconds: 2
APP启动类
package com.zsy.hystrix_service;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
//通过注解@EnableEurekaClient 表明自己是一个eurekaclient.
@EnableEurekaClient
//注解开启Hystrix
@EnableHystrix
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
/**
* 向程序的ioc注入一个bean: restTemplate;
* 并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。
*/
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
Controller
package com.zsy.hystrix_service.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.zsy.hystrix_service.service.UserService;
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/getName")
public String getName(@RequestParam String name) {
return "test?name="+userService.getName(name);
}
@RequestMapping("/test")
public String test(@RequestParam String name) {
return "test";
}
}
Service
package com.zsy.hystrix_service.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@Service
public class UserService {
@Autowired
RestTemplate restTemplate;
String eurekaclientURL = "http://zsy-app/test.html?name=";
//该注解对该方法创建了熔断器的功能,并指定了fallbackMethod熔断方法
//有个坑 这个方法fallbackMethod = "failError"的方法参数还得和getAge原方法的一样才行
@HystrixCommand(fallbackMethod = "failError")
public String getName(String name) {
return restTemplate.getForObject(eurekaclientURL + name, String.class);
}
public String failError(String name) {
return "test?name="+"err,"+name;
}
}
成功执行的效果:
此时我们把调用链接修改下,如下图:
重启再运行的控制台输出:
证明已经进入了fallback方法,实现了失败调用熔断的效果。