1.概述
Spring cloud gateway是spring官方基于Spring 5.0、Spring Boot2.0和Project Reactor等技术开发的网关,Spring Cloud Gateway旨在为微服务架构提供简单、有效和统一的API路由管理方式,Spring Cloud Gateway作为Spring Cloud生态系统中的网关,目标是替代Netflix Zuul,其不仅提供统一的路由方式,并且还基于Filer链的方式提供了网关基本的功能,例如:安全、监控/埋点、限流等。
创建一个空的module项目
添加pom依赖
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>log4j-over-slf4j</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> <version>2.1.2.RELEASE</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
创建一个启动类
@SpringBootApplication @ServletComponentScan @EnableZuulProxy public class GatewayApplication extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(GatewayApplication.class); } }
配置文件
server: port: 8888 spring: application: name: dxtgateway cloud: gateway: discovery: locator: enabled: true lowerCaseServiceId: true ribbon: ReadTimeout: 60000 ConnectTimeout: 60000 # cloud: # config: # profile: dev # label: master # discovery: # enabled: true # service-id: spring-cloud-config eureka: client: service-url: defaultZone: http://127.0.0.1:8761/eureka instance: status-page-url-path: /actuator/info metadata-map.management: context-path: /actuator health-check-url-path: /actuator/health management: endpoints: web: exposure: include: '*' health: show-details: ALWAYS zuul: routes: xxapi: path: /xxapi/** serviceId: xxapi xxyapi: path: /xxyapi/** serviceId:xxyapi ignored-patterns:
简单的gateway就搭建好了,这时候先启动eureka,在启动gateway就可以看到 gateway在注册中心出现了,Nginx请求代理到gateway上,gateway通过实例分开, xxapi,xxyapi等等