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

相关推荐
smilecold1 天前
SpringCloud 入门 - Gateway 网关与 OpenFeign 服务调用
spring cloud·gateway
xiaodaidai丶2 天前
Eureka的自我保护机制
云原生·eureka·springcloud
INFINI Labs4 天前
Elasticsearch 备份:方案篇
大数据·elasticsearch·搜索引擎·gateway·snapshot·backup·ccr
纤瘦的鲸鱼4 天前
Spring Gateway 全面解析:从入门到进阶实践
java·spring·gateway
INFINI Labs6 天前
如何使用 INFINI Gateway 对比 ES 索引数据
大数据·elasticsearch·gateway·easysearch
m0_651593918 天前
位置透明性、Spring Cloud Gateway与reactor响应式编程的关系
java·spring cloud·系统架构·gateway
nvd119 天前
使用gateway api来实现GKE 的pods 从外部访问
gateway·googlecloud
罗不俷9 天前
【Kubernetes】(二十)Gateway
容器·kubernetes·gateway
森林-10 天前
Spring Cloud Netflix Eureka:从微服务基础到高可用集群实战
微服务·云原生·eureka·springcloud
月夕·花晨10 天前
Gateway-过滤器
java·分布式·spring·spring cloud·微服务·gateway·sentinel