Skip to content

14251104246/spring-cloud-gateway-demo

Repository files navigation

示例项目地址:https://github.com/14251104246/spring-cloud-gateway-demo/tree/cloud2.x-gateway

本示例项目使用的是spring gateway 2.0(基于spring cloud 2.0),而spring gateway1.x(貌似可不集成spring cloud)功能较为单一,不深入了解。spring gateway1.x示例项目地址:https://github.com/14251104246/spring-cloud-gateway-demo/tree/cloud1.x-gateway

依赖

  • pom文件添加如下配置
    • 注意:spring-beans依赖是必须添加的,不然会报java.lang.NoSuchFieldError: logger错误
<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-gateway</artifactId>
			<version>1.0.2.RELEASE</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>

<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-gateway</artifactId>
	</dependency>
	
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>5.0.5.RELEASE</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

配置

  • 下面配置了两个简单的uri路由配置
    • 第一个是路由到本机的9000端口上
    • 第二个路由到注册到配置中心的spring-cloud-producer服务中
    • 第三个是路由到百度网页上
# 网关配置
spring:
  cloud:
    gateway:
      routes:
        # 路由到本机的9000端口上
      - id: hello_route
        uri: http://localhost:9000
        predicates:
        - Path=/producer/**
        filters:
        - StripPrefix=1
        # 集成eureka注册中心的配置示例
      - id: hello_ribbon_route
        uri: lb://spring-cloud-producer
        predicates:
        - Path=/producerInEureka/**
        filters:
        - StripPrefix=1
        # 路由到百度页面
      - id: baidu_route
        uri: http://www.baidu.com
        predicates:
        - Path=/baidu/**
        filters:
        - StripPrefix=1
  • 如果你想关闭gateway,可以使用配置spring.cloud.gateway.enabled=false

测试

参考

About

spring cloud gateway 示例项目

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages