SpringCloud生态框架(五)-Gateway负载均衡式转发

背景:在分布式架构中,有时候一个服务的并发量达到上限,怎么办呢?以前传统的更多的是增加服务器用Nginx转发来实现,因此呢spring出了一个组件叫做gateway,它和zuul是大同小异,这里就不介绍区别了,接下来我们进入主题。

一.老规矩新建一个module是maven格式的。

引入jar

jar版本

<properties>
    <lcn.version>5.0.2.RELEASE</lcn.version>
    <config.version>2.1.1.RELEASE</config.version>
    <jdbc.version>5.2.2.RELEASE</jdbc.version>
    <mysql.version>5.1.6</mysql.version>
    <eureka.version>2.1.1.RELEASE</eureka.version>
    <gateway.version>2.1.5.RELEASE</gateway.version>
    <hystrix.version>2.1.5.RELEASE</hystrix.version>
    <redis.version>2.1.9.RELEASE</redis.version>
</properties>
<dependencies>
    <!--    config start    -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-client</artifactId>
        <version>${config.version}</version>
    </dependency>
    <!--config end-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        <version>${eureka.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
        <version>${gateway.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        <version>${hystrix.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis-reactive</artifactId>
        <version>${redis.version}</version>
    </dependency>
<!--这里是为了剔除tomcat包,不然会报启动错误-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

二.配置文件如上一篇文章所写

spring:
  application:
    name: serviceConfig
  profiles:
    active: git
  cloud:
    config:
      label: master
#这里我的gateway配置文件命名为gateway-master
      profile: gateway-master
      uri: http://localhost:9527/serviceConfig
      discovery:
        enabled: true
        service-id: fool-cloud-config
eureka:
  instance:
    prefer-ip-address: true
  client:
    serviceUrl:
      defaultZone: http://admin:admin@127.0.0.1:9526/eureka/eureka
    enabled: true
三.远程文件放到前面文章所写的github地址里面内容如下

server:
  #端口号
  port: 8105

##数据库连接信息
spring:
  #  profiles:
  #    active: dev
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
          # 服务名小写
          lower-case-service-id: true
      routes:
    # =====================================
      - id: SERVICEBASIC
          # lb代表从注册中心获取服务,且已负载均衡方式转发

#遇到下面断言转发到什么服务
        uri: lb://FOOL-CLOUD-EXAMPLE
        predicates:

#断言转发碰到什么路径开始转发
        - Path=/example/**
        filters:
        - StripPrefix=0
     
  #====================================
  application:
    name: inter-hsp-gateway
  redis:
    host:  10.10.10.242
    #redis端口号
    port: 6379
    #redis 密码
    password:
eureka:
  instance:
    prefer-ip-address: true
  client:
    serviceUrl:
      defaultZone: http://admin:admin@127.0.0.1:9526/eureka/eureka/
    enabled: true

# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always
logging:
  level:
    org:
      springframework:
        cloud:
          gateway: debug 

           
 四.新建启动类只需要两个注解

@SpringBootApplication
@EnableDiscoveryClient

五.启动主类,见到gateway的配置文件端口即成功。下一篇文章我们将讲解gateway的转发知识点

                                                                                                            更多技术交流请加入QQ群:260292706

                                                                                                                                                         Owen

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud Gateway是Spring Cloud生态中的一个API网关框架,可以实现请求的路由、限流、鉴权、监控等功能。其中,负载均衡是Spring Cloud Gateway中非常重要的功能之一。本篇文章就来介绍一下Spring Cloud Gateway负载均衡功能。 在Spring Cloud Gateway中,负载均衡是通过路由(Route)来实现的。每一个路由对应一个或多个服务实例,Spring Cloud Gateway会根据负载均衡策略,将请求转发到对应的服务实例。 Spring Cloud Gateway支持多种负载均衡策略,包括: 1. RoundRobinLoadBalancer:轮询负载均衡策略,每个服务实例轮流接收请求。 2. WeightedResponseTimeLoadBalancer:响应时间加权负载均衡策略,对每个服务实例的响应时间进行加权,响应时间短的服务实例接收更多的请求。 3. ZoneAvoidanceRule:区域避让负载均衡策略,对于不同区域的服务实例,Spring Cloud Gateway会尽选择同一区域的服务实例来处理请求。 除了以上几种负载均衡策略之外,Spring Cloud Gateway还支持自定义负载均衡策略。在自定义负载均衡策略时,需要实现Spring Cloud Gateway的LoadBalancerClient接口和LoadBalancer接口。 下面是一个示例,展示了如何在Spring Cloud Gateway中使用RoundRobinLoadBalancer策略: ```java @Configuration public class GatewayConfig { @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route(r -> r.path("/foo/**") .uri("lb://service-foo") .id("service-foo") .lb(new RoundRobinLoadBalancer())) .build(); } } ``` 在上面的示例中,我们配置了一个路由,将请求转发到service-foo服务实例。并且指定了负载均衡策略为RoundRobinLoadBalancer。 除了在配置文件中配置负载均衡策略之外,Spring Cloud Gateway还支持通过请求头、请求参数等方,动态设置负载均衡策略。这样可以在运行时根据实际情况,动态调整负载均衡策略。 总的来说,Spring Cloud Gateway负载均衡功能非常强大,支持多种负载均衡策略,并且支持自定义负载均衡策略。这为我们构建高可用、高性能的分布式系统提供了很大的便利。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值