Chinaunix首页 | 论坛 | 博客
  • 博客访问: 613668
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2022-04-25 10:54:32


点击(此处)折叠或打开


  1. POM文件依赖

  2. <dependencies>
  3.     <dependency>
  4.         <groupId>org.springframework.boot</groupId>
  5.         <artifactId>spring-boot-starter</artifactId>
  6.     </dependency>

  7.     <dependency>
  8.         <groupId>org.springframework.cloud</groupId>
  9.         <artifactId>spring-cloud-starter-gateway</artifactId>
  10.         <version>3.0.2</version>
  11.     </dependency>

  12.     <dependency>
  13.         <groupId>org.springframework.boot</groupId>
  14.         <artifactId>spring-boot-starter-test</artifactId>
  15.         <scope>test</scope>
  16.     </dependency>
  17. </dependencies>

  18. 我在父组件中加入的nacos的相关依赖如下:

  19. <dependency>
  20.     <groupId>com.alibaba.cloud</groupId>
  21.     <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  22.     <version>${nacos.version}</version>
  23. </dependency>
  24. <dependency>
  25.     <groupId>com.alibaba.cloud</groupId>
  26.     <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  27.     <version>${nacos.version}</version>
  28. </dependency>


  29. nacos的版本如下:

  30. <properties>
  31.     <nacos.version>2021.1</nacos.version>
  32. </properties>
  33. gateway的application.yml文件配置如下:


  34. server:
  35.   port: 9040

  36. spring:
  37.   application:
  38.     name: gateway
  39.   cloud:
  40.     gateway:
  41.       routes:
  42.         - id: consumer
  43.           uri: lb://consumer
  44.           # uri: http://localhost:9010
  45.           predicates:
  46.             - Path=/**
  47.     nacos:
  48.       discovery:
  49.         server-addr: localhost:8848
  50.         metadata:
  51.           preserved.heart.beat.interval: 3 #心跳间隔。时间单位:秒。心跳间隔
  52.           preserved.heart.beat.timeout: 6 #心跳暂停。时间单位:秒。 即服务端6秒收不到客户端心跳,会将该客户端注册的实例设为不健康:
  53.           preserved.ip.delete.timeout: 9 #Ip删除超时。时间单位:秒。即服务端9秒收不到客户端心跳,会将该客户端注册的实例删除:
  54. 当我通过uri: localhost:9010去调用服务时,是可以调用的,但是当我用uri lb://consumer时就无法调用服务,报错503.

  55. 解决办法是:(实例项目fhadmin.cn)
  56. 加入feign依赖。

  57. <dependencies>
  58.     <dependency>
  59.         <groupId>org.springframework.boot</groupId>
  60.         <artifactId>spring-boot-starter</artifactId>
  61.     </dependency>

  62.     <dependency>
  63.         <groupId>org.springframework.cloud</groupId>
  64.         <artifactId>spring-cloud-starter-gateway</artifactId>
  65.         <version>3.0.2</version>
  66.     </dependency>

  67.     <!--fegin组件-->
  68.     <dependency>
  69.         <groupId>org.springframework.cloud</groupId>
  70.         <artifactId>spring-cloud-starter-openfeign</artifactId>
  71.         <version>3.0.2</version>
  72.     </dependency>
  73.     <!-- Feign Client for loadBalancing -->
  74.     <dependency>
  75.         <groupId>org.springframework.cloud</groupId>
  76.         <artifactId>spring-cloud-loadbalancer</artifactId>
  77.         <version>3.0.2</version>
  78.     </dependency>

  79.     <dependency>
  80.         <groupId>org.springframework.boot</groupId>
  81.         <artifactId>spring-boot-starter-test</artifactId>
  82.         <scope>test</scope>
  83.     </dependency>
  84. </dependencies>

  85. 猜测原因:nacos兼容feign,feign集成ribbon,默认实现负载均衡;或许是nacos不兼容springcloud gateway自带的ribbon。


阅读(708) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~