2020尚硅谷-谷粒商城(02)分布式环境搭建

分布式解决方案

nacos 配置中心、注册中心
ribbon 负载均衡
openfeign rpc调用
sentinel 流量控制、熔断
gateway api网关
sleuth 调用链监控
seata 分布式事务

nacos简单配置

  1. 在 common 模块中,导入nacos 相关依赖。这样各个微服务模块都可以使用。
  2. 配置文件改为 bootstrap.yml 其中只写服务名称、端口、环境、nacos 地址
spring:
  profiles:
    active: dev # 环境
  cloud:
    nacos:
      discovery:# nacos 注册中心
        server-addr: 192.168.2.3:8848
      config: // nacos 配置中心
        server-addr: 192.168.2.3:8848
        file-extension: yaml
        namespace: product # 读取配置时所在空间
  application:
    name: gulimall-product # 服务名称
server:
  port: 35200

main函数主启动上,添加服务发现注解@EnableDiscoveryClient

  1. 在 nacos 网页上添加namespace和每个服务对应的配置信息在这里插入图片描述
    注意 Date Id 按照 服务名-环境.yaml 书写

nacos 持久化

  1. 首先准备好相关数据库。建表语句如下。(踩坑nacos 不支持mysql utf8mb4,所以不要在 my.cnf 中指定这种字符集)

https://blog.csdn.net/samHuangLiang/article/details/104966744

  1. 修改数据库的隔离级别

SET @@transaction_isolation=‘READ-COMMITTED’;

  1. 下载 docker 环境 nacos,并启动

docker pull nacos/nacos-server:1.2.1

docker run --env MODE=standalone --name nacosGrain -d -p 8848:8848 nacos/nacos-server:1.2.1

  1. 进入容器配置 nacos

docker exec -it nacosGrain bash
vi conf/application.properties

修改数据库相关配置

# spring
server.servlet.contextPath=${SERVER_SERVLET_CONTEXTPATH:/nacos}
server.contextPath=/nacos
server.port=${NACOS_SERVER_PORT:8848}
spring.datasource.platform=mysql
nacos.cmdb.dumpTaskInterval=3600
nacos.cmdb.eventTaskInterval=10
nacos.cmdb.labelTaskInterval=300
nacos.cmdb.loadDataAtStart=false
db.num=${MYSQL_DATABASE_NUM:1}
db.url.0=jdbc:mysql://192.168.2.3:23306/gulimall_nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&serverTimezone=Asia/Shanghai
# db.url.1=jdbc:mysql://${MYSQL_SERVICE_HOST}:${MYSQL_SERVICE_PORT:3306}/${MYSQL_SERVICE_DB_NAME}?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=123456

gateway 微服务网关搭建

  1. 新建 gulimall-gateway 模块,引入 gateway 依赖和 common 依赖
  2. 主启动加上 @EnableDiscoveryClient
  3. 配置文件
spring:
  application:
    name: gulimall-gateway
  profiles:
    active: dev 
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.2.13:8848
      config:
        server-addr: 192.168.2.13:8848
        file-extension: yaml
        namespace: gateway
  1. nacos 中配置文件
spring:
  cloud:
    gateway:
      routes:
        - id: route_baidu
          uri: https://www.baidu.com
          predicates:
            - Query=url,baidu
        - id: route_qq
          uri: https://www.qq.com
          predicates:
            - Query=url,qq
server:
  port: 20088
  1. 测试

踩坑。如果在启动网关时,报错:没有找到数据库配置信息,那么在main函数上加上
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})

gulimall_pms 商品 drop table if exists pms_attr; drop table if exists pms_attr_attrgroup_relation; drop table if exists pms_attr_group; drop table if exists pms_brand; drop table if exists pms_category; drop table if exists pms_category_brand_relation; drop table if exists pms_comment_replay; drop table if exists pms_product_attr_value; drop table if exists pms_sku_images; drop table if exists pms_sku_info; drop table if exists pms_sku_sale_attr_value; drop table if exists pms_spu_comment; drop table if exists pms_spu_images; drop table if exists pms_spu_info; drop table if exists pms_spu_info_desc; /*==============================================================*/ /* Table: pms_attr */ /*==============================================================*/ create table pms_attr ( attr_id bigint not null auto_increment comment '属性id', attr_name char(30) comment '属性名', search_type tinyint comment '是否需要检索[0-不需要,1-需要]', icon varchar(255) comment '属性图标', value_select char(255) comment '可选值列表[用逗号分隔]', attr_type tinyint comment '属性类型[0-销售属性,1-基本属性,2-既是销售属性又是基本属性]', enable bigint comment '启用状态[0 - 禁用,1 - 启用]', catelog_id bigint comment '所属分类', show_desc tinyint comment '快速展示【是否展示在介绍上;0-否 1-是】,在sku中仍然可以调整', primary key (attr_id) ); alter table pms_attr comment '商品属性'; /*==============================================================*/ /* Table: pms_attr_attrgroup_relation */ /*==============================================================*/ create table pms_attr_attrgroup_relation ( id bigint not null auto_increment comment 'id', attr_id bigint comment '属性id', attr_group_id bigint comment '属性分组id', attr_sort int comment '属性组内排序', primary key (id) ); alter table pms_attr_attrgroup_relation comment '属性&
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值