1. 引言
一般而言,一个服务都是部署了多台机器的,那么在这种情况下,当其中一个服务挂了以后Hystrix是怎么处理的呢?
为了验证这个问题,我们准备两个服务:user-api 和 app-gateway,再加一个Eureka Server
2. 服务搭建
2.1. 注册中心
关于这一部分,参见《SpringCloud学习笔记(1)——Eureka》
2.2. 服务提供方
复制代码
4.0.0
com.cjs.example
user-api
0.0.1-SNAPSHOT
jar
user-api
org.springframework.boot
spring-boot-starter-parent
2.0.5.RELEASE
UTF-8
UTF-8
1.8
Finchley.SR1
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
复制代码
复制代码
server:
port: 8081
spring:
application:
name: user-api
eureka:
instance:
instance-id: 192.168.1.1:${server.port}
hostname: 192.168.1.1
client:
service-url:
defaultZone: http://192.168.1.1:8761/eureka/
复制代码
复制代码
package com.cjs.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class UserApiApplication {
public static void main(String[] args) {
SpringApplication.run(UserApiApplication.class, args);
}
@RequestMapping("/sayHello")
public String sayHello() {
System.out.println("lalala");
return "hello";
}
}
复制代码
这里通过控制打印日志来观察调用的是哪个机器上的该服务
2.3. 服务消费方
pom.xml
复制代码
4.0.0
com.cjs.example
app-gatewate
0.0.1-SNAPSHOT
jar
app-gatewate
org.springframework.boot
spring-boot-starter-parent
2.0.5.RELEASE
UTF-8
UTF-8
1.8
Finchley.SR1
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
复制代码
application.yml
复制代码
server:
port: 8080
spring:
application:
name: app-gateway
eureka:
instance:
instance-id: 192.168.1.1:${server.port}
hostname: 192.168.1.1
client:
service-url:
defaultZone: http://192.168.1.1:8761/eureka/
feign:
hystrix:
enabled: true
复制代码
AppGatewayApplication.java
复制代码
package com.cjs.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableCircuitBreaker
@EnableFeignClients
@SpringBootApplication
public class AppGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(AppGatewayApplication.class, args);
}
}
复制代码
FeignClient
复制代码
package com.cjs.example.service;
import com.cjs.example.fallback.UserClientFallback;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient(value = "user-api", fallback = UserClientFallback.class)
public interface UserClient {
@RequestMapping("/sayHello")
String sayHello();
}
复制代码
Hystrix
复制代码
package com.cjs.example.fallback;
import com.cjs.example.service.UserClient;
import org.springframework.stereotype.Component;
@Component
public class UserClientFallback implements UserClient {
@Override
public String sayHello() {
return "Oh! Error!!!";
}
}
复制代码
UserController.java
复制代码
package com.cjs.example.controller;
import com.cjs.example.service.UserClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserClient userClient;
@RequestMapping("/hello")
public String hello() {
return userClient.sayHello();
}
}
复制代码
目录结构
3. 演示
1、依次启动各服务:eureka-demo-server,user-api(两台),app-gateway
2、浏览器查看 http://192.168.1.1:8761/
3、通过请求 http://localhost:8080/user/hello 令app-gateway调用user-api服务(使用FeignClient调用,并集成Hystrix)
4、观察控制打印“lalala”,同时页面输出“hello”
5、停掉其中一台user-api,发现在短时间内会执行回退操作(即页面返回“Oh! Error!!!”),随后恢复正常(因为请求打到另一台机器上了)
由此,可以看出
首先,一般Hystrix 和 Ribbon一起使用的,而且必要有注册中心,在这种情况下,如果调用某个服务失败(或者拒绝、超时等),会尝试调用其它机器上的该服务,但这种切换需要一定的时间,并不是无缝切换的(也就是说,并不是在服务调用失败或拒绝的时候理解换到另一台机器,在所有机器都尝试过以后都失败的情况下才执行回退逻辑,不是这样的,可以想象一下,单纯依靠Hystrix的能力是不能做到这一点的,况且,Hystrix本身设计就不是用来做这个事情的,它设计是用来阻止级联失败,用快速失败来代替请求排队的,但是有了客户端负载均衡器以后,当其中一台机器挂了以后,请求会转发到其它机器上。因此,切换是负载均衡器做的,跟断路器没有关系)
4. 小结
Ribbon是一个客户端的负载均衡器,它决定选择哪一台机器来调用
Hystrix是一个断路器,它将服务调用进行隔离,用快速失败来代替排队,阻止级联调用失败。它的目的是不让服务挂掉。
二者之间没有关系,因为设计它们的目的不一样,解决的问题也不一样
再回到开头提到的问题,即使没有Hystrix,一台机器挂掉以后,后续的请求也会转到其它机器上。只是有了Hystrix以后,遇到这种失败的时候会执行回退操作。
5. 其它
《Feign Hystrix Fallbacks》
《Hystrix介绍》
《Hystrix是如何工作的》
《Spring Boot 集成 Hystrix》
《SpringCloud学习笔记(3)——Hystrix》
《SpringCloud学习笔记(2)——Ribbon》
《SpringCloud学习笔记(1)——Eureka》
感谢您的阅读,如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮。欢迎各位转载,但必须在文章页面中给出作者和原文连接。https://www.cnblogs.com/cjsblog/p/9687458.html