SpringBoot整合Dubbo+zk

Zookeeper安装

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

一、搭建SpringBoot项目

请参考SpringBoot分模块项目搭建

二、整合Dubbo

  • 在父级pom文件引入dubbo和zk依赖
        
        
            io.dubbo.springboot
            spring-boot-starter-dubbo
            1.0.0
            
                     
                    com.alibaba
                    dubbo
                
                     
                    com.101tec
                    zkclient
                
            
        

        
        
            com.alibaba
            dubbo
            2.6.0
            
            
                
                    org.javassist
                    javassist
                
            
        

        
        
            com.github.sgroschupf
            zkclient
            0.1
            
                
                    zookeeper
                    org.apache.zookeeper
                
            
        

        
            org.apache.zookeeper
            zookeeper
            3.4.9
            
                
                    slf4j-log4j12
                    org.slf4j
                
                
                    log4j
                    log4j
                
            
        
  • 在服务提供者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
  • 使用dubbo管理页面查看
    Dubbo的管理控制台dubbo-admin https://blog.csdn.net/qq_28988969/article/details/79866111

image.png

四、注意

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

服务调用异常

-解决方案

实体类序列化

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

异常

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

        
        
            com.zhg
            interface
            0.0.1-SNAPSHOT
            
                
                    org.springframework.boot
                    spring-boot-starter-data-jpa
                
                
                    mysql
                    mysql-connector-java
                
            
        

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

你可能感兴趣的:(SpringBoot整合Dubbo+zk)