SpringCloudGateway+Nacos注册与转发Netty+WebSocket

背景

项目中有个拍卖服务是长连接的,需要加入到注册中心中方便统一的管理,并且方便动态扩容。

问题

Nacos没有对长连接的服务注册的支持,需要手动实现把服务注册上线下线,感知服务状态。并需要支持域名转发WebSocket的请求。

实现

  • 自动注册,下线服务

    @Async
    public void start() {
    log.info("=================Netty服务开启==================");
    try {
    //ServerBootstrap 是一个启动类
    ServerBootstrap serverBootstrap = new ServerBootstrap();
    // 设置主从线程组
    // ... 业务
    // 注册到nacos
    nettyNacosService.registerNamingService();

    复制代码
              bindFuture = serverBootstrap.bind(nettyNacosService.getNettPort()).sync();
              log.info("netty监听端口:{},后台服务器启动.....", nettyNacosService.getNettPort());
          } catch (Exception e) {
              log.error("netty服务器启动异常:{}", e.getMessage());
          }
      }
    
    
    
      public void destroy() {
          //  将NacosNetty注销服务
          nettyNacosService.deregisterInstance();
    
          log.info("=================Netty服务关闭==================");
          if (bindFuture != null) {
              bindFuture.channel().closeFuture();
          }
          bossGroup.shutdownGracefully();
          workerGroup.shutdownGracefully();
      }

    // 注册伪代码:
    /**
    * 将Netty服务注册进Nacos
    */
    public void registerNamingService() {
    try {
    Properties properties = getNacosProperties();
    NamingService namingService = NamingFactory.createNamingService(properties);

    复制代码
              Instance nettyInstance = getNettyInstance();
              namingService.registerInstance(nettyInstance.getServiceName(),getGroup(),nettyInstance);
          } catch (Exception e) {
              throw new RuntimeException(e);
          }
      }
    
      private Properties getNacosProperties() {
          Properties properties = new Properties();
          properties.setProperty(PropertyKeyConst.SERVER_ADDR, nacosDiscoveryProperties.getServerAddr());
          properties.setProperty(PropertyKeyConst.NAMESPACE, nacosDiscoveryProperties.getNamespace());
          properties.setProperty(PropertyKeyConst.USERNAME, nacosDiscoveryProperties.getUsername());
          properties.setProperty(PropertyKeyConst.PASSWORD, nacosDiscoveryProperties.getPassword());
          return properties;
      }
    
      /**
       * 将NacosNetty注销服务
       */
      public void deregisterInstance() {
          try {
              Properties properties = getNacosProperties();
              NamingService namingService = NamingFactory.createNamingService(properties);
    
              Instance nettyInstance = getNettyInstance();
              namingService.deregisterInstance(nettyInstance.getServiceName(),getGroup(),nettyInstance);
          } catch (Exception e) {
              throw new RuntimeException(e);
          }
      }
  • gateway增加路由

    {
    "id": "web-socket",
    "order": 3,
    "predicates": [{
    "args": {
    "pattern": "/ws/**"
    },
    "name": "Path"
    }],
    "filters":[{"name":"StripPrefix","args":{"parts":"1"}}],
    "uri": "lb:ws://web-socket"
    }

  • nginx配置
    这里支持长连接的配置跟http稍微有点不同。
    主要看这个配置:$http_upgrade

    map http_upgrade connection_upgrade {
    default upgrade;
    '' close;
    }

    location /ws {
    proxy_pass https://abc.com/web-socket # 这里改为对应的域名
    proxy_http_version 1.1;
    proxy_set_header Upgrade http_upgrade; proxy_set_header Connection connection_upgrade;
    }

相关推荐
Java不加班4 分钟前
Java 后端定时任务实现方案与工程化指南
后端
心在飞扬32 分钟前
RAG 进阶检索学习笔记
后端
Moment34 分钟前
想要长期陪伴你的助理?先从部署一个 OpenClaw 开始 😍😍😍
前端·后端·github
Das1_35 分钟前
【Golang 数据结构】Slice 底层机制
后端·go
得物技术35 分钟前
深入剖析Spark UI界面:参数与界面详解|得物技术
大数据·后端·spark
古时的风筝37 分钟前
花10 分钟时间,把终端改造成“生产力武器”:Ghostty + Yazi + Lazygit 配置全流程
前端·后端·程序员
Cache技术分享38 分钟前
340. Java Stream API - 理解并行流的额外开销
前端·后端
初次攀爬者40 分钟前
RocketMQ 消息可靠性保障与堆积处理
后端·消息队列·rocketmq
ygxb1 小时前
如何去创建一个规范化的Agent SKIll?
后端·ai编程·claude
JxWang051 小时前
Task01:环境搭建,初识数据库
后端