SpringBoot集成Dubbo、Mybatis和Redis

8 篇文章 0 订阅
2 篇文章 0 订阅

SpringBoot集成Dubbo的基础上在生产者中集成Mybatis和Redis,这样能把各个框架的优点都集成到项目中,让整体项目趋于一个最优的效果

  • 先看看项目目录:
    在这里插入图片描述
    SpringBootProject项目:
    在这里插入图片描述
  • 在pom.xml文件中需要的配置以及导入的依赖:
<!--导入Dubbo的起步依赖-->
        <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>org.example</groupId>
            <artifactId>SpringBoot-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!--加载MyBatis整合SpringBoot起步依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <!--引入MySQL的JDBC驱动包-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!--redis起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
	<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <resources>
            <!--把src/main/java目录下的xml文件也编译到class下面去-->
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>

            <!--把src/main/resources目录下的所有配置文件也编译到class下面去-->
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>

            <!--springboot使用的web资源要编译到META-INF/resources-->
            <resource>
                <directory>src/main/webapp</directory>
                <targetPath>META-INF/resources</targetPath>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
        </resources>
    </build>
  • StudentMapper中:
import com.example.tom.entity.Student;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface StudentMapper {
    public Student selectOneStudent(String id);
}

注意:这里要导入的是org.apahce.ibatis.annotations.Mapper的包

  • StudentMapper.xml文件:
<mapper namespace="com.example.tom.mapper.StudentMapper">
    <select id="selectOneStudent" resultType="com.example.tom.entity.Student">
        select *from student where id=#{id}
    </select>
</mapper>

注意:由于该配置文件和StudentMapper在同一个目录下所以不需要在application.properties文件中声明文件的位置

  • 在UserServiceImpl文件中:
@Component
@Service(interfaceClass = UserService.class,timeout = 15000)
public class UserServiceImpl implements UserService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public String sayHi(String name) {
        return "hello "+name;
    }

    @Override
    public Student queryOneStudent(String id) {
        Student student=(Student)redisTemplate.opsForValue().get("studentKey");
        if(student==null){
            /**使用同步锁
             * */
            synchronized (this){
                student=(Student)redisTemplate.opsForValue().get("studentKey");
                if(student==null){
                    System.out.println("查询数据库");
                    student=studentMapper.selectOneStudent(id);
                    redisTemplate.opsForValue().set("studentKey",student);
                }else{
                    System.out.println("查询Redis");
                }
            }
        }else{
            System.out.println("查询Redis");
        }
        return student;
    }
}

注意:@Service注解导入的是com.alibaba.dubbo.config.annotation.Service的包

  • 在该项目的application.properties文件中的配置:
#配置内嵌Tomcat的端口号
server.port=80

#配置项目访问的跟路径
server.servlet.context-path=/SpringBootProject

#配置Dubbo,需要有,不能少
spring.application.name=SpringBootProject
#表示提供者,可以省略
spring.dubbo.server=true
#注册中心
spring.dubbo.registry=zookeeper://127.0.0.1:2181

#数据库配置信息
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC

#配置Redis连接信息(单机版)
spring.redis.host=127.0.0.1
spring.redis.port=6379
# 连接超时时间(毫秒)
spring.redis.timeout=15000
#由于刚安装的Redis是没有密码的所以行需要注释掉
#spring.redis.password=123456

SpringBoot-interface项目:
在这里插入图片描述

由于这三个项目中需要在实体类在该项目中定义声明好就行了,此外由于数据录入Redis或者Dubbo传输需要序列化,所以所有的实体类都需要继承Serializable

  • 在pom.xml文件中:
<dependencies>
        <!--导入当前目录下的其他模块-->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>SpringBoot-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

SpringBoot-consumer项目:
在这里插入图片描述

  • 在pom.xml文件中:
<!--导入Dubbo的起步依赖-->
        <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>org.example</groupId>
            <artifactId>SpringBoot-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>
  • application.properties文件:
#配置内嵌Tomcat的端口号
server.port=9090

#配置Dubbo,需要有,不能少
spring.application.name=SpringBoot-consumer

spring.dubbo.appname=SpringBoot-consumer
#注册中心
spring.dubbo.registry=zookeeper://127.0.0.1:2181
  • 在Controller层调用远程的服务接口:
 		@GetMapping("/boot/select")
        public Object queryOneStudentInfo(){
                return userService.queryOneStudent("1");
        }

注意:由于在Controller层类中使用了@RestController注解,所以在这里返回的Object会给浏览器传输一个JSON格式的数据

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值