springcloud组件介绍

Spring Cloud 体系技术综合应用概览
在这里插入图片描述

springcloud 概述

spring Cloud 是微服务架构的一种实现框架

  • 整合的组件可以有很多组件;常见的组件有:eureka注册中心,Gateway网关,Ribbon负载均衡,Feign服务调用,Hystrix熔断器。在有需要的时候项目添加对于的启动器依赖即可。
  • 版本特征:以英文单词命名(伦敦地铁站名)

springCloud坐标

            <!-- springCloud -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR1</version>
            </dependency>
服务注册中心eureka

eureka坐标

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

配置文件

server:
  # 端口设置 有传参使用传参值;没有使用默认10086
  port: ${port:10086}

spring:
  application:
    name: eureka_server

eureka:
  client:
    service-url:
      # eureka 服务地址,如果是集群的话;需要指定其它集群eureka地址
      # 端口设置 有传参使用传参值;没有使用默认 http://127.0.0.1:10086/eureka
      defaultZone: ${defaultZone:http://127.0.0.1:10086/eureka/}

    # 集群才需要
    register-with-eureka: false
    # 不拉取服务
    fetch-registry: false
  server:
    # 服务失效剔除时间间隔,默认60秒(单位毫秒)
    eviction-interval-timer-in-ms: 60000
    # 关闭自我保护模式(默认是打开的;即爆红提示,开发时可关闭)
    enable-self-preservation: false

服务剔除 :

当Eureka Client和 Eureka Server不再有心跳时,Eureka Server会将该服务实例从服务注册列表中删除。

自我保护机制

Eureka Server在运行期间会去统计心跳失败比例在15分钟之内是否低于85%,如果低于85%,Eureka Server即会进入自我保护机制。

自我保护机制是为了防止误杀服务而提供的一个机制。当个别客户端出现心跳失联时,则认为是客户端的问题,剔除掉客户端;当 Eureka 捕获到大量的心跳失败时,则认为可能是网络问题,进入自我保护机制;当客户端心跳恢复时,Eureka 会自动退出自我保护机制。

注意:spingboot启动类必须加上@EnableEurekaServer注解;声明当前类为Eureka服务。

客户端

客户端坐标

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

配置文件

server:
  port: 9090

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db2
    username: root
    password: root
  application:
    name: user_service

mybatis:
  type-aliases-package: li.chen.com.user.pojo

# 服务注册
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka

  instance:
    # 更倾向使用ip地址,而不是host名
    prefer-ip-address: true
    # ip地址
    ip-address: 127.0.0.1
    # 续约间隔 ,默认30秒
    lease-renewal-interval-in-seconds: 5
    # 服务失效时间, 默认90秒
    lease-expiration-duration-in-seconds: 5

服务注册:

将服务注册到eureka中

设置ip地址
服务续约

Eureka Client会每隔 30 秒发送一次心跳来续约。 通过续约来告知 Eureka Server该Eureka Client运行正常,没有出现问题。

服务失效:

如果 Eureka Server在90秒内没有收到Eureka Client的续约,Eureka Server端会将实例从其注册表中删除。

注:spingboot启动类必须加上@EnableEurekaServer注解;声明当前类为Eureka服务。

负载均衡Ribbon

Ribbon提供了轮询、随机两种负载均衡算法(默认是轮询)可以实现从地址列表中使用负载均衡算法获取地址进行服务调用。

Ribbon负载均衡:在执行RestTemplate发送服务地址请求的时候,使用负载均衡拦截器拦截,根据服务名获取服务地址列表,使用Ribbon负载均衡算法从服务地址列表中选择一个服务地址,访问该地址获取服务数据。

package li.chen.com.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableDiscoveryClient   //开启Eureka客户端发现功能
@SpringBootApplication
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class,args);
    }

    @Bean
    @LoadBalanced  //负载均衡的注解
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

}

即:在实例化RestTemplate的时候使用@LoadBalanced,服务地址直接可以使用服务名。

熔断器Hystrix

熔断机制是应对雪崩效应的一种微服务链路保护机制。

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

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

  • 解耦及服务降级
    解耦 不用对每一个controller文件每个方法进行编写对应的FallBack方法;直接父接口上实现了FallBack方法,通过这样一种方式去维护起来就能实现解耦,也顺便完成了降级的机制。
    在这里插入图片描述
组件Feign

整合了ribbon和hystrix功能

导入坐标

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

启动类加上@EnableFeignClients注解

application.yml主要配置

#Feign的ribbon配置
ribbon:
  ConnectTimeout: 1000 # 连接超时时长
  ReadTimeout: 2000 # 数据通信超时时长
  MaxAutoRetries: 0 # 当前服务器的重试次数
  MaxAutoRetriesNextServer: 0 # 重试多少次服务
  OkToRetryOnAllOperations: false # 是否对所有的请求方式都重试
  #随机策略
  NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

#Feign的Hystrix配置
feign:
  hystrix:
    enabled: true # 开启Feign的熔断功能
  compression:
    request:
      enabled: true # 开启请求压缩
      mime-types: text/html,application/xml,application/json # 设置压缩的数据类型
      min-request-size: 2048 # 设置触发压缩的大小下限
    response:
      enabled: true
      
logging:  #日志
  level:
    li.chen.com.consumer: debug     
  • 负载均衡
  • 服务熔断
  • 请求压缩
  • 日志级别
Gateway组件

Spring Cloud Gateway的核心就是一系列的过滤器,可以将客户端的请求转发到不同的微服务。主要作用:过滤和路由。

路由

坐标

    <dependencies>
        <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>
    </dependencies>

配置文件

spring:
  application:
    name: api-gateway
  cloud:
    gateway:
      routes:
        # 路由id,可以任意
        - id: user-service-route
          # 代理的服务地址
          #uri: http://127.0.0.1:9090
          # lb表示从eureka中获取具体服务(lb 之后的服务名必须要在eureka中注册才能使用);动态代理
          uri: lb://UserService
          # 路由断言: 可以匹配映射路径
          predicates:
            #- Path=/**  全匹配
            - Path=/UserController/**

lb表示从eureka中获取具体服务(lb 之后的服务名必须要在eureka中注册才能使用)
Gateway直接通过网关访问user-service服务,那么consumer-demo已经没用了

过滤器
  • 前缀过滤器
    在这里插入图片描述
  • 后缀过滤器
    -
  • 全局过滤器
    在这里插入图片描述
自定义过滤器
  • 自定义局部过滤器
    继承AbstractGatewayFilterFactory

application.yml配置
在这里插入图片描述
自定义局部过滤器;继承AbstractGatewayFilterFactory

  • 自定义全局过滤器
    不需要配置yml,直接编写继承GlobalFilter就可以
    在这里插入图片描述
负载均衡和熔断机制

Gateway中默认集成了Ribbon负载均衡和Hystrix熔断机制。参考之前的配置即可

Gateway 跨域配置

在js请求访问中,如果访问的地址与当前服务器的域名、ip或者端口号不一致则称为跨域请求。

  • http://localhost:8080中的js —访问—> http://localhost:9091的数据,因为端口不同,是跨域请求。
spring:
  application:
    name: api-gateway
  cloud:
    gateway:
      routes:
        # 路由id,可以任意
        - id: user-service-route
          # 代理的服务地址
          #uri: http://127.0.0.1:9090
          # lb表示从eureka中获取具体服务 ;lb 之后编写的服务名必须要在eureka中注册才能使用
          uri: lb://UserService
          # 路由断言: 可以匹配映射路径
          predicates:
            #- Path=/UserController/**
            #- Path=/**
            - Path=/api/UserController/**
          filters:
            #1表示过滤1个路径,2表示两个路径,以此类推
            - StripPrefix=1
            - MyParam=name #自定义过滤器  :  名称  + GatewayFilterFactory就是类名称
            - 
      # 跨域请求
      globalcors:
        corsConfigurations:
          '[/**]':
            #allowedOrigins: * # 这种写法或者下面的都可以,*表示全部
            allowedOrigins:
              - "http://docs.spring.io"
            allowedMethods:
              - GET

在这里插入图片描述

可以允许来自 http://docs.spring.io 的get请求方式获取服务数据。

allowedOrigins : 指定允许访问的服务器地址,如http://docs.spring.io。

‘[/**]’ : 表示对所有访问到网关服务器的请求地址

服务注册中心

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

岿然如故

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值