Zookeeper第三话 -- Springboot基于zk+dubbo实现远程服务调用

289 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

本文主要记录springboot + zk + dubbo完成远程通信服务。 坏境配置:zookeeper3.7、dubbojar包3.0.6

1.项目架构

在这里插入图片描述 就一个接口,一个实现,这里贴一下service接口类

public interface HelloService {
    String hello(String name);
}

2.yaml配置

dubbo:
  application:
    name: dubbo-service #客户端仅此名称不同
  registry:
    protocol: zookeeper
    address: 192.168.0.221:2181
    timeout: 60000
  protocol:
    name: dubbo
    port: 9999

3.pom文件

基于springboot 2.5.6

<dependencies>
 <dependency>
  <groupId>com.example</groupId>
  <artifactId>dubbo-api</artifactId>
  <version>1.0.0</version>
 </dependency>
 <dependency>
  <groupId>org.apache.dubbo</groupId>
  <artifactId>dubbo-spring-boot-starter</artifactId>
  <version>3.0.6</version>
 </dependency>
 <dependency>
  <groupId>org.apache.dubbo</groupId>
  <artifactId>dubbo-dependencies-zookeeper</artifactId>
  <version>3.0.6</version>
  <exclusions>
   <exclusion>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
   </exclusion>
  </exclusions>
  <type>pom</type>
 </dependency>
</dependencies>

4.service实现

//添加特有的dubbo服务标识 如果不添加dubbo扫描包 则需要添加Component注解交给spring管理
@DubboService
@Component
public class HelloServiceImpl implements HelloService {
    @Override
    public String hello(String name) {
        System.out.println("调用dubbo服务");
        return "hello my name is " + name;
    }
}

5.客户端controller接口

@RestController
public class DubboController {

 //使用dubbo注解
    @DubboReference
    HelloService helloService;

    @GetMapping("/hello")
    public String hello(@RequestParam String name) {
        return helloService.hello(name);
    }
}

6.启动服务

需要添加@EnableDubbo注解,最后出现此日志视为成功

c.dubbo.service.DubboServiceApplication  : Started DubboServiceApplication in 39.246 seconds (JVM running for 41.205)

以上就是本章的全部内容了。

上一篇:Zookeeper第二话 -- Springboot基于zk watch机制实现备用机 下一篇:Zookeeper第四话 -- zookeeper高可用集群搭建

书山有路勤为径,学海无涯苦作舟