分布式电商项目二十:添加商品微服务的路由

添加商品微服务的路由

上一章配置好了跨域请求的规则,这一章给商品的微服务配置路由使得请求能够正确得到商品微服务的响应。配置商品微服务的路由需要分为两步:
一、在网关中添加路由规则
来到gateway模块的yml配置文件,添加商品微服务的路由配置:特别注意前后顺序会影响路由规则

spring:
  cloud:
    gateway:
      routes:
#        - id: baidu_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://mall-product
          predicates:
            - Path=/api/product/**
          filters:
            - RewritePath=/api/(?<segment>.*),/$\{
   segment}
        #后台管理
        - id: admin_route
          uri: lb://renren-fast
          predicates:
            - Path=/api/**
          filters:
            - RewritePath=/api/(?<segment>.*),/renren-fast/$\{
   segment}

二、将product商品服务添加到配置中心
首先进入到Nacos注册中心,添加product的命名空间来保存它的配置文件:
在这里插入图片描述
根据优惠券服务的相同配置方法,新建一个bootstrap.properties 配置文件:

spring.application.name=mall-product
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.namespace=e790d39b-5888-4fd7-a78a-cb7158e97cff

注意这里的命名空间内容在Nacos配置中心里面
在这里插入图片描述
之后在yml文件中添加:

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
  application:
    name: mall-product

最后在主程序上添加注解:开启注册服务功能

@EnableDiscoveryClient
@MapperScan("com.lastingwar.mall.product.dao")
@SpringBootApplication
public class MallProductApplication {
   

    public static void main(String[] args) {
   
        SpringApplication.run(MallProductApplication.class, args)<
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值