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

相关推荐
iiYcyk20 小时前
Ribbon和LoadBalance-负载均衡
springcloud
hqxstudying1 天前
Kafka 深入研究:从架构革新到性能优化的全面解析
java·开发语言·微服务·kafka·springcloud
瑞瑞绮绮2 天前
分布式事务的Java实践
java·分布式·springcloud
摘星编程3 天前
Nginx 502 Bad Gateway:从 upstream 日志到 FastCGI 超时复盘
网络·nginx·gateway·php-fpm·fastcgi
网硕互联的小客服3 天前
504 Gateway Timeout:服务器作为网关或代理时未能及时获得响应如何处理?
运维·服务器·gateway
Pierre_4 天前
通过SpringCloud Gateway实现API接口镜像请求(陪跑)网关功能
spring·spring cloud·gateway
kk在加油5 天前
智能门卫:Gateway
gateway
小安同学iter5 天前
Spring Cloud Gateway 网关(五)
java·开发语言·spring cloud·微服务·gateway
JAVA学习通5 天前
Spring Cloud ------ Gateway
java·spring cloud·gateway
麦兜*7 天前
MongoDB 聚合管道(Aggregation)高级用法:数据统计与分析
java·数据库·后端·mongodb·springboot·springcloud