SpringBoot---(4) Spring Boot 集成SSM框架和Dubbo分布式框架

一、SpringBoot集成SSM框架和Dubbo分布式框架思路

1、接口工程:存放实体类bean和业务接口

2、服务提供者:它是一个SpringBoot框架web项目,集成Mybatis

(1)添加依赖:Mybatis依赖,Mysql驱动依赖,Dubbo依赖,zookeeper依赖,接口工程
(2)配置Springboot核心配置文件
  • a、配置连接数据库
  • b、配置dubbo

2、服务消费者:他是一个SpringBoot框架web项目,集成JSP,Dubbo

(1)添加依赖:Dubbo依赖,zookeeper依赖,解析JSP页面的依赖,接口工程
(2)配置Springboot核心配置文件
  • a、配置视图解析器
  • b、配置dubbo

二、集成SSM框架的基本步骤:

1、创建好三个工程项目
  • 接口工程(Maven工程)
    在这里插入图片描述

  • 服务提供者工程(SpringBoot Web工程)
    在这里插入图片描述

  • 服务消费者工程(SpringBoot Web工程)
    在这里插入图片描述

2、在服务提供者项目中创建Mybatis的逆向工程

在这里插入图片描述

注意
这里通过逆向工程创建的实体类对象应该放置在接口工程项目中,这时我们需要修改GeneratorMapper.xml文件里的配置

在这里插入图片描述

怎么样创建Mybatis的逆向工程和哪些需要注意的地方,可以到个人博客Mybatis章节中查看

  • 这里通过逆向工程生成的model包下的实体类Student对象需要进行序列化
public class Student implements Serializable {
    
    
    private Integer id;

    private String name;

    private Integer age;
3、整理和修改服务提供者的pom.xml文件的依赖

添加Mybatis依赖,Mysql驱动依赖,Dubbo依赖,注册中心zookeeper的依赖,接口工程的依赖

<dependencies>
        <!--解决使用@ConfigurationProperties 注解出现警告问题-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <!--SpringBoot框架web项目起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </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>
        </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>


        <!--接口工程-->
        <dependency>
            <groupId>com.hcz</groupId>
            <artifactId>014-springboot-ssm-interface</artifactId>
            <version>1.0.0</version>
            <scope>compile</scope>
        </dependency>

    </dependencies>
4、配置服务提供者核心配置文件application.properties
server.port=8081
server.servlet.context-path=/

#设置连接数据库的配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3366/springboot
spring.datasource.username=root
spring.datasource.password=123456

#设置Dubbo的配置
spring.application.name=015-springboot-dubbo-provider
#当前工程是一个服务提供者
spring.dubbo.server=true
#设置注册中心
spring.dubbo.registry=zookeeper://localhost:2181
5、整理和修改服务消费者的pom.xml文件的依赖

添加Dubbo依赖,注册中心zookeeper的依赖,接口工程的依赖,解析JSP页面的依赖

<dependencies>
        <!--解决使用@ConfigurationProperties 注解出现警告问题-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </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>
            <version>2.0.0</version>
        </dependency>
        <!--注册中心-->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>
        <!--接口工程-->
        <dependency>
            <groupId>com.hcz</groupId>
            <artifactId>014-springboot-ssm-interface</artifactId>
            <version>1.0.0</version>
        </dependency>

        <!--SpringBoot集成JSP,仅仅只是解析JSP页面的依赖-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>
    </dependencies>
6、配置服务消费者核心配置文件application.properties
server.port=8080
server.servlet.context-path=/

#设置Dubbo的配置
spring.application.name=016-springboot-dubbo-consumer
#指定注册中心
spring.dubbo.registry=zookeeper://localhost:2181

#配置视图解析器
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
7、在服务消费者项目中创建控制类方法

在这里插入图片描述

@Controller
public class StudentController {
    
    

    private StudentService studentService;

    @RequestMapping(value = "/student/detail/{id}")
    public String studentDetail(Model model,
                                @PathVariable("id") Integer id){
    
    

        Student student = studentService.queryStudentById(id);

        model.addAttribute("student",student);

        return "studentDetail";
    }
}

这里引入了业务层接口StudentService,并在接口工程中创建这个接口类

8、在接口工程中创建接口类

在这里插入图片描述

public interface StudentService {
    
    
    //根据学生id来查询
    Student queryStudentById(Integer id);
}
9、在服务提供者项目中实现该接口的实现类

在这里插入图片描述

@Component
@Service(interfaceClass = StudentService.class,version = "1.0.0")
public class StudentServiceImpl implements StudentService {
    
    

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public Student queryStudentById(Integer id) {
    
    
        return studentMapper.selectByPrimaryKey(id);
    }
}

1、将该实现类注入到Spring容器,使用注解@Component而不是@Service注解
2、使用注解@Service将该接口实现类暴露出去(注意区别和以前的注入注解,这里的为@Service(com.alibaba.dubbo.config.annotation))
3、这里使用注解@Autowired通过Dao层调用了数据库里面的数据,所以需要配置注解扫描器,而这里是通过在Application类中配置

在这里插入图片描述

10、服务消费者引用暴露出来的接口实现类

使用注解@Reference引用暴露出来的接口实现类

在这里插入图片描述

11、在Application类中添加注解来开启Dubbo配置

在这里插入图片描述

12、创建jsp响应页面

在这里插入图片描述

13、在服务消费者项目pom.xml文件中指定JSP编译目录

在这里插入图片描述

14、在服务提供者项目pom.xml文件中配置映射文件的位置

在这里插入图片描述

15、开启zookeeper服务和注册中心服务

在这里插入图片描述

16、启动服务提供项目,然后再启动服务消费者项目

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/hcz666/article/details/115417825