一、引入依赖(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;
}
}
四、测试



