好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

SpringCloud 服务网关路由规则的坑及解决

一、场景简述

笔者最近用到SpringCloud 服务网关的时候,进行服务网关的路由测试,发现无法路由自己设置的规则,测试的时候如下

通过错误排查发现,原来是路由规则写错了!

路由规则如下(错误)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

#端口

server:

   port: 8080

spring:

   #该配置文件中的配置,对应的服务名称是wc-gateway

   application:

     name: wc-gateway

   profiles:

     active: dev

#服务网关配置

zuul:

   host:

     connect-timeout-millis: 60000

     socket-timeout-millis: 60000

     #路由规则

     routes:

       api:

         path: /api/user/**

         serviceId: wc-client-user

二、解决方案

只需要将routes及以下的属性左移,与host相等级别即可

修改后的路由规则

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

#端口

server:

   port: 8080

spring:

   #该配置文件中的配置,对应的服务名称是wc-gateway

   application:

     name: wc-gateway

   profiles:

     active: dev

#服务网关配置

zuul:

   host:

     connect-timeout-millis: 60000

     socket-timeout-millis: 60000

   #路由规则

   routes:

     api:

       path: /api/user/**

       serviceId: wc-client-user

好了,问题解决,我们重启应用测试,测试结果和预期一样。

SpringCloud 进阶之Zuul(路由网关)

1. Zuul(路由网关)

Zuul 包含了对请求的路由和过滤两个最主要的功能;

路由功能:负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础;

过滤功能:负责对请求的处理过程进行干预,是实现请求校验,服务聚合等功能的基础;

Zuul 服务最终还是会注册进Eureka;

1.1 路由基本配置

新建 microservicecloud-zuul-gateway-9527

?

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

33

34

35

36

37

38

39

40

41

42

43

44

// pom.xml

<!-- zuul 路由网关 -->

< dependency >

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

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

</ dependency >

< dependency >

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

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

</ dependency >

// application.yml

server:

   port: 9527

spring:

   application:

     name: microservicecloud-zuul-gateway

eureka:

   client:

     service-url:

       defaultZone: http://eureka7001测试数据:7001/eureka/,http://eureka7002测试数据:7002/eureka/,http://eureka7003测试数据:7003/eureka/

instance:

   instance-id: gateway-9527测试数据

   prefer-ip-address: true

   info:

     app.name: noodles-microcloud

     company.name: HdhCmsTestgoogle测试数据

     build.artifactId: $project.artifactId$

     build.version: $project.version$

// hosts 修改: 127.0.0.1   myzuul测试数据

// 主启动类

@SpringBootApplication

@EnableZuulProxy

public class Zuul_9527_StartSpringCloudApp {

     public static void main(String[] args) {

         SpringApplication.run(Zuul_9527_StartSpringCloudApp.class, args);

     }

}

// 启动

// 三个Eureka集群

// microservicecloud-provider-dept-8001

// 路由

// 测试访问:

// 不用路由: http://localhost:8001/dept/get/1

// 使用路由: http://myzuul测试数据:9527/microservicecloud-dept/dept/get/1

1.2 Zuul 路由访问映射规则

?

1

2

3

4

5

6

7

8

9

10

// microservicecloud-zuul-gateway-9527

// 修改 application.yml

zuul:

   ignored-services: microservicecloud-dept      # 将原有路由关闭

   routes:

     prefix: /test       # 设置统一公共前缀, 访问地址:http: //myzuul测试数据:9527/test/mydept/dept/get/1

     mydept.serviceId: microservicecloud-dept

     mydept.path: /mydept/**

// 修改之前,访问地址: http://myzuul测试数据:9527/microservicecloud-dept/dept/get/1

// 修改之后,访问地址: http://myzuul测试数据:9527/mydept/dept/get/1

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://xulinjie.blog.csdn.net/article/details/83046181

查看更多关于SpringCloud 服务网关路由规则的坑及解决的详细内容...

  阅读:18次