文章目录:
1.1 maven父工程(普通Java工程):管理JDK编译级别、子工程需要用到的依赖
1.3 第二个子工程(SpringBoot工程):对应服务提供者
1.4 第三个子工程(SpringBoot工程):对应服务消费者
2.1 maven父工程(普通Java工程):管理JDK编译级别、子工程需要用到的依赖
2.3 第二个子工程(SpringBoot工程):对应服务提供者
2.4 第三个子工程(SpringBoot工程):对应服务消费者
1.写在前面
这个大综合案例,我使用Maven多模块管理来实现,就像之前创建SpringBoot项目一样,它的pom文件中会有一个父工程,而父工程中又会有一个父工程,在这个父工程(通俗的说爷爷工程)中,就是各种各样需要用到的依赖。
也就是说,这个案例,先是有一个maven父工程,它是一个SpringBoot工程,它来管理其他模块中需要用到的依赖、以及JDK的编译级别。而这里集成了Dubbo,那么肯定就会有接口工程、服务提供者、服务消费者这三个工程,而这三个工程都会是上面提到的maven父工程的三个子工程。
OKOK,说了这么多,下面直接放上这四个maven工程的架构图。
1.1 maven父工程(普通Java工程):管理JDK编译级别、子工程需要用到的依赖
1.2 第一个子工程(普通Java工程):对应接口工程
1.3 第二个子工程(SpringBoot工程):对应服务提供者
1.4 第三个子工程(SpringBoot工程):对应服务消费者
2.该案例的所有源码
2.1 maven父工程(普通Java工程):管理JDK编译级别、子工程需要用到的依赖
在这个maven工程中,什么都没有,只有一个pom文件(maven项目的核心!!!) 。因为我写的这个案例是SpringBoot集成XXX,所有我需要代替之前创建SpringBoot工程的那些功能,在这里指定这个maven工程为一个SpringBoot工程(也就是pom文件中 <parent> 标签中的内容),这样我才可以管理JDK编译级别、子工程需要用到的依赖(也就是pom文件中 <properties>、<dependencyManagement> 标签中的内容)。
而声明该maven工程为父工程需要两个条件:①将 <packaging> 标签的内容设置为pom;②删除该工程的src目录。
-
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-
<modelVersion>4.0.0
</modelVersion>
-
-
<parent>
-
<groupId>org.springframework.boot
</groupId>
-
<artifactId>spring-boot-starter-parent
</artifactId>
-
<version>2.5.0
</version>
-
<relativePath/>
<!-- lookup parent from repository -->
-
</parent>
-
-
<groupId>com.szh.springboot
</groupId>
-
<artifactId>017-springboot-parent
</artifactId>
-
<version>1.0.0
</version>
-
-
<packaging>pom
</packaging>
-
-
<properties>
-
<java.version>11
</java.version>
-
<dubbo-spring-boot-starter-version>2.0.0
</dubbo-spring-boot-starter-version>
-
<zkclient-version>0.4
</zkclient-version>
-
<mybatis-spring-boot-starter-version>2.1.4
</mybatis-spring-boot-starter-version>
-
</properties>
-
-
<!-- 管理SpringBoot父工程没有管理的依赖 -->
-
<dependencyManagement>
-
<dependencies>
-
-
<!-- Dubbo集成SpringBoot框架的起步依赖 -->
-
<dependency>
-
<groupId>com.alibaba.spring.boot
</groupId>
-
<artifactId>dubbo-spring-boot-starter
</artifactId>
-
<version>${dubbo-spring-boot-starter-version}
</version>
-
</dependency>
-
-
<!-- zookeeper注册中心 -->
-
<dependency>
-
<groupId>org.apache.zookeeper
</groupId>
-
<artifactId>zookeeper
</artifactId>
-
<version>3.4.6
</version>
-
<exclusions>
-
<exclusion>
-
<groupId>org.slf4j
</groupId>
-
<artifactId>slf4j-log4j12
</artifactId>
-
</exclusion>
-
</exclusions>
-
</dependency>
-
<dependency>
-
<groupId>com.101tec
</groupId>
-
<artifactId>zkclient
</artifactId>
-
<version>${zkclient-version}
</version>
-
</dependency>
-
-
<!-- MyBatis集成SpringBoot框架的起步依赖 -->
-
<dependency>
-
<groupId>org.mybatis.spring.boot
</groupId>
-
<artifactId>mybatis-spring-boot-starter
</artifactId>
-
<version>${mybatis-spring-boot-starter-version}
</version>
-
</dependency>
-
-
</dependencies>
-
</dependencyManagement>
-
-
</project>
2.2 第一个子工程(普通Java工程):对应接口工程
首先来看它的pom文件,这里有一个 <parent> 标签,那么意思就是说它有一个父工程叫:017-springboot-parent
<relativePath>../017-springboot-parent/pom.xml</relativePath> 的意思是通过相对路径找到父工程的pom文件。
-
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-
<modelVersion>4.0.0
</modelVersion>
-
-
<parent>
-
<artifactId>017-springboot-parent
</artifactId>
-
<groupId>com.szh.springboot
</groupId>
-
<version>1.0.0
</version>
-
<relativePath>../017-springboot-parent/pom.xml
</relativePath>
-
</parent>
-
-
<artifactId>018-springboot-dubbo-ssm-interface
</artifactId>
-
-
<properties>
-
<project.build.sourceEncoding>UTF-8
</project.build.sourceEncoding>
-
<maven.compiler.source>11
</maven.compiler.source>
-
<maven.compiler.target>11
</maven.compiler.target>
-
</properties>
-
-
</project>
因为这是一个接口工程,所有它还需要实体bean(集成Dubbo的实体bean必须实现序列化)和servic业务接口。
-
package com.szh.springboot.entity;
-
-
import java.io.Serializable;
-
-
public
class Student implements Serializable {
-
-
private Integer id;
-
-
private String name;
-
-
private Integer age;
-
-
//getter and setter
-
}
-
package com.szh.springboot.service;
-
-
import com.szh.springboot.entity.Student;
-
-
/**
-
*
-
*/
-
public
interface StudentService {
-
-
Student queryStudentById(Integer id);
-
-
}
2.3 第二个子工程(SpringBoot工程):对应服务提供者
首先来看它的pom文件。在 <parent> 标签中指定了它的父工程,这里面就用到了 017 工程中管理的各种依赖,此时我们就不需要再声明版本号了,因为父工程017中已经有了!!!
-
<?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>
-
-
<parent>
-
<artifactId>017-springboot-parent
</artifactId>
-
<groupId>com.szh.springboot
</groupId>
-
<version>1.0.0
</version>
-
<relativePath>../017-springboot-parent/pom.xml
</relativePath>
-
</parent>
-
-
<artifactId>019-springboot-ssm-dubbo-provider
</artifactId>
-
-
<dependencies>
-
-
<!-- SpringBoot框架web项目起步依赖 -->
-
<dependency>
-
<groupId>org.springframework.boot
</groupId>
-
<artifactId>spring-boot-starter-web
</artifactId>
-
</dependency>
-
-
<!-- Dubbo集成SpringBoot框架的起步依赖 -->
-
<dependency>
-
<groupId>com.alibaba.spring.boot
</groupId>
-
<artifactId>dubbo-spring-boot-starter
</artifactId>
-
</dependency>
-
-
<!-- zookeeper注册中心 -->
-
<dependency>
-
<groupId>org.apache.zookeeper
</groupId>
-
<artifactId>zookeeper
</artifactId>
-
<version>3.4.6
</version>
-
<exclusions>
-
<exclusion>
-
<groupId>org.slf4j
</groupId>
-
<artifactId>slf4j-log4j12
</artifactId>
-
</exclusion>
-
</exclusions>
-
</dependency>
-
<dependency>
-
<groupId>com.101tec
</groupId>
-
<artifactId>zkclient
</artifactId>
-
</dependency>
-
-
<!-- MyBatis集成SpringBoot框架的起步依赖 -->
-
<dependency>
-
<groupId>org.mybatis.spring.boot
</groupId>
-
<artifactId>mybatis-spring-boot-starter
</artifactId>
-
</dependency>
-
-
<!-- MySQL驱动 -->
-
<dependency>
-
<groupId>mysql
</groupId>
-
<artifactId>mysql-connector-java
</artifactId>
-
</dependency>
-
-
<!-- 接口工程 -->
-
<dependency>
-
<groupId>com.szh.springboot
</groupId>
-
<artifactId>018-springboot-dubbo-ssm-interface
</artifactId>
-
<version>1.0.0
</version>
-
</dependency>
-
-
</dependencies>
-
-
<build>
-
-
<resources>
-
<resource>
-
<directory>src/main/java
</directory>
-
<includes>
-
<include>**/*.xml
</include>
-
</includes>
-
</resource>
-
<resource>
-
<directory>src/main/resources
</directory>
-
<includes>
-
<include>**/*.*
</include>
-
</includes>
-
</resource>
-
</resources>
-
-
<plugins>
-
-
<plugin>
-
<groupId>org.springframework.boot
</groupId>
-
<artifactId>spring-boot-maven-plugin
</artifactId>
-
</plugin>
-
-
</plugins>
-
</build>
-
-
</project>
因为它是一个SpringBoot工程,是一个集成Dubbo之后的服务提供者,所以它还需要dao、mapper、对接口工程中接口方法的实现。
-
package com.szh.springboot.mapper;
-
-
import com.szh.springboot.entity.Student;
-
-
public
interface StudentMapper {
-
-
Student selectByPrimaryKey(Integer id);
-
-
}
-
<?xml version="1.0" encoding="UTF-8"?>
-
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
-
<mapper namespace="com.szh.springboot.mapper.StudentMapper">
-
-
<resultMap id="BaseResultMap" type="com.szh.springboot.entity.Student">
-
<id column="id" jdbcType="INTEGER" property="id" />
-
<result column="name" jdbcType="VARCHAR" property="name" />
-
<result column="age" jdbcType="INTEGER" property="age" />
-
</resultMap>
-
<sql id="Base_Column_List">
-
id, name, age
-
</sql>
-
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
-
select
-
<include refid="Base_Column_List" />
-
from t_student
-
where id = #{id,jdbcType=INTEGER}
-
</select>
-
-
</mapper>
-
package com.szh.springboot.service.impl;
-
-
import com.alibaba.dubbo.config.annotation.Service;
-
import com.szh.springboot.entity.Student;
-
import com.szh.springboot.mapper.StudentMapper;
-
import com.szh.springboot.service.StudentService;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.stereotype.Component;
-
-
/**
-
*
-
*/
-
@Component
-
@Service(interfaceClass = StudentService.class,version = "1.0.0",timeout = 15000)
-
public
class StudentServiceImpl implements StudentService {
-
-
@Autowired
-
private StudentMapper studentMapper;
-
-
@Override
-
public Student queryStudentById(Integer id) {
-
return studentMapper.selectByPrimaryKey(id);
-
}
-
}
同时配置SpringBoot的核心配置文件。
-
# 配置内嵌tomcat端口号和上下文根
-
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&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8
-
spring.datasource.username=root
-
spring.datasource.password=12345678
-
-
# 配置Dubbo
-
spring.application.name=019-springboot-ssm-dubbo-provider
-
spring.dubbo.server=true
-
spring.dubbo.registry=zookeeper://localhost:2181
最后是SpringBoot项目启动入口类。
-
package com.szh.springboot;
-
-
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
-
import org.mybatis.spring.annotation.MapperScan;
-
import org.springframework.boot.SpringApplication;
-
import org.springframework.boot.autoconfigure.SpringBootApplication;
-
-
@SpringBootApplication
-
@MapperScan(basePackages = "com.szh.springboot.mapper")
-
@EnableDubboConfiguration
-
public
class Application {
-
-
public static void main(String[] args) {
-
SpringApplication.run(Application.class, args);
-
}
-
-
}
2.4 第三个子工程(SpringBoot工程):对应服务消费者
首先来看它的pom文件。 在 <parent> 标签中指定了它的父工程。
-
<?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>
-
-
<parent>
-
<artifactId>017-springboot-parent
</artifactId>
-
<groupId>com.szh.springboot
</groupId>
-
<version>1.0.0
</version>
-
<relativePath>../017-springboot-parent/pom.xml
</relativePath>
-
</parent>
-
-
<artifactId>020-springboot-ssm-dubbo-consumer
</artifactId>
-
-
<dependencies>
-
-
<!-- SpringBoot框架集成Thymeleaf前端模板引擎的起步依赖 -->
-
<dependency>
-
<groupId>org.springframework.boot
</groupId>
-
<artifactId>spring-boot-starter-thymeleaf
</artifactId>
-
</dependency>
-
-
<!-- SpringBoot框架web项目的起步依赖 -->
-
<dependency>
-
<groupId>org.springframework.boot
</groupId>
-
<artifactId>spring-boot-starter-web
</artifactId>
-
</dependency>
-
-
<!-- Dubbo集成SpringBoot框架的起步依赖 -->
-
<dependency>
-
<groupId>com.alibaba.spring.boot
</groupId>
-
<artifactId>dubbo-spring-boot-starter
</artifactId>
-
</dependency>
-
-
<!-- zookeeper注册中心 -->
-
<dependency>
-
<groupId>org.apache.zookeeper
</groupId>
-
<artifactId>zookeeper
</artifactId>
-
<version>3.4.6
</version>
-
<exclusions>
-
<exclusion>
-
<groupId>org.slf4j
</groupId>
-
<artifactId>slf4j-log4j12
</artifactId>
-
</exclusion>
-
</exclusions>
-
</dependency>
-
<dependency>
-
<groupId>com.101tec
</groupId>
-
<artifactId>zkclient
</artifactId>
-
</dependency>
-
-
<!-- 接口工程 -->
-
<dependency>
-
<groupId>com.szh.springboot
</groupId>
-
<artifactId>018-springboot-dubbo-ssm-interface
</artifactId>
-
<version>1.0.0
</version>
-
</dependency>
-
-
</dependencies>
-
-
<build>
-
<plugins>
-
<plugin>
-
<groupId>org.springframework.boot
</groupId>
-
<artifactId>spring-boot-maven-plugin
</artifactId>
-
</plugin>
-
</plugins>
-
</build>
-
-
</project>
因为它是一个SpringBoot工程,是一个集成Dubbo之后的服务消费者,所以它还需要一个控制层方法的实现,以及响应的html页面。
-
package com.szh.springboot.controller;
-
-
import com.alibaba.dubbo.config.annotation.Reference;
-
import com.szh.springboot.entity.Student;
-
import com.szh.springboot.service.StudentService;
-
import org.springframework.stereotype.Controller;
-
import org.springframework.ui.Model;
-
import org.springframework.web.bind.annotation.PathVariable;
-
import org.springframework.web.bind.annotation.RequestMapping;
-
-
/**
-
*
-
*/
-
@Controller
-
public
class StudentController {
-
-
@Reference(interfaceClass = StudentService.class,version = "1.0.0",check = false)
-
private StudentService studentService;
-
-
@RequestMapping(value = "/student/detail/{id}")
-
public String studentDetail(@PathVariable("id") Integer id, Model model) {
-
Student student=studentService.queryStudentById(id);
-
model.addAttribute(
"student",student);
-
return
"studentDetail";
-
}
-
}
-
<!DOCTYPE html>
-
<html lang="en" xmlns:th="http://www.thymeleaf.org">
-
<head>
-
<meta charset="UTF-8">
-
<title>Title
</title>
-
</head>
-
<body>
-
<h3>学生详情
</h3>
-
学生编号:
<span th:text="${student.id}">
</span>
<br/>
-
学生姓名:
<span th:text="${student.name}">
</span>
<br/>
-
学生年龄:
<span th:text="${student.age}">
</span>
<br/>
-
</body>
-
</html>
最后是SpringBoot的核心配置文件,以及SpringBoot项目启动入口类。
-
# 配置内嵌tomcat的端口号和上下文根
-
server.port=
8080
-
server.servlet.context-path=/
-
-
# 关闭Thymeleaf的页面缓存开关
-
spring.thymeleaf.cache=
false
-
# 配置Thymeleaf前后缀
-
spring.thymeleaf.prefix=classpath:/templates/
-
spring.thymeleaf.suffix=.html
-
-
# 配置字符编码格式
-
server.servlet.encoding.enabled=
true
-
server.servlet.encoding.force=
true
-
server.servlet.encoding.charset=UTF-
8
-
-
# 配置Dubbo
-
spring.application.name=
020-springboot-ssm-dubbo-consumer
-
spring.dubbo.registry=zookeeper://localhost:
2181
-
package com.szh.springboot;
-
-
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
-
import org.springframework.boot.SpringApplication;
-
import org.springframework.boot.autoconfigure.SpringBootApplication;
-
-
@SpringBootApplication
-
@EnableDubboConfiguration
-
public
class Application {
-
-
public static void main(String[] args) {
-
SpringApplication.run(Application.class, args);
-
}
-
-
}
2.5 启动测试!!!
启动的步骤如下:👇👇👇
- 启动zookeeper注册中心(zkServer.cmd),我这里直接就在Windows端启动了。
- 启动服务提供者的Tomcat。
- 启动服务消费者的Tomcat。
- 到浏览器中输入请求方法中定义的 url 访问就可以了。
转载:https://blog.csdn.net/weixin_43823808/article/details/117389459