当前位置: 代码迷 >> 综合 >> SpringCloud:Hystrix(F版本)- Zuul网关设置
  详细解决方案

SpringCloud:Hystrix(F版本)- Zuul网关设置

热度:18   发布时间:2023-12-13 08:51:56.0

SpringCloud:Hystrix(F版本)

断路器简介

Netflix开源了Hystrix组件,实现了断路器模式,SpringCloud对这一组件进行了整合。 在微服务架构中,一个请求需要调用多个服务是非常常见的

使用的原因

Spring Cloud可以用RestTemplate+Ribbon和Feign来调用。为了保证其高可用,单个服务通常会集群部署。由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet容器的线程资源会被消耗完毕,导致服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的“雪崩”效应。

为了解决这个问题,业界提出了断路器模型。

Feign中使用断路器

结果图
在这里插入图片描述

Feign是自带断路器的,Spring Cloud没有默认打开。需要在配置文件中配置打开它,在配置文件加以下代码:

feign.hystrix.enabled=true

feign:  hystrix:   enabled: true okhttp:   enabled: true httpclient:   enabled: true

在需要熔断的接口服务,注解中加上fallback的指定类:

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;@FeignClient(value = "service-hi",fallback = SchedualServiceHiHystric.class)
public interface SchedualServiceHi {
    @RequestMapping(value = "/hi",method = RequestMethod.GET)String sayHiFromClientOne(@RequestParam(value = "name") String name);
}

指定的熔断类需要对应的接口

import org.springframework.stereotype.Component;@Component
public class SchedualServiceHiHystric implements SchedualServiceHi{
    @Overridepublic String sayHiFromClientOne(String name) {
    return "sorry!"+ name;}
}

如果出现指定调用的服务没有启动或者出现其他的情况,则会启动fallback = SchedualServiceHiHystric.class指定类

SpringCloud :路由器和过滤器:Zuul

微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现、服务消费、负载均衡、断路器、智能路由、配置管理等,由这几个基础组件相互协作,共同组建了一个简单的微服务系统。

比如,通过使用Spring Cloud Eureka实现高可用的服务注册中心以及实现微服务的注册与发现;通过Spring Cloud Ribbon或Feign实现服务间负载均衡的接口调用;同时,为了使分布式系统更为健壮,对于依赖的服务调用使用Spring Cloud Hystrix 来进行包装,实现线程隔离并加入熔断机制,以避免在微服务架构中因个别服务出现异常而引起级联故障蔓延。通过上述思路,我们可以设计出类似下图的基础系统架构。

[外在这里插入图片描述

在该架构中,服务集群包含内部服务ServiceA和Service B,它们都会向Eureka Server 集群进行注册与订阅服务,而Open Service是一个对外的RESTfulAPI服务,它通过F5、Nginx等网络设备或工具软件实现对各个微服务的路由与负载均衡,并公开给外部的客户端调用。

路由在微服务体系结构的一个组成部分。例如,/可以映射到您的Web应用程序,/api/users映射到用户服务,并将/api/shop映射到商店服务。Zuul是Netflix的基于JVM的路由器和服务器端负载均衡器

构建网关工程

  • spring-cloud-starter-hystrix:依赖用来在网关服务中实现对微服务转发时候的保护机制,通过线程隔离和断路器,防止微服务的故障引发API网关资源无法释放,从而影响其他应用的对外服务。。
  • spring-cloud-starter-ribbon:该依赖用来实现在网关服务进行路由转发时候的客户端负载均衡以及请求重试。
  • spring-boot-starter-actuator:该依赖用来提供常规的微服务管理端点。另外,在Spring Cloud Zuul中还特别提供了/routes端点来返回当前的所有路由规则。

入口applicaton类加上注解@EnableZuulProxy,开启zuul的功能:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;@EnableZuulProxy
@EnableEurekaClient //都是能够让注册中心能够发现,扫描到改服务。
@SpringBootApplication
public class ServiceZuulApplication {
    public static void main(String[] args) {
    SpringApplication.run(ServiceZuulApplication.class, args);}}

application.yml的配置代码:

eureka:client:serviceUrl:defaultZone: http://localhost:8761/eureka/
server:port: 8769
spring:application:name: service-zuul
zuul:routes:api-b:path: /api-b/**serviceId: service-feign
ication:name: service-zuul
zuul:routes:api-b:path: /api-b/**serviceId: service-feign
以/api-b/开头的请求都转发给service-feign服务;