• SpringBoot综合案例——Dubbo-Zookeeper-MyBatis-MySQL-Redis


    SpringBoot综合案例——Dubbo-Zookeeper-MyBatis-MySQL-Redis

    多模块管理(父工程),接口工程,生产者,消费者

    父工程管理,接口工程、生产者、消费者三个工程

    使用MyBatis的逆向生成插件生成数据库表对应的实体类、映射文件、DAO接口

    1. 父工程(maven java)

    1.1 packaging 设置为pom

    <packaging>pom</packaging>
    
    • 1

    1.2 删除src文件夹

    在这里插入图片描述

    1.3 指定父工程为SpringBoot工程

    <?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>
    
        <groupId>com.guo</groupId>
        <artifactId>01-springboot-parent</artifactId>
        <version>1.0-SNAPSHOT</version>
        <modules>
            <module>../02-springboot-dubbo-ssm-interfacce</module>
        </modules>
        <parent>
            <artifactId>spring-boot-starter-parent</artifactId>
            <groupId>org.springframework.boot</groupId>
            <version>2.7.0</version>
            <relativePath/>
        </parent>
    
        <packaging>pom</packaging>
        <properties>
            <java.version>1.8</java.version>
            <dubbo-spring-boot-starter-version>2.0.0</dubbo-spring-boot-starter-version>
            <zkclient-version>0.10</zkclient-version>
            <mybatis-spring-boot-starter-version>2.2.1</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>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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    2. 接口工程(Maven Java)

    在这里插入图片描述

    2.1 指定父工程

    <?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">
        <parent>
            <artifactId>01-springboot-parent</artifactId>
            <groupId>com.guo</groupId>
            <version>1.0-SNAPSHOT</version>
            <relativePath>../01-springboot-parent/pom.xml</relativePath>
        </parent>
        <modelVersion>4.0.0</modelVersion>
        <artifactId>02-springboot-dubbo-ssm-interfacce</artifactId>
    </project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2.1 创建实体(并序列化)和接口

    MyBatis逆向工程 —【解决生成多个实体——解决映射文件重复出现BaseResultMap】

    • 实体通过MyBatis逆向工程插件自动生成。
    • 创建接口

    StudentService.java

    package com.guo.springboot.service;
    import com.guo.springboot.model.Student;
    public interface StudentService {
        /**
         * 根据学生ID查看学生详情
         * @param id
         * @return
         */
        Student queryStudentById(Integer id);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3. 服务提供者 (SpringBoot工程)

    在这里插入图片描述

    3.1 引入依赖

    Dubbo Zookeeper MyBatis MySQL Redis 接口工程

    <?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>01-springboot-parent</artifactId>
            <groupId>com.guo</groupId>
            <version>1.0-SNAPSHOT</version>
            <relativePath>../01-springboot-parent/pom.xml</relativePath>
        </parent>
        <artifactId>springboot-dubbo-ssm-provider</artifactId>
    
        <!--
            Dubbo Zookeeper MyBatis MySQL Redis 接口工程
        -->
        <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>com.101tec</groupId>
                <artifactId>zkclient</artifactId>
            </dependency>
            <!--MySQL驱动-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <!--MyBatis集成SpringBoot框架起步依赖-->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
            </dependency>
            <!--SpringBoot集成Redis起步依赖-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>
            <!--接口工程 , 自己创建的Java工程的依赖版本号不需要由父工程再次管理-->
            <dependency>
                <groupId>com.guo</groupId>
                <artifactId>02-springboot-dubbo-ssm-interfacce</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
        <build>
            <resources>
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.xml
                    
                
                
                    src/main/resources
                    
                        **/*.*</include>
                    </includes>
                </resource>
            </resources>
            <plugins>
                <!--Mybatis代码自动生成插件-->
                <plugin>
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-generator-maven-plugin</artifactId>
                    <version>1.3.3</version>
                    <configuration>
                        <!--配置: 文件的位置 和 文件名 !!! 当前的mybatis-generator.xml在根目录下【src同级】!!!-->
                        <configurationFile>mybatis-generator.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>
            </plugins>
        </build>
    </project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94

    3.2 核心配置文件

    application-dev.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/study?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai
    spring.datasource.username=root
    spring.datasource.password=123456
    
    #设置Dubbo配置
    spring.application.name=03-springboot-dubbo-ssm-provider
    #设置当前工程为服务提供者
    spring.dubbo.server=true
    
    #指定注册中心
    spring.dubbo.registry=zookeeper://192.168.0.128:2181
    
    #设置redis配置
    spring.redis.host=192.168.0.128
    spring.redis.port=6379
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    application.properties

    spring.profiles.active=dev
    
    • 1

    3.3 接口实现类

    实现接口工程中接口的方法

    src/main/java/com/guo/springboot/service/StudentServiceImpl.java

    package com.guo.springboot.service;
    
    import com.alibaba.dubbo.config.annotation.Service;
    import com.guo.springboot.mapper.StudentMapper;
    import com.guo.springboot.model.Student;
    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);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    4. 服务消费者 (SpringBoot工程)

    4.1 引入依赖

    <?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>01-springboot-parent</artifactId>
            <groupId>com.guo</groupId>
            <version>1.0-SNAPSHOT</version>
            <relativePath>../01-springboot-parent/pom.xml</relativePath>
        </parent>
        <artifactId>springboot-dubbo-ssm-consumer</artifactId>
    
        <!--
            Dubbo Zookeeper Thymeleaf  Redis 接口工程
        -->
        <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>com.101tec</groupId>
                <artifactId>zkclient</artifactId>
            </dependency>
            <!--接口工程-->
            <dependency>
                <groupId>com.guo</groupId>
                <artifactId>02-springboot-dubbo-ssm-interfacce</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
        <build>
            <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>
            </plugins>
        </build>
    </project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    4.2 核心配置文件

    配置多环境的配置文件通过核心配置文件application.properties指定使用哪个环境配置文件

    application-dev.properties

    # 设置内嵌tomcat端口号
    server.port=8080
    #设置上下文根
    server.servlet.context-path=/
    
    #设置Dubbo配置
    spring.application.name=04-springboot-dubbo-ssm-consumer
    #指定注册中心
    spring.dubbo.registry=zookeeper://192.168.0.128:2181
    
    #关闭页面缓存
    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    application.properties

    spring.profiles.active=dev
    
    • 1

    4.3 控制层

    URL请求采用RESTful风格

    src/main/java/com/guo/springboot/web/StudentController.java

    package com.guo.springboot.web;
    import com.alibaba.dubbo.config.annotation.Reference;
    import com.guo.springboot.model.Student;
    import com.guo.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;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    public class StudentController {
        @Reference(interfaceClass = StudentService.class,version = "1.0.0",check = false    )
        private StudentService studentService;
        @RequestMapping("/student/detail/{id}")
        public String studentDetail(Model model, @PathVariable("id") Integer id){
    
            //根据学生id查询详情
            Student student = studentService.queryStudentById(id);
            model.addAttribute("student",student);
            return "studentDetail";
        }
        @RequestMapping("/hello")
        public @ResponseBody String Hello(){
            return "Hello";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    4.4 studentDetail页面

    DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>学生详情title>
    head>
    <body>
    学生编号:<span th:text="${student.id}">span><br/>
    学生姓名:<span th:text="${student.name}">span><br/>
    学生性别:<span th:text="${student.sex}">span><br/>
    学生年龄:<span th:text="${student.age}">span><br/>
    body>
    html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    5. 启动程序

    • 先启动Zookeeper、Redis服务

    • 启动服务提供者和服务消费者

    • 通过浏览器URL访问

    5.1 Linux上启动Zookeeper、Redis服务截图

    1. Zookeeper服务启动
    在这里插入图片描述

    2. Redis服务启动
    在这里插入图片描述通过查看进程确认服务正常运行
    在这里插入图片描述

    5.2 通过浏览器访问结果截图

    在这里插入图片描述

    6. 附录—Zookeeper、Redis服务在Linux上启动关闭命令

    注册中心-Zookeeper~Linux下载安装及配置 :https://blog.csdn.net/qq_45896330/article/details/125752574
    启动命令:./zkServer.sh start
    关闭命令:./zkServer.sh stop

    Redis简介-下载安装-基本使用-1:https://blog.csdn.net/qq_45896330/article/details/124649718

    启动redis服务时,指定配置文件:redis-server redis.conf &
    通过redis- cli命令关闭 redis-cli shutdown

  • 相关阅读:
    go语言第三方包导入
    ReentrantLock工作原理
    能率携手梦想改造家,打造适老化住宅新典范
    ArduPilot开源代码之AP_RangeFinder
    2021icpc南京 D
    Java - 将TXT文本文件转换为PDF文件
    MBR10200CT-ASEMI智能AI应用MBR10200CT
    Boundary (topology)
    HDFS组成架构、文件块大小
    Go结构体深度探索:从基础到应用
  • 原文地址:https://blog.csdn.net/qq_45896330/article/details/126564096