谷粒商城分布式基础篇
必看
本文很多的东西都来自这位大佬的文档
我的仓库地址,里面有很多好东西
架构&介绍
环境的搭建
简单的操作
我使用的vagrant是我自己打包好的,可观看该博客自定义box,或者在博客最下面找到云盘地址
-
创建模块gulimall-coupon、gulimall-member、gulimall-order、gulimall-product、gulimall-ware
-
创建虚拟机(建议使用vagrant),在里面安装docker。并使用docker 运行mysql、redis、nacos。
- 可以进入gulimall/data/1.基础篇/vagrant 目录,然后在终端执行
vagrant up
就会搭建好环境。
- 可以进入gulimall/data/1.基础篇/vagrant 目录,然后在终端执行
-
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 目录删除。
-
初始化数据库。
- gulimall_pms、gulimall_oms、gulimall_ums、gulimall_wms、gulimall_sms
- 进入gulimall/data/1.基础篇/vagrant 目录,然后在终端执行
bash db_init.sh
就会初始化好数据库环境。
-
逆向工程生成 coupon、member、order、product、ware模块的代码。
- 修改renren-generator 模块里面的application.yml、generator.properties就能根据数据库逆向生成代码。
- 修改 template/Controller.java.vm,将里面有关shiro 的注解都注释掉,因为,我们不用shiro做权限控制。
- 逆向生成的代码,需要一些通用的工具类,所以我们创建一个通用模块gulimall-common。
-
给coupon、member、order、product、ware模块 配置数据库连接信息、nacos discovery 、 nacos config。
-
创建gateway 模块。
- 规定:统一调用前缀。 api/product…
- 根据官方文档配置
-
idea 的配置工具,能一次启动我们配置好的项目。
-
运行renren-fast
- 在配置文件中加上nacos 的地址,已经配置好项目名,然后启动即可。
启动renren-fast-vue
-
使用vscode 运行renren-fast-vue 项目。
-
需要先安装node.js 。这个软件会替我们安装npm 这个包管理工具。
-
安装运行步骤
# 首先把项目文件夹下的package.json里面的node-sass4.9.0改成4.9.2 $ pwd /Users/haitao/Desktop/gulimall/renren-fast-vue $ npm i node-sass --sass_binary_site=https://npm.taobao.org/mirrors/node-sass/ $ npm install -f $ npm run dev
-
关掉权限检查。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2XvM18wF-1592837435384)(./http://haitaoss-markdown.oss-cn-shenzhen.aliyuncs.com/2020/06/22/image20200622171248088.png)]
-
js同源策略
解决:在网关添加一个filter
@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); } }
-
renren-fast 也配置了同源策略,我们将它删除
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iYlVrTK7-1592837435389)(./http://haitaoss-markdown.oss-cn-shenzhen.aliyuncs.com/2020/06/22/image20200622171933143.png)]
-
终于顺利登陆了
-
测试使用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>
官方使用步骤
-
首先,修改 pom.xml 文件,引入 Nacos Config Starter。
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency>
-
在应用的 /src/main/resources/bootstrap.properties 配置文件中配置 Nacos Config 元数据
spring.application.name=nacos-config-example spring.cloud.nacos.config.server-addr=127.0.0.1:8848
-
完成上述两步后,应用会从 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[