springcloud复习2

Hystrix断路器

Hystrix是一个用于处理分布式系统的延迟和容错的开源库,在分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等,Hystrix能够保证在一个依赖出问题的情况下,不会导致整体服务失败,避免级联故障,以提高分布式系统的弹性。
 
“断路器”本身是一种开关装置,当某个服务单元发生故障之后,通过断路器的故障监控(类似熔断保险丝),向调用方返回一个符合预期的、可处理的备选响应(FallBack),而不是长时间的等待或者抛出调用方无法处理的异常,这样就保证了服务调用方的线程不会被长时间、不必要地占用,从而避免了故障在分布式系统中的蔓延,乃至雪崩。

官网资料 https://github.com/Netflix/Hystrix/wiki/How-To-Use


服务熔断
熔断机制是应对雪崩效应的一种微服务链路保护机制。
当扇出链路的某个微服务不可用或者响应时间太长时,会进行服务的降级,进而熔断该节点微服务的调用,快速返回"错误"的响应信息。当检测到该节点微服务调用响应正常后恢复调用链路。在SpringCloud框架里熔断机制通过Hystrix实现。Hystrix会监控微服务间调用的状况,当失败的调用到一定阈值,缺省是5秒内20次调用失败就会启动熔断机制。熔断机制的注解是@HystrixCommand。

@HystrixCommand报异常后如何处理?

一旦调用服务方法失败并抛出了错误信息后,会自动调用@HystrixCommand标注好的fallbackMethod调用类中的指定方法

主启动类:

@SpringBootApplication
@EnableEurekaClient //本服务启动后会自动注册到eureka服务中
@EnableDiscoveryClient //服务发现
@EnableCircuitBreaker //对hystrix熔断机制的支持
public class Hystrix8001 {
public static void main(String[] args) {
SpringApplication.run(Hystrix8001.class, args);
}
}

application.yml配置:

server:
port: 8001

mybatis:
config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径
type-aliases-package: com.hourui.springcloud.entities # 所有Entity别名类所在包
mapper-locations:
- classpath:mybatis/mapper/**/*.xml # mapper映射文件

spring:
application:
name: microservicecloud-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包
url: jdbc:mysql://localhost:3306/cloudDB01 # 数据库名称
username: root
password: root
dbcp2:
min-idle: 5 # 数据库连接池的最小维持连接数
initial-size: 5 # 初始化连接数
max-total: 5 # 最大连接数
max-wait-millis: 200 # 等待连接获取的最大超时时间

eureka:
client: #客户端注册进eureka服务列表内
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
instance:
instance-id: microservicecloud-dept8001-hystrix
prefer-ip-address: true #访问路径可以显示IP地址
info:
app.name: hourui-microservicecloud
company.name: www.hourui.com
build.artifactId: $project.artifactId$
build.version: $project.version$

controller层:

@RestController
public class DeptController {

@Autowired
private DeptService service = null;

@RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
//一旦调用服务方法失败并抛出了错误信息后,会自动调用@HystrixCommand标注好的fallbackMethod调用类中的指定方法
@HystrixCommand(fallbackMethod = "processHystrix_Get")
public Dept get(@PathVariable("id") Long id)
{

Dept dept = this.service.get(id);

if (null == dept) {
throw new RuntimeException("该ID:" + id + "没有没有对应的信息");
}

return dept;
}

public Dept processHystrix_Get(@PathVariable("id") Long id)
{
return new Dept().setDeptno(id).setDname("该ID:" + id + "没有没有对应的信息,null--@HystrixCommand")
.setDb_source("no this database in MySQL");
}

}

 

服务降级

整体资源快不够了,忍痛将某些服务先关掉,待渡过难关,再开启回来。

服务降级处理是在客户端实现完成的,与服务端没有关系

主启动类:

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class Feign80 {
public static void main(String[] args) {
SpringApplication.run(Feign80.class, args);
}
}

application.yml配置:

server:
port: 80

feign:
hystrix:
enabled: true

eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

controller层:

@RestController
public class DeptController_Consumer
{
@Autowired
private DeptClientService service;

@RequestMapping(value = "/consumer/dept/get/{id}")
public Dept get(@PathVariable("id") Long id)
{
return this.service.get(id);
}

@RequestMapping(value = "/consumer/dept/list")
public List<Dept> list()
{
return this.service.list();
}

@RequestMapping(value = "/consumer/dept/add")
public Object add(Dept dept)
{
return this.service.add(dept);
}
}

service层:

@FeignClient(value = "MICROSERVICECLOUD-DEPT",fallbackFactory=DeptClientServiceFallbackFactory.class)
public interface DeptClientService
{
@RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
public Dept get(@PathVariable("id") long id);

@RequestMapping(value = "/dept/list", method = RequestMethod.GET)
public List<Dept> list();

@RequestMapping(value = "/dept/add", method = RequestMethod.POST)
public boolean add(Dept dept);
}

@Component // 不要忘记添加,不要忘记添加
public class DeptClientServiceFallbackFactory implements FallbackFactory<DeptClientService>
{
@Override
public DeptClientService create(Throwable throwable)
{
return new DeptClientService() {
@Override
public Dept get(long id)
{
return new Dept().setDeptno(id).setDname("该ID:" + id + "没有没有对应的信息,Consumer客户端提供的降级信息,此刻服务Provider已经关闭")
.setDb_source("no this database in MySQL");
}

@Override
public List<Dept> list()
{
return null;
}

@Override
public boolean add(Dept dept)
{
return false;
}
};
}
}

总结:

根据已经有的DeptClientService接口新建一个实现了FallbackFactory接口的类DeptClientServiceFallbackFactory
此时服务端provider已经down了,但是我们做了服务降级处理,让客户端在服务端不可用时也会获得提示信息而不会挂起耗死服务器。

例如访问:http://localhost/consumer/dept/get/1 ,故意关闭服务端


服务监控hystrixDashboard:

除了隔离依赖服务的调用以外,Hystrix还提供了准实时的调用监控(Hystrix Dashboard),Hystrix会持续地记录所有通过Hystrix发起的请求的执行信息,并以统计报表和图形的形式展示给用户,包括每秒执行多少请求多少成功,多少失败等。Netflix通过hystrix-metrics-event-stream项目实现了对以上指标的监控。Spring Cloud也提供了Hystrix Dashboard的整合,对监控内容转化成可视化界面。
 

主启动类:

@SpringBootApplication
@EnableHystrixDashboard
public class Dashboard9001 {
public static void main(String[] args) {
SpringApplication.run(Dashboard9001.class, args);
}
}

application.yml配置:

server:
port: 9001

所有Provider微服务提供类(8001/8002/8003)都需要监控依赖配置

   <!-- actuator监控信息完善 -->
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-actuator</artifactId>
   </dependency>

启动该服务9001和8001服务端和eureka注册中心

访问地址:http://localhost:8001/hystrix.stream

                  http://localhost:8001/dept/get/1

多次刷新http://localhost:8001/dept/get/1 ,观察状况

 监控窗口如何看?

总结就是7色1圈1线,

实心圆:共有两种含义。它通过颜色的变化代表了实例的健康程度,它的健康度从绿色<黄色<橙色<红色递减。
该实心圆除了颜色的变化之外,它的大小也会根据实例的请求流量发生变化,流量越大该实心圆就越大。所以通过该实心圆的展示,就可以在大量的实例中快速的发现故障实例和高压力实例。

曲线:用来记录2分钟内流量的相对变化,可以通过它来观察到流量的上升和下降趋势。


 zuul路由网关

Zuul包含了对请求的路由和过滤两个最主要的功能:
其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础.

Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得。
 
    注意:Zuul服务最终还是会注册进Eureka
 
提供=代理+路由+过滤三大功能

官网资料 https://github.com/Netflix/zuul/wiki/Getting-Started

主启动类:

@SpringBootApplication
@EnableZuulProxy
public class Zuul9527 {
public static void main(String[] args) {
SpringApplication.run(Zuul9527.class, args);
}
}

application.yml配置:

server: 
port: 9527

spring:
application:
name: microservicecloud-zuul-gateway

eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka
instance:
instance-id: gateway-9527.com
prefer-ip-address: true

zuul:
#ignored-services: microservicecloud-dept
prefix: /hourui
ignored-services: "*"
routes:
mydept.serviceId: microservicecloud-dept
mydept.path: /mydept/**

info:
app.name: hourui-microcloud
company.name: www.hourui.com
build.artifactId: $project.artifactId$
build.version: $project.version$

application.yml配置:

测试不用路由:http://localhost:8001/dept/get/2
测试用路由:http://myzuul.com:9527/hourui/mydept/dept/get/2

猜你喜欢

转载自www.cnblogs.com/liuyi13535496566/p/12199315.html