整合SpringBoot+Dubbo(Zookeeper)+redis(Linux)+mybatis的简易项目框架

整合SpringBoot+Dubbo(Zookeeper)+redis(Linux)+mybatis的简易项目框架,第1张

整合SpringBoot+Dubbo(Zookeeper)+redis(Linux)+mybatis的简易项目框架

springboot整合使用dubbo大体分为三大部分

暴露的API接口(dubbo-interface-API)

服务的提供者(dubbo-service-provider)

服务的消费者(dubbo-service-consumer)

下面是实现细节


1.对外暴露的API接口(dsubbo-interface-API)

对于接口工程的创建 使用了maven 但没使用任何骨架

目录结构为一个实体类和一个服务接口

实体类的创建要注意实现对象的序列化 (实现Serializable 接口)

public class Student implements Serializable {
    private static final long serialVersionUID = 1245946014621298931L;
    private Integer id;
    private String name;
    private int age;
    
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + ''' +
                ", age=" + age +
                '}';
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
public interface StudentService  {
    Student queryStudent(String name);
    int addStudent(Student student);
}


2.服务的提供者(dubbo-service-provider)

对于服务的提供者 这里创建使用了maven的spring Initializr 添加了一大部分的springboot起步依赖 

(mysql驱动 mybatis框架 redis驱动等起步依赖)

 

2.1 处理pom文件

对于暴露接口工程/dubbo/zookeeper的依赖还需要手动添加

    
        
            XXXXXX.XXXXXX
            dubbo-api
            1.0.0
        
        
        
            org.apache.dubbo
            dubbo-spring-boot-starter
            2.7.8
        
        
        
            org.apache.dubbo
            dubbo-dependencies-zookeeper
            2.7.8
            pom
            
                
                    slf4j-log4j12
                    org.slf4j
                
            
        

框架自动生成pom文件中的一些内容

2.2编写StudentDao接口

StudentDao接口来写业务的查询和添加功能

public interface StudentDao {

    Student selectStudentByName(@Param("name")String name);

    int insertStudent(Student student);

}
2.3处理mybatis.xml 文件

实现Dao接口的映射文件








    

    
        insert into student(name ,age) values(#{name},#{age})
    


2.4处理StudentServer接口的实现类---StudentServerImpl
@DubboService(interfaceClass = StudentService.class,version = "1.0",timeout = 5000)
public class StudentServiceImpl implements StudentService {

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private StudentDao studentDao;

    
    @Override
    public int addStudent(Student student) {
        int result = 0;
        if(student.getName() != null){
            Student stu =studentDao.selectStudentByName(student.getName());
            //应该是student不应该是student.getName() 不然会空指针
            if(stu.getName() != null){
                result = 2;
            }else{
                result =studentDao.insertStudent(student);
            }
        }else {
            result = 3;
        }
        return result;
    }

    //查询时会出现缓存穿透的现象
    @Override
    public Student queryStudent(String name) {
        //对键值分别序列化
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Student.class));

        Student student = null;
        final String USER_KEY="STUDENT:";

        if(name != null){
            String key = USER_KEY + name;
            student = (Student) redisTemplate.opsForValue().get(key);
            System.out.println("从redis查数据" + student);
            if(student == null){
                student = studentDao.selectStudentByName(name);
                System.out.println("从数据库查数据" + student);
                if(student != null){
                    redisTemplate.opsForValue().set(key,student);
                }else {
                    redisTemplate.opsForValue().set(key,Student.defaultStudent());
                }
            }
        }
        return student;
    }

}
@DubboService(interfaceClass = StudentService.class,version = "1.0",timeout = 5000)
实现dubbo服务 设置需要的暴露的公共项目 版本号 超时等属性

@Resource:private RedisTemplate redisTemplate;
使用redis需要的注解 通过RedisTemplate封装好的各种方法来 *** 作Redis 进行序列化 反序列化等 

@Resource:private StudentDao studentDao;
spring实现自动注入
2.5处理application.properties文件
#配置dubbo
spring.application.name=dubbo-provider
dubbo.scan.base-packages=xxx.xxxxxxx.service
dubbo.registry.address=zookeeper://localhost:2181

#配置redis
#redis部署在Linux上 使用虚拟机的ip 未使用密码
spring.redis.host=192.168.xxx.xxx
spring.redis.port=6379

#配置mybatis
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

#配置mysql数据源
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=0120

mybatis.mapper-locations=classpath:mapper/*.xml---->通过主配置文件使mybatis找到mapper文件的位置
2.6 处理springboot主启动项
@SpringBootApplication
@EnableDubbo
@MapperScan(basePackages = "com.xxxxxx.Dao")
public class DemoApplication {

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

}
@SpringBootApplication
使用springboot核心注解(复合注解)

@EnableDubbo
启用dubbo服务

@MapperScan(basePackages = "com.xxxxxx.Dao")
(使用mybatis时 把Dao接口与java文件分开存储) 
再配置注解对包进行扫描---->通过springboot主类找到接口位置 

....................................未完成

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5715651.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存