服务网关Gateway_入门案例

创建cloud-gateway-gateway9527工程

image-20220215101632986

pom文件引入依赖

<dependencies>
    <!--  引入网关Gateway依赖   -->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.22</version>
    </dependency>
  </dependencies>

新增application.yml


server:
  port: 9527

spring:
  cloud:
    gateway:
      routes:
        # 路由ID,没有固定规则但要求唯一,建议配合服务名
        - id: cloud-order-openfeign-consumer
        # 匹配后提供服务的路由地址
          uri: http://localhost:80
        # 断言3
        # http://localhost:9527/order/index
          predicates:
            # 路径相匹配的进行路由
            - Path=/order/*

主启动类

@Slf4j
@EnableEurekaClient
@SpringBootApplication
public class GatewayMain {
  public static void main(String[] args) {
    SpringApplication.run(GatewayMain.class,args);
    log.info("********** GatewayMain 服务启动成功 *********");
   }
}

测试

  • 启动注册中心 7001,7002
  • 启动服务提供者80
  • 启动网关服务9527

请求localhost:9527/order/index