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,就是配置文件里面指定的。

你可能感兴趣的:(java,eureka,eureka,spring,boot,gateway)