gateway网关快速入门

第一步:

创建网关模块

第二部:

引入依赖

 <dependencies>
<!--        网关-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
<!--        nacos-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
<!--        负载均衡-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>
    </dependencies>

第三步:

编写启动类

@SpringBootApplication

public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}

第四步:

配置路由

server:
  port: 8080
spring:
  application:
    name: gateway
  profiles:
    active: dev
  cloud:
    nacos:
      server-addr: nacos所处的地址
    gateway:
      routes:
        - id: item-service # 路由规则id,自定义,唯一
          uri: lb://item-service #路由目标微服务,lb代表负载均衡
          predicates:            #路由断言,判断请求是否符合规则,符合则路由到目标
            - Path=/items/**,/search/** # 以请求路径做判断,以/items或者/search开头则符合           
                                      , 表示或者,即如果需要多个路径就可以加,隔开继续添加
        - id: user-service
          uri: lb://user-service
          predicates:
            - Path=/addresses/**,/users/**

logging:
  level:
    com.hmall: debug
  pattern:
    dateformat: HH:mm:ss:SSS
  file:
    path: "logs/${spring.application.name}"

路由断言:

用的最多的就是path,也会用header做灰度测频率也很低)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值