Eureka注册中心:《使用IntelliJ IDEA创建Spring Cloud服务注册中心》
服务提供者创建:《使用IntelliJ IDEA创建Spring Cloud的Eureka Client》
Ribbon实现负载均衡:《使用IntelliJ IDEA创建Ribbon项目实现负载均衡》
集成Feign的项目:《使用IntelliJ IDEA创建集成Feign的项目简化服务调用的网络连接》
Ribbon项目中使用Hystrix熔断器:《使用IntelliJ IDEA在Spring Cloud的Ribbon项目中使用Hystrix熔断器》
Feign项目中使用Hystrix熔断器:《使用IntelliJ IDEA在Spring Cloud的Feign项目中使用Hystrix熔断器》
创建熔断器监控项目
File---new---module---Spring Assistant
点击next
如下图选择Hystrix Dashboard
点击next
配置熔断器监控项目
resources目录下新建application.yml,application.yml的功能和application.properties是一样的,但yml文件是树状结构,有更好的层次感,更易于理解。然后,删除原有的application.properties。如下图
application.yml
server:
port: 8950
spring:
application:
name: acyx-hystrix-dashboard
给AcyxhystrixdashboardApplication.java添加注解@EnableHystrixDashboard
package com.acyx.hystrixdashboard;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
@EnableHystrixDashboard
public class AcyxhystrixdashboardApplication {
public static void main(String[] args) {
SpringApplication.run(AcyxhystrixdashboardApplication.class, args);
}
}
pom.xml中添加如下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
创建Feign项目使用Hystrix熔断器
前面已经有2篇讲述熔断器使用的文章,分别如下:
Ribbon项目中使用Hystrix熔断器:《使用IntelliJ IDEA在Spring Cloud的Ribbon项目中使用Hystrix熔断器》
Feign项目中使用Hystrix熔断器:《使用IntelliJ IDEA在Spring Cloud的Feign项目中使用Hystrix熔断器》
在本文中,将针对Feign项目中使用Hystrix熔断器的项目进行讲解。
File---new---module---Spring Assistant
点击next
点击next
点击Finish
AcyxfeignthreeApplication.java
package com.acyx.feign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableHystrix
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class AcyxfeignthreeApplication {
public static void main(String[] args) {
SpringApplication.run(AcyxfeignthreeApplication.class, args);
}
}
HomeFeignService.java
package com.acyx.feign.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "acyx-stock")
public interface HomeFeignService {
@RequestMapping(value = "/home")
String homeFeign(@RequestParam(value = "name") String name);
}
HomeFeignController.java
package com.acyx.feign.controller;
import com.acyx.feign.service.HomeFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HomeFeignController {
@Autowired
private HomeFeignService homeFeignService;
@RequestMapping("/homeFeign")
public String homeFeign(String name){
return homeFeignService.homeFeign(name);
}
}
application.yml 开启hystrix熔断器。
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8102
spring:
application:
name: acyx-feign
feign:
hystrix:
enabled: true
pom.xml中添加如下内容:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-openfeign-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
HomeFeignServiceHystric.java
package com.acyx.feign.service.impl;
import com.acyx.feign.service.HomeFeignService;
import org.springframework.stereotype.Component;
@Component
public class HomeFeignServiceHystric implements HomeFeignService {
@Override
public String homeFeign(String name) {
return "sorry, ACYX-STOCK is not normal! Hystrix fallbackMethod execute";
}
}
HomeFeignService.java中给@FeignClient添加fallback,修改后如下:
package com.acyx.feign.service;
import com.acyx.feign.service.impl.HomeFeignServiceHystric;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "acyx-stock",fallback = HomeFeignServiceHystric.class)
public interface HomeFeignService {
@RequestMapping(value = "/home")
String homeFeign(@RequestParam(value = "name") String name);
}
acyxfeignthree项目pom.xml中添加依赖。其中spring-cloud-starter-netflix-hystrix是熔断器相关的依赖。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
spring-boot-starter-actuator默认情况下会暴露health和info端点,需要单独开启暴露hystrix.stream端点。
修改acyxfeignthree项目的application.yml,追加如下内容:
management:
endpoints:
web:
exposure:
include: health, info, hystrix.stream
此时,application.yml内容如下:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8102
spring:
application:
name: acyx-feign
feign:
hystrix:
enabled: true
management:
endpoints:
web:
exposure:
include: health, info, hystrix.stream
在acyxfeignthree项目中的添加注解@EnableHystrix,修改后的AcyxfeignthreeApplication.java
package com.acyx.feign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableHystrix
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class AcyxfeignthreeApplication {
public static void main(String[] args) {
SpringApplication.run(AcyxfeignthreeApplication.class, args);
}
}
此时,启动acyxfeignthree,控制台报如下异常:
搜索maven com.netflix.hystrix.contrib
acyxfeignthree的pom.xml中添加如下依赖:
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
<version>1.5.18</version>
</dependency>
测试项目
依次启动Eureka注册中心、acyxstock、acyxstocktwo、acyxfeignthree,然后在浏览器中访问:http://127.0.0.1:8761
在浏览器中输入:http://localhost:8102/actuator/hystrix.stream ,在没有任务服务请求的情况下,访问该链接显示的页面将不断的显示ping:
此时,在浏览器的新选项卡输入http://localhost:8102/homeFeign?name=test
刷新
此时,回到http://localhost:8102/actuator/hystrix.stream ,刷新,会不断显示下图中内容
启动acyxhystrixdashboard,在浏览器中输入:http://localhost:8950/hystrix/ ,显示如下图页面
在上图椭圆标注的区域中输入:http://localhost:8102/actuator/hystrix.stream 点击Monitor Stream,即对acyxfeignthree熔断器进行监控
只要在浏览器的新选项卡输入http://localhost:8102/homeFeign?name=test 有对acyxfeignthree的服务请求,上图中显示的数据就会发生变化,如下图为请求了2次,显示为数据2,且曲线对应发生变化。不同颜色的数据代表不同的状态,可以对照右上角,如:Success、Timeout等