谷粒商城基础篇

必看

本文很多的东西都来自这位大佬的文档

仓库地址

架构&介绍

image-20200622180758067

image-20200622180732260

image-20200622180855941

环境的搭建

简单的操作

我使用的vagrant是我自己打包好的,可观看该博客自定义box,或者在博客最下面找到云盘地址

  1. 创建模块gulimall-coupon、gulimall-member、gulimall-order、gulimall-product、gulimall-ware

  2. 创建虚拟机(建议使用vagrant),在里面安装docker。并使用docker 运行mysql、redis、nacos。

    • 可以进入gulimall/data/1.基础篇/vagrant 目录,然后在终端执行 vagrant up 就会搭建好环境。
  3. clone 人人开源,完成前后端的搭建,以及逆向生成代码。

    • 克隆这三个人人开源项目
    git clone git@gitee.com:renrenio/renren-generator.git
    git clone git@gitee.com:renrenio/renren-fast-vue.git
    git clone git@gitee.com:renrenio/renren-fast.git  
    
    • 把clone 的这三个项目里面的.git 目录删除。
  4. 初始化数据库。

    • gulimall_pms、gulimall_oms、gulimall_ums、gulimall_wms、gulimall_sms
    • 进入gulimall/data/1.基础篇/vagrant 目录,然后在终端执行 bash db_init.sh 就会初始化好数据库环境。
  5. 逆向工程生成 coupon、member、order、product、ware模块的代码。

    • 修改renren-generator 模块里面的application.yml、generator.properties就能根据数据库逆向生成代码。
    • 修改 template/Controller.java.vm,将里面有关shiro 的注解都注释掉,因为,我们不用shiro做权限控制。
    • 逆向生成的代码,需要一些通用的工具类,所以我们创建一个通用模块gulimall-common。
  6. 给coupon、member、order、product、ware模块 配置数据库连接信息、nacos discovery 、 nacos config。

  7. 创建gateway 模块。

  8. idea 的配置工具,能一次启动我们配置好的项目。

    image-20200622163837870

    image-20200622163820138

  9. 运行renren-fast

    • 在配置文件中加上nacos 的地址,已经配置好项目名,然后启动即可。

启动renren-fast-vue

  1. 使用vscode 运行renren-fast-vue 项目。

    1. 需要先安装node.js 。这个软件会替我们安装npm 这个包管理工具。

    2. 安装运行步骤

      # 首先把项目文件夹下的package.json里面的node-sass4.9.0改成4.9.2
      $ pwd
      /Users/haitao/Desktop/gulimall/renren-fast-vue
      $ npm uninstall node-sass
      $ npm i node-sass --sass_binary_site=https://npm.taobao.org/mirrors/node-sass/
      $ npm install -f
      $ npm run dev 
      
    
    
  2. 关掉权限检查。

    image-20200622171248088

    image-20200622171210544

  3. js同源策略

    image-20200622171530658

    解决:在网关添加一个filter

    ```java
    @Configuration
    public class GulimallCorsConfiguration {
    
        @Bean
        public CorsWebFilter corsWebFilter(){
            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    
            CorsConfiguration corsConfiguration = new CorsConfiguration();
    
            //1、配置跨域
            corsConfiguration.addAllowedHeader("*");
            corsConfiguration.addAllowedMethod("*");
            corsConfiguration.addAllowedOrigin("*");
            corsConfiguration.setAllowCredentials(true);
    
            source.registerCorsConfiguration("/**",corsConfiguration);
            return new CorsWebFilter(source);
        }
    }
    
    
    
  4. renren-fast 也配置了同源策略,我们将它删除

    image-20200622171933143

  5. 终于顺利登陆了

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BgtCcJ1Q-1597548345294)(./1.基础篇.assets/image-20200622172104411.jpg)]
    

测试使用feign进行远程服务调用

@RestController
public class TestFeignController {
   
    @Autowired
    CouponFeignService couponFeignService;

    @GetMapping("/test")
    public R test() {
   
        Map<String, Object> params = new HashMap<>();
        R list = couponFeignService.list(params);
        return R.ok().put("data", list);

    }
}
//@FeignClient(value = "gulimall-coupon")
// 添加到IOC 容器中,如果当前包与主启动类同包或者子包下。就能被扫描然后加入IOC 容器中
// 为了以防万一,我们可以在主启动类上特定指定一下feign接口的包路径
@FeignClient(value = "gulimall-gateway")
public interface CouponFeignService {
   
    /**
     * 我们给方法传递的参数会转换为json 封装到请求体中。
     * 目标服务想获取我们传递的数据得从请求体中获取,所以得使用 @RequestBody 注解
     */
    @RequestMapping("/api/coupon/coupon/list")
    public R list(@RequestBody Map<String, Object> params);
}

分布式组件的选择

概述

  • 注册中心:nacos
  • 配置中心:nacos
  • 服务调用:openFeign
  • 服务网关:gateway

Springcloud 版本选择

https://spring.io/projects/spring-cloud-alibaba

Spring Cloud Version Spring Cloud Alibaba Version Spring Boot Version
-------- -------- --------
Spring Cloud Greenwich 2.1.x.RELEASE 2.1.x.RELEASE
Spring Cloud Finchley 2.0.x.RELEASE 2.0.x.RELEASE
Spring Cloud Edgware 1.5.x.RELEASE 1.5.x.RELEASE

https://spring.io/projects/spring-cloud

Release Train Boot Version
Hoxton 2.2.x
Greenwich 2.1.x
Finchley 2.0.x
Edgware 1.5.x
Dalston 1.5.x

使用 docker 启动 nacos

$ docker run --name nacos01 -d \
-p 8848:8848 \
--privileged=true \
--restart=always \
-e JVM_XMS=512m \
-e JVM_XMX=2048m \
-e MODE=standalone \
-e PREFER_HOST_MODE=hostname \
nacos/nacos-server:1.1.4

nacos 做注册中心

引入依赖

 <dependency>
   <groupId>com.alibaba.cloud</groupId>
   <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

编写配置文件将服务注册到nacos中

spring:
  # nacos
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.1.10:8848
  # 不指定名字无法注册进nacos
  application:
    name: gulimall-coupon

在启动类上加上这个注解开启服务发现的功能

@SpringBootApplication
@EnableDiscoveryClient
public class GulimallCouponApplication {
   

    public static void main(String[] args) {
   
        SpringApplication.run(GulimallCouponApplication.class, args);
    }

}

nacos 做配置中心

引入依赖

 <dependency>
   <groupId>com.alibaba.cloud</groupId>
   <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

官方使用步骤

  1. 首先,修改 pom.xml 文件,引入 Nacos Config Starter

     <dependency>
         <groupId>com.alibaba.cloud</groupId>
         <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
     </dependency>
    
  2. 在应用的 /src/main/resources/bootstrap.properties 配置文件中配置 Nacos Config 元数据

     spring.application.name=nacos-config-example
     spring.cloud.nacos.config.server-addr=127.0.0.1:8848
    
  3. 完成上述两步后,应用会从 Nacos Config 中获取相应的配置,并添加在 Spring Environment 的 PropertySources 中。这里我们使用 @Value 注解来将对应的配置注入到 SampleController 的 userName 和 age 字段,并添加 @RefreshScope 打开动态刷新功能。配置的加载:如果配置中心和当前应用的配置文件中都配置了相同的项,优先使用配置中心的配置

     @RefreshScope 
     class SampleController {
         
    
     	@Value("${user.name}") 
     	String userName;
    
     	@Value("${user.age}")
     	int age;
     }
    

默认读取配置中心的文件

# 这么配置默认找的是 public->DEFAULT->appName.properties 文件(命名空间、组、文件名)
spring.application.name=appName
spring.cloud.nacos.config.server-addr=127.0.0.1:8848

同时加载多个配置集

spring.application.name=gulimall-coupon
spring.cloud.nacos.config.server-addr=192.168.1.10:8848
spring.cloud.nacos.config.namespace=b4e817d4-8bdd-4e13-a917-14c8abef6296
# 这个是默认配置文件的加载,默认的DEFAULT ,加载的是组里面的 ${spring.application.name}.properties 文件
spring.cloud.nacos.config.group=prod

# 扩展配置

# 读取 b4e817d4-8bdd-4e13-a917-14c8abef6296->prood->datasource.yml
spring.cloud.nacos.config.ext-config[0].data-id=datasource.yml
spring.cloud.nacos.config.ext-config[0].group=prod
spring.cloud.nacos.config.ext-config[0].refresh=true 

# 读取 b4e817d4-8bdd-4e13-a917-14c8abef6296->prood->mybatis.yml
spring.cloud.nacos.config.ext-config[1].data-id=mybatis.yml
spring.cloud.nacos.config.ext-config[1].group=prod
spring.cloud.nacos.config.ext-config[1].refresh=true

# 读取 b4e817d4-8bdd-4e13-a917-14c8abef6296->prood->other.yml
spring.cloud.nacos.config.ext-config[2].data-id=other.yml
spring.cloud.nacos.config.ext-config[2].group=prod
spring.cloud.nacos.config.ext-config[2].refresh=true

老师总结的知识点

2、细节
*  1)、命名空间:配置隔离;
*      默认:public(保留空间);默认新增的所有配置都在public空间。
*      1、开发,测试,生产:利用命名空间来做环境隔离。
*         注意:在bootstrap.properties;配置上,需要使用哪个命名空间下的配置,
*         spring.cloud.nacos.config.namespace=9de62e44-cd2a-4a82-bf5c-95878bd5e871
*      2、每一个微服务之间互相隔离配置,每一个微服务都创建自己的命名空间,只加载自己命名空间下的所有配置
*
*  2)、配置集:所有的配置的集合,说白了就是我们在nacos里面写个每一个配置文件。
*
*  3)、配置集ID:类似文件名。
*      Data ID:类似文件名
*
*  4)、配置分组:
*      默认所有的配置集都属于:DEFAULT_GROUP;
*      1111,618,1212
*
* 项目中的使用:每个微服务创建自己的命名空间,使用配置分组区分环境,dev,test,prod
*
* 3、同时加载多个配置集
* 1)、微服务任何配置信息,任何配置文件都可以放在配置中心中
* 2)、只需要在bootstrap.properties说明加载配置中心中哪些配置文件即可
* 3)、@Value,@ConfigurationProperties。。。
* 以前SpringBoot任何方法从配置文件中获取值,都能使用。
* 配置中心有的优先使用配置中心中的,

openFeign 调用服务

概述

  • openFeign 是基于面向接口的实现。我们只需要编写好接口,openFeign就能帮我们创建好接口调用的代理对象,我们直接使用这个代理对象即可。
  • openFeign 集成了RestTemplate,真正帮我们发送rest请求的是RestTemplate。
  • openFeign 还集成了ribbon,也就是具有负载均衡的效果。
  • 注意:将
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
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 '属性&

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值