springcloud hystrix 容错机制-爱代码爱编程
1.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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>springcloudDemo</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.weitao</groupId>
<artifactId>springcloud-hystrix</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-hystrix</name>
<description>Demo project for Spring Boot</description>
<properties>
<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.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>springcloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
2.application.yml
server:
port: 8060
spring:
application:
name: hystrix
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${server.port}
#断路器,起到降级作用,避免服务雪崩
feign:
hystrix:
enabled: true
management:
endpoints:
web:
exposure:
include: 'hystrix.stream'
hystrix:
dashboard:
proxy-stream-allow-list: localhost
3.代码结构
4.具体代码
package com.example.springcloud.controller;
import com.example.springcloud.fegin.FeginDept;
import com.example.springcloud.pojo.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/hystrix")
public class HystrixController {
@Autowired
private FeginDept feginDept;
@GetMapping("/select")
public List<Dept> get() {
return feginDept.selectAll();
}
@GetMapping("/select/{id}")
public Dept selectByID(@PathVariable(name = "id") Long id) {
return feginDept.selectByID(id);
}
@PostMapping(value = "/save", produces = "application/json")
public boolean saveDept(@RequestBody Dept dept) {
return feginDept.saveDept(dept);
}
//假如服务提供者没启动,直接访问/handle/getInfo 那么容错机制就会调用FeginDept fallback属性设置FeginError 里的方法
@GetMapping("/getInfo")
public String getInfo() {
return feginDept.getInfo();
}
}
package com.example.springcloud.fegin;
import com.example.springcloud.pojo.Dept;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.List;
/**
* 接口注解方式
* value="provider" 调用提供者的应用名称
*/
@FeignClient(value = "provider")
public interface FeginDept {
@PostMapping(value = "/dept/save",produces = "application/json")
boolean saveDept(@RequestBody Dept dept);
@GetMapping("/dept/select/{id}")
Dept selectByID(@PathVariable("id") Long id);
@GetMapping("/dept/select")
List<Dept> selectAll();
@GetMapping("/dept/getInfo")
String getInfo();
}
package com.example.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
@EnableHystrix
@EnableHystrixDashboard //声明启用可视化数据监控
public class SpringcloudHystrixApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudHystrixApplication.class, args);
}
}
5.运行注册中心,提供者,和 Hystrix模块入口
6. http://localhost:8060/actuator/hystrix.stream
这个界面一直ping输出
7.访问接口
http://localhost:8060/hystrix/select
8. http://localhost:8060/hystrix
这个打开hystrix仪表盘 ,可以输入http://localhost:8060/actuator/hystrix.stream
9.跳转到页面