依赖 dubbo
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.8</version>
</dependency>
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 原来在yaml自动配属性中看到 xml中也有-->
<!--dubbo应用-->
<dubbo:application name="${spring.application.name}">
<!--关闭qos-->
<dubbo:parameter key="qos.enable" value="false"/>
</dubbo:application>
<!--protocol dubbo -1-->
<dubbo:protocol name="dubbo" port="-1"/>
<!--注册中心-->
<dubbo:registry
id="dubboNacos"
protocol="nacos"
address="localhost:8848"
use-as-config-center="false"
use-as-metadata-center="false"/>
<!--消费者配置接口-->
<dubbo:reference id="attachApi" interface="com.tarena.tp.attach.server.client.AttachApi"
check="fasle" registry="dubboNacos"/>
</beans>
依赖provider接口 添加到调用模块domain
<!--远程调用attach-->
<dependency>
<groupId>com.tarena.tp.attach</groupId>
<version>1.0.0-SNAPSHOT</version>
<artifactId>luban-attach-server-client-api</artifactId>
</dependency>
导入到当前启动worker-server进程中
@ImportResource注解
//配置类中导入xml文件
@ImportResource("classpath:applicationContext.xml")
domain层代码 补充远程调用
@Autowired
private AttachApi attachApi;
private Long saveWorker(WorkerCreateParam workerCreateParam) throws BusinessException {
//如果当前用户在数据库中有师傅信息,就删除 重新入住师傅信息
workerRepository.delete(workerCreateParam.getUserId());
Long id=workerRepository.save(workerCreateParam);
//TODO rpc 调用 将身份信息,worker-id,和当前图片传递给attach系统,做更新
//远程调用
try{
//远程调用,更新已经上传的图片,绑定到当前worker
createAttach(id,workerCreateParam.getAttachList());
}catch (Exception e){
//远程调用失败
throw new BusinessException(ResultEnum.ATTACH_FAILED);
}
return id;
}
//人为定义的类型
private static final Integer businessType=100;
private void createAttach(Long businessId, List<IdCardParam> attachList) {
//要将attachList 图片(attach服务 没有绑定业务)
//封装远程调用参数 attach执行一个简单update attach set bysiness_id=#{参数} business_id=#{师傅id}
//where id=#{当前图片id}
List<AttachUpdateParam> attachUpdateParams=new ArrayList<>();
//封装
for (IdCardParam idCardParam : attachList) {
//参数元素
AttachUpdateParam attachUpdateParam=new AttachUpdateParam();
//是否是封面
attachUpdateParam.setIsCover(idCardParam.getType());
// 图片id businessId businessType 主要数据
attachUpdateParam.setId(idCardParam.getId());
attachUpdateParam.setBusinessType(businessType);
attachUpdateParam.setBusinessId(businessId.intValue());
//添加到list
attachUpdateParams.add(attachUpdateParam);
}
//调用远程
attachApi.batchUpdateAttachByIdList(attachUpdateParams);
}