美文网首页
SpringBoot整合Dubbo+zk

SpringBoot整合Dubbo+zk

作者: 蜻蜓队长家长 | 来源:发表于2019-07-11 11:44 被阅读0次

    Zookeeper安装

    云服务器搭建五:linux安装Zookeeperhttps://www.jianshu.com/p/992ce53f26a9

    一、搭建SpringBoot项目

    请参考SpringBoot分模块项目搭建

    二、整合Dubbo

    • 在父级pom文件引入dubbo和zk依赖
            <!--springboot整合dubbo-->
            <dependency>
                <groupId>io.dubbo.springboot</groupId>
                <artifactId>spring-boot-starter-dubbo</artifactId>
                <version>1.0.0</version>
                <exclusions>
                    <exclusion>     <!--此处排除掉低版本的dubbo依赖-->
                        <groupId>com.alibaba</groupId>
                        <artifactId>dubbo</artifactId>
                    </exclusion>
                    <exclusion>     <!--排除低版本zookeeper依赖-->
                        <groupId>com.101tec</groupId>
                        <artifactId>zkclient</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    
            <!--dubbo-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.6.0</version>
                <!--排除javassist防止与spring data jpa中冲突-->
                <exclusions>
                    <exclusion>
                        <groupId>org.javassist</groupId>
                        <artifactId>javassist</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    
            <!--zookeeper-->
            <dependency>
                <groupId>com.github.sgroschupf</groupId>
                <artifactId>zkclient</artifactId>
                <version>0.1</version>
                <exclusions>
                    <exclusion>
                        <artifactId>zookeeper</artifactId>
                        <groupId>org.apache.zookeeper</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
    
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>3.4.9</version>
                <exclusions>
                    <exclusion>
                        <artifactId>slf4j-log4j12</artifactId>
                        <groupId>org.slf4j</groupId>
                    </exclusion>
                    <exclusion>
                        <artifactId>log4j</artifactId>
                        <groupId>log4j</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
    
    • 在服务提供者resources加入yml配置
    spring:
      #发布应用程序配置
      application:
        name: zhg-dubbo-provider
    
      dubbo:
        application:
          name: zhg-dubbo-provider   #别名,提供方应用信息,用于计算依赖关系
        registry:
          address: zookeeper://47.107.105.158:2181 #使用zookeeper注册中心暴露服务地址
        protocol:
          name: dubbo
          port: -1         #配置暴露服务端口
        scan: com.zhg.service   #扫描需要暴露的服务
    
    • 在消费者resources中添加yml配置
    spring:
      application:
        name: zhg-dubbo-consumer
      dubbo:
        application:
          name: zhg-dubbo-consumer   #别名,消费方应用信息,不要与提供方相同
        registry:
          address: zookeeper://47.107.105.158:2181 #注册中心地址,调用服务
        scan: com.zhg.api     #dubbo注解扫描
      servlet:
        multipart:
          max-file-size: 100MB
          max-request-size: 100MB
    

    三、使用和测试

    • 在业务实现接口实现类中加入@Service注解(注意:是引入com.alibaba.dubbo.config.annotation.Service)
    package com.zhg.service.impl;
    
    import com.alibaba.dubbo.config.annotation.Service;
    import com.zhg.common.responseResult.ResponseResult;
    import com.zhg.dao.UserDao;
    import com.zhg.pojo.User;
    import com.zhg.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    
    @Service(version = "1.0")
    public class UserServiceImpl implements UserService {
    
        @Autowired
        private UserDao userDao;
    
        @Override
        public ResponseResult findUserByName(String name) {
            try{
                User user = userDao.findByName(name).orElse(null);
                return ResponseResult.success(user);
            }catch (Exception e){
                return ResponseResult.failure(e.getMessage());
            }
        }
    }
    
    • 在消费者使用@Reference注解调用服务提供者的接口
    package com.zhg.api;
    
    import com.alibaba.dubbo.config.annotation.Reference;
    import com.zhg.common.fastDFS.FastDFSClient;
    import com.zhg.common.responseResult.ResponseResult;
    import com.zhg.pojo.User;
    import com.zhg.service.UserService;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.multipart.MultipartFile;
    
    @RestController
    @RequestMapping("/user")
    @Api(value = "/user", tags = { "测试接口user" })
    public class UserControll {
    
        @Reference(version = "1.0",timeout = 6000)
        private UserService userService;
    
        @ApiOperation(value = "通过用户名查询用户信息", response = User.class, httpMethod = "GET", notes = "通过用户名查询用户信息")
        @RequestMapping(value = "/find",method = RequestMethod.GET)
        public ResponseResult findByName(String name){
            return userService.findUserByName(name);
        }
    
        @ApiOperation(value = "上传文件", response = User.class, httpMethod = "POST", notes = "上传文件")
        @RequestMapping(value = "/upload",method = RequestMethod.POST)
        public ResponseResult findByName(@RequestParam("file")MultipartFile file){
            try{
                FastDFSClient fast = new FastDFSClient();
                String fileAbsolutePath = fast.uploadFile(file.getBytes(),file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1));
                return ResponseResult.success(fileAbsolutePath);
            }catch (Exception e){
                return ResponseResult.failure(e.getMessage());
            }
    
        }
    
    }
    
    image.png

    四、注意

    1.服务调用时出现java.io.Serializable

    服务调用异常

    -解决方案

    实体类序列化

    2.启动消费者出现数据库相关驱动加载失败等问题

    异常

    -解决方案 排除引入其它模块无关的jar(数据库、jpa、mybatis等)

            <!--引入接口模块-->
            <dependency>
                <groupId>com.zhg</groupId>
                <artifactId>interface</artifactId>
                <version>0.0.1-SNAPSHOT</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-data-jpa</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    

    实例参考demo:https://github.com/YoungAdmin/springbootdemo-dubbo.git

    相关文章

      网友评论

          本文标题:SpringBoot整合Dubbo+zk

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