微服务项目【mybatis-plus与微服务注册】
创始人
2024-05-25 07:06:36
0

Mybatis与微服务注册

一、SpringBoot整合MybatisPlus

创建自动生成代码子模块

  1. 基于maven方式创建子模块zmall-generator,用于结合mybatis-plus生成代码。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VkBuOKKw-1676076678392)(images\zmall-generator.jpg)]

  1. 在公共模块zmall-common中注释掉mybatis的依赖引入,改换成mybatis-plus依赖引入

com.baomidoumybatis-plus-boot-starter3.4.0

  1. 在zmall-generator中引入mybatis-plus-generator依赖。该模块专用于mybatis-plus的代码生成,所以单独在此引入该依赖即可。

com.baomidoumybatis-plus-generator3.4.0

  1. 在src/main/resources下创建templates目录,并导入mybatis-generator生成代码模板页
  2. 在src/main/java下创建包com.zking.zmall,并导入generator下的CodeGenerator类用于代码生成
  3. 修改CodeGenerator类基本生成参数,并生成代码
//数据库连接参数
public static String driver = "com.mysql.jdbc.Driver";
public static String url = "jdbc:mysql://localhost:3306/zmall?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true";
public static String username="root";
public static String password="1234";
//父级别包名称
public static String parentPackage = "com.xujie.zmall";
//项目名设置(如果是SpringCloud项目则需要设置,其他为""即可)
public static String projectName="/zmall-generator";
//代码生成的目标路径
public static String generateTo = "/src/main/java";
//mapper.xml的生成路径
public static String mapperXmlPath = "/src/main/resources/mapper";
//控制器的公共基类,用于抽象控制器的公共方法,null值表示没有父类
public static String baseControllerClassName ;
//业务层的公共基类,用于抽象公共方法
public static String baseServiceClassName ;
//作者名
public static String author = "许缘";
//模块名称,用于组成包名
public static String modelName = "model";

注意:

  • 修改数据库连接URL中的数据库名、数据库账号和密码;
  • 修改父级别包名称
  • 修改项目名,如果是SpringCloud项目则修改,不是则默认“”

创建商品服务子模块

  1. 基于Spring Initializr方式创建商品服务模块zmall-product
    在这里插入图片描述

  2. 在主模块pom.xml中加入商品服务子模块zmall-product

zmall-commonzmall-userzmall-generatorzmall-product

  1. 配置商品服务子模块zmall-product的application.yml配置文件
server:port: 8020
spring:application:name: zmall-productdatasource:#type连接池类型 DBCP,C3P0,Hikari,Druid,默认为Hikaritype: com.zaxxer.hikari.HikariDataSourcedriver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/zmall?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=trueusername: rootpassword: 1234freemarker:suffix: .htmltemplate-loader-path: classpath:/templates/
#mybatis-plus配置
mybatis-plus:#所对应的 XML 文件位置mapper-locations: classpath*:/mapper/*Mapper.xml#别名包扫描路径type-aliases-package: com.xujie.zmall.modelconfiguration:#驼峰命名规则map-underscore-to-camel-case: true
#日志配置
logging:level:com.zking.zmall.mapper: debug
  1. 在商品服务子模块中启动类上添加
@SpringBootApplication
@MapperScan({"com.zking.zmall.mapper"})
public class ZmallProductApplication {public static void main(String[] args) {SpringApplication.run(ZmallProductApplication.class, args);}
}
  1. 将公共子模块中生成的service层代码复制到商品服务子模块zmall-product中,并删除掉非商品相关的service接口及实现类

在这里插入图片描述

  1. 创建junit实现接口测试
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class ProductServiceImplTest {@Autowiredprivate IProductService productService;@Beforepublic void setUp() throws Exception {}@Afterpublic void tearDown() throws Exception {}@Testpublic void queryProduct() {List list = productService.list();list.forEach(System.out::println);}
}

二、SpringBoot整合Freeamarker

  1. 在公共模块zmall-common中引入freemarker依赖
org.springframework.bootspring-boot-starter-freemarker

  1. 在商品子模块zmall-product中添加首页和商品详情页面及公共资源(js/css/images)
  • 将资料中Index.html、Product.html和js/css/images等等添加到项目的templates和static目录下,最好请将Index.html、Product.html页面首字母改成小写
  • 导入资料目录中的common目录到项目的templates目录下
  • 将页面中的头部申明修改成(支持H5风格)
  • 在页面中通过<#include>指令引入common目录中的head.html
  1. 创建ProductController定义请求方法
@Controller
public class ProductController {@Autowiredprivate IProductService productService;@RequestMapping("/index.html")public String index(Model model){//按照商品的销量降序排序获取销量排名Top5的商品List products = productService.list(new QueryWrapper().orderByDesc("hot").last("limit 5"));model.addAttribute("top5",products);return "index";}@RequestMapping("/product.html")public String detail(Model model,Integer id){//根据商品ID查询商品详情信息Product product = productService.getById(id);model.addAttribute("product",product);return "product";}
}
  1. 在index.html中绑定热门数据和product.html中绑定商品详情数据

三、SpringBoot整合微服务&gateway&nginx

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-eIaF8IQt-1676076678489)(images\二级域名.png)]

请求链路要求:客户端发送请求先经过nginx,再用nginx转至内部访问网关gateway,最后由网关服务的路由规则转发到微服务的内部服务。

整合微服务之商品服务zmall-product

在公共模块zmall-common中导入微服务相关依赖


com.alibaba.cloudspring-cloud-starter-alibaba-nacos-discovery

org.springframework.cloudspring-cloud-starter-openfeign

com.alibaba.cloudspring-cloud-starter-alibaba-nacos-config

配置商品服务模块zmall-product的application.yml文件

spring:application:name: zmall-productcloud:nacos:discovery:server-addr: localhost:8848

修改启动类,向nacos进行注册

@EnableDiscoveryClient
@SpringBootApplication
@MapperScan({"com.zking.zmall.mapper"})
public class ZmallProductApplication {public static void main(String[] args) {SpringApplication.run(ZmallProductApplication.class, args);}
}

创建并配置网关gateway服务

  1. 基于Spring initializr方式创建网关模块gateway
  2. 配置pom.xml添加nacos和gateway的依赖
4.0.0
com.zkingzmall1.0-SNAPSHOT

zmall-gateway
org.springframework.cloudspring-cloud-starter-gatewaycom.alibaba.cloudspring-cloud-starter-alibaba-nacos-discovery	org.projectlomboklombokorg.apache.commonscommons-lang3

  1. 修改启动类,向nacos进行注册
@EnableDiscoveryClient
@SpringBootApplication
public class ZmallGatewayApplication {public static void main(String[] args) {SpringApplication.run(ZmallGatewayApplication.class, args);}
}
  1. 配置application.yml设置gateway路由转发规则
server:port: 8000
spring:application:name: zmall-gatewaycloud:nacos:discovery:server-addr: localhost:8848gateway:routes:- id: product_routeuri: lb://zmall-product # lb指的是从nacos中按照名称获取微服务,并遵循负载均衡策略predicates:- Path=/product-serv/**filters:- StripPrefix=1
  1. 将项目网页素材中的公共静态资源js/css/images复制到gateway网关服务中

这里请注意了,之前在商品服务模块zmall-product中已经配置了项目的静态资源,为什么还要在gateway网关服务中再配置一次呢?这是因为当请求经过gateway网关服务后会进行断言条件匹配和条件路径截取等操作,从而导致gateway网关路由转发后静态资源失效404的问题,所以特此在gateway网关服务中也配置一次项目网页素材中的公共静态资源js/css/images,确保能正常访问。

解决方案:(使用nginx动静分离方式实现)
配置静态资源访问服务器,将各个微服务模块中的静态访问资源迁移到静态资源访问服务器中,然后通过http方式访问即可。

安装配置SwitchHosts

  1. 直接双击exe文件即可安装SwitchHosts
    提取地址:https://pan.baidu.com/s/18sXp6oA-p9FG2Q6F9m2vgQ
    提取码:jxw8
  2. 进入C:\Windows\System32\drivers\etc目录,设置hosts文件访问权限并取消只读模式
  3. 打开SwitchHosts设置一级域名

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RNoJkpPC-1676076678494)(images\20220817212155.jpg)]

安装配置Windows版nginx

  1. 解压nginx-1.18.0.zip至任意目录
    提取地址:https://pan.baidu.com/s/1nhbG7nPhO7_kHy_Rdc3wtQ
    提取码:5b6h
  2. 进入conf目录,并修改nginx.conf配置文件
server
{listen 80;server_name zmall.com;proxy_redirect off;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;location / {proxy_pass http://127.0.0.1:8000/;}
}
  1. 最后运行nginx根目录下的nginx.exe启动nginx

请求链路测试

单独访问商品服务:http://localhost:8020/index.html

通过gateway访问:http://localhost:8000/product-serv/index.html

通过nginx访问:http://zmall.com/product-serv/index.html

相关内容

热门资讯

喜欢穿一身黑的男生性格(喜欢穿... 今天百科达人给各位分享喜欢穿一身黑的男生性格的知识,其中也会对喜欢穿一身黑衣服的男人人好相处吗进行解...
网络用语zl是什么意思(zl是... 今天给各位分享网络用语zl是什么意思的知识,其中也会对zl是啥意思是什么网络用语进行解释,如果能碰巧...
发春是什么意思(思春和发春是什... 本篇文章极速百科给大家谈谈发春是什么意思,以及思春和发春是什么意思对应的知识点,希望对各位有所帮助,...
为什么酷狗音乐自己唱的歌不能下... 本篇文章极速百科小编给大家谈谈为什么酷狗音乐自己唱的歌不能下载到本地?,以及为什么酷狗下载的歌曲不是...
苏州离哪个飞机场近(苏州离哪个... 本篇文章极速百科小编给大家谈谈苏州离哪个飞机场近,以及苏州离哪个飞机场近点对应的知识点,希望对各位有...
家里可以做假山养金鱼吗(假山能... 今天百科达人给各位分享家里可以做假山养金鱼吗的知识,其中也会对假山能放鱼缸里吗进行解释,如果能碰巧解...
华为下载未安装的文件去哪找(华... 今天百科达人给各位分享华为下载未安装的文件去哪找的知识,其中也会对华为下载未安装的文件去哪找到进行解...
四分五裂是什么生肖什么动物(四... 本篇文章极速百科小编给大家谈谈四分五裂是什么生肖什么动物,以及四分五裂打一生肖是什么对应的知识点,希...
怎么往应用助手里添加应用(应用... 今天百科达人给各位分享怎么往应用助手里添加应用的知识,其中也会对应用助手怎么添加微信进行解释,如果能...
客厅放八骏马摆件可以吗(家里摆... 今天给各位分享客厅放八骏马摆件可以吗的知识,其中也会对家里摆八骏马摆件好吗进行解释,如果能碰巧解决你...