当前位置:   article > 正文

springboot集成gateway和eureka,StripPrefix说明_gateway stripprefix

gateway stripprefix

eureka集成

依赖引入

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>
  • 1
  • 2
  • 3
  • 4

启动类配置

在启动类上面添加@EnableEurekaServer注解即可

yml配置

配置日志打印级别是为了阻止eureka心跳检测的日志输出

server:
  port: 1001
  servlet:
    context-path: /
spring:
  application:
    name: eureka-server
eureka:
  client:
    fetch-registry: false
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:1001/eureka/
  instance:
    hostname: localhost
  server:
    enable-self-preservation: false
    eviction-interval-timer-in-ms: 10000

logging:
  level:
    com.netflix: warn
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

gateway集成

依赖引入

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-gateway</artifactId>
		</dependency>
  • 1
  • 2
  • 3
  • 4

启动类配置

yml配置

server:
  port: 1004
spring:
  application:
    name: gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: false
          #开启小写验证,默认feign根据服务名查找都是用的全大写
          lowerCaseServiceId: true
      routes:
        - id: producer-develop
          uri: lb://producer-develop
          predicates:
            - Path=/api/producer-develop/**
          filters:
            - StripPrefix=2
        - id: producer-test
          uri: lb://producer-test
          predicates:
            - Path=/api/producer-test/**
          filters:
            - StripPrefix=2
eureka:
  instance:
    preferIpAddress: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
  client:
    service-url:
      defaultZone: http://localhost:1001/eureka/
  • 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

StripPrefix属性说明

StripPrefix作用是去掉部分URL路径,下面说明示例,网关端口为1004,原接口服务端口为1003,原接口实际请求路径为:http://localhost:1003/producer/test/blog/test

如果StripPrefix=2,path配置为如下

predicates:
            - Path=/**
          filters:
            - StripPrefix=2
  • 1
  • 2
  • 3
  • 4

则请求路径应该为http://localhost:1004///producer/test/blog/test,即你在端口路径后面任意添加两段前缀都可以访问到该接口,如果path配置为如下

predicates:
            - Path=/api/producer-develop/**
          filters:
            - StripPrefix=2
  • 1
  • 2
  • 3
  • 4

则请求路径应该为http://localhost:1004/api/producer-develop/producer/test/blog/test,即前缀必须是api/producer-develop,就是配置文件里面指定的。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/544987
推荐阅读
相关标签
  

闽ICP备14008679号