美文网首页
spring5x-dubbo

spring5x-dubbo

作者: 宇宙小神特别萌 | 来源:发表于2019-12-13 16:51 被阅读0次
    spring5x-dubbo目录.png
    • spring5x-dubbo-api #为service、web提供实体类和接口
    • spring5x-dubbo-service #提供者
    • spring5x-dubbo-web #消费者

    spring5x-dubbo-api 搭建项目

    dubbo_1.png

    1.实体类

    package com.zja.model.entity;
    
    import io.swagger.annotations.ApiModel;
    import io.swagger.annotations.ApiModelProperty;
    import lombok.Data;
    
    import java.io.Serializable;
    import java.util.Date;
    import java.util.List;
    
    /**
     * @author ZhengJa
     * @description User 实体类
     * @data 2019/10/29
     */
    @Data
    @ApiModel("用户信息实体类")
    public class UserEntity implements Serializable {
    
        @ApiModelProperty(value = "默认:mysql自增,oracle序列")
        private Integer id;
        @ApiModelProperty("用户名")
        private String userName;
        @ApiModelProperty("年龄")
        private Integer age;
        @ApiModelProperty("不传值,后台创建时间")
        private Date createTime;
    
        @ApiModelProperty("订单信息")
        private List<OrdersEntity> ordersEntityList;
        @ApiModelProperty("所属组信息")
        private List<GroupEntity> groupEntityList;
    }
    
    

    实体类还有其他的,可以到github拉代码查看,此次讲述的重点时dubbo

    2.api接口:为service、web提供的接口

    package com.zja.service;
    
    
    import com.github.pagehelper.PageInfo;
    import com.zja.model.dto.UserEntityDTO;
    import com.zja.model.entity.UserEntity;
    
    import java.util.List;
    
    /**
     * @author ZhengJa
     * @description
     * @data 2019/11/14
     */
    public interface UserService {
    
        //静态插入数据:通用方法
        int insertUser(UserEntity userEntity);
    
        //动态插入数据: mysql用法,id自增
        int insertUserMysql(UserEntity userEntity);
        //动态插入数据:oracle用法,id使用序列
        int insertUserOracle(UserEntity userEntity);
    
        //mybatis批量插入数据:mysql用法,id自增
        int mysqlBatchSaveUser(List<UserEntity> userEntities);
        //mybatis批量插入数据:oracle用法,id使用序列
        int oracleBatchSaveUser(List<UserEntity> userEntities);
    
        //按id查询用户
        UserEntityDTO queryUserById(Integer id);
        //查询所有用户
        List<UserEntity> queryAllUser();
    
        //获取分页结果
        List<UserEntity> getPagingResults(int pageNum, int pageSize);
        //获取分页结果及分页信息
        PageInfo<UserEntity> queryPageInfo(int pageNum, int pageSize);
    
        //更新数据-改数据
        int updateUser(UserEntity userEntity);
    
        //删除数据
        int delUser(Integer id);
    }
    
    

    spring5x-dubbo-service 搭建项目

    基于spring5x-base 基础模块 新增功能:

    • 1、spring集成 dubbo依赖和xml配置

    项目架构:spring5.x+mybatis+druid/c3p0+mysql/oracle+dubbo
    功能:spring项目启动自动执行sql文件; dozer映射功能

    以下只贴dubbo有关配置和代码,本篇仅将dubbo使用。

    1、spring集成 dubbo依赖和xml配置

    dubboy依赖

            <!--引入api接口层模块service、DTO、Entity-->
            <dependency>
                <groupId>com.zja</groupId>
                <artifactId>spring5x-dubbo-api</artifactId>
                <version>0.1.RELEASE</version>
            </dependency>
    
            <!--dubbo 依赖-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.6.2</version>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>4.0.0</version>
            </dependency>
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>3.4.13</version>
            </dependency>
    
    

    spring-dubbo-provider.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    
        <!--定义当前应用名称,主要用于注册中心的信息保存,这个名称可以任意填写-->
        <dubbo:application name="${dubbo.application.name}"/>
        <!--定义dubbo注册中心的地址-->
        <dubbo:registry protocol="zookeeper" address="${dubbo.registry.address}"/>
        <!--定义dubbo所在服务执行时暴露给客户端的端口-->
        <dubbo:protocol name="dubbo" port="${dubbo.protocol.port}"/>
        <!--定义远程服务提供者操作的超时时间-->
        <dubbo:provider timeout="${dubbo.provider.timeout}"/>
    
        <!--定义dubbo远程服务的接口,声明需要暴露的服务接口
            interface 定义的接口
            ref Service 中对应实现类首字母小写
            version是需要考虑到版本一致问题-->
        <dubbo:service interface="com.zja.service.CascadeService" ref="cascadeServiceImpl" version="${dubbo.interface.version}"/>
        <dubbo:service interface="com.zja.service.UserService" ref="userServiceImpl" version="${dubbo.interface.version}"/>
    
        <!-- 和本地 bean 一样实现服务 -->
        <bean id="cascadeService" class="com.zja.service.CascadeService" abstract="true"/>
        <bean id="userService" class="com.zja.service.UserService" abstract="true"/>
    
    </beans>
    
    

    spring5x-dubbo-web 搭建项目

    基于spring5x-base 基础模块 新增功能:

    • 1、spring集成 dubbo依赖和xml配置
    • 2、web层调用
    • 3、项目的github和博客地址

    项目架构:spring5.x+dubbo

    以下只贴dubbo有关配置和代码,本篇仅将dubbo使用。

    1、spring集成 dubbo依赖和xml配置

    dubboy依赖

            <!--引入api接口层模块service、DTO、Entity-->
            <dependency>
                <groupId>com.zja</groupId>
                <artifactId>spring5x-dubbo-api</artifactId>
                <version>0.1.RELEASE</version>
            </dependency>
    
            <!--dubbo 依赖-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.6.2</version>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>4.0.0</version>
            </dependency>
            <!--默认是最新的3.5.x版本,而我版本安装zk是3.4.9,所以要用3.4.x版本,不然报异常-->
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>3.4.13</version>
            </dependency>
    
    

    spring-dubbo-consumer.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    
        <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
        <dubbo:application name="${dubbo.application.name}"/>
    
        <!--Dubbo 缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止 Spring 初始化完成,以便上线时,能及早发现问题,默认 check="true"。-->
        <!--可以关闭所有服务的启动时检查 -->
        <dubbo:consumer check="false" />
    
        <!--定义dubbo调用本地注册中心zk服务地址-->
        <dubbo:registry id="local" protocol="zookeeper" address="${dubbo.local.registry.address}"/>
        <!--定义dubbo调用远程注册中心zk服务地址-->
        <!--<dubbo:registry id="remote" protocol="zookeeper" address="${dubbo.remote.registry.address}"/>-->
    
        <!--定义dubbo所在服务执行时暴露给客户端的端口-->
        <dubbo:protocol name="dubbo" port="${dubbo.protocol.port}"/>
    
    
        <!-- 生成远程服务代理,可以和本地 bean 一样使用 Service-->
        <!--本地(local)-->
        <dubbo:reference registry="local" id="cascadeService" interface="com.zja.service.CascadeService" version="${dubbo.interface.version}"/>
        <dubbo:reference registry="local" id="userService" interface="com.zja.service.UserService" version="${dubbo.interface.version}"/>
    
        <!--本地(local)-->
        <!--<dubbo:reference registry="local" interface="com.zja.service.CascadeService" id="bannerService"
                         version="${dubbo.interface.version}" check="false"/>-->
        <!--远程(remote)-->
        <!--<dubbo:reference registry="remote" id="permissionSupplier" interface="com.dist.dcc.security.auth.api.PermissionSupplier"
                         check="false" version="${dubbo.remote.interface.version}" />-->
    </beans>
    
    

    2、web层调用

    package com.zja.controller;
    
    import com.github.pagehelper.PageInfo;
    import com.zja.model.entity.UserEntity;
    import com.zja.service.UserService;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import io.swagger.annotations.ApiParam;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    /**
     * @author ZhengJa
     * @description MybatisController 测试类
     * @data 2019/10/29
     */
    @RestController
    @RequestMapping("rest/dubbo")
    @Api(tags = {"DubboMybatisController"}, description = "dubbo简单测试")
    public class DubboMybatisController {
    
        @Autowired
        private UserService userService;
    
        @PostMapping("insertUser")
        @ApiOperation(value = "静态插入数据:通用方法,必须传id值且id>0", notes = "插入数据(id不自增或不使用序列,必须传id值且id>0)", httpMethod = "POST")
        public int insertUser(@RequestBody UserEntity userEntity) {
            return this.userService.insertUser(userEntity);
        }
    
        @PostMapping("insertUserMysql")
        @ApiOperation(value = "动态插入数据: mysql用法 id自增,不传id值", notes = "插入数据(id自增,不传id值)", httpMethod = "POST")
        public int insertUserMysql(@RequestParam String userName,@RequestParam Integer age) {
            UserEntity userEntity = new UserEntity();
            userEntity.setUserName(userName);
            userEntity.setAge(age);
            userEntity.setCreateTime(new Date());
            return this.userService.insertUserMysql(userEntity);
        }
    
        @PostMapping("insertUserOracle")
        @ApiOperation(value = "动态插入数据:oracle用法 id使用序列,不传id值", notes = "插入数据(id使用序列,不传id值)", httpMethod = "POST")
        public int insertUserOracle(@RequestParam String userName,@RequestParam Integer age) {
            UserEntity userEntity = new UserEntity();
            userEntity.setUserName(userName);
            userEntity.setAge(age);
            userEntity.setCreateTime(new Date());
            return this.userService.insertUserOracle(userEntity);
        }
    
        @PostMapping("mysqlBatchSaveUser")
        @ApiOperation(value = "mybatis+mysql批量插入数据: mysql用法 id自增", notes = "插入数据(id自增)", httpMethod = "POST")
        public int mysqlBatchSaveUser(@ApiParam(value = "count 批量插入几条",defaultValue = "5") @RequestParam Integer count) {
    
            List<UserEntity> entityList = new ArrayList<>();
            for (int i=0;i<count;i++){
                UserEntity userEntity = new UserEntity();
                userEntity.setUserName("Zhengja_"+i);
                userEntity.setAge(20+i);
                userEntity.setCreateTime(new Date());
                entityList.add(userEntity);
            }
            return this.userService.mysqlBatchSaveUser(entityList);
        }
    
        @PostMapping("oracleBatchSaveUser")
        @ApiOperation(value = "mybatis+oracle批量插入数据: oracle用法 id不使用序列", notes = "插入数据(id不能使用序列)", httpMethod = "POST")
        public int oracleBatchSaveUser(@ApiParam(value = "count 批量插入几条",defaultValue = "5") @RequestParam Integer count) {
    
            List<UserEntity> entityList = new ArrayList<>();
            for (int i=0;i<count;i++){
                UserEntity userEntity = new UserEntity();
                //批量插入没有提交,无法获取递增的序列值,所以,oracle注意,id不能使用序列,会报异常 “违反唯一约束条件”
                userEntity.setId(100+i);
                userEntity.setUserName("Zhengja_"+i);
                userEntity.setAge(20+i);
                userEntity.setCreateTime(new Date());
                entityList.add(userEntity);
            }
            return this.userService.oracleBatchSaveUser(entityList);
        }
    
        @GetMapping("queryUserById")
        @ApiOperation(value = "按id查询用户", notes = "按id查询数据", httpMethod = "GET")
        public Object queryUserById(@RequestParam Integer id) {
            return this.userService.queryUserById(id);
        }
    
        @GetMapping("queryAllUser")
        @ApiOperation(value = "查询所有用户", notes = "查询所有数据", httpMethod = "GET")
        public List<UserEntity> queryAllUser() {
            return this.userService.queryAllUser();
        }
    
        @GetMapping("getpage")
        @ApiOperation(value = "获取分页结果", notes = "分页查询", httpMethod = "GET")
        public List<UserEntity> getPagingResults(@ApiParam("页码值") @RequestParam int pageNum, @ApiParam("每页显示条数") @RequestParam int pageSize) {
            return this.userService.getPagingResults(pageNum, pageSize);
        }
    
        @GetMapping("getpageinfo")
        @ApiOperation(value = "获取分页结果及分页信息", notes = "分页查询", httpMethod = "GET")
        public PageInfo<UserEntity> queryPageInfo(@ApiParam("页码值") @RequestParam int pageNum, @ApiParam("每页显示条数") @RequestParam int pageSize) {
            return this.userService.queryPageInfo(pageNum, pageSize);
        }
    
        @PutMapping("updateUser")
        @ApiOperation(value = "更新用户信息", notes = "更新数据-改数据", httpMethod = "PUT")
        public int updateUser(@RequestBody UserEntity userEntity) {
            return this.userService.updateUser(userEntity);
        }
    
        @DeleteMapping("delUser")
        @ApiOperation(value = "删除数据", notes = "删除数据", httpMethod = "DELETE")
        public int delUser(@RequestParam Integer id) {
            return this.userService.delUser(id);
        }
    
    }
    
    

    3、项目的github和简书博客地址

    github:

    博客:

    相关文章

      网友评论

          本文标题:spring5x-dubbo

          本文链接:https://www.haomeiwen.com/subject/cfofnctx.html