springboot集成dubbo、ssm、redis、JSP (笔记)
一、创建空项目
二、创建接口工程(module -->不需要勾选webapp)
三、创建 提供者、消费者(需要勾选webapp)
四、提供者添加generator依赖
<plugins>
<!--mybatis 代码自动生成插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<!--配置文件的位置-->
<configurationFile>GeneratorMapper.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
五、添加逆向工程文件并更改路径,注:需连接数据库后执行
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- 指定连接数据库的 JDBC 驱动包所在位置,指定到你本机的完整路径 -->
<classPathEntry location="D:\Java\jar\mysql-connector-java-8.0.16.jar"/>
<!-- 配置 table 表信息内容体,targetRuntime 指定采用 MyBatis3 的版本 -->
<context id="tables" targetRuntime="MyBatis3">
<!-- 抑制生成注释,由于生成的注释都是英文的,可以不让它生成 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- 配置数据库连接信息 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC"
userId="root"
password="123456">
</jdbcConnection>
<!-- 生成 model 类,targetPackage 指定 model 类的包名, targetProject 指定
生成的 model 放在 eclipse 的哪个工程下面-->
<javaModelGenerator targetPackage="com.lz.springboot.model"
targetProject="D:\Java\IntelliJ IDEA 2019.3.1\
springboot-projects\springboot-dubbo-ssm-interface\src\main\java">
<property name="enableSubPackages" value="false"/>
<property name="trimStrings" value="false"/>
</javaModelGenerator>
<!-- 生成 MyBatis 的 Mapper.xml 文件,targetPackage 指定 mapper.xml 文件的
包名, targetProject 指定生成的 mapper.xml 放在 eclipse 的哪个工程下面 -->
<sqlMapGenerator targetPackage="com.lz.springboot.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- 生成 MyBatis 的 Mapper 接口类文件,targetPackage 指定 Mapper 接口类的包
名, targetProject 指定生成的 Mapper 接口放在 eclipse 的哪个工程下面 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.lz.springboot.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- 数据库表名及对应的 Java 模型类名 -->
<table tableName="t_student" domainObjectName="Student"
enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
</context>
</generatorConfiguration>
六、执行
七、执行后生成model及mapper文件
八、添加提供者依赖 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lz.springboot</groupId>
<artifactId>springboot-dubbo-ssm-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--springboot项目web工程起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.3</version>
</dependency>
<!--接口工程-->
<dependency>
<groupId>com.lz.springboot</groupId>
<artifactId>springboot-dubbo-ssm-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--dubbo集成springboot依赖-->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--注册中心-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<!--mybatis集成springboot起步依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<!--springboot集成reds起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.4.3</version>
</dependency>
<!-- zookeeper依赖-->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.6.0</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<plugins>
<!--mybatis 代码自动生成插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<!--配置文件的位置-->
<configurationFile>GeneratorMapper.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<mainClass>com.lz.springboot.SpringbootDubboSsmProviderApplication</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
九、添加消费者依赖 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lz.springboot</groupId>
<artifactId>springboot-dubbo-ssm-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>
<dependencies>
<!--springboot框架web项目起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--接口工程依赖-->
<dependency>
<groupId>com.lz.springboot</groupId>
<artifactId>springboot-dubbo-ssm-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--zookeeper注册中心-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<!--dubbo集成springboot框架起步依赖-->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--springboot集成jsp,仅仅只是展示jsp页面需要添加解析jsp页面的依赖-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<!--指定文件夹-->
<resources>
<resource>
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>*.*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!--springboot编译打包插件-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<mainClass>com.lz.springboot.SpringbootDubboSsmConsumerApplication</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
十、添加接口、实现类、控制层(+webapp文件夹)
指定webapp文件夹
同时在pom文件指定路径
十一、编写提供者和消费者配置文件
1.提供者 application.properties
# 应用服务 WEB 访问端口
server.port=8081
#上下文根
server.servlet.context-path=/
#设置数据库连接信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8&useSSL=true
spring.datasource.username=root
spring.datasource.password=123456
#设置dubbo配置
spring.application.name=springboot-dubbo-ssm-privader
#设置当前工程为服务提供者
spring.dubbo.server=true
#设置注册中心
spring.dubbo.registry=zookeeper://192.168.163.128:2181
#设置redis配置
spring.redis.host=192.168.163.128
spring.redis.port=6379
2.消费者 application.properties
#应用服务 WEB 访问端口
server.port=8080
#设置上下文根
server.servlet.context-path=/
#设置消费者名称
spring.application.name=springboot-dubbo-ssm-consumer
#设置zookeeper注册中心
spring.dubbo.registry=zookeeper://192.168.163.128:2181
#配置视图解析器
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
十二、编写StudentService接口
package com.lz.springboot.service;
import com.lz.springboot.model.Student;
public interface StudentService {
//根据学生id查询详情
Student queryStudentById(Integer id);
//获取学生总人数
Integer queryAllStudentCount();
}
**十三、编写StudentServiceImpl实现类
1.编写实现类
package com.lz.springboot.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.lz.springboot.mapper.StudentMapper;
import com.lz.springboot.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
@Component
@Service(interfaceName = "com.lz.springboot.service.StudentService", version = "1.0.0", timeout = 25000) //暴露接口服务
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
//注入redis模板对象
@Autowired
private RedisTemplate<Object, Object> redisTemplate;
@Override
public Student queryStudentById(Integer id) {
return studentMapper.selectByPrimaryKey(id);
}
//首先去redis缓存中查询,如果有直接查询。如果没有,则去数据库查询并存放到redis缓存中
//目的:提升系统性能,提升用户体验
@Override
public Integer queryAllStudentCount() {
Integer allStudentCount = (Integer) redisTemplate.opsForValue().get("allStudentCount");
//判断是否有值
if (null == allStudentCount) {
//去数据库查询
allStudentCount = studentMapper.selectAllStudentCount();
//并存放到redis缓存中,15,TimeUnit.SECONDS: 存放15秒
redisTemplate.opsForValue().set("allStudentCount",allStudentCount,15, TimeUnit.SECONDS);
}
return allStudentCount;
}
}
2.在提供者启动类上加 扫描注解 和 开启dubbo配置注解
3.在消费者启动类加开启dubbo注解
十四、添加StudentController控制层和jsp页面
package com.lz.springboot.web;
import com.alibaba.dubbo.config.annotation.Reference;
import com.lz.springboot.model.Student;
import com.lz.springboot.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class StudentController {
@Reference(interfaceName = "com.lz.springboot.service.StudentService",version = "1.0.0",timeout = 25000,check = false)
private StudentService studentService;
@RequestMapping("/student/detail/{id}")
public String studentDetatil(Model model, @PathVariable("id") Integer id){
Student student = studentService.queryStudentById(id);
model.addAttribute("student",student);
return "studentDetatil";
}
@GetMapping("/student/all/count")
@ResponseBody
public Object queryAllStudentCount(){
Integer allStudentCount = studentService.queryAllStudentCount();
return "学生总人数为:"+allStudentCount;
}
}
十五、启动redis、zookeeper(开启 ./zkServer.sh start 结束./zkServer.sh stop)
十六、启动提供者、消费者
十七、运行
---------------------------------------------------END--------------------------------------------------------