首先一定确保zookeeper正常工作
公共接口模块
为了简化代码,可以把需要其他服务调用的接口,抽取为公共接口模块dubbo-common-service,
public interface TestService {
public String clientTest();
}
服务提供方
导入dubbo、zookeeper依赖
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-x-discovery</artifactId>
<version>5.2.0</version>
</dependency>
<!--自己建立的公共接口包-->
<dependency>
<groupId>com.psh</groupId>
<artifactId>dubbo-common-service</artifactId>
<version>1.0</version>
</dependency>
配置文件为:
server:
port: 6610 #端口
dubbo:
application:
name: dubbo-client #别名
registry:
address: zookeeper://192.168.158.159:2181 #zookeeper 地址 + 端口
timeout: 230000 # 如果zookeeper是放在远程服务器上超时时间请设置长一些,不然很容易超时连接失败
protocol:
port: 20881 # 不同服务使用不同端口,不然会报错
name: dubbo
提供方实现接口并暴露服务,在接口实现类增加@DubboService
@DubboService
public class ClientServiceImpl implements TestService {
@Override
public String clientTest() {
return "远程调用成功";
}
}
启动类加上@EnableDubbo
@EnableDubbo
@SpringBootApplication
public class DubboApplicationClient {
public static void main(String[] args) {
SpringApplication.run(DubboApplicationClient.class);
}
}
接口调用方
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-x-discovery</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.psh</groupId>
<artifactId>dubbo-common-service</artifactId>
<version>1.0</version>
</dependency>
server:
port: 6620
dubbo:
application:
name: dubbo-client #别名
registry:
address: zookeeper://192.168.158.159:2181 #zookeeper 地址 + 端口
timeout: 230000 # 如果zookeeper是放在远程服务器上超时时间请设置长一些,不然很容易超时连接失败
protocol:
port: 20882 # 不同服务使用不同端口,不然会报错
name: dubbo
启动类
@EnableDubbo
@SpringBootApplication
public class DubboApplicationServer {
public static void main(String[] args) {
SpringApplication.run(DubboApplicationServer.class);
}
}
controller接口
@RestController
public class DubooServerApplication {
@Autowired
private ServerService serverService;
@GetMapping("/test")
public String test(String name){
return serverService.serverTest(name);
}
}
通过本服务的Service 来远程调用接口
public interface ServerService {
public String serverTest(String name) ;
}
@Service
public class ServerServiceImpl implements ServerService {
@DubboReference
private TestService testService;
public String serverTest(String name) {
return "返回结果为:"+testService.clientTest()+",参数为:"+name;
}
}
访问controller接口,得到:
远程调用成功