SpringCoud Gateway实战

目录

一 创建 gulimall-gateway 微服务

二 引入 gulimall-common 依赖和 spring-cloud-starter-gateway 依赖

三 开启网关的服务注册和发现

四 创建 application.properties 文件,指定服务名和注册中心地址

五 在nacos创建配置中心的命名空间和配置

六 创建 bootstrap.properties 文件,添加如下配置,指明配置中心地址和所属命名空间

七 启动服务 gulimall-gateway

八 测试


一 创建 gulimall-gateway 微服务

二 引入 gulimall-common 依赖和 spring-cloud-starter-gateway 依赖

<dependency>
    <groupId>com.atguigu.gulimall</groupId>
    <artifactId>gulimall-common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- 引入 Spring Cloud gateway-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

三 开启网关的服务注册和发现

@EnableDiscoveryClient
// 排除和数据源相关的配置
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class GulimallGatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GulimallGatewayApplication.class, args);
    }
}

四 创建 application.properties 文件,指定服务名和注册中心地址

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

五 在nacos创建配置中心的命名空间和配置

六 创建 bootstrap.properties 文件,添加如下配置,指明配置中心地址和所属命名空间

spring.application.name=gulimall-coupon

spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.namespace=b3298fbf-2c21-4886-93ba-1154efb68130

七 启动服务 gulimall-gateway

八 测试

现在想要实现针对于“http://localhost:88/hello?url=baidu”,转发到“https://www.baidu.com”,针对于“http://localhost:88/hello?url=qq”的请求,转发到“https://www.qq.com/

1 创建 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

2 启动 gulimall-gateway

3 访问:http://localhost:88/hello?url=baidu

访问:http://localhost:88/hello?url=qq

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值