SpringCloudAlibaba-整合gateway(五)

目录地址:

SpringCloudAlibaba整合-CSDN博客

gateway作为网关,是一个单独的模块

1.添加依赖

XML 复制代码
<!-- SpringCloud Alibaba Nacos -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

<!-- SpringCloud Alibaba Nacos Config -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

<!--gateway-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!--gateway使用lb,必须要这个依赖-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

2.启动类添加注解,开启nacos配置

3.添加bootstrap文件

XML 复制代码
server:
  port: 8084
# Spring
spring:
  application:
    # 应用名称
    name: my-gateway
  profiles:
    active: gateway-test
  cloud:
    #nacos
    nacos:
      discovery:
        # 服务注册地址
        server-addr: 127.0.0.1:8848
        namespace: fcfd084d-dea1-4a49-922b-2fa4d4e70dd8
        group: dev
    gateway:
      discovery:
        locator:
          #开启注册中心路由功能
          #是否与服务发现组件进行结合,通过serviceId转发到具体的服务实例。默认为false,设为true便开启通过服务中心的自动根据 serviceId 创建路由的功能
          enabled: true
      routes:
        - id: order-service
          uri: lb://my-order
#          uri: http://127.0.0.1:8090/
          predicates:
            - Path=/order/**
          filters:
            - StripPrefix=1 #会截取test从url中删除

4.启动gateway服务,此时用8084端口访问接口 http://127.0.0.1:8084/order/getUserById 访问成功

相关推荐
PXM的算法星球4 天前
spring gateway配合nacos实现负载均衡
spring·gateway·负载均衡
1990_super5 天前
使用ceph-deploy安装和配置RADOS Gateway (RGW)并使用S3访问集群
ceph·gateway
北极糊的狐8 天前
接口返回504 Gateway Time-out 错误,这意味着请求在网关或代理服务器等待上游服务器响应时超时。以下是可能的原因和排查建议:
数据库·gateway
sg_knight8 天前
Spring Cloud Gateway全栈实践:动态路由能力与WebFlux深度整合
java·spring boot·网关·spring·spring cloud·微服务·gateway
放纵日放纵11 天前
微服务—Gateway
微服务·架构·gateway
你我约定有三11 天前
分布式微服务--GateWay(1)
java·开发语言·分布式·微服务·架构·gateway
毛小茛12 天前
Spring Cloud Gateway 实现登录校验:构建统一认证入口
springcloud
William一直在路上14 天前
KONG API Gateway中的核心概念
网络·gateway·kong
●VON16 天前
重生之我在暑假学习微服务第七天《微服务之服务治理篇》
java·学习·微服务·云原生·nacos·架构·springcloud