分布式基础篇
csdn机制问题导致图片丢失,可以查看本人的个人博客:谷粒商城-P45-P58
1. 三级分类
1.1 sql脚本
1.2 查出所有分类以及子分类
1.2.1 后段代码编写
在gulimall-product的src/main/java/com/rabbitboss/gulimall/product/controller/CategoryController.java`➕以下代码
/**
* 查处所有分类以及子分类,以树形结构组装起来
*/
@RequestMapping("/list/tree")
//@RequiresPermissions("product:category:list")
public R list(@RequestParam Map<String, Object> params){
// PageUtils page = categoryService.queryPage(params);
List<CategoryEntity> entities = categoryService.listWithTree();
return R.ok().put("data", entities);
}
src/main/java/com/rabbitboss/gulimall/product/service/CategoryService.java
添加以下代码
List<CategoryEntity> listWithTree();
src/main/java/com/rabbitboss/gulimall/product/service/impl/CategoryServiceImpl.java
添加以下代码
@Override
public List<CategoryEntity> listWithTree() {
//1.查出所有分类
List<CategoryEntity> entities = baseMapper.selectList(null);
//2。组装成父子树状结构
//2.1找到所有的一级分类
List<CategoryEntity> level1Menu = entities.stream().filter(categoryEntity ->
categoryEntity.getParentCid() == 0
).map(menu -> {
//1。找到子分类
menu.setChildren(getChildrens(menu, entities));
return menu;
}).sorted((menu1, menu2) ->
//2。排序
(menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort())
).collect(Collectors.toList());
return level1Menu;
}
/**
* 递归查找所有菜单的子菜单
*
* @param root
* @param all
* @return
*/
private List<CategoryEntity> getChildrens(CategoryEntity root, List<CategoryEntity> all) {
List<CategoryEntity> children = all.stream().filter((categoryEntity) -> {
return categoryEntity.getParentCid() == root.getCatId();
}).map(categoryEntity -> {
//1。找到子分类
categoryEntity.setChildren(getChildrens(categoryEntity, all));
return categoryEntity;
}).sorted((menu1, menu2) ->
//2。排序
(menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort())
).collect(Collectors.toList());
return children;
}
1.2.2前端代码编写
1.2.2.1配置网关路由与路径重写
接着操作后台
localhost:8001 , 点击系统管理,菜单管理,新增
目录
商品系统
一级菜单
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-a2SMyzJ5-1651674156447)(https://b3logfile.com/file/2022/05/solo-fetchupload-2215375846762372900-191c3030.png)]
注意地址栏http://localhost:8001/#/product-category 可以注意到product-category我们的/被替换为了-
比如sys-role具体的视图在renren-fast-vue/views/modules/sys/role.vue
所以要自定义我们的product/category视图的话,就是创建mudules/product/category.vue
1.2.2.2前端代码
<template>
<el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
</template>
<script>
// 这里可以导入其他文件(比如:组件,工具 js,第三方插件 js,json 文件,图片文件等等)
// 例如:import 《组件名称》 from '《组件路径》 ';
export default {
// import 引入的组件需要注入到对象中才能使用
components: {
},
props: {
},
data () {
return {
data: [],
defaultProps: {
children: 'children',
label: 'label'
}
}
},
// 计算属性 类似于 data 概念
computed: {
},
// 监控 data 中的数据变化
watch: {
},
// 方法集合
methods: {
handleNodeClick (data) {
console.log(data)
},
getMenus () {
this.dataListLoading = true
this.$http({
url: this.$http.adornUrl('/product/category/list/tree'),
method: 'get'
// params: this.$http.adornParams({
// 'page': this.pageIndex,
// 'limit': this.pageSize,
// 'roleName': this.dataForm.roleName
// })
}).then(data => {
console.log('成功获取到菜单数据...', data)
})
}
},
// 生命周期 - 创建完成(可以访问当前this 实例)
created () {
this.getMenus()
},
// 生命周期 - 挂载完成(可以访问 DOM 元素)
mounted () {
},
beforeCreate () {
},
beforeMount () {
}, // 生命周期 - 挂载之前
beforeUpdate () {
}, // 生命周期 - 更新之前
updated () {
}, // 生命周期 - 更新之后
beforeDestroy () {
}, // 生命周期 - 销毁之前
destroyed () {
}, // 生命周期 - 销毁完成
activated () {
} // 如果页面有 keep-alive 缓存功能,这个函数会触发
}
</script>
<style scoped>
</style>
他要给8080发请求读取数据,但是数据是在10000端口上,如果找到了这个请求改端口那改起来很麻烦。
-
方法1是改vue项目里的全局配置
-
方法2是搭建个网关,让网关路由到10000
Ctrl+Shift+F全局搜索 在static/config/index.js里 window.SITE_CONFIG['baseUrl'] = 'http://localhost:88/api'; 接着让重新登录http://localhost:8001/#/login,验证码是请求88的,所以不显示。而验证码是来源于fast后台的 现在的验证码请求路径为,http://localhost:88/api/captcha.jpg 原始的验证码请求路径:http://localhost:8001/renren-fast/captcha.jpg
-
88是GateWay的端口
那么怎么将88:/api
转发到renren-fast上呢?看下面的步骤
# 1.在renren-fast模块中引入common模块 ➡️ pom.xml中引入common,因为common中集成了Nacos
<dependency>
<groupId>com.rabbitboss.gulimall</groupId>
<artifactId>gulimall-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
# 2.在renren-fast模块的application.yml文件中添加以下内容
application:
name: renren-fast
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
# 3.在renren-fast的启动类上加上注解 @EnableDiscoveryClient
# 4在gulimall-gateway的application.yml添加以下代码
spring:
cloud:
gateway:
routes:
- id: admin_route
uri: lb://renren-fast
predicates:
- Path=/api/**
filters:
- RewritePath=/api/(?<segment>.*),/renren-fast/$\{
segment}
在重启renren-fast的时候可能会出错,具体错误请看下面解决过程
1.2.3renren-fast相关报错
renren-fast启动报错:
Your project setup is incompatible with our requirements due to following reasons:
- Spring Boot [2.2.4.RELEASE] is not compatible with this Spring Cloud release train
Action:
Consider applying the following actions:
- Change Spring Boot version to one of the following versions [2.6.x, 2.7.x]
因为在Common模块里面是用的3.1的Nacos,最低支持SpringBoot2.6的版本
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xqp6Z97p-1651674156448)(https://b3logfile.com/file/2022/05/solo-fetchupload-2673391811449110828-19d9724b.png)]
所以需要将renren-fast模块里面的pom.xml把SpringBoot版本指定成2.6.6和谷粒商城其他模块的版本保持一致
renren-fast编译报错
javax.validation.constraints不存在
请看另外一篇博客javax.validation.constraints 不存在
启动报错
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. 2022-04-30 16:25:02.551 ERROR 35690 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: The dependencies of some of the beans in the application context form a cycle: sysLoginController (field private io.renren.modules.sys.service.SysUserService io.renren.modules.sys.controller.SysLoginController.sysUserService) ┌─────┐ | sysUserService (field private io.renren.modules.sys.service.SysRoleService io.renren.modules.sys.service.impl.SysUserServiceImpl