springboot集成gateway和eureka,StripPrefix说明

eureka集成

依赖引入

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

启动类配置

在启动类上面添加@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

gateway集成

依赖引入

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

启动类配置

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/

StripPrefix属性说明

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

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

predicates:
            - Path=/**
          filters:
            - StripPrefix=2

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

predicates:
            - Path=/api/producer-develop/**
          filters:
            - StripPrefix=2

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值