Spring Cloud Gateway

# Spring Cloud Gateway

地址:https://spring.io/projects/spring-cloud-gateway

三大核心概念:

  • Route(路由):
  • Predicate(断言):
  • Filter(过滤器):

(1)spring向导创建网关,选择网关

image-20220213152405548

image-20220213152539303

(2)nacos增加gateway配置

增加gateway命名空间

image-20220213155346010

gulimall-gateway.properties

spring:
    application:
        name: gulimall-gateway

image-20220213155415901

(3)bootstrap.properties配置

spring.application.name=gulimall-gateway
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.namespace=debd0e0d-de9a-48c8-b7c1-dbed3b775402

image-20220213155518505

(4)application.properties配置

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

image-20220213155631774

(5)application.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

image-20220213155716208

(6)主程序加上@EnableDiscoveryClient,@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})

如果不加exclude= {DataSourceAutoConfiguration.class,排除datasource相关配置,会报错Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured,这里暂时用不上datasource的一些相关属性

在gateway服务中开启注册服务发现@EnableDiscoveryClient,配置nacos注册中心地址applicaion.properties。这样gateway也注册到了nacos中,其他服务就能找到nacos,网关也能通过nacos找到其他服务

@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
@EnableDiscoveryClient

image-20220213155747723

posted @   peng_boke  阅读(67)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
点击右上角即可分享
微信分享提示