springboot集成 dubbo ,redis ,mybatis ,spring ,springmvc,jsp

5 篇文章 0 订阅
2 篇文章 0 订阅

springboot集成 dubbo ,redis ,mybatis ,spring ,springmvc,jsp

在linux中,开启服务zookeepr,redis时一定要

关闭防火墙关闭防火墙关闭防火墙


关闭防火墙llinux命令


systemctl stop firewalld.service

查看防火墙是否被关闭

systemctl status firewalld

开发目录结构
a.接口工程:存放实体bean和业务接口
b.服务提供者:她是一个springboot框架web项目,集成mybatis,redis
-添加依赖:mybatis依赖 mysql驱动依赖 dubbo依赖 zookeeper依赖 redis 依赖 ,接口工程
-配置springboot核心配置文件
-配置连接数据库
配置连接redis
配置dubbo

c,服务消费者:它是一个springboot框架web项目,集成jsp,dubbo
-添加依赖:dubbo依赖,zookeeper一俩,解析jsp页面的依赖,接口工程
配置springboot核心配置文件
-配置视图解析器
-配置dubbo

第一步
首先创建三个工程,一个maven-java工程,两个springboot工程,
分别时接口工程,提供者工程,消费者工程。
如图所示
在这里插入图片描述
接口工程目录
在这里插入图片描述
编写StudentService类

package com.zzuli.springboot.service;

import com.zzuli.springboot.model.Student;

public interface StudentService {

    Student queryStudentById(Integer id);

     Integer queryAllStudentCount();
}

第二步
配置provider提供者,首先集成mybatis,利用逆向工程,生成sql映射文件,持久层接口,集成redis,集成dubbo
提供者目录
在这里插入图片描述

首先配置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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zzuli.springboot</groupId>
    <artifactId>005-springboot-ssm-dubbo-provider</artifactId>
    <version>0.0.1</version>
    <name>005-springboot-ssm-dubbo-provider</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
<!--        springboot 项目框架起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

<!--        springboot项目框架测试起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </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>
        </dependency>

<!--        springboot集成redis-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

<!--        添加接口工程依赖-->
        <dependency>
            <groupId>com.zzuli.springboot</groupId>
            <artifactId>004-springboot-ssm-dubbo-interface</artifactId>
            <version>1.0</version>
        </dependency>


    </dependencies>

    <build>

        <!--        手动指定资源文件夹  sql映射文件-->
        <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>src/main/resources/generatorMapper.xml</configurationFile>
                        <overwrite>true</overwrite>
                        <verbose>true</verbose>
                </configuration>
            </plugin>


<!--            springboot打包插件-->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

generatorMapper.xml配置

<?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:\mysql-connector-java-5.1.46\mysql-connector-java-5.1.46.jar"/>
    <!--配置table表信息内容体,targetRuntime 指定采用MyBatis3的版本-->
    <context id="tables" targetRuntime="MyBatis3">
        <!--抑制生成注释,由于生成的注释都是英文的,可以不让它生成-->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <!--配置数据库连接信息-->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/springboot?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=true"
                        userId="root"
                        password="root">
            <property name="nullCatalogMeansCurrent" value="true"/>

        </jdbcConnection>

        <!--生成model 类,targetPackage 指定 model 类的包名,targetProject 指定
        生成的 model放在idea的哪个工程(如果要生成到其他工程下,要用绝对路径  如下)下面-->
        <javaModelGenerator targetPackage="com.zzuli.springboot.model"

                            targetProject="C:\Users\郭双飞\IdeaProjects\004-springboot-ssm-dubbo-interface\src\main\java">
            <property name="enableSubPackages" value="false"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--生成 MyBatis的Mapper.xml文件,targetPackage 指定 mapper.xml文件的包名,targetProject 指定生成的 mapper.xml放在 idea的哪个工程下面
        -->
        <sqlMapGenerator targetPackage="com.zzuli.springboot.mapper"
                         targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>
        <!--生成 MyBatis的 Mapper接口类文件,targetPackage 指定 Mapper 接口类的包名,targetProject 指定生成的 Mapper 接口放在idea 的哪个工程下面
        -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.zzuli.springboot.mapper"
                             targetProject="src/main/java">
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>
        <!--数据库表名及对应的Java模型类名-->
        <table tableName="t_sudent" domainObjectName="Student"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false" />

    </context>
</generatorConfiguration>

运行逆向工程插件,自动生成sql映射文件和mapper(持久层)代码
配置application.properties文件

#配置tomcat内嵌端口号
server.port=8081
#设置上下文根
server.servlet.context-path=/



#设置连接数据库的信息
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=true
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver



#配置dubbo
#设置提供者服务的连接名称 和moudle名称一致
spring.application.name=005-springboot-ssm-dubbo-provider
#声明当前工程为提供者
spring.dubbo.server=true
#设置注册中心
spring.dubbo.registry=zookeeper://192.168.98.130:2181






#设置redis的配置
#设置redis的密码
spring.redis.password=root
#设置redis的端口号
spring.redis.port=6379
#设置redis的地址
spring.redis.host=192.168.98.130

配置springboot入口类Application

package com.zzuli.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.zzuli.springboot.mapper")   //扫描mapper包以及子包 创建对象
@EnableDubboConfiguration   //开启dubbo配置
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}





配置提供者的实现接口的类StudentServiceImpl

package com.zzuli.springboot.service.serviceimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.zzuli.springboot.mapper.StudentMapper;
import com.zzuli.springboot.model.Student;
import com.zzuli.springboot.service.StudentService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;


@Component                   //将接口实现了放入到spring容器中
@Service(interfaceClass = StudentService.class,version = "1.0.0",timeout = 35000)           //注解暴露接口
public class StudentServiceImpl implements StudentService {

//
    @Resource
    private StudentMapper studentMapper;



//    使用注解注入redis缓存对象,通过这个对象可以直接操作redis数据库,
    @Resource
    private RedisTemplate<Object,Object> redisTemplate;


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



//    通过redis数据库进行查询
    @Override
    public Integer queryAllStudentCount() {

//        首先使用redis缓存中执行,如果有:直接使用   如果没有,存入到redis缓存中
//        减低关系性数据库的压力,提高性能

//        使用redis模板提供的方法,进行访问,指定  key值
        Integer allStudentCount= (Integer) redisTemplate.opsForValue().get("k12");   //k1是再redis中设置的key值  这个结果是返回key的value值


//        当key   value  不存在的时候  存入到redis中
        if (null==allStudentCount){
            allStudentCount=studentMapper.selectAllStudentCount();
//            将从关系性数据库中查到的值  放入到 redis中,并且设置这个 键值对 再redis中存放的时间
            redisTemplate.opsForValue().set("k1",allStudentCount,30, TimeUnit.SECONDS);
        }


        return allStudentCount;
    }
}

第三步
配置消费者
集成redis,dubbo,jsp还要创建一个webapp包 放在resources资源包中用来创建和存放jsp文件,并且指定路径(在pom.xml文件中也要指定路径)
消费者目录结构
在这里插入图片描述
配置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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zzuli.springboot</groupId>
    <artifactId>006-springboot-ssm-dubbo-consumer</artifactId>
    <version>0.0.1</version>
    <name>006-springboot-ssm-dubbo-consumer</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
<!--        springboot项目框架起步依赖-->
        <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>

<!--        zookeeper注册中心-->
        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.10</version>
        </dependency>



<!--        接口工程-->
        <dependency>
            <groupId>com.zzuli.springboot</groupId>
            <artifactId>004-springboot-ssm-dubbo-interface</artifactId>
            <version>1.0</version>
        </dependency>

<!--        springboot集成jsp-->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>



    </dependencies>

    <build>
<!--        指定web配置文件位置-->
        <resources>
            <resource>
                <directory>src/main/webapp</directory>
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>*.*</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

配置application.properties文件

#设置内嵌的tomcat端口号
server.port=8080
server.servlet.context-path=/


#设置dubbo的配置
#声明服务名称 和工程名称保持一致
spring.application.name=006-springboot-ssm-dubbo-consumer
#声明注册中心
spring.dubbo.registry=zookeeper://192.168.98.130:2181



#配置jsp
声明视图解析器
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp

创建一个控制类StudentController

package com.zzuli.springboot.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zzuli.springboot.model.Student;

import com.zzuli.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 {


//    通过注解给引用属性赋值,通过dubbo注册中心
   @Reference(interfaceClass = StudentService.class,version = "1.0.0",check = false)
    private StudentService studentService;




//    默认使用 get  set方式传参
    @RequestMapping(value = "/student/{id}")
    public String studentDetail(Model model,@PathVariable("id") Integer id){
        Student student=studentService.queryStudentById(id);
//        利用model将对象放到 请求域中
        model.addAttribute("student",student);

//        请求页面跳转
        return "ss";

    }


//    使用redis的方法
    @GetMapping(value = "/redis/all")
    @ResponseBody      //开启json数据注解  返回值为json数据
    public Object allStudentCount(){
        Integer count=studentService.queryAllStudentCount();
        return "学生总人数为"+count;
    }

}

配置消费者的springboot入口类

package com.zzuli.springboot;

import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableDubboConfiguration    //开启dubbo的配置
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

最后一步
编写ss.jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>学生详情</title>
</head>
<body>

<h1>学生姓名:${student.name}</h1>
<h1>学生id:${student.id}</h1>
<h1>学生年龄:${student.age}</h1>

<h1>hello world</h1>
</body>
</html>

在linux中开启redis. zookeeper服务(关闭防火墙)-------->开启提供者------->开启消费者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值