谷粒商城学习日记(11)——使用spring cloud alibaba的gateway组件作api网关(测试)

使用spring cloud alibaba的gateway组件作api网关

使用gateway

gateway流程图

流程
创建,使用initilizer,Group:com.xfwang.gulimall,Artifact: gulimall-gateway,package:com.xfwang.gulimall.gateway。 搜索gateway选中。

开启注册服务发现@EnableDiscoveryClient
配置nacos注册中心地址applicaion.properties

spring.cloud.nacos.discovery.server-addr=123.57.234.18:8848
spring.application.name=gulimall-gateway
server.port=88

bootstrap.properties 填写nacos配置中心地址

spring.application.name=gulimall-gateway

spring.cloud.nacos.config.server-addr=123.57.234.18:8848
spring.cloud.nacos.config.namespace=b5c3bff7-f421-416a-a0af-a7ddabaf42c9

再去nacos里创建命名空间gateway(项目与项目用命名空间隔离),然后在命名空间里创建文件guilmall-gateway.yml

spring:
  cloud:
    gateway:
      routes:
        - id: test_route
          uri: https://www.baidu.com
          predicates:
            - Query=url,baidu

        - id: qq_route
          uri: https://www.qq.com
          predicates:
            - Query=url,qq

        - id: product_route
          uri: lb://gulimall-product
          predicates:
            - Path=/api/product/**
          filters:
            - RewritePath=/api/(?<segment>.*),/$\{segment}

        - id: third_party_route
          uri: lb://gulimall-third-party
          predicates:
            - Path=/api/thirdparty/**
          filters:
            - RewritePath=/api/thirdparty/(?<segment>.*),/$\{segment}

        - id: member_route
          uri: lb://gulimall-member
          predicates:
            - Path=/api/member/**
          filters:
            - RewritePath=/api/(?<segment>.*),/$\{segment}

        - id: ware_route
          uri: lb://gulimall-ware
          predicates:
            - Path=/api/ware/**
          filters:
            - RewritePath=/api/(?<segment>.*),/$\{segment}

        - id: admin_route
          uri: lb://renren-fast
          predicates:
            - Path=/api/**
          filters:
            - RewritePath=/api/(?<segment>.*),/renren-fast/$\{segment}



测试

启动类加注解
去除数据库的配置

@SpringBootApplication(exclude = {
    
    DataSourceAutoConfiguration.class})

浏览器输入
localhost:88?url=baidu
就会跳转百度

猜你喜欢

转载自blog.csdn.net/menxinziwen/article/details/114681086