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;
    }

相关推荐
qianshang2333 小时前
SQL注入学习总结
网络·数据库·渗透
云小逸4 小时前
【网络通信】同一网段与不同网段的通信原理
网络·网络安全
惊讶的猫5 小时前
探究StringBuilder和StringBuffer的线程安全问题
java·开发语言
jmxwzy5 小时前
Spring全家桶
java·spring·rpc
Halo_tjn5 小时前
基于封装的专项 知识点
java·前端·python·算法
珠海西格5 小时前
“主动预防” vs “事后补救”:分布式光伏防逆流技术的代际革命,西格电力给出标准答案
大数据·运维·服务器·分布式·云计算·能源
Fleshy数模5 小时前
从数据获取到突破限制:Python爬虫进阶实战全攻略
java·开发语言
像少年啦飞驰点、6 小时前
零基础入门 Spring Boot:从“Hello World”到可上线的 Web 应用全闭环指南
java·spring boot·web开发·编程入门·后端开发
zr5268554476 小时前
PCIe-PN卡(三格电子)
网络
苍煜6 小时前
万字详解Maven打包策略:从基础插件到多模块实战
java·maven