SpringCloud组件

799 阅读6分钟

~~本文已参与「新人创作礼」活动,一起开启掘金创作之路。~~

一、常用组件

服务治理Spring Cloud Eureka
客户端负载均衡Spring Cloud Ribbon
服务容错保护Spring Cloud Hystrix
声明式服务调用Spring Cloud Feign/OpenFeign
API网关服务Spring Cloud GateWay
分布式配置中心Spring Cloud Config

二、服务治理——Eureka

Eureka共两部分:

  • 服务注册中心(服务端)——Eureka Server
  • 服务对象(客户端)——Eureka Client。Eureka Client分为服务提供者服务消费者,其中服务消费者可能既是服务提供者又是服务消费者,服务提供者必须注入到注册中心,才能被服务消费者正常调用,单纯的服务消费者可以不注入到Eureak Server中

1、Eureka工作机制

1) 服务注册中心

  • 失效剔除:默认每隔60秒将当前清单中超时(默认90秒)没有续约的服务剔除出去
  • 自我保护:EurekaServer 在运行期间,会统计心跳失败的比例在15分钟之内是否低于85%(通常由于网络不稳定导致)。 Eureka Server会将当前的实例注册信息保护起来, 让这些实例不会过期,尽可能保护这些注册信息。

2) 服务对象

服务提供者

  • 服务注册:启动的时候会通过发送REST请求将自己注册到Eureka Server上

  • 服务续约:服务提供者会维持一个心跳用来持续告诉Eureka Server,防止被剔除

  • 服务下线:当服务实例进行正常的关闭操作,会给Eureka Server发送一个服务下线的REST请求,

    服务消费者

  • 获取服务:当启动服务消费者时,会给Eureka Server发送REST请求,获取服务注册中心中的服务清单

  • 服务调用:获取到服务清单后,可以通过服务名称获取该服务的元数据信息

2、Eureka Server配置

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  server:
    # 开启自我保护
    enable-self-preservation: true
  client:
    # 是否要注册到其他Server上, 默认为true,会注册自身
    register-with-eureka: false
    # 是否需要拉取服务信息,默认为true,由于注册中心的职责就是维护服务实例,它并不需要去检索服务,故设置为false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/     

启动类使用@EnableEurekaServer注解

3、Eureka Client配置

server:
  port: 8762
spring:
  application:
    name: provider     # 服务提供名
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/   # Eureka Server url     

启动类使用**@EnableEurekaClient@EnableDiscoveryClient**注解修饰

三、负载均衡——Ribbon

负载均衡分了两种类型:

  • 客户端负载均衡(Ribbon)

    • 服务实例的清单在客户端,客户端进行负载均衡算法分配。
    • (从上面的知识我们已经知道了:客户端可以从Eureka Server中得到一份服务清单,在发送请求时通过负载均衡算法,在多个服务器之间选择一个进行访问)
  • 服务端负载均衡(Nginx)

    • 服务实例的清单在服务端,服务器进行负载均衡算法分配

四、容错保护——Hystrix

Spring Cloud Hystrix实现了断路器线程隔离等一系列服务保护功能。

  • Fallback(失败快速返回):当某个服务单元发生故障(类似用电器发生短路)之后,通过断路器的故障监控(类似熔断保险丝), 向调用方返回一个错误响应, 而不是长时间的等待。这样就不会使得线程因调用故障服务被长时间占用不释放,避免了故障在分布式系统中的蔓延。
  • 资源/依赖隔离(线程池隔离):它会为每一个依赖服务创建一个独立的线程池,这样就算某个依赖服务出现延迟过高的情况,也只是对该依赖服务的调用产生影响, 而不会拖慢其他的依赖服务。

Hystrix提供几个熔断关键参数:滑动窗口大小(20)、 熔断器开关间隔(5s)、错误率(50%)

  • 每当20个请求中,有50%失败时,熔断器就会打开,此时再调用此服务,将会直接返回失败,不再调远程服务。
  • 直到5s钟之后,重新检测该触发条件,判断是否把熔断器关闭,或者继续打开。

五、服务调用——OpenFeign

Spring Cloud Feign基于 Netflix Feign 实现,整合了 Spring Cloud Ribbon 与 Spring Cloud Hystrix, 除了整合这两者的强大功能之外,它还提 供了声明式的服务调用(不再通过RestTemplate)。

Spring Cloud OpenFeign在Feign的基础上支持了SpringMVC的注解,并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务。

引入依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

OpenFeign远程调用服务:

@FeignClient("provider")    // Eureka中的服务名
public interface FeignInterface {
    // 服务对应接口,直接通过 FeignInterface.test() 即可调用该接口
    @RequestMapping("/client/test")     
    String test();
}

六、网关服务——GateWay

依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

配置:

server:
  port: 8765
spring:
  application:
    name: service-gateway
  cloud:
    gateway:
      # 开启网关
      enabled: true
      discovery:
        locator:
          # 开启自动路由,以服务名建立路由
          enabled: true
          # 将服务名改为小写,否则url中指定服务名时必须大写
          lower-case-service-id: true
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

通过网关访问接口格式:http://网关地址:网关端口/服务名/服务对外提供的url

七、配置中心——Config

Spring Boot服务启动时会加载application.yml/application.properties配置文件,Spring Cloud中有”引导上下文“的概念,引导上下文会加载bootstrap.yml/bootstrap.properties配置文件,即bootstrap.yml/bootstrap.properties会在Spring Boot服务启动之前加载,具有更高的优先级。默认情况下bootstrap.yml/bootstrap.properties中的属性不能被覆盖。

从Spring Boot 2.4版本开始,配置文件加载方式进行了重构。因此若要使用bootstrap.yml来进行配置,需要在pom中增加一个依赖,否则bootstrap.yml配置不生效:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

也就是说:SpringCloud Config 使用的SpringBoot为2.4或之后的版本,那么 Config 客户端必须再pom中新增spring-cloud-starter-bootstrap依赖

1、Config Server 的application.yml配置

也可以使用bootstrap.yml来配置,但是一定要加前面说的依赖:

server:
  port: 2021
spring:
  application:
    # 这个name是config server的名称,可以随便起  
    name: springcloud-config-server
  cloud:
    config:
      server:
        git:
          # git目录  
          uri: https://gitee.com/chenzm_ob/SpringCloud-ConfigDemo
          # git对应的用户名、密码
          username: 
          password: 
          # 指定目录,即从哪个目录下选择
          search-paths: conf

2、Config Client 的配置:

1)图简单、省事,可以只有一个bootstrap.yml配置文件,同样记得加依赖:

server:
  port: 2022

spring:
  application:
    name: config-erueka-client
  profiles:
    active: prod
  cloud:
    config:
      # config server服务中心
      uri: http://localhost:2021
      # 项目名称
      # 如想要读取springcloud-config-dev.yml文件的内容时,name的值为springcloud-config
      name: ${spring.application.name}
      label: master
      # 
      profile: ${spring.profiles.active}

2)bootstrap.yml只配置config部分内容,其它内容由application.yml配置

bootstrap.yml内容:

server:
  port: 2022

spring:
  application:
    name: config-erueka-client
  profiles:
    active: prod
  cloud:
    config:
      # config server服务中心
      uri: http://localhost:2021
      # 项目名称
      # 如想要读取springcloud-config-dev.yml文件的内容时,name的值为springcloud-config
      name: ${spring.application.name}
      label: master
      # 配置环境,如上需配置为dev
      profile: ${spring.profiles.active}

application.yml内容:

如果bootstrap.yml也配置了这些内容,那么会被覆盖,因为bootstrap先加载,application后加载

server:
  port: 2023
spring:
#  application:
#    name: config-single-server
  profiles:
    active: dev