Hystrix:可以簡單認為是,服務的調用者(消費者)調用服務提供者提供的服務時的異常處理。
Feign整合Hystrix服務容錯
基本環境
參考:https://blog.csdn.net/admin_15082037343/article/details/107071725
- Fallback方式
- demo-client添加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
- 啟動類新增注解@EnableHystrix
package com.demo.client;
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;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrix
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
}
- application.yml啟動斷路器
feign:
hystrix:
enabled: true
- 新增類UserClientHystrix
package com.demo.client.fallback;
import com.demo.client.api.UserClient;
import com.demo.client.entity.User;
import org.springframework.stereotype.Component;
@Component
public class UserClientHystrix implements UserClient {
@Override
public Object findUser(Integer pageNo, Integer size) {
return "{}";
}
@Override
public User save(User user) {
return new User();
}
@Override
public User findById(String id) {
return new User();
}
@Override
public String deleteById(String id) {
return "-1";
}
}
這裡可以了解為調用服務失敗傳回預設值
- @FeignClient指定fallback
- 驗證
此時啟動注冊中心和服務消費者,不啟動服務提供者,通路接口,不會報錯
{},就是在UserClientHystrix 的findUser失敗是傳回的預設值。
- FallbackFactory方式
- 新增類UserClientFallbackFactory
package com.demo.client.fallback;
import com.demo.client.api.UserClient;
import com.demo.client.entity.User;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;
@Component
public class UserClientFallbackFactory implements FallbackFactory<UserClient> {
@Override
public UserClient create(Throwable throwable) {
throwable.printStackTrace();
return new UserClient() {
@Override
public Object findUser(Integer pageNo, Integer size) {
return "{}";
}
@Override
public User save(User user) {
return new User();
}
@Override
public User findById(String id) {
return new User();
}
@Override
public String deleteById(String id) {
return "-1";
}
};
}
}
- @FeignClient更新
這裡用fallbackFactory 而不是fallback
Hystrix監控
application.yml新增配置
management:
endpoints:
web:
exposure:
include: health,info,hystrix.stream
浏覽器通路:http://127.0.0.1:8082/actuator/hystrix.stream
整合Dashboard檢視監控資料
- 這裡建立一個子產品demo-dashboard,并添加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
- 啟動類
package com.demo.dashboard;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
@EnableHystrixDashboard
public class DashboardApplication {
public static void main(String[] args) {
SpringApplication.run(DashboardApplication.class, args);
}
}
- application.yml
server:
port: 9011
spring:
application:
name: demo-dashboard
這裡沒有注冊到注冊中心
- 浏覽器通路:http://127.0.0.1:9011/hystrix 這裡的位址為http://127.0.0.1:8082/actuator/hystrix.stream
Turbine聚合叢集資料
- demo-dashboard中添加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
- 啟動類新增注解
package com.demo.dashboard;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
@SpringBootApplication
@EnableHystrixDashboard
@EnableDiscoveryClient
@EnableTurbine
public class DashboardApplication {
public static void main(String[] args) {
SpringApplication.run(DashboardApplication.class, args);
}
}
- application.yml
eureka:
client:
service-url:
defaultZone: http://admin:[email protected]:8761/eureka/
turbine:
app-config: demo-client
aggregator:
cluster-config: default
cluster-name-expression: new String("default")
turbine.app-config:需要監控的應用
這裡需要注冊到注冊中心
- 啟動,浏覽器通路:http://127.0.0.1:9011/turbine.stream
- 浏覽器通路: http://127.0.0.1:9011/hystrix 填寫路徑
源碼參考
https://github.com/phone15082037343/demo.git