Spring Cloud Gateway + Eureka
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
application.yml
server:
port: 8769
spring:
application:
name: gateway
cloud:
gateway:
routes:
- id: service-feign
uri: lb://service-feign
predicates:
- Path= /service-feign/**
filters:
- RewritePath=/service-feign/(?<path>.*), /$\{path}
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/
instance:
lease-renewal-interval-in-seconds: 10
lease-expiration-duration-in-seconds: 30
hostname: localhost
关于route的uri配置项,
通过注册到eureka的serviceid进行负载均衡:
uri: lb:// + serviceid
也可以直接做转发 如 :
uri: http://baidu.com
官方文档predicates配置
启动类加入@EnableEurekaClient注解
@EnableEurekaClient
@SpringBootApplication
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
参考文章
https://dzone.com/articles/quick-guide-to-microservices-with-spring-boot-20-e