一、搭建方式
Springboot版本:2.3.1.RELEASE
SpringCloud版本:Hoxton.SR9
项目截图:
二、搭建过程
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>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.hystrix</groupId>
<artifactId>springcloud-hystrix</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-hystrix</name>
<packaging>jar</packaging>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</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>
<!--Eureka客户端配置 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--引入Feign支持 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</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-actuator</artifactId>
</dependency>
<!--可视化熔断器监控 Hystrix Dashboard -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</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>
</project>
application.yml配置:
# 服务名称
spring:
application:
name: springcloud-hystrix
server:
port: 8093
servlet:
context-path: /
eureka:
client:
service-url:
defaultZone: http://admin:admin@eureka-server2:10002/eureka/,http://admin:admin@eureka-server1:10001/eureka/
instance:
instance-id: springcloud-hystrix-8093 #定义微服务的名称
prefer-ip-address: true #是否显示IP和端口
feign:
hystrix:
enabled: true #启动熔断器机制
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 3000 #设定断路器超时时间
dashboard:
proxy-stream-allow-list: "*"
Application启动类:
package com.example.hystrix;
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
public class SpringcloudHystrixApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudHystrixApplication.class, args);
}
}
启动项目后,页面dashboard页面如图:
这里我以消费者consumer项目为例,进行监控,被监控项目pom.xml文件需要添加依赖:
<!--添加断路器配置 -->
<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>
被监控服务application.yml文件中添加配置:
feign:
hystrix:
enabled: true #启动熔断器机制
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 3000 #设定断路器超时时间
management:
endpoints:
web:
exposure:
include: "*"
添加监控配置文件DashboardConfig:
package com.example.consumer.config;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.Servlet;
@Configuration
@EnableCircuitBreaker
public class DashboardConfig {
@Bean
public ServletRegistrationBean<Servlet> getServlet(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean<Servlet> registrationBean = new ServletRegistrationBean<>(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/actuator/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
在controller方法上添加注解:
//查询所有用户的信息
@GetMapping("/getAllUser")
@HystrixCommand(fallbackMethod="getAllUser")
public List<User> getAllUser() {
return userFeignClient.getAllUser();
}
在接口调用添加注解写明fallbackFactory方法:
package com.example.consumer.client;
import com.example.consumer.fallback.UserFeignClientFallbackFactory;
import com.example.api.pojo.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
@FeignClient(value = "springcloud-provider", fallbackFactory = UserFeignClientFallbackFactory.class)//指定服务提供者的服务名称和fallback类
public interface UserFeignClient {
//查询所有用户的信息
@GetMapping("/getAllUser")
List<User> getAllUser();
}
实现fallbackFactory方法:
package com.example.consumer.fallback;
import com.example.consumer.client.UserFeignClient;
import com.example.api.pojo.User;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName: UserFeignClientFallbackFactory
* @Description: 用户服务返回预期错误信息
* @author:
* @date:
*/
@Component(value = "userFeignClient")
public class UserFeignClientFallbackFactory implements FallbackFactory<UserFeignClient> {
@Override
public UserFeignClient create(Throwable throwable) {
return new UserFeignClient() {
@Override
public List<User> getAllUser() {
User user = User.builder()
.id(0)
.name("后台服务器异常,查询用户失败")
.age(0)
.sex("").build();
List<User> userList = new ArrayList<User>();
userList.add(user);
return userList;
}
};
}
}
在Hystrix监控输入框输入监控的服务,以消费者为例:http://localhost:8092/actuator/hystrix.stream
可以点击消费者查询接口,查看监控可得两个方法都查询成功:
停止provider服务,再次调用消费者接口可得该返回结果是我们服务降级后的返回结果:
查看监控面板结果可看出在feign调用方法上失败了:
三、上述总结
以上就是Hystrix服务搭建使用过程,目前Hystrix已经停止更新了,可替换产品可用Sentinel。
下一篇: Zipkin链路服务搭建(6).