栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

spingboot2整合dubbo

spingboot2整合dubbo

一、引入依赖(Dubbo 如今已被阿里卖给 Apache 维护和管理,所以最好是引入apache下面的包,网上七七八八的包一大堆,着实让人眼花缭乱,什么com.alibaba.boot,com.alibaba.spring.boot,io.dubbo.springboot等,都快看吐了)

        
            org.apache.dubbo
            dubbo-spring-boot-starter
            2.7.8
        
        
            org.apache.curator
            curator-framework
            4.0.1
            
                
                    org.slf4j
                    slf4j-log4j12
                
            
        
        
            org.apache.curator
            curator-recipes
            4.0.1
        
    

注:之前引入的ort.apache.curator相关的包是2.12.0版本,但是启动会报java.lang.NoClassDefFoundError:org/apache/curator/framework/recipes/cache/TreeCacheListener的错误,把版本改成4.0.1之后就不会了,试了各种版本,坑的一批

二、服务提供者

1、application.yml配置

dubbo:
  application:
    name: sys-server
  protocol:
    name: dubbo
    port: 20880
  registry:
    address: zookeeper://xxxx:12181
  scan:
    base-packages: com.sys.server.service

其中dubbo.scan.base-packages可以用@EnableDubbo(scanbasePackages = "com.sys.server.service")代替,二者选其一

2、创建服务类,用@DubboService注解表明该类为服务提供者(低版本用@Service)

import com.sys.server.inteface.common.ISystemCommonRpcService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;


@Component
@DubboService(interfaceName = "systemCommonRpcServiceImpl", version = "1.0.0")
public class SystemCommonRpcServiceImpl implements ISystemCommonRpcService {

    @Override
    public String getSystemInfo() {
        return "获取系统信息";
    }
}

3、创建调用接口

@RestController
@RequestMapping("/webapi")
public class CmServerBusiController {

    @Autowired
    private ICmServerBusiService cmServerBusiService;

    @GetMapping("/select")
    public String selectDataInfo() {
        Map result = cmServerBusiService.selectDataInfo();
        return JSONUtil.toJsonStr(result);
    }
}

三、服务消费者

1、application.yml配置文件

dubbo:
  application:
    name: cm-server
  protocol:
    name: dubbo
    port: 20880
  registry:
    address: zookeeper://xxxx:12181
  scan:
    base-packages: com.cm.server.service

注:消费者如果只是消费,可不用配置dubbo.scan.base-packages

2、调用方( @DubboReference注入需要调用的服务,低版本用@Reference)

Slf4j
@Service
public class CmServerBusiServiceImpl implements ICmServerBusiService {

    @Autowired
    private CmServerBusiMapper cmServerBusiMapper;

    @DubboReference(version = "1.0.0")
    private ISystemCommonRpcService systemCommonRpcService;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    @ReadDataSource
    public Map selectDataInfo() {
        log.info("============日志打印");
        Map map = cmServerBusiMapper.selectDataInfo();
        String str = systemCommonRpcService.getSystemInfo();
        map.put("system", str);
        return map;
    }
}

四、测试

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/654010.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号