gateway网关路由断言+nocas实现

1、注意gateway中用webflux启动网页,不能加webmvc,也就是spring-boot-starter-web依赖,因为里面有springmvc,所以如果要加,的除去springmvc的依赖;
摘自网上大佬的一个回答:
因为gateway和zuul不一样,gateway用的是长连接,netty-webflux,zuul1.0用的就是同步webmvc。所以你的非gateway子项目启动用的是webmvc,你的gateway启动用的是webflux. spring-boot-start-web和spring-boot-start-webflux相见分外眼红。不能配置在同一pom.xml,或者不能在同一项目中出现。
eg项目:
结构:
gateway网关路由断言+nocas实现_第1张图片

依赖


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.2.2.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>com.examplegroupId>
    <artifactId>demoartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>demoname>
    <description>Demo project for Spring Bootdescription>
    <properties>
        <java.version>1.8java.version>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>


        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webfluxartifactId>
        dependency>

        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-gatewayartifactId>
            <version>2.2.1.RELEASEversion>
        dependency>

        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
            <version>2.2.4.RELEASEversion>
        dependency>


    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

yml文件(1、动态路由):

server:
  port: 8889

spring:
  cloud:
    gateway:
      routes:
        - id: path_route
          uri: lb://dtest
          predicates:
            - Path=/myWebsocket/000
    nacos:
      discovery:
        server-addr: localhost:8848

  application:
    name: demogateway

实际访问地址:http://localhost:8889/myWebsocket/000

yml文件(2、网址路径):

server:
  port: 8889

spring:
  cloud:
    gateway:
      routes:
        - id: path_route
          uri: http://www.baidu.com
          predicates:
            - Path=/*
    nacos:
      discovery:
        server-addr: localhost:8848

  application:
    name: demogateway

实际访问访问地址:http://localhost:8889/

配置规则:
spring:
cloud:
gateway:
routes:
- id: path_route
uri: lb://dtest #配置的实际想访问的动态地址(nacos注册的)
predicates:
- Path=/myWebsocket/000 #路径符合条件的(断言这个路径要是实际路由有的那个地址)

注意这里的 - Path=后面的路径要是你真正访问网站上有的,模糊匹配可以用/**

实例测试:

服务端(被调用端)

pom:

<dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.4.RELEASE</version>
        </dependency>

yml:

server:
  port: 9999
spring:
  application:
    name: dtest
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

只用提供接口就可以了:

package com.example.dtest.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/myWebsocket")
public class WebsocketController {

    @GetMapping("/000")
    public String myWebsocket(){

        return "webSocketTest";

    }


}

我这里返回的是一个html界面:
在这里插入图片描述

DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>WebSocket测试title>
head>

<body>

<p>WebSocketp>
<input type="text" value="ws://127.0.0.1:58080/webSocket" id="textId">
<button type="button" onclick="testWebSocket()">测试button>

<script type="text/javascript" src="/jquery/jquery-3.6.0.js">script>
<script>
    const state = ['连接尚未建立', '连接已建立,可以进行通信', '连接正在进行关闭', '连接已经关闭或者连接不能打开'];
    let url0 = $("#textId").val();
    console.log("url0=========="+url0);

    function testWebSocket() {
        url0 = $("#textId").val();
        console.log("url0=========="+url0);
        // var ws = new WebSocket('ws://127.0.0.1:58080/webSocket');
        var ws = new WebSocket(url0);

        console.log('ws连接状态:' + ws.readyState + ' --- ' + state[ws.readyState]);
        //监听是否连接成功
        ws.onopen = function () {
            console.log('ws连接状态:' + ws.readyState + ' --- ' + state[ws.readyState]);
            //连接成功则发送一个数据
            ws.send("{'uid':'123'}");//这是一个测试数据
            console.log('已发送一个数据')
        }
        //接听服务器发回的信息并处理展示
        ws.onmessage = function (data) {
            console.log('接收到来自服务器的消息:');
            console.log(data);
            //完成通信后关闭WebSocket连接
            ws.close();
            console.log('正在关闭...');
        }
        //监听连接关闭事件
        ws.onclose = function () {
            //监听整个过程中websocket的状态
            console.log('ws连接状态:' + ws.readyState + ' --- ' + state[ws.readyState]);
        }
        //监听并处理error事件
        ws.onerror = function (error) {
            console.log(error);
        }
        console.log("全部代码已执行完毕,WebSocket处于异步监听状态");
    }
script>

body>

html>

客户端也就是gatway实现服务端:

pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>

<!--        要把web的去掉-->
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>dtestcommon</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <!--        &lt;!&ndash;gateway&ndash;&gt;-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.4.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
        </dependency>


        <!--         https://mvnrepository.com/artifact/com.alibaba/dubbo-->
<!--        <dependency>-->
<!--            <groupId>com.alibaba</groupId>-->
<!--            <artifactId>dubbo</artifactId>-->
<!--            <version>2.5.3</version>-->
<!--            <exclusions>-->
<!--                <exclusion>-->
<!--                    <groupId>org.springframework</groupId>-->
<!--                    <artifactId>spring</artifactId>-->
<!--                </exclusion>-->
<!--                <exclusion>-->
<!--                    <groupId>org.jboss.netty</groupId>-->
<!--                    <artifactId>netty</artifactId>-->
<!--                </exclusion>-->
<!--            </exclusions>-->
<!--        </dependency>-->

        <!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo-spring-boot-starter -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.8</version>
        </dependency>



    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

yml:

server:
  port: 8889

spring:
  cloud:
    gateway:
      routes:
        - id: path_route
          uri: lb://dtest  #配置的实际想访问的动态地址(nacos注册的)
          predicates:
#            - Path=/myWebsocket/000  #路径符合条件的(断言这个路径要是实际路由有的那个地址,要是真实访问地址服务器里面有的地址)
            - Path=/myWebsocket/**  #路径符合条件的(断言这个路径要是实际路由有的那个地址,要是真实访问地址服务器里面有的地址)
    nacos:
      discovery:
        server-addr: localhost:8848

  application:
    name: demogateway

dubbo:
  application: #应用配置,用于配置当前应用信息,不管该应用是提供者还是消费者。
    name: Consumer
  registry: #注册中心配置,用于配置连接注册中心相关信息。
    address: nacos://127.0.0.1:8848
  protocol: #协议配置,用于配置提供服务的协议信息,协议由提供方指定,消费方被动接受。
    name: dubbo
    port: 20880
  scan:
    base-packages: com.example.dtestgateway.dubboConsumer.service #服务暴露与发现消费所在的package

更多配置可以参考官网!!!

测试:

http://192.168.1.21:8889/myWebsocket/000
gateway网关路由断言+nocas实现_第2张图片
成功!
相当于id是gateway的服务id,但路径是真实的路径,当然也可以配置

你可能感兴趣的:(gateway)