赞
踩
有时间出个demo项目(下次一定) gitee代码仅供参考
spring:
cloud:
gateway:
routes:
- id: dh-web-socket-service
uri: lb:ws://dh-web-socket-service:30229
predicates:
- Path=/CYHLW/ws/**
filters:
- StripPrefix=2
1. ws升级成功
2. 但是连接服务失败了
package com.dh.websocket.core.configuration; import com.alibaba.cloud.nacos.NacosDiscoveryProperties; import com.alibaba.nacos.api.naming.NamingFactory; import com.alibaba.nacos.api.naming.NamingService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import javax.annotation.PostConstruct; import java.net.InetAddress; import java.util.Properties; /** * web socket寄存器nacos配置 * * @author yolo * @date 2024/3/18 14:58 */ @Configuration public class WebSocketRegisterNacosConfiguration { @Value("${netty.socket.application-name}") private String applicationName; @Value("${netty.socket.port}") private int port; @Autowired private NacosDiscoveryProperties nacosDiscoveryProperties; @PostConstruct public void start() { // 注册到Nacos里 registerNamingService(applicationName, port); } /** * 注册到 nacos 服务中 * * @param nettyName netty服务名称 * @param nettyPort netty服务端口 */ private void registerNamingService(String nettyName, int nettyPort) { try { NamingService namingService = NamingFactory.createNamingService(nacosDiscoveryProperties.getNacosProperties()); InetAddress address = InetAddress.getLocalHost(); namingService.registerInstance(nettyName, address.getHostAddress(), nettyPort); } catch (Exception e) { throw new RuntimeException(e); } } }
spring: cloud: gateway: routes: - id: dh-web-socket-service uri: lb://dh-web-socket-service predicates: - Path=/dh/wsocket-service/** filters: - StripPrefix=2 - id: dh-web-socket uri: lb:ws://dh-web-socket predicates: - Path=/dh/wsocket/** filters: - StripPrefix=2
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。