Dubbo小案例

Dubbo小案例,第1张

Dubbo小案例

目录

1、建立数据库

2、创建项目 (Parent)

(1)pom.xml

3、创建pojo项目 

(1)com.example.pojo

Dept:

Emp:

4、创建mapper项目 

(1)pom.xml

(2)application.yml

5、新建api项目 

(1)pom.xml

6、新建provider项目 

(1)pom.xml

(2)application.yml 

(3)com.example

7、实现Dept查询工作 

(1)api:com.example.dubbo.service

(2)provider:com.example.dubbo.service.impl

(3)在mapper项目中补充对数据库的 *** 作

com.example.mapper

mybatis:

(4)新建dept项目 

pom.xml

application.yml 

com.example

com.example.service

com.example.service.impl

com.example.controller 

resources /templates/dept.html  

8、实现部门列表工作、图片上传

(1)api:com.example.dubbo.service 

(2)provider:com.example.dubbo.service.impl

(3)在mapper项目中补充对数据库的 *** 作 

com.example.mapper

mybatis 

(4)新建emp项目

pom.xml

application.yml

com.example

com.example.serever 

com.example.serever.impl 

com.example.controller  

resources /templates/empadd-add.html

(5)补充dept项目代码实现部门员工查看

com.example.service

com.example.service,impl

com.example.controller 

resources /templates/showEmp.html 

resources /templates/dept.html  

9、界面展示


1、建立数据库
create table dept(
id int(11) primary key auto_increment,
name varchar(20)
);

insert into dept values(default,'开发部');
insert into dept values(default,'产品部');

create table emp(
id int(11) primary key auto_increment,
name varchar(20),
photo varchar(200),
did int(11),
ConSTRAINT fk_emp_dept FOREIGN key (did) REFERENCES dept(id)
);
2、创建父项目 (Parent) (1)pom.xml
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.10.RELEASE
    
    
        
            
                org.springframework.boot
                spring-boot-starter
                2.1.10.RELEASE
            
            
                org.springframework.boot
                spring-boot-starter-web
                2.1.10.RELEASE
            
            
                org.springframework.boot
                spring-boot-starter-thymeleaf
                2.1.10.RELEASE
            
            
                org.apache.dubbo
                dubbo-spring-boot-starter
                2.7.3
            
            
                org.apache.curator
                curator-recipes
                4.2.0
            
            
                org.apache.curator
                curator-framework
                4.2.0
            
            
                org.mybatis.spring.boot
                mybatis-spring-boot-starter
                2.1.1
            
            
                mysql
                mysql-connector-java
                8.0.21
            
            
                commons-io
                commons-io
                2.6
            
        
    

用于统一子项目的依赖的版本号。 

3、创建pojo项目  (1)com.example.pojo Dept:
public class Dept implements Serializable {

    private Integer id;

    private String name  ;

    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;
    }

    @Override
    public String toString() {
        return "Dept{" +
                "id=" + id +
                ", name='" + name + ''' +
                '}';
    }
}
Emp:
public class Emp implements Serializable {

    private Integer id ;

    private String name ;

    private String photo ;

    private Integer did ;

    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 Integer getDid() {
        return did;
    }

    public void setDid(Integer did) {
        this.did = did;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photo) {
        this.photo = photo;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "id=" + id +
                ", name='" + name + ''' +
                ", photo='" + photo + ''' +
                ", did=" + did +
                '}';
    }
}
4、创建mapper项目  (1)pom.xml
   
        
            pojo
            com.example
            1.0-SNAPSHOT
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
        
        
            mysql
            mysql-connector-java
        
    
(2)application.yml
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mydubbo?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    username: root
    password: root
mybatis:
  mapper-locations: classpath:mybatis/*.xml
  type-aliases-package: com.example.pojo
5、新建api项目  (1)pom.xml
    
        
            pojo
            com.example
            1.0-SNAPSHOT
        
    
6、新建provider项目  (1)pom.xml
   
        
            mapper
            com.example
            1.0-SNAPSHOT
        
        
            api
            com.example
            1.0-SNAPSHOT
        
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.apache.dubbo
            dubbo-spring-boot-starter
        
        
            org.apache.curator
            curator-recipes
        
        
            org.apache.curator
            curator-framework
        
    
(2)application.yml 
dubbo:
  application:
    name: dubbo-provider
  registry:
    address: zookeeper://192.168.1.129:2181
#加载其它配置文件,加载其它的application-*.yml文件
#多个名称之间用逗号分隔
spring:
  profiles:
    active: mybatis
(3)com.example
@SpringBootApplication
@EnableDubbo
@MapperScan("com.example.mapper")
public class ProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class,args);
    }
}
7、实现Dept查询工作  (1)api:com.example.dubbo.service
public interface DeptDubboService {
    public List findAllDept();
}
(2)provider:com.example.dubbo.service.impl
@Service
public class DeptDubboServiceImpl implements DeptDubboService{
    @Autowired
    private DeptMapper deptMapper;
    @Override
    public List findAllDept() {
        return deptMapper.findAll();
    }
}
(3)在mapper项目中补充对数据库的 *** 作 com.example.mapper
public interface DeptMapper {
    public List findAll();
}
mybatis:

    
        select id , name from dept ;
    
(4)新建dept项目  pom.xml
  
        
            api
            com.example
            1.0-SNAPSHOT
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.apache.dubbo
            dubbo-spring-boot-starter
        
        
            org.apache.curator
            curator-recipes
        
        
            org.apache.curator
            curator-framework
        
    
application.yml 
dubbo:
  application:
    name: dubbo-dept-consumer
  registry:
    address: zookeeper://192.168.1.129:2181
com.example
@SpringBootApplication
@EnableDubbo
public class DeptApplication {
    public static void main(String[] args) {
        SpringApplication.run(DeptApplication.class,args);
    }
}
com.example.service
public interface DeptService {
    public List findAll();
}
com.example.service.impl
@Service
public class DeptServiceImpl implements DeptService {
    @Reference
    private DeptDubboService deptDubboService;
    @Override
    public List findAll() {
        return deptDubboService.findAllDept();
    }

}

在dept项目中新建接口,在其实现类中注入api接口对象,调用api中接口方法。

通过dept中接口的实现类调用api的方法。 

com.example.controller 
@Controller
public class DeptController {
    @Autowired
    private DeptService deptService;
    @GetMapping("/dept")
    public String showDept(Model model){
        model.addAttribute("list",deptService.findAll());
        return "dept";
    }

}

注入了deptService接口对象。

model.addAttribute:向前台传递数据,数据名为:list,数据内容为:deptService.findAll()。

resources /templates/dept.html  



    
    Title


    
        
            编号
            部门名称
            查看
        
        
            
            
             查看 
        
    

     
8、实现部门列表工作、图片上传 (1)api:com.example.dubbo.service 
public interface EmpDubboService {
    public int insertEmp(Emp emp);
    public List findEmpByDeptID(Integer did);
}
(2)provider:com.example.dubbo.service.impl
@Service
public class EmpDubboServiceImpl implements EmpDubboService {
    @Autowired
    private EmpMapper empMapper;

    @Override
    public int insertEmp(Emp emp) {
        return empMapper.insertEmp(emp);
    }

    @Override
    public List findEmpByDeptID(Integer did) {
        return empMapper.findEmpByDeptId(did);
    }
}
(3)在mapper项目中补充对数据库的 *** 作  com.example.mapper
public interface EmpMapper {
    public int insertEmp(Emp emp);
    public List findEmpByDeptId(Integer did);
}
mybatis 


    
        insert into emp (name,photo,did) values (#{name},#{photo},#{did})
    

    

(4)新建emp项目 pom.xml
   
        
            api
            com.example
            1.0-SNAPSHOT
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
        
            org.apache.dubbo
            dubbo-spring-boot-starter
        
        
            org.apache.curator
            curator-recipes
        
        
            org.apache.curator
            curator-framework
        
    
application.yml
dubbo:
  application:
    name: dubbo-emp-consumer
  registry:
     address: zookeeper://192.168.1.129:2181

server:
  port: 8081
com.example
@SpringBootApplication
@EnableDubbo
public class EmpApplication {
    public static void main(String[] args) {
        SpringApplication.run(EmpApplication.class , args);
    }
}
com.example.serever 
public interface EmpService {
    public List showAll() ;
    public int insert(Emp emp, MultipartFile file);
}
com.example.serever.impl 
@Service
public class EmpServiceImpl implements EmpService {

    @Reference
    private DeptDubboService deptDubboService;

    @Reference
    private EmpDubboService empDubboService ;

    @Override
    public List showAll() {
        return deptDubboService.findAllDept();
    }

    @Override
    public int insert(Emp emp, MultipartFile file) {
        try {
            //通过spring容器获取HttpServletRequest对象
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            //通过HttpServletRequest对象 获取图片上传的路径
            String path = request.getServletContext().getRealPath("/img");
            System.out.println("path == " + path);
            //为了上传到服务器中的图片的名称不重复 编写随机数
            long currentTimeMills = System.currentTimeMillis();
            Random random = new Random();

            String fileName = currentTimeMills + "" + random.nextInt(1000);
            String oldName = file.getOriginalFilename() ;
            //通过图片的原名称获取图片的后缀名
            fileName += oldName.substring(oldName.lastIndexOf("."));

            File pathFile = new File(path);
            //第一次上传图片 检查目录是否存在 如果不存在 创建响应目录
            if(!pathFile.exists())
            {
                pathFile.mkdirs();
            }
            //图片上传
            file.transferTo(new File(path , fileName));

            //封住emp对象 把图片路径封装到emp对象中
            emp.setPhoto("http://localhost:8081/img/"+fileName);

            //把Emp保存到数据库中
            return empDubboService.insertEmp(emp);

        } catch (IOException e) {
            e.printStackTrace();
        }


        return 0;
    }
}
com.example.controller  
@Controller
public class EmpController {

    @Autowired
    private EmpService empService ;

    @GetMapping("/empadd")
    public String empAdd(Model model)
    {
        model.addAttribute("list" , empService.showAll());

        return "emp-add";
    }

    @PostMapping("/add")
    public String add(Emp emp , MultipartFile file)
    {
        empService.insert(emp , file);
        return "emp-add";
    }
}
resources /templates/empadd-add.html



    
    Title



    

(5)补充dept项目代码实现部门员工查看 com.example.service
public interface DeptService {
    public List findAll();
    public List findEmpByDeptId(Integer did);
}
com.example.service,impl
@Service
public class DeptServiceImpl implements DeptService {
    @Reference
    private DeptDubboService deptDubboService;
    @Reference
    private EmpDubboService empDubboService;
    @Override
    public List findAll() {
        return deptDubboService.findAllDept();
    }

    @Override
    public List findEmpByDeptId(Integer did) {
        return empDubboService.findEmpByDeptID(did);
    }
}
com.example.controller 
@Controller
public class DeptController {
    @Autowired
    private DeptService deptService;
    @GetMapping("/dept")
    public String showDept(Model model){
        model.addAttribute("list",deptService.findAll());
        return "dept";
    }
    @GetMapping("/showEmp")
    public String showEmp(Integer did,Model model){
        model.addAttribute("list",deptService.findEmpByDeptId(did));
        return "showEmp";
    }
}
resources /templates/showEmp.html 



    
    Title


    

        
            姓名
            头像
        

        
            
            
        
    

resources /templates/dept.html  



    
    Title



    

       
           编号
           部门名称
           查看
       

        
            
            
            查看
        
    

9、界面展示

重新启动项目照片会丢失。

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

原文地址: https://outofmemory.cn/zaji/5717531.html

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

发表评论

登录后才能评论

评论列表(0条)

保存