ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

SpringCloud之GateWay动态路由及转发

2021-07-12 10:31:21  阅读:486  来源: 互联网

标签:product http service SpringCloud uri 服务 GateWay 路由


一、路由配置

1.在pom.xml中引入依赖

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

2.application.yml中写入配置

server:
  port: 8080 #端口
spring:
  application:
    name: api-gateway-server #服务名称
  #配置SpringCloudGateway的路由
  cloud:
    gateway:
      routes:
      #配置路由: 路由id,路由到微服务的uri,断言(判断条件)
      - id: product-service   #保持唯一
        #uri: http://127.0.0.1:9001  #目标微服务请求地址
        uri: lb://service-product #lb://根据微服务名称从注册中心拉取服务请求路径
        predicates:
        - Path=/product/**  #路由匹配条件
#配置Eureka
eureka:
  client:
    service-url:
      defaultZone: http://localhost:9000/eureka/
  instance:
    prefer-ip-address: true #使用ip地址注册

二、重写转发路径

server:
  port: 8080 #端口
spring:
  application:
    name: api-gateway-server #服务名称
  #配置SpringCloudGateway的路由
  cloud:
    gateway:
      routes:
      #配置路由: 路由id,路由到微服务的uri,断言(判断条件)
      - id: product-service   #保持唯一
        #uri: http://127.0.0.1:9001  #目标微服务请求地址
        uri: lb://service-product #lb://根据微服务名称从注册中心拉取服务请求路径
        predicates:
        #- Path=/product/**  #路由匹配条件
        - Path=/product-service/**  #将当前请求转发到 http://127.0.0.1:9001/product/1
        filters: #配置路由过滤器   http://localhost:8080/product-service/product/1 -->http://127.0.0.1:9001/product/1
        - RewritePath=/product-service/(?<segment>.*), /$\{segment}  #路由重写的过滤器,在yml中 $写为$\
#配置Eureka
eureka:
  client:
    service-url:
      defaultZone: http://localhost:9000/eureka/
  instance:
    prefer-ip-address: true #使用ip地址注册

三、微服务名称转发

server:
  port: 8080 #端口
spring:
  application:
    name: api-gateway-server #服务名称
  #配置SpringCloudGateway的路由
  cloud:
    gateway:
      routes:
      #配置路由: 路由id,路由到微服务的uri,断言(判断条件)
      - id: product-service   #保持唯一
        #uri: http://127.0.0.1:9001  #目标微服务请求地址
        uri: lb://service-product #lb://根据微服务名称从注册中心拉取服务请求路径
        predicates:
        #- Path=/product/**  #路由匹配条件
        - Path=/product-service/**  #将当前请求转发到 http://127.0.0.1:9001/product/1
        filters: #配置路由过滤器   http://localhost:8080/product-service/product/1 -->http://127.0.0.1:9001/product/1
        - RewritePath=/product-service/(?<segment>.*), /$\{segment}  #路由重写的过滤器,在yml中 $写为$\
      #配置自动的根据微服务名称进行路由转发 http://localhost:8080/service-product/product/1
      discovery:
        locator:
          enabled: true  #开启根据微服务名称自动转发
          lower-case-service-id: true #微服务名称以小写形式呈现
#配置Eureka
eureka:
  client:
    service-url:
      defaultZone: http://localhost:9000/eureka/
  instance:
    prefer-ip-address: true #使用ip地址注册

标签:product,http,service,SpringCloud,uri,服务,GateWay,路由
来源: https://blog.csdn.net/demoq123456789/article/details/118612439

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有