1、创建一个springboot工程名字gateway
2、pom.xml中添加依赖:
<dependencies>
<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>
</dependencies>
3、启动类配置:
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayStarter {
public static void main(String[] args) {
SpringApplication.run(GatewayStarter.class, args);
}
}
4、applicaiont.yml配置信息,主要配置路由:
server:
port: 10010
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:10086/eureka
instance:
prefer-ip-address: true
spring:
application:
name: api-gateway
cloud:
gateway:
routes:#路由配置
- id: user-service-route
#uri: http://127.0.0.1:9992
uri: lb://demo-provider#动态路由
predicates:
- Path=/user/**
5、测试