一款基于Netty的API网关上线了

吴书松
吴书松
发布于 2025-08-23 / 19 阅读
0
0

一款基于Netty的API网关上线了

项目名称:bronze-gateway

项目地址:

github:https://github.com/wushusong/bronze-gateway

gitee:https://gitee.com/wushusong/bronze-gateway

gitcode:https://gitcode.com/wushusong/bronze-gateway

1、springboot集成

springboot版本要求:2.7.18及以上

引入依赖

        <dependency>
            <groupId>io.github.wushusong</groupId>
            <artifactId>bronze-gateway-core</artifactId>
            <version>1.0.6</version>
        </dependency>

yml配置

gateway:
  port: 9999
  backendResponseTimeoutMs: 6000 # 后端响应超时时间
  clientWriteTimeoutMs: 6000 # 客户端写入超时时间
  connectTimeoutMs: 6000 # 连接超时时间
  maxConnectionsPerHost: 2000 # 每个主机的最大并发连接数(如果没有配置,使用合理默认值)
  maxPendingAcquires: 20000 # 每个主机最大等待连接数(如果没有配置,使用合理默认值)
  routes:
    - id: wss-test-gw # 服务名称,每个服务唯一
      path: /wss-test-gw/ # 服务访问路径,注意,前后都要斜杠,不能有*号
      instances: # 服务实例
        - serviceId: wss-test-gw-1 # 服务实例名称,同一个服务下,不同实例,serviceId要求唯一
          url: http://192.168.1.239:8089 # 服务地址,到端口一层即可
          weight: 1 # 权重,目前系统默认轮询策略
#        - serviceId: wss-test-gw-2 # 模拟服务2 该服务百分百失败
#          url: http://192.168.1.111:8089
#          weight: 1
    - id: jm-cloud-gw
      path: /jm-cloud-gw/ # 注意,前后都要斜杠,不能有*号
      loadBalancerType: roundRobinLoadBalancer # 轮询策略
      instances:
        - serviceId: jm-cloud-gw-1 # 模拟服务2 该服务是正常服务
          url: http://192.168.1.240:23500
          weight: 1
        - serviceId: jm-cloud-gw-2 # 模拟服务2 该服务百分百失败
          url: http://192.168.1.111:23500
          weight: 1
          gray: true # 灰度发布实例 当满足灰度条件的请求,都会进入该服务,否则跳转到正常服务 如下面配置的是基于请求头的灰度,当请求头有wss,则进入该服务
      # 灰度发布配置
      grayReleaseConfig:
        enabled: true
        headerBased: # 基于请求头
          headerName: VERSION # 请求头名称
          headerValues: # 请求头值
            - "wss"

  filters:
#    - name: AuthFilter  # 与@Component("AuthFilter")匹配
#      order: -100
#    - name: RateLimitFilter  # 与@Component("RateLimitFilter")匹配
#      order: -90
#      args:
#        permitsPerSecond: "1000"

#熔断配置
  resilience:
    failureRateThreshold: 50 # 失败率阈值百分比
    slowCallRateThreshold: 50 # 慢调用率阈值百分比
    slowCallDurationThreshold: 10 # 慢调用持续时间阈值
    waitDurationInOpenState: 60 # 熔断器开启状态持续时间
    permittedNumberOfCallsInHalfOpenState: 5 # 半开状态允许的调用次数
    minimumNumberOfCalls: 2 # 计算失败率所需的最小调用次数
    slidingWindowSize: 5 # 滑动窗口大小

如果要用到熔断、启动类上添加注解@WssResilienceEnable

测试

自定义限流,继承接口Filter

启动

测试

请求地址:http://192.168.1.239:9999/jm-cloud-gw/admin/tenant/getTenantById/2?a=111

会根据yml配置。匹配:jm-cloud-gw 代理到实例:jm-cloud-gw,并代理到地址 http://192.168.1.240:23500,完整代理后地址: http://192.168.1.240:23500/admin/tenant/getTenantById/2?a=111

限流

压测


评论