这里面使用Dubbo进行调用的
依赖
版本:2.2.3.RELEASE
<!-- dubbo -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
<version>${spring-cloud-alibaba.version}</version>
</dependency>
配置文件
dubbo:
application:
name: ${spring.application.name}
id: ${spring.application.name}
scan:
#扫描使用dubbo的@Service注解的包
base-packages: cn.com.alibaba.service
protocol:
#dubbo名称
name: dubbo
#-1表示端口自增
port: -1
#注册到nacos中
registry: nacos://localhost:8848
Java代码
- 新建一个api接口模块,主要是为了方便其他项目引用
-
增加注解
//添加dubbo注解,低版本的直接是Service
@DubboService
public class AServiceImpl implements IAService {
@Override
public String getInfo(String name) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "A-Hello,"+name;
}
}
-
在需要调用的项目中国引用,如service-a需要使用service-b的方法,就在service-a的pom文件中引用b的service-b-api
<!-- b的api提取 -->
<dependency>
<artifactId>service-b-api</artifactId>
<groupId>cn.com.alibaba</groupId>
</dependency>
controller里面引用的时候需要dubbo注解
@DubboReference(generic = false,check=false,interfaceClass = IBService.class,interfaceName = "cn.com.alibaba.IBService")
private IBService ibService;
@DubboReference(generic = false,check=false,interfaceClass = IAService.class,interfaceName = "cn.com.alibaba.IAService")
private IAService iaService;