springcloud-GetWay

一、项目搭建

1.引入jar包

   <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

springCloud 项目需要和springboot 的版本对应上,要不然会经常出现,项目启动找不到类或者找不到方法。springcloud和springboot的版本对应关系可以在springCloud官网或者spring官网进行查询

https://docs.spring.io/spring-cloud/docs/Hoxton.SR8/reference/html/

在这里插入图片描述
或者在spring官网
https://spring.io/
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

2. 启动类配置

如果配合配置中心使用,加上配置服务发现注解 @EnableDiscoveryClient

@SpringBootApplication
@EnableDiscoveryClient
public class ZhangGetwayApplication {
    public static void main(String[] args) {
        SpringApplication.run(ZhangGetwayApplication.class, args);
    }

}

3.配置文件

eureka:
    client:
        service-url:
            defaultZone: http://localhost:8761/eureka/
    instance:
      instance-id: gateway #主机名称修改
      prefer-ip-address: true #访问路径可以显示ip
      hostname: cloud-gateway-service
server:
    port: 8070

spring:
    application:
        name: zhang-getway
    main:
     # 标记以显式请求特定类型的Web应用程序。如果未设置,则基于类路径自动检测。 设置getway 项目启动的web类型为 reactive
        web-application-type: reactive
    cloud:
        gateway:
            routes:
                - id: payment_routh #payment_route    #路由的ID,没有固定规则但要求唯一,建议配合服务名
                  uri: http://localhost:8001      #匹配后提供服务的路由地址
                  predicates:
                      - Path=/payment/timeout/**        # 断言,路径相匹配的进行路由

                - id: payment_routh2 #payment_route    #路由的ID,没有固定规则但要求唯一,建议配合服务名
                  uri: http://localhost:8001          #匹配后提供服务的路由地址
                  predicates:
                      - Path=/payment/ok/**         # 断言,路径相匹配的进行路由


在应用为reactive 类型的时候需要移除掉 的依赖, 移除spring-boot-starer-web依赖即,
类似这样的
在这里插入图片描述

4.启动测试类进行测试访问

在这里插入图片描述

getway功能简介

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值