广告位联系
返回顶部
分享到

springcloud gateway无法路由问题的解决

java 来源:互联网 作者:佚名 发布时间:2023-05-14 13:43:39 人浏览
摘要

现在企业微服务架构基本上都是用springcloud体系了,在国内基本上新项目都用springcloud alibaba,而且基本上都是所有服务聚合在一个父项目中。 springcloud gateway可以实现路由负载均衡等等

现在企业微服务架构基本上都是用springcloud体系了,在国内基本上新项目都用springcloud alibaba,而且基本上都是所有服务聚合在一个父项目中。

springcloud gateway可以实现路由负载均衡等等功能,但是应用过程中,会有一些坑。

描述问题

配置的没问题如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

server:

  port: 9999

spring:

  application:

    name: gateway-server

  cloud:

    nacos:

      discovery:

        server-addr: 192.168.229.7:8848

    gateway:

      discovery:

        locator:

          enabled: true ##开启了会自动匹配路由规则

      routes: ##配置了手动路由规则,上面的自动开启失效

        - id: nacos-provider

          uri: lb://nacos-provider

          predicates:

            - Path=/mmm/**

provider代码:

1

2

3

4

5

6

7

8

9

10

@RestController

@RequestMapping("/mmm")

public class MMMController {

    @Value("${server.port}")

    private String port;

    @GetMapping("/get")

    public String get(){

        return port;

    }

}

但是测试始终无效:

分析原因

springcloud项目基本上都聚合到一个父项目中,里面各种子模块如provider、consumer、sentinel、gateway…,其他模块都没问题,但是springcloud gateway有点特殊,因为它依赖了web webflux ,就会有冲突,所以基本上在pom中就要排除web,这样就不会应该父项目依赖了web导致冲突了

springcloud gateway 的pom排除web

知识星球

1

2

3

4

5

6

7

8

9

10

<dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-gateway</artifactId>

    <exclusions>

        <exclusion>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-web</artifactId>

        </exclusion>

    </exclusions>

</dependency>

启动会成功,但是项目会有报错提示:

**********************************************************

Spring MVC found on classpath, which is incompatible with Spring Cloud Gateway at this time. Please remove spring-boot-starter-web dependency.

**********************************************************

最后路由不到指定url。

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Fri Mar 17 16:34:53 CST 2023
There was an unexpected error (type=Not Found, status=404).
No message available

解决思路

如何解决呢?我的意见是把springcloud gateway项目独立出来,不要聚合到父项目中,这样就不会有web以来冲突了

独立的springcloud gateway

pom文件如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

<?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 http://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.13.RELEASE</version>

        <relativePath/> <!-- lookup parent from repository -->

    </parent>

    <groupId>com.mmm.springcloud.study</groupId>

    <artifactId>spring-cloud-gateway-demo</artifactId>

    <version>1.0-SNAPSHOT</version>

    <properties>

        <maven.compiler.source>8</maven.compiler.source>

        <maven.compiler.target>8</maven.compiler.target>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    </properties>

    <dependencies>

        <dependency>

            <groupId>com.alibaba.cloud</groupId>

            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>

            <version>2.2.5.RELEASE</version>

        </dependency>

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-gateway</artifactId>

            <version>2.2.3.RELEASE</version>

        </dependency>

    </dependencies>

</project>

因为没有聚合到父项目中,所以不需要额外提出web,启动后直接访问,成功!


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 : https://blog.csdn.net/u013673252/article/details/129623008
相关文章
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计