5.MidBook项目经验之MongoDB,Nacos,网关
1.医院查询接口
//系统1(signsignMD5加密后) ----> 系统2(数据库signMD5加密 相对比),好处在于网络之间传输不会得到直接得到sign
2.上传和删除科室信息
//map转jsonString,然后再转为对象//保存需要查数据库是否存在,存在修改,不存在添加//接口的包引入不对导致调用引包错误//注意条件和分页//好用!!! monogodb自己生成的id,唯一标识,删除前先查有没有数据,再通过id删除//根据spring data monog根据条件查询,不能传入对象,而查询的条件//命名也有规范
Department findDepartmentByHoscodeAndDepcode(String hoscode,String depcode);
3.上传/删除/查询排班接口
//前端遍历插入数据接口,后端只需要写传入一条数据的接口
4.nacos 注册中心和服务调用(不同模块之间调用) 连接调用者和被调用者
//启动nacos.exe :8848/nacos
- 引入依赖,写配置文件指定地址,启动类加注解@EnableDisvoveryClient //记得写spring服务名,会显示
- 两个模块分别写controller,(为什么不用http调用,而用feign,系统内部调用可用feign,外部内部可用http)
- 使用feign创建模块,引入依赖,写个接口 @FeignClient(“service-cmn nacos服务名称”) ,使用方EnableFeignClients(basePackage=“com.atguigu”) 都要引依赖
//补全Mapping的路径,!!!PathVariable(“name”)记得指定名称- 服务引入openfeign,模块引入具体依赖
- 启动类加上 代表开启调用其他服务,需要在feign创建指定的包
@EnableFeignClients(basePackages = “com.atguigu”)- 在service注入这个接口
- 使用流进行遍历
pages.getContent.stream().forEach(item->{
this.xxx(item);//调用方法直接修改值
})
5.根据code得到省份,省市联动(点击省,显示市的数据) 前端change事件,更新医院上线状态
6.医院详情信息(为了信息更加清晰,可以将对象拆分为多个对象在map返回) 不要在原来的数据修改
//所有科室展开树型
//对集合进行分组
Map<String,List<Department>> map= list.stream().collect(Collectors.groupingBy(Department::getBigcode));
//将数据库数据处理成为,Vo对象,children
7.统计排班数
//spring data不方便,使用mongo原生可封装条件(像mybatis写xml sql语句一样)
Criteria.where("hoscode").is(hoscode).and("depcode").is(depcode);Aggregation agg //进行日期分组和统计数量,然后分页(在mongo聚合为一个对象)//转为VO对象//日期转星期几,使用工具类joda-time ,字符串转日期new DateTime(str).toDate();list.stream().forEach((item)->{})//实体类可以加个list可以设置其他参数
8.网关, nginx就是网关 ,spring cloud Gateway(更简洁,功能更加强大,不用写注解解决跨域问题,还有服务熔断,服务限流的功能) 需要配合注册中心
- 创建项目(注册到注册中心,处理路由)引入naocs和gateway依赖
- 项目中写配置文件,可以使用前缀匹配转发,和转发到nacos的服务名
id server.port=81
# 服务名
spring.application.name=service-gateway# nacos服务地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848#使用服务发现路由
spring.cloud.gateway.discovery.locator.enabled=true#设置路由id
spring.cloud.gateway.routes[0].id=service-hosp
#设置路由的uri
spring.cloud.gateway.routes[0].uri=lb://service-hosp
#设置路由断言,代理servicerId为auth-service的/auth/路径
spring.cloud.gateway.routes[0].predicates= Path=/*/hosp/**#设置路由id
spring.cloud.gateway.routes[1].id=service-cmn
#设置路由的uri
spring.cloud.gateway.routes[1].uri=lb://service-cmn
#设置路由断言,代理servicerId为auth-service的/auth/路径
spring.cloud.gateway.routes[1].predicates= Path=/*/cmn/**
3.项目中写配置类,解决跨域
@Configuration
public class CorsConfig {@Beanpublic CorsWebFilter corsFilter() {CorsConfiguration config = new CorsConfiguration();config.addAllowedMethod("*");config.addAllowedOrigin("*");config.addAllowedHeader("*");UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());source.registerCorsConfiguration("/**", config);return new CorsWebFilter(source);}
}
4.在启动类启动前!!!要@CrossOrigin需要去掉
9.swagger参数名记得写正确,不然测试的参数也会不一样
@ApiParam(name = "hoscode", value = "hoscode", required = true) @PathVariable String hoscode) {