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 访问成功

相关推荐
爱学习的小可爱卢20 小时前
SpringCloud——微服务排错实战:从Gateway到Feign全链路解析
运维·springcloud
全栈Blue2 天前
Spring Cloud 微服务核心组件学习笔记
springcloud
tryxr2 天前
Spring Cloud Gateway
java·开发语言·数据库·网关·spring cloud·gateway
Jay Kay3 天前
SGLang Model Gateway 特性详解(Cache-Aware 之外)
gateway·sglang
欢醉3 天前
一次P0 故障:微服务线程数莫名暴涨 10 倍,线程问题踩坑
springboot·线程·springcloud
专注_每天进步一点点5 天前
SLB(绑定弹性公网ip)-gateway-业务pod,gateway上出现reset by peer,业务pod上没有reset by peer
服务器·tcp/ip·gateway
振宇i7 天前
springcloudAlibaba2025.0.0.0组件版本对应关系
springcloud·mybatis-plus·依赖关系
振宇i7 天前
Sentinel Dashboard 1.8.9 部署+Systemd 托管完整操作文档
sentinel·cloudalibaba
mashirro10 天前
gateway服启动输出台显示
gateway
豆瓣鸡12 天前
网关、服务 userId 透传
java·微服务·gateway